1515 */
1616package com .googlecode .androidannotations .rclass ;
1717
18- import java .util .Iterator ;
19- import java .util .Set ;
18+ import java .io .BufferedReader ;
19+ import java .io .File ;
20+ import java .io .FileNotFoundException ;
21+ import java .io .FileReader ;
22+ import java .io .IOException ;
2023
24+ import javax .annotation .processing .Filer ;
25+ import javax .annotation .processing .Messager ;
2126import javax .annotation .processing .ProcessingEnvironment ;
22- import javax .lang .model .element .Element ;
23- import javax .lang .model .element .PackageElement ;
2427import javax .lang .model .element .TypeElement ;
2528import javax .lang .model .util .Elements ;
29+ import javax .tools .Diagnostic .Kind ;
30+ import javax .tools .FileObject ;
31+ import javax .tools .StandardLocation ;
2632
27- import com .googlecode .androidannotations .annotations .Enhance ;
2833import com .googlecode .androidannotations .helper .AnnotationHelper ;
2934import com .googlecode .androidannotations .model .AnnotationElements ;
3035
@@ -34,31 +39,55 @@ public CompiledRClassFinder(ProcessingEnvironment processingEnv) {
3439 super (processingEnv );
3540 }
3641
37- public IRClass find (AnnotationElements extractedModel ) {
42+ public IRClass find (AnnotationElements extractedModel ) throws IOException {
43+
44+ File manifestFile = findManifestFile ();
45+
46+ String rClassPackage = extractPackage (manifestFile );
47+
48+ if (rClassPackage == null ) {
49+ Messager messager = processingEnv .getMessager ();
50+ messager .printMessage (Kind .WARNING , "Could not find the AndroidManifest.xml file in " + manifestFile .getAbsolutePath ());
51+ return IRClass .EMPTY_R_CLASS ;
52+ }
3853
3954 Elements elementUtils = processingEnv .getElementUtils ();
55+ String rClass = rClassPackage + ".R" ;
56+ TypeElement rType = elementUtils .getTypeElement (rClass );
4057
41- Set <? extends Element > annotatedElements = extractedModel .getAnnotatedElements (Enhance .class );
58+ if (rType == null ) {
59+ Messager messager = processingEnv .getMessager ();
60+ messager .printMessage (Kind .WARNING , "The AndroidManifest.xml file was found, but not the compiled R class: " + rClass );
61+ return IRClass .EMPTY_R_CLASS ;
62+ }
4263
43- Iterator <? extends Element > iterator = annotatedElements .iterator ();
64+ return new RClass (rType );
65+ }
4466
45- if (iterator .hasNext ()) {
46- TypeElement firstLayoutAnnotatedElement = (TypeElement ) iterator .next ();
67+ private String extractPackage (File manifestFile ) throws FileNotFoundException , IOException {
68+ FileReader reader = new FileReader (manifestFile );
69+ BufferedReader br = new BufferedReader (reader );
4770
48- // TODO better handling at finding R class
49- PackageElement firstActivityPackage = elementUtils . getPackageOf ( firstLayoutAnnotatedElement );
71+ String line ;
72+ while (( line = br . readLine ()) != null ) {
5073
51- TypeElement rType = elementUtils . getTypeElement ( firstActivityPackage . getQualifiedName () + "." + "R" );
74+ ManifestPackageExtractor extractor = new ManifestPackageExtractor ( line );
5275
53- if (rType != null ) {
54- return new RClass (rType );
55- } else {
56- printAnnotationError (firstLayoutAnnotatedElement , Enhance .class , "In order to find the R class, all Activities annotated with @" + Enhance .class .getSimpleName () + " should belong to the same package as the R class, which is not the case for: " + firstLayoutAnnotatedElement );
57- return IRClass .EMPTY_R_CLASS ;
76+ if (extractor .matches ()) {
77+ return extractor .extract ();
5878 }
59- } else {
60- return IRClass .EMPTY_R_CLASS ;
6179 }
80+ return null ;
81+ }
82+
83+ private File findManifestFile () throws IOException {
84+ Filer filer = processingEnv .getFiler ();
85+
86+ FileObject res = filer .getResource (StandardLocation .CLASS_OUTPUT , "" , "" );
87+ File projectRoot = new File (res .toUri ()).getParentFile ();
88+
89+ File androidManifestFile = new File (projectRoot , "AndroidManifest.xml" );
90+ return androidManifestFile ;
6291 }
6392
6493}
0 commit comments