|
27 | 27 | import javax.lang.model.element.TypeElement; |
28 | 28 | import javax.lang.model.util.Elements; |
29 | 29 | import javax.tools.Diagnostic.Kind; |
30 | | -import javax.tools.FileObject; |
31 | | -import javax.tools.StandardLocation; |
| 30 | +import javax.tools.JavaFileObject; |
32 | 31 |
|
33 | 32 | import com.googlecode.androidannotations.helper.AnnotationHelper; |
34 | 33 | import com.googlecode.androidannotations.model.AnnotationElements; |
35 | 34 |
|
36 | 35 | public class CompiledRClassFinder extends AnnotationHelper implements RClassFinder { |
37 | 36 |
|
38 | | - public CompiledRClassFinder(ProcessingEnvironment processingEnv) { |
39 | | - super(processingEnv); |
40 | | - } |
| 37 | + private static final int MAX_PARENTS_FROM_SOURCE_FOLDER = 10; |
41 | 38 |
|
42 | | - public IRClass find(AnnotationElements extractedModel) throws IOException { |
| 39 | + public CompiledRClassFinder(ProcessingEnvironment processingEnv) { |
| 40 | + super(processingEnv); |
| 41 | + } |
43 | 42 |
|
44 | | - File manifestFile = findManifestFile(); |
| 43 | + public IRClass find(AnnotationElements extractedModel) throws IOException { |
45 | 44 |
|
46 | | - String rClassPackage = extractPackage(manifestFile); |
| 45 | + File manifestFile = findManifestFile(); |
47 | 46 |
|
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 | | - } |
| 47 | + String rClassPackage = extractPackage(manifestFile); |
53 | 48 |
|
54 | | - Elements elementUtils = processingEnv.getElementUtils(); |
55 | | - String rClass = rClassPackage + ".R"; |
56 | | - TypeElement rType = elementUtils.getTypeElement(rClass); |
| 49 | + if (rClassPackage == null) { |
| 50 | + Messager messager = processingEnv.getMessager(); |
| 51 | + messager.printMessage(Kind.WARNING, "Could not find the AndroidManifest.xml file in " + manifestFile.getAbsolutePath()); |
| 52 | + return IRClass.EMPTY_R_CLASS; |
| 53 | + } |
57 | 54 |
|
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 | | - } |
| 55 | + Elements elementUtils = processingEnv.getElementUtils(); |
| 56 | + String rClass = rClassPackage + ".R"; |
| 57 | + TypeElement rType = elementUtils.getTypeElement(rClass); |
63 | 58 |
|
64 | | - return new RClass(rType); |
65 | | - } |
| 59 | + // processingEnv.getFiler().getResource(StandardLocation.SOURCE_OUTPUT, |
| 60 | + // rClassPackage, "R.java"); |
66 | 61 |
|
67 | | - private String extractPackage(File manifestFile) throws FileNotFoundException, IOException { |
68 | | - FileReader reader = new FileReader(manifestFile); |
69 | | - BufferedReader br = new BufferedReader(reader); |
| 62 | + if (rType == null) { |
| 63 | + Messager messager = processingEnv.getMessager(); |
| 64 | + messager.printMessage(Kind.WARNING, "The AndroidManifest.xml file was found, but not the compiled R class: " + rClass); |
| 65 | + return IRClass.EMPTY_R_CLASS; |
| 66 | + } |
70 | 67 |
|
71 | | - String line; |
72 | | - while ((line = br.readLine()) != null) { |
| 68 | + return new RClass(rType); |
| 69 | + } |
73 | 70 |
|
74 | | - ManifestPackageExtractor extractor = new ManifestPackageExtractor(line); |
| 71 | + private String extractPackage(File manifestFile) throws FileNotFoundException, IOException { |
| 72 | + FileReader reader = new FileReader(manifestFile); |
| 73 | + BufferedReader br = new BufferedReader(reader); |
75 | 74 |
|
76 | | - if (extractor.matches()) { |
77 | | - return extractor.extract(); |
78 | | - } |
79 | | - } |
80 | | - return null; |
81 | | - } |
| 75 | + String line; |
| 76 | + while ((line = br.readLine()) != null) { |
82 | 77 |
|
83 | | - private File findManifestFile() throws IOException { |
84 | | - Filer filer = processingEnv.getFiler(); |
| 78 | + ManifestPackageExtractor extractor = new ManifestPackageExtractor(line); |
85 | 79 |
|
86 | | - FileObject res = filer.getResource(StandardLocation.CLASS_OUTPUT, "", ""); |
87 | | - File projectRoot = new File(res.toUri()).getParentFile(); |
| 80 | + if (extractor.matches()) { |
| 81 | + return extractor.extract(); |
| 82 | + } |
| 83 | + } |
| 84 | + return null; |
| 85 | + } |
88 | 86 |
|
89 | | - File androidManifestFile = new File(projectRoot, "AndroidManifest.xml"); |
90 | | - return androidManifestFile; |
91 | | - } |
| 87 | + private File findManifestFile() throws IOException { |
| 88 | + Filer filer = processingEnv.getFiler(); |
| 89 | + |
| 90 | + JavaFileObject dummySourceFile = filer.createSourceFile("dummy" + System.currentTimeMillis()); |
| 91 | + String dummySourceFilePath = dummySourceFile.toUri().toString(); |
| 92 | + |
| 93 | + if (dummySourceFilePath.startsWith("file:")) { |
| 94 | + dummySourceFilePath = dummySourceFilePath.substring("file:".length()); |
| 95 | + } |
| 96 | + |
| 97 | + Messager messager = processingEnv.getMessager(); |
| 98 | + messager.printMessage(Kind.NOTE, "Dummy source file: " + dummySourceFilePath); |
| 99 | + |
| 100 | + File sourcesGenerationFolder = new File(dummySourceFilePath).getParentFile(); |
| 101 | + |
| 102 | + File projectRoot = sourcesGenerationFolder.getParentFile(); |
| 103 | + |
| 104 | + File androidManifestFile = new File(projectRoot, "AndroidManifest.xml"); |
| 105 | + for (int i = 0; i < MAX_PARENTS_FROM_SOURCE_FOLDER; i++) { |
| 106 | + if (androidManifestFile.exists()) { |
| 107 | + break; |
| 108 | + } else { |
| 109 | + if (projectRoot.getParentFile() != null) { |
| 110 | + projectRoot = projectRoot.getParentFile(); |
| 111 | + androidManifestFile = new File(projectRoot, "AndroidManifest.xml"); |
| 112 | + } else { |
| 113 | + break; |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + if (!androidManifestFile.exists()) { |
| 119 | + throw new IllegalStateException("Could not find the AndroidManifest.xml file, going up from path " + sourcesGenerationFolder.getAbsolutePath() + " found using dummy file [" + dummySourceFilePath + "] (max atempts: " + MAX_PARENTS_FROM_SOURCE_FOLDER + ")"); |
| 120 | + } else { |
| 121 | + messager.printMessage(Kind.NOTE, "AndroidManifest.xml file found: " + androidManifestFile.toString()); |
| 122 | + } |
| 123 | + |
| 124 | + return androidManifestFile; |
| 125 | + } |
92 | 126 |
|
93 | 127 | } |
0 commit comments