Skip to content

Commit 1fb5909

Browse files
committed
Get a fresh instance of the element and the annotation mirror to report the error
1 parent 1ed0c4a commit 1fb5909

1 file changed

Lines changed: 86 additions & 14 deletions

File tree

  • AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/logger/appender

‎AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/logger/appender/MessagerAppender.java‎

Lines changed: 86 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
22
* Copyright (C) 2010-2016 eBusiness Information, Excilys Group
3+
* Copyright (C) 2016-2017 the AndroidAnnotations project
34
*
45
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
56
* use this file except in compliance with the License. You may obtain a copy of
@@ -21,6 +22,9 @@
2122
import javax.annotation.processing.Messager;
2223
import javax.lang.model.element.AnnotationMirror;
2324
import javax.lang.model.element.Element;
25+
import javax.lang.model.element.ElementKind;
26+
import javax.lang.model.element.ExecutableElement;
27+
import javax.lang.model.element.VariableElement;
2428
import javax.tools.Diagnostic.Kind;
2529

2630
import org.androidannotations.logger.Level;
@@ -42,9 +46,7 @@ public void open() {
4246
}
4347

4448
@Override
45-
public void append(Level level, Element element,
46-
AnnotationMirror annotationMirror, String message) {
47-
49+
public void append(Level level, Element element, AnnotationMirror annotationMirror, String message) {
4850
if (messager == null) {
4951
return;
5052
}
@@ -61,8 +63,8 @@ public void append(Level level, Element element,
6163
public synchronized void close(boolean lastRound) {
6264
if (lastRound) {
6365
for (Message error : errors) {
64-
messager.printMessage(error.kind, error.message,
65-
error.element, error.annotationMirror);
66+
ElementDetails elementDetails = error.getElementDetails();
67+
messager.printMessage(error.kind, error.message, elementDetails.getElement(), elementDetails.getAnnotationMirror());
6668
}
6769
errors.clear();
6870
}
@@ -85,20 +87,90 @@ private Kind resolveKind(Level level) {
8587
}
8688

8789
private class Message {
88-
Kind kind;
89-
String message;
90-
Element element;
91-
AnnotationMirror annotationMirror;
92-
93-
Message(Kind kind, String message, Element element,
94-
AnnotationMirror annotationMirror) {
95-
super();
90+
private final Kind kind;
91+
private final String message;
92+
private final String annotationMirrorString;
93+
private final List<String> elements = new LinkedList<>();
94+
95+
Message(Kind kind, String message, Element element, AnnotationMirror annotationMirror) {
9696
this.kind = kind;
9797
this.message = message;
98+
this.annotationMirrorString = annotationMirror == null ? null : annotationMirror.toString();
99+
100+
if (element != null) {
101+
Element enclosingElement = element;
102+
do {
103+
elements.add(0, enclosingElement.toString());
104+
enclosingElement = enclosingElement.getEnclosingElement();
105+
} while (!enclosingElement.getKind().equals(ElementKind.PACKAGE));
106+
}
107+
}
108+
109+
private AnnotationMirror getAnnotationMirror(Element element) {
110+
if (element == null) {
111+
return null;
112+
}
113+
114+
for (AnnotationMirror mirror : element.getAnnotationMirrors()) {
115+
if (mirror.toString().equals(annotationMirrorString)) {
116+
return mirror;
117+
}
118+
}
119+
return null;
120+
}
121+
122+
private Element getElement() {
123+
if (elements.isEmpty()) {
124+
return null;
125+
}
126+
127+
List<String> localElements = new LinkedList<>(elements);
128+
Element element = processingEnv.getElementUtils().getTypeElement(localElements.remove(0));
129+
while (localElements.size() > 0) {
130+
if (element instanceof ExecutableElement) {
131+
ExecutableElement method = (ExecutableElement) element;
132+
for (VariableElement param : method.getParameters()) {
133+
if (param.toString().equals(localElements.get(0))) {
134+
localElements.remove(0);
135+
element = param;
136+
break;
137+
}
138+
}
139+
} else {
140+
for (Element elem : element.getEnclosedElements()) {
141+
if (elem.toString().equals(localElements.get(0))) {
142+
localElements.remove(0);
143+
element = elem;
144+
break;
145+
}
146+
}
147+
}
148+
}
149+
150+
return element;
151+
}
152+
153+
private ElementDetails getElementDetails() {
154+
Element element = getElement();
155+
return new ElementDetails(element, getAnnotationMirror(element));
156+
}
157+
}
158+
159+
private static class ElementDetails {
160+
private final Element element;
161+
private final AnnotationMirror annotationMirror;
162+
163+
ElementDetails(Element element, AnnotationMirror annotationMirror) {
98164
this.element = element;
99165
this.annotationMirror = annotationMirror;
100166
}
101167

102-
}
168+
public Element getElement() {
169+
return element;
170+
}
103171

172+
public AnnotationMirror getAnnotationMirror() {
173+
return annotationMirror;
174+
}
175+
}
104176
}

0 commit comments

Comments
 (0)