|
15 | 15 | */ |
16 | 16 | package org.androidannotations.helper; |
17 | 17 |
|
18 | | -import com.sun.codemodel.*; |
| 18 | +import static com.sun.codemodel.JExpr._new; |
| 19 | +import static com.sun.codemodel.JExpr.lit; |
| 20 | +import static org.androidannotations.helper.ModelConstants.GENERATION_SUFFIX; |
| 21 | + |
| 22 | +import java.io.StringWriter; |
| 23 | +import java.lang.annotation.Annotation; |
| 24 | +import java.lang.reflect.Field; |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.Collections; |
| 27 | +import java.util.HashMap; |
| 28 | +import java.util.LinkedHashMap; |
| 29 | +import java.util.List; |
| 30 | +import java.util.Map; |
| 31 | + |
| 32 | +import javax.lang.model.element.AnnotationMirror; |
| 33 | +import javax.lang.model.element.AnnotationValue; |
| 34 | +import javax.lang.model.element.Element; |
| 35 | +import javax.lang.model.element.ExecutableElement; |
| 36 | +import javax.lang.model.element.TypeElement; |
| 37 | +import javax.lang.model.element.TypeParameterElement; |
| 38 | +import javax.lang.model.element.VariableElement; |
| 39 | +import javax.lang.model.type.ArrayType; |
| 40 | +import javax.lang.model.type.DeclaredType; |
| 41 | +import javax.lang.model.type.TypeKind; |
| 42 | +import javax.lang.model.type.TypeMirror; |
| 43 | +import javax.lang.model.type.WildcardType; |
| 44 | +import javax.lang.model.util.ElementFilter; |
| 45 | +import javax.lang.model.util.Types; |
| 46 | + |
19 | 47 | import org.androidannotations.annotations.EBean; |
20 | 48 | import org.androidannotations.holder.EBeanHolder; |
21 | 49 | import org.androidannotations.holder.EComponentHolder; |
22 | 50 | import org.androidannotations.holder.GeneratedClassHolder; |
23 | 51 |
|
24 | | -import javax.lang.model.element.*; |
25 | | -import javax.lang.model.type.*; |
26 | | -import javax.lang.model.util.ElementFilter; |
27 | | -import javax.lang.model.util.Types; |
28 | | -import java.io.StringWriter; |
29 | | -import java.lang.reflect.Field; |
30 | | -import java.util.*; |
31 | | - |
32 | | -import static com.sun.codemodel.JExpr._new; |
33 | | -import static com.sun.codemodel.JExpr.lit; |
34 | | -import static org.androidannotations.helper.ModelConstants.GENERATION_SUFFIX; |
| 52 | +import com.sun.codemodel.JAnnotatable; |
| 53 | +import com.sun.codemodel.JAnnotationUse; |
| 54 | +import com.sun.codemodel.JBlock; |
| 55 | +import com.sun.codemodel.JClass; |
| 56 | +import com.sun.codemodel.JCodeModel; |
| 57 | +import com.sun.codemodel.JDefinedClass; |
| 58 | +import com.sun.codemodel.JExpr; |
| 59 | +import com.sun.codemodel.JExpression; |
| 60 | +import com.sun.codemodel.JFieldRef; |
| 61 | +import com.sun.codemodel.JFormatter; |
| 62 | +import com.sun.codemodel.JInvocation; |
| 63 | +import com.sun.codemodel.JMethod; |
| 64 | +import com.sun.codemodel.JMod; |
| 65 | +import com.sun.codemodel.JStatement; |
| 66 | +import com.sun.codemodel.JSuperWildcard; |
| 67 | +import com.sun.codemodel.JType; |
| 68 | +import com.sun.codemodel.JVar; |
35 | 69 |
|
36 | 70 | public class APTCodeModelHelper { |
37 | 71 |
|
@@ -157,9 +191,12 @@ public JMethod overrideAnnotatedMethod(ExecutableElement executableElement, Gene |
157 | 191 | String methodName = executableElement.getSimpleName().toString(); |
158 | 192 | JClass returnType = typeMirrorToJClass(executableElement.getReturnType(), holder, actualTypes); |
159 | 193 | JMethod method = holder.getGeneratedClass().method(JMod.PUBLIC, returnType, methodName); |
160 | | - method.annotate(Override.class); |
161 | 194 | addNonAAAnotations(method, executableElement.getAnnotationMirrors(), holder); |
162 | 195 |
|
| 196 | + if (!hasAnnotation(method, Override.class)) { |
| 197 | + method.annotate(Override.class); |
| 198 | + } |
| 199 | + |
163 | 200 | for (Map.Entry<String, JClass> typeDeclaration : methodTypes.entrySet()) { |
164 | 201 | method.generify(typeDeclaration.getKey(), typeDeclaration.getValue()); |
165 | 202 | } |
@@ -217,11 +254,31 @@ public void addNonAAAnotations(JAnnotatable annotatable, List<? extends Annotati |
217 | 254 | public void addAnnotation(JAnnotatable annotatable, AnnotationMirror annotationMirror, GeneratedClassHolder holder) { |
218 | 255 | Map<? extends ExecutableElement, ? extends AnnotationValue> parameters = annotationMirror.getElementValues(); |
219 | 256 |
|
220 | | - JAnnotationUse annotate = annotatable.annotate(typeMirrorToJClass(annotationMirror.getAnnotationType(), holder)); |
| 257 | + if (!hasAnnotation(annotatable, annotationMirror)) { |
| 258 | + JClass annotation = typeMirrorToJClass(annotationMirror.getAnnotationType(), holder); |
| 259 | + JAnnotationUse annotate = annotatable.annotate(annotation); |
| 260 | + |
| 261 | + for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> param : parameters.entrySet()) { |
| 262 | + param.getValue().accept(new AnnotationParamExtractor(annotate, holder, this), param.getKey().getSimpleName().toString()); |
| 263 | + } |
| 264 | + } |
| 265 | + } |
| 266 | + |
| 267 | + private boolean hasAnnotation(JAnnotatable annotatable, AnnotationMirror annotationMirror) { |
| 268 | + return hasAnnotation(annotatable, annotationMirror.getAnnotationType().toString()); |
| 269 | + } |
221 | 270 |
|
222 | | - for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> param : parameters.entrySet()) { |
223 | | - param.getValue().accept(new AnnotationParamExtractor(annotate, holder, this), param.getKey().getSimpleName().toString()); |
| 271 | + private boolean hasAnnotation(JAnnotatable annotatable, Class<? extends Annotation> annotationClass) { |
| 272 | + return hasAnnotation(annotatable, annotationClass.getCanonicalName()); |
| 273 | + } |
| 274 | + |
| 275 | + private boolean hasAnnotation(JAnnotatable annotatable, String annotationFQN) { |
| 276 | + for (JAnnotationUse annotation : annotatable.annotations()) { |
| 277 | + if (annotation.getAnnotationClass().fullName().equals(annotationFQN)) { |
| 278 | + return true; |
| 279 | + } |
224 | 280 | } |
| 281 | + return false; |
225 | 282 | } |
226 | 283 |
|
227 | 284 | public void callSuperMethod(JMethod superMethod, GeneratedClassHolder holder, JBlock callBlock) { |
@@ -339,7 +396,7 @@ public JDefinedClass createDelegatingAnonymousRunnableClass(EComponentHolder hol |
339 | 396 | /** |
340 | 397 | * Gets all of the methods of the class and includes the methods of any |
341 | 398 | * implemented interfaces. |
342 | | - * |
| 399 | + * |
343 | 400 | * @param typeElement |
344 | 401 | * @return full list of methods. |
345 | 402 | */ |
|
0 commit comments