2121import static org .androidannotations .helper .ModelConstants .TRACE_OPTION ;
2222import static org .androidannotations .rclass .ProjectRClassFinder .RESOURCE_PACKAGE_NAME_OPTION ;
2323
24+ import java .io .FileNotFoundException ;
2425import java .io .IOException ;
2526import java .net .URL ;
2627import java .util .Collections ;
124125import org .androidannotations .annotations .sharedpreferences .Pref ;
125126import org .androidannotations .annotations .sharedpreferences .SharedPref ;
126127import org .androidannotations .exception .ProcessingException ;
128+ import org .androidannotations .exception .VersionMismatchException ;
127129import org .androidannotations .generation .CodeModelGenerator ;
128130import org .androidannotations .helper .AndroidManifest ;
129131import org .androidannotations .helper .AndroidManifestFinder ;
285287public class AndroidAnnotationProcessor extends AbstractProcessor {
286288
287289 private final Properties properties = new Properties ();
290+ private final Properties propertiesApi = new Properties ();
288291 private final TimeStats timeStats = new TimeStats ();
289292 private final ErrorHelper errorHelper = new ErrorHelper ();
290293
@@ -296,11 +299,27 @@ public synchronized void init(ProcessingEnvironment processingEnv) {
296299
297300 Messager messager = processingEnv .getMessager ();
298301
299- loadPropertyFile ();
302+ try {
303+ loadPropertyFile ();
304+ loadApiPropertyFile ();
305+ checkApiAndCoreVersions ();
306+ } catch (Exception e ) {
307+ messager .printMessage (Diagnostic .Kind .ERROR , "AndroidAnnotations processing failed: " + e .getMessage ());
308+ }
300309
301310 timeStats .setMessager (messager );
302311
303312 messager .printMessage (Diagnostic .Kind .NOTE , "Starting AndroidAnnotations annotation processing" );
313+
314+ }
315+
316+ private void checkApiAndCoreVersions () throws VersionMismatchException {
317+ String apiVersion = getAAApiVersion ();
318+ String coreVersion = getAAProcessorVersion ();
319+
320+ if (!apiVersion .equals (coreVersion )) {
321+ throw new VersionMismatchException ("AndroidAnnotation version for API (" + apiVersion + ") and core (" + coreVersion + " doesn't match. Please check your classpath)" );
322+ }
304323 }
305324
306325 @ Override
@@ -319,23 +338,34 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
319338 return true ;
320339 }
321340
322- private void loadPropertyFile () {
323- String filename = "androidannotations-version .properties" ;
341+ private void loadPropertyFile () throws FileNotFoundException {
342+ String filename = "androidannotations.properties" ;
324343 try {
325344 URL url = getClass ().getClassLoader ().getResource (filename );
326345 properties .load (url .openStream ());
327346 } catch (Exception e ) {
328- e .printStackTrace ();
347+ throw new FileNotFoundException (filename + " couldn't be parsed" );
348+ }
349+ }
329350
330- Messager messager = processingEnv .getMessager ();
331- messager .printMessage (Diagnostic .Kind .NOTE , "AndroidAnnotations processing failed because " + filename + " couldn't be parsed : " + e .getLocalizedMessage ());
351+ private void loadApiPropertyFile () throws FileNotFoundException {
352+ String filename = "androidannotations-api.properties" ;
353+ try {
354+ URL url = getClass ().getClassLoader ().getResource (filename );
355+ propertiesApi .load (url .openStream ());
356+ } catch (Exception e ) {
357+ throw new FileNotFoundException (filename + " couldn't be parsed" );
332358 }
333359 }
334360
335361 private String getAAProcessorVersion () {
336362 return properties .getProperty ("version" , "3.0+" );
337363 }
338364
365+ private String getAAApiVersion () {
366+ return propertiesApi .getProperty ("version" , null );
367+ }
368+
339369 private void processThrowing (Set <? extends TypeElement > annotations , RoundEnvironment roundEnv ) throws ProcessingException , Exception {
340370 if (nothingToDo (annotations , roundEnv )) {
341371 return ;
0 commit comments