Skip to content

Commit 03c1edf

Browse files
committed
Improvements based on the Hibernate Metamodel Generator, as well as more comments
1 parent 98d4f37 commit 03c1edf

3 files changed

Lines changed: 41 additions & 14 deletions

File tree

‎AndroidAnnotations/src/main/java/com/googlecode/androidannotations/AndroidAnnotationProcessor.java‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Set;
2222

2323
import javax.annotation.processing.Messager;
24+
import javax.annotation.processing.ProcessingEnvironment;
2425
import javax.annotation.processing.RoundEnvironment;
2526
import javax.annotation.processing.SupportedSourceVersion;
2627
import javax.lang.model.SourceVersion;
@@ -145,24 +146,43 @@
145146
StringArrayRes.class })
146147
@SupportedSourceVersion(SourceVersion.RELEASE_6)
147148
public class AndroidAnnotationProcessor extends AnnotatedAbstractProcessor {
149+
150+
@Override
151+
public synchronized void init(ProcessingEnvironment processingEnv) {
152+
super.init(processingEnv);
153+
154+
Messager messager = processingEnv.getMessager();
155+
messager.printMessage(Diagnostic.Kind.NOTE, "Starting AndroidAnnotations compile time annotation processing");
156+
}
157+
158+
/**
159+
* We do not need multiple round processing, since the generated classes do
160+
* not need to be processed.
161+
*/
162+
private boolean alreadyProcessed = false;
148163

149164
@Override
150165
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
151-
Messager messager = processingEnv.getMessager();
152-
messager.printMessage(Diagnostic.Kind.NOTE, "AndroidAnnotations compile time annotation processing");
153166
try {
154167
processThrowing(annotations, roundEnv);
155168
} catch (Exception e) {
156169
handleException(annotations, roundEnv, e);
157170
}
158-
return false;
171+
return true;
159172
}
160173

161174
private void processThrowing(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) throws IOException {
175+
176+
if (roundEnv.processingOver() || annotations.size() == 0 || alreadyProcessed) {
177+
return;
178+
} else {
179+
alreadyProcessed = true;
180+
}
181+
162182
AnnotationElementsHolder extractedModel = extractAnnotations(annotations, roundEnv);
163183

164184
AndroidManifest androidManifest = extractAndroidManifest();
165-
185+
166186
IRClass rClass = findRClasses(androidManifest);
167187

168188
AndroidSystemServices androidSystemServices = new AndroidSystemServices();

‎AndroidAnnotations/src/main/java/com/googlecode/androidannotations/generation/SourceCodewriter.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ public SourceCodewriter(Filer filer) {
3030
@Override
3131
public OutputStream openBinary(JPackage pkg, String fileName) throws IOException {
3232
String qualifiedClassName = toQualifiedClassName(pkg, fileName);
33-
3433
try {
3534
JavaFileObject sourceFile = filer.createSourceFile(qualifiedClassName);
3635
return sourceFile.openOutputStream();
@@ -40,7 +39,9 @@ public OutputStream openBinary(JPackage pkg, String fileName) throws IOException
4039
* cannot delete existing files, unless using a dirty hack. Files a
4140
* created twice when the same file is created from different
4241
* annotation rounds. Happens when renaming classes, and for
43-
* Background executor.
42+
* Background executor. It also probably means I didn't fully
43+
* understand how annotation processing works. If anyone can point
44+
* me out...
4445
*/
4546
return VOID_OUTPUT_STREAM;
4647
}

‎AndroidAnnotations/src/main/java/com/googlecode/androidannotations/helper/AndroidManifestFinder.java‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,35 @@
1919
import org.w3c.dom.NodeList;
2020

2121
public class AndroidManifestFinder {
22-
22+
2323
private static final int MAX_PARENTS_FROM_SOURCE_FOLDER = 10;
24-
24+
2525
private ProcessingEnvironment processingEnv;
26-
26+
2727
public AndroidManifestFinder(ProcessingEnvironment processingEnv) {
2828
this.processingEnv = processingEnv;
2929
}
30-
31-
public AndroidManifest extractAndroidManifest(){
30+
31+
public AndroidManifest extractAndroidManifest() {
3232
try {
3333
return extractAndroidManifestThrowing();
3434
} catch (Exception e) {
3535
throw new RuntimeException(e);
3636
}
3737
}
38-
39-
private AndroidManifest extractAndroidManifestThrowing() throws Exception{
38+
39+
private AndroidManifest extractAndroidManifestThrowing() throws Exception {
4040
File androidManifestFile = findManifestFileThrowing();
4141
return parseThrowing(androidManifestFile);
4242
}
4343

44+
/**
45+
* We use a dirty trick to find the AndroidManifest.xml file, since it's not
46+
* available in the classpath. The idea is quite simple : create a fake
47+
* class file, retrieve its URI, and start going up in parent folders to
48+
* find the AndroidManifest.xml file. Any better solution will be
49+
* appreciated.
50+
*/
4451
private File findManifestFileThrowing() throws IOException {
4552
Filer filer = processingEnv.getFiler();
4653

@@ -119,5 +126,4 @@ private AndroidManifest parseThrowing(File androidManifestFile) throws Exception
119126
return new AndroidManifest(applicationPackage, activityQualifiedNames);
120127
}
121128

122-
123129
}

0 commit comments

Comments
 (0)