-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathDaggerAAIntegrationProcessor.java
261 lines (229 loc) · 9.77 KB
/
DaggerAAIntegrationProcessor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package info.piwai.cleanandroidcode;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Filer;
import javax.annotation.processing.Messager;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.ElementFilter;
import javax.lang.model.util.Elements;
import javax.tools.Diagnostic;
import javax.tools.FileObject;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;
import javax.tools.StandardLocation;
@SupportedSourceVersion(SourceVersion.RELEASE_6)
@SupportedAnnotationTypes({
/*
* Only supports @EActivity and @EFragment, since this is just a proof of
* concept. This can be extended quite easily though.
*/
//
"com.googlecode.androidannotations.annotations.EActivity", //
"com.googlecode.androidannotations.annotations.EFragment", //
})
public class DaggerAAIntegrationProcessor extends AbstractProcessor {
private static final String CACHE_PACKAGE_NAME = "";
private static final String CACHE_FILE_NAME = "DaggerAAIntegrationCache.txt";
/**
* We us this to avoid opening files twice in different rounds of the same
* processing, because it otherwhise fails. We shouldn't do this, but rather
* collect instructions on what we should do and output the module in the
* last round.
*/
boolean alreadyProcessed = false;
@Override
public synchronized void init(ProcessingEnvironment processingEnv) {
super.init(processingEnv);
Messager messager = processingEnv.getMessager();
messager.printMessage(Diagnostic.Kind.NOTE, "Init of DaggerAAIntegrationProcessor");
}
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
if (!alreadyProcessed) {
Set<TypeElement> annotatedElements = getAnnotatedElements(roundEnv);
generateDaggerModule(annotatedElements);
cacheAnnotatedElements(annotatedElements);
alreadyProcessed = true;
}
return false;
}
private Set<TypeElement> getAnnotatedElements(RoundEnvironment roundEnv) {
Set<TypeElement> annotationTypeElements = getAnnotationTypeElements();
Set<TypeElement> roundAnnotatedElements = getRoundAnnotatedElements(roundEnv, annotationTypeElements);
Set<TypeElement> cachedElements = loadCachedAnnotatedElement(roundAnnotatedElements);
Set<TypeElement> cachedAnnotatedElements = filterAnnotatedElements(cachedElements, annotationTypeElements);
Set<TypeElement> annotatedElements = new HashSet<TypeElement>();
annotatedElements.addAll(roundAnnotatedElements);
annotatedElements.addAll(cachedAnnotatedElements);
return annotatedElements;
}
private Set<TypeElement> filterAnnotatedElements(Set<TypeElement> typeElements, Set<TypeElement> annotationTypeElements) {
Set<TypeElement> annotatedTypeElements = new HashSet<TypeElement>();
for (TypeElement typeElement : typeElements) {
List<? extends AnnotationMirror> annotationMirrors = typeElement.getAnnotationMirrors();
boolean typeElementIsAnnotated = false;
for (AnnotationMirror annotationMirror : annotationMirrors) {
Element annotationElement = annotationMirror.getAnnotationType().asElement();
if (annotationElement instanceof TypeElement) {
TypeElement annotationTypeElement = (TypeElement) annotationElement;
if (annotationTypeElements.contains(annotationTypeElement)) {
typeElementIsAnnotated = true;
break;
}
} else {
reportNote(typeElement + " is annotated with an annotation element " + annotationElement + " that is not a TypeElement");
}
}
if (typeElementIsAnnotated) {
annotatedTypeElements.add(typeElement);
} else {
reportNote("Cached element " + typeElement + " is not annotated any more");
}
}
return annotatedTypeElements;
}
private Set<TypeElement> getAnnotationTypeElements() {
Set<String> annotationQualifiedNames = getSupportedAnnotationTypes();
Set<TypeElement> annotationTypeElements = new HashSet<TypeElement>();
Elements elementUtils = processingEnv.getElementUtils();
for (String annotationQualifiedName : annotationQualifiedNames) {
TypeElement annotationTypeElement = elementUtils.getTypeElement(annotationQualifiedName);
if (annotationTypeElement != null) {
annotationTypeElements.add(annotationTypeElement);
} else {
reportError("Could not find annotation type element for " + annotationQualifiedName);
}
}
return annotationTypeElements;
}
private Set<TypeElement> getRoundAnnotatedElements(RoundEnvironment roundEnv, Set<TypeElement> annotationTypeElements) {
Set<TypeElement> annotatedElements = new HashSet<TypeElement>();
for (TypeElement annotationTypeElement : annotationTypeElements) {
Set<? extends Element> elementsAnnotatedWithAnnotation = roundEnv.getElementsAnnotatedWith(annotationTypeElement);
Set<TypeElement> typesAnnotatedWithAnnotation = ElementFilter.typesIn(elementsAnnotatedWithAnnotation);
annotatedElements.addAll(typesAnnotatedWithAnnotation);
}
return annotatedElements;
}
/**
*
* @param roundAnnotatedElements
* the annotated elements found in the current round will not be
* loaded
*/
private Set<TypeElement> loadCachedAnnotatedElement(Set<TypeElement> roundAnnotatedElements) {
JavaFileManager.Location location = StandardLocation.SOURCE_OUTPUT;
Filer filer = processingEnv.getFiler();
try {
FileObject resource = filer.getResource(location, CACHE_PACKAGE_NAME, CACHE_FILE_NAME);
final boolean IGNORE_ENCODING_ERRORS = true;
BufferedReader reader = new BufferedReader(resource.openReader(IGNORE_ENCODING_ERRORS));
Elements elementUtils = processingEnv.getElementUtils();
HashSet<TypeElement> cachedAnnotatedTypeElements = new HashSet<TypeElement>();
String annotatedElementQualifiedName;
while ((annotatedElementQualifiedName = reader.readLine()) != null) {
if (annotatedElementQualifiedName.startsWith("#")) {
continue;
}
TypeElement annotatedTypeElement = elementUtils.getTypeElement(annotatedElementQualifiedName);
if (annotatedTypeElement != null) {
if (!roundAnnotatedElements.contains(annotatedTypeElement)) {
cachedAnnotatedTypeElements.add(annotatedTypeElement);
}
} else {
reportNote("Could not find cached annotated type element for " + annotatedTypeElement);
}
}
reader.close();
return cachedAnnotatedTypeElements;
} catch (Exception e) {
reportNote("Could not load annotated elements cache (fine in case of full build)", e);
return Collections.emptySet();
}
}
private void generateDaggerModule(Set<TypeElement> annotatedElements) {
reportNote("Entry points found: " + annotatedElements);
Filer filer = processingEnv.getFiler();
try {
String packageName = "info.piwai.cleanandroidcode";
String moduleClassName = "EntryPointsModule";
JavaFileObject sourceFile = filer.createSourceFile(packageName + "." + moduleClassName);
BufferedWriter writer = new BufferedWriter(sourceFile.openWriter());
writer.write("package " + packageName + ";");
writer.newLine();
writer.write("import dagger.Module;");
writer.newLine();
writer.write("@Module(");
writer.newLine();
writer.write(" entryPoints = {");
writer.newLine();
for (TypeElement typeElement : annotatedElements) {
String qualifiedName = typeElement.getQualifiedName().toString();
String aaGeneratedQualifiedName = qualifiedName + "_";
writer.write(" " + aaGeneratedQualifiedName + ".class,");
writer.newLine();
}
writer.write(" },");
writer.newLine();
writer.write(" complete = false");
writer.newLine();
writer.write(")");
writer.newLine();
writer.write("public final class " + moduleClassName + " {");
writer.newLine();
writer.write("}");
writer.newLine();
writer.close();
} catch (IOException e) {
reportNote("Could not generate Dagger module", e);
}
}
private void cacheAnnotatedElements(Set<TypeElement> annotatedElements) {
try {
TypeElement[] annotatedElementsArray = annotatedElements.toArray(new TypeElement[0]);
JavaFileManager.Location location = StandardLocation.SOURCE_OUTPUT;
Filer filer = processingEnv.getFiler();
FileObject resource = filer.createResource(location, CACHE_PACKAGE_NAME, CACHE_FILE_NAME, annotatedElementsArray);
BufferedWriter writer = new BufferedWriter(resource.openWriter());
writer.write("# This file has been generated, please do not edit it");
writer.newLine();
for (TypeElement element : annotatedElements) {
writer.write(element.getQualifiedName().toString());
writer.newLine();
}
writer.close();
} catch (IOException e) {
reportNote("Could not cache annotated elements " + annotatedElements.toString() + " (fine in case of full build)", e);
}
}
private void reportError(String message) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, message);
}
private void reportNote(String message) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, message);
}
private void reportNote(String message, Throwable throwable) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, message + "\nException:\n" + stackTraceToString(throwable));
}
private String stackTraceToString(Throwable throwable) {
StringWriter writer = new StringWriter();
PrintWriter pw = new PrintWriter(writer);
throwable.printStackTrace(pw);
return writer.toString();
}
}