Skip to content

Commit 32d7434

Browse files
committed
better printing of unexpected exceptions
1 parent 40d94a1 commit 32d7434

1 file changed

Lines changed: 18 additions & 14 deletions

File tree

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

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
package com.googlecode.androidannotations;
1717

1818
import java.io.IOException;
19+
import java.io.PrintWriter;
20+
import java.io.StringWriter;
1921
import java.util.Set;
2022

2123
import javax.annotation.processing.Messager;
@@ -88,9 +90,9 @@
8890
import com.googlecode.androidannotations.processor.AnnotatedAbstractProcessor;
8991
import com.googlecode.androidannotations.processor.SupportedAnnotationClasses;
9092
import com.googlecode.androidannotations.rclass.AndroidRClassFinder;
93+
import com.googlecode.androidannotations.rclass.CompiledRClassFinder;
9194
import com.googlecode.androidannotations.rclass.CoumpoundRClass;
9295
import com.googlecode.androidannotations.rclass.IRClass;
93-
import com.googlecode.androidannotations.rclass.CompiledRClassFinder;
9496
import com.googlecode.androidannotations.rclass.RClassFinder;
9597
import com.googlecode.androidannotations.validation.BeforeCreateValidator;
9698
import com.googlecode.androidannotations.validation.ClickValidator;
@@ -152,26 +154,28 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
152154
try {
153155
processThrowing(annotations, roundEnv);
154156
} catch (Exception e) {
157+
158+
String errorMessage = "Unexpected error. Please report an issue on AndroidAnnotations, with the following content: " + stackTraceToString(e);
159+
messager.printMessage(Diagnostic.Kind.ERROR, errorMessage);
155160

156-
Throwable rootCause = e;
157-
while (rootCause.getCause() != null) {
158-
rootCause = rootCause.getCause();
159-
}
160-
161-
StackTraceElement firstElement = e.getStackTrace()[0];
162-
StackTraceElement rootFirstElement = rootCause.getStackTrace()[0];
163-
String errorMessage = e.toString() + " " + firstElement.toString() + " root: " + rootCause.toString() + " " + rootFirstElement.toString();
164-
165-
messager.printMessage(Diagnostic.Kind.ERROR, "Unexpected annotation processing exception: " + errorMessage);
166-
e.printStackTrace();
167-
161+
/*
162+
* Printing exception as an error on a random element. The exception is not related to this element, but otherwise it wouldn't show up in eclipse.
163+
*/
164+
168165
Element element = roundEnv.getElementsAnnotatedWith(annotations.iterator().next()).iterator().next();
169-
messager.printMessage(Diagnostic.Kind.ERROR, "Unexpected annotation processing exception (not related to this element, but otherwise it wouldn't show up in eclipse) : " + errorMessage, element);
166+
messager.printMessage(Diagnostic.Kind.ERROR, errorMessage, element);
170167
}
171168

172169
return false;
173170
}
174171

172+
private String stackTraceToString(Throwable e) {
173+
StringWriter writer = new StringWriter();
174+
PrintWriter pw = new PrintWriter(writer);
175+
e.printStackTrace(pw);
176+
return writer.toString();
177+
}
178+
175179
private void processThrowing(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) throws IOException {
176180
AnnotationElementsHolder extractedModel = extractAnnotations(annotations, roundEnv);
177181

0 commit comments

Comments
 (0)