From 585cd2d9c4cea97afcbc9db2da85f1316afad78e Mon Sep 17 00:00:00 2001 From: yDelouis Date: Mon, 28 Apr 2014 19:15:23 +0200 Subject: [PATCH 1/5] Copy non AA annotations in generated class Annotations on class, methods and method parameters are copied in generated class. SubscribeHandler and ProduceHandler are no longer necessaries. --- .../handler/AnnotationHandlers.java | 22 +- .../handler/ProduceHandler.java | 76 ------- .../handler/SubscribeHandler.java | 76 ------- .../helper/APTCodeModelHelper.java | 188 ++++++++---------- .../helper/AnnotationHelper.java | 43 ++-- .../holder/BaseGeneratedClassHolder.java | 20 +- 6 files changed, 108 insertions(+), 317 deletions(-) delete mode 100644 AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/handler/ProduceHandler.java delete mode 100644 AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/handler/SubscribeHandler.java diff --git a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/handler/AnnotationHandlers.java b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/handler/AnnotationHandlers.java index e95c9fa0e3..0ed955e2ce 100644 --- a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/handler/AnnotationHandlers.java +++ b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/handler/AnnotationHandlers.java @@ -15,22 +15,7 @@ */ package org.androidannotations.handler; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -import javax.annotation.processing.ProcessingEnvironment; - -import org.androidannotations.handler.rest.DeleteHandler; -import org.androidannotations.handler.rest.GetHandler; -import org.androidannotations.handler.rest.HeadHandler; -import org.androidannotations.handler.rest.OptionsHandler; -import org.androidannotations.handler.rest.PostHandler; -import org.androidannotations.handler.rest.PutHandler; -import org.androidannotations.handler.rest.RestHandler; -import org.androidannotations.handler.rest.RestServiceHandler; +import org.androidannotations.handler.rest.*; import org.androidannotations.helper.AndroidManifest; import org.androidannotations.helper.OptionsHelper; import org.androidannotations.holder.GeneratedClassHolder; @@ -40,6 +25,9 @@ import org.androidannotations.process.ProcessHolder; import org.androidannotations.rclass.IRClass; +import javax.annotation.processing.ProcessingEnvironment; +import java.util.*; + public class AnnotationHandlers { private List> annotationHandlers = new ArrayList>(); @@ -109,8 +97,6 @@ public AnnotationHandlers(ProcessingEnvironment processingEnvironment) { add(new SeekBarTouchStartHandler(processingEnvironment)); add(new SeekBarTouchStopHandler(processingEnvironment)); add(new ServiceActionHandler(processingEnvironment)); - add(new SubscribeHandler(processingEnvironment)); - add(new ProduceHandler(processingEnvironment)); add(new InstanceStateHandler(processingEnvironment)); add(new HttpsClientHandler(processingEnvironment)); add(new OnActivityResultHandler(processingEnvironment)); diff --git a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/handler/ProduceHandler.java b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/handler/ProduceHandler.java deleted file mode 100644 index 19ffbecfdf..0000000000 --- a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/handler/ProduceHandler.java +++ /dev/null @@ -1,76 +0,0 @@ -/** - * Copyright (C) 2010-2014 eBusiness Information, Excilys Group - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed To in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package org.androidannotations.handler; - -import static org.androidannotations.helper.CanonicalNameConstants.PRODUCE; - -import javax.annotation.processing.ProcessingEnvironment; -import javax.lang.model.element.Element; -import javax.lang.model.element.ExecutableElement; - -import org.androidannotations.helper.APTCodeModelHelper; -import org.androidannotations.helper.CanonicalNameConstants; -import org.androidannotations.helper.TargetAnnotationHelper; -import org.androidannotations.holder.EComponentHolder; -import org.androidannotations.model.AnnotationElements; -import org.androidannotations.process.IsValid; - -import com.sun.codemodel.JMethod; - -public class ProduceHandler extends BaseAnnotationHandler { - - private final TargetAnnotationHelper annotationHelper; - private final APTCodeModelHelper codeModelHelper = new APTCodeModelHelper(); - - public ProduceHandler(ProcessingEnvironment processingEnvironment) { - super(CanonicalNameConstants.PRODUCE, processingEnvironment); - annotationHelper = new TargetAnnotationHelper(processingEnv, getTarget()); - } - - @Override - public void validate(Element element, AnnotationElements validatedElements, IsValid valid) { - if (!annotationHelper.enclosingElementHasEnhancedComponentAnnotation(element)) { - valid.invalidate(); - return; - } - - ExecutableElement executableElement = (ExecutableElement) element; - - /* - * We check that twice to skip invalid annotated elements - */ - validatorHelper.enclosingElementHasEnhancedComponentAnnotation(executableElement, validatedElements, valid); - - validatorHelper.returnTypeIsNotVoid(executableElement, valid); - - validatorHelper.isPublic(element, valid); - - validatorHelper.doesntThrowException(executableElement, valid); - - validatorHelper.isNotFinal(element, valid); - - validatorHelper.param.zeroParameter(executableElement, valid); - } - - @Override - public void process(Element element, EComponentHolder holder) throws Exception { - ExecutableElement executableElement = (ExecutableElement) element; - - JMethod delegatingMethod = codeModelHelper.overrideAnnotatedMethod(executableElement, holder); - - delegatingMethod.annotate(refClass(PRODUCE)); - } -} diff --git a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/handler/SubscribeHandler.java b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/handler/SubscribeHandler.java deleted file mode 100644 index 64377149fb..0000000000 --- a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/handler/SubscribeHandler.java +++ /dev/null @@ -1,76 +0,0 @@ -/** - * Copyright (C) 2010-2014 eBusiness Information, Excilys Group - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed To in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package org.androidannotations.handler; - -import static org.androidannotations.helper.CanonicalNameConstants.SUBSCRIBE; - -import javax.annotation.processing.ProcessingEnvironment; -import javax.lang.model.element.Element; -import javax.lang.model.element.ExecutableElement; - -import org.androidannotations.helper.APTCodeModelHelper; -import org.androidannotations.helper.CanonicalNameConstants; -import org.androidannotations.helper.TargetAnnotationHelper; -import org.androidannotations.holder.EComponentHolder; -import org.androidannotations.model.AnnotationElements; -import org.androidannotations.process.IsValid; - -import com.sun.codemodel.JMethod; - -public class SubscribeHandler extends BaseAnnotationHandler { - - private final TargetAnnotationHelper annotationHelper; - private final APTCodeModelHelper codeModelHelper = new APTCodeModelHelper(); - - public SubscribeHandler(ProcessingEnvironment processingEnvironment) { - super(CanonicalNameConstants.SUBSCRIBE, processingEnvironment); - annotationHelper = new TargetAnnotationHelper(processingEnv, getTarget()); - } - - @Override - public void validate(Element element, AnnotationElements validatedElements, IsValid valid) { - if (!annotationHelper.enclosingElementHasEnhancedComponentAnnotation(element)) { - valid.invalidate(); - return; - } - - ExecutableElement executableElement = (ExecutableElement) element; - - /* - * We check that twice to skip invalid annotated elements - */ - validatorHelper.enclosingElementHasEnhancedComponentAnnotation(executableElement, validatedElements, valid); - - validatorHelper.returnTypeIsVoid(executableElement, valid); - - validatorHelper.isPublic(element, valid); - - validatorHelper.doesntThrowException(executableElement, valid); - - validatorHelper.isNotFinal(element, valid); - - validatorHelper.param.hasExactlyOneParameter(executableElement, valid); - } - - @Override - public void process(Element element, EComponentHolder holder) throws Exception { - ExecutableElement executableElement = (ExecutableElement) element; - - JMethod delegatingMethod = codeModelHelper.overrideAnnotatedMethod(executableElement, holder); - - delegatingMethod.annotate(refClass(SUBSCRIBE)); - } -} diff --git a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/APTCodeModelHelper.java b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/APTCodeModelHelper.java index 735791daf6..bb158a84f5 100644 --- a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/APTCodeModelHelper.java +++ b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/APTCodeModelHelper.java @@ -15,52 +15,23 @@ */ package org.androidannotations.helper; -import static com.sun.codemodel.JExpr._new; -import static com.sun.codemodel.JExpr.lit; -import static org.androidannotations.helper.ModelConstants.GENERATION_SUFFIX; - -import java.io.StringWriter; -import java.lang.reflect.Field; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -import javax.lang.model.element.Element; -import javax.lang.model.element.ExecutableElement; -import javax.lang.model.element.TypeElement; -import javax.lang.model.element.TypeParameterElement; -import javax.lang.model.element.VariableElement; -import javax.lang.model.type.ArrayType; -import javax.lang.model.type.DeclaredType; -import javax.lang.model.type.TypeKind; -import javax.lang.model.type.TypeMirror; -import javax.lang.model.type.WildcardType; -import javax.lang.model.util.ElementFilter; -import javax.lang.model.util.Types; - +import com.sun.codemodel.*; import org.androidannotations.annotations.EBean; import org.androidannotations.holder.EBeanHolder; import org.androidannotations.holder.EComponentHolder; import org.androidannotations.holder.GeneratedClassHolder; -import com.sun.codemodel.JBlock; -import com.sun.codemodel.JClass; -import com.sun.codemodel.JCodeModel; -import com.sun.codemodel.JDefinedClass; -import com.sun.codemodel.JExpr; -import com.sun.codemodel.JExpression; -import com.sun.codemodel.JFieldRef; -import com.sun.codemodel.JFormatter; -import com.sun.codemodel.JInvocation; -import com.sun.codemodel.JMethod; -import com.sun.codemodel.JMod; -import com.sun.codemodel.JStatement; -import com.sun.codemodel.JSuperWildcard; -import com.sun.codemodel.JType; -import com.sun.codemodel.JVar; +import javax.lang.model.element.*; +import javax.lang.model.type.*; +import javax.lang.model.util.ElementFilter; +import javax.lang.model.util.Types; +import java.io.StringWriter; +import java.lang.reflect.Field; +import java.util.*; + +import static com.sun.codemodel.JExpr._new; +import static com.sun.codemodel.JExpr.lit; +import static org.androidannotations.helper.ModelConstants.GENERATION_SUFFIX; public class APTCodeModelHelper { @@ -69,50 +40,12 @@ public JClass typeMirrorToJClass(TypeMirror type, GeneratedClassHolder holder) { } private JClass typeMirrorToJClass(TypeMirror type, GeneratedClassHolder holder, Map substitute) { - if (type instanceof DeclaredType) { - DeclaredType declaredType = (DeclaredType) type; - - String declaredTypeName = declaredType.asElement().toString(); - - JClass declaredClass = holder.refClass(declaredTypeName); - - List typeArguments = declaredType.getTypeArguments(); - - List typeArgumentJClasses = new ArrayList(); - for (TypeMirror typeArgument : typeArguments) { - typeArgumentJClasses.add(typeMirrorToJClass(typeArgument, holder, substitute)); - } - if (typeArgumentJClasses.size() > 0) { - declaredClass = declaredClass.narrow(typeArgumentJClasses); - } - - return declaredClass; + return typeMirrorToJClass((DeclaredType) type, holder, substitute); } else if (type instanceof WildcardType) { - WildcardType wildcardType = (WildcardType) type; - - TypeMirror bound = wildcardType.getExtendsBound(); - if (bound == null) { - bound = wildcardType.getSuperBound(); - if (bound == null) { - return holder.classes().OBJECT.wildcard(); - } - return superWildcard(typeMirrorToJClass(bound, holder, substitute)); - } - - TypeMirror extendsBound = wildcardType.getExtendsBound(); - - if (extendsBound == null) { - return holder.classes().OBJECT.wildcard(); - } else { - return typeMirrorToJClass(extendsBound, holder, substitute).wildcard(); - } + return typeMirrorToJClass((WildcardType) type, holder, substitute); } else if (type instanceof ArrayType) { - ArrayType arrayType = (ArrayType) type; - - JClass refClass = typeMirrorToJClass(arrayType.getComponentType(), holder, substitute); - - return refClass.array(); + return typeMirrorToJClass((ArrayType) type, holder, substitute); } else { TypeMirror substituted = substitute.get(type.toString()); if (substituted != null && type != substituted) { @@ -122,20 +55,48 @@ private JClass typeMirrorToJClass(TypeMirror type, GeneratedClassHolder holder, } } - private JClass superWildcard(final JClass bound) { - return new JSuperWildcard(bound); + private JClass typeMirrorToJClass(DeclaredType declaredType, GeneratedClassHolder holder, Map substitute) { + String declaredTypeName = declaredType.asElement().toString(); + + JClass declaredClass = holder.refClass(declaredTypeName); + + List typeArguments = declaredType.getTypeArguments(); + + List typeArgumentJClasses = new ArrayList(); + for (TypeMirror typeArgument : typeArguments) { + typeArgumentJClasses.add(typeMirrorToJClass(typeArgument, holder, substitute)); + } + if (typeArgumentJClasses.size() > 0) { + declaredClass = declaredClass.narrow(typeArgumentJClasses); + } + + return declaredClass; } - public static class Parameter { - public final String name; - public final JClass jClass; + private JClass typeMirrorToJClass(WildcardType wildcardType, GeneratedClassHolder holder, Map substitute) { + TypeMirror bound = wildcardType.getExtendsBound(); + if (bound == null) { + bound = wildcardType.getSuperBound(); + if (bound == null) { + return holder.classes().OBJECT.wildcard(); + } + return new JSuperWildcard(typeMirrorToJClass(bound, holder, substitute)); + } - public Parameter(String name, JClass jClass) { - this.name = name; - this.jClass = jClass; + TypeMirror extendsBound = wildcardType.getExtendsBound(); + + if (extendsBound == null) { + return holder.classes().OBJECT.wildcard(); + } else { + return typeMirrorToJClass(extendsBound, holder, substitute).wildcard(); } } + private JClass typeMirrorToJClass(ArrayType arrayType, GeneratedClassHolder holder, Map substitute) { + JClass refClass = typeMirrorToJClass(arrayType.getComponentType(), holder, substitute); + return refClass.array(); + } + private Map getActualTypes(Types typeUtils, DeclaredType baseClass, TypeMirror annotatedClass) { List superTypes = new ArrayList(); superTypes.add(annotatedClass); @@ -188,33 +149,23 @@ public JMethod overrideAnnotatedMethod(ExecutableElement executableElement, Gene actualTypes.keySet().removeAll(methodTypes.keySet()); - String methodName = executableElement.getSimpleName().toString(); - JClass returnType = typeMirrorToJClass(executableElement.getReturnType(), holder, actualTypes); - - List parameters = new ArrayList(); - for (int i = 0; i < executableElement.getParameters().size(); i++) { - VariableElement parameter = executableElement.getParameters().get(i); - - String parameterName = parameter.getSimpleName().toString(); - JClass parameterClass = typeMirrorToJClass(parameter.asType(), holder, actualTypes); - parameters.add(new Parameter(parameterName, parameterClass)); - } - - JMethod existingMethod = findAlreadyGeneratedMethod(holder.getGeneratedClass(), methodName, parameters); - + JMethod existingMethod = findAlreadyGeneratedMethod(executableElement, holder); if (existingMethod != null) { return existingMethod; } + String methodName = executableElement.getSimpleName().toString(); + JClass returnType = typeMirrorToJClass(executableElement.getReturnType(), holder, actualTypes); JMethod method = holder.getGeneratedClass().method(JMod.PUBLIC, returnType, methodName); method.annotate(Override.class); + addNonAAAnotations(method, executableElement.getAnnotationMirrors(), holder); for (Map.Entry typeDeclaration : methodTypes.entrySet()) { method.generify(typeDeclaration.getKey(), typeDeclaration.getValue()); } - for (Parameter parameter : parameters) { - method.param(JMod.FINAL, parameter.jClass, parameter.name); + for (VariableElement parameter : executableElement.getParameters()) { + addParamToMethod(method, parameter, JMod.FINAL, holder, actualTypes); } for (TypeMirror superThrownType : executableElement.getThrownTypes()) { @@ -227,12 +178,15 @@ public JMethod overrideAnnotatedMethod(ExecutableElement executableElement, Gene return method; } - private JMethod findAlreadyGeneratedMethod(JDefinedClass definedClass, String methodName, List parameters) { + private JMethod findAlreadyGeneratedMethod(ExecutableElement executableElement, GeneratedClassHolder holder) { + JDefinedClass definedClass = holder.getGeneratedClass(); + String methodName = executableElement.getSimpleName().toString(); + List parameters = executableElement.getParameters(); method: for (JMethod method : definedClass.methods()) { if (method.name().equals(methodName) && method.params().size() == parameters.size()) { int i = 0; for (JVar param : method.params()) { - String searchedParamType = parameters.get(i).jClass.name(); + String searchedParamType = typeMirrorToJClass(parameters.get(i).asType(), holder).name(); if (!param.type().name().equals(searchedParamType)) { continue method; } @@ -244,6 +198,24 @@ private JMethod findAlreadyGeneratedMethod(JDefinedClass definedClass, String me return null; } + private void addParamToMethod(JMethod method, VariableElement parameter, int mod, GeneratedClassHolder holder, Map actualTypes) { + String parameterName = parameter.getSimpleName().toString(); + JClass parameterClass = typeMirrorToJClass(parameter.asType(), holder, actualTypes); + JVar param = method.param(mod, parameterClass, parameterName); + addNonAAAnotations(param, parameter.getAnnotationMirrors(), holder); + } + + public void addNonAAAnotations(JAnnotatable annotatable, List annotationMirrors, GeneratedClassHolder holder) { + for (AnnotationMirror annotationMirror : annotationMirrors) { + JClass annotationClass = typeMirrorToJClass(annotationMirror.getAnnotationType(), holder); + if (!annotationClass.fullName().startsWith("org.androidannotations") + && annotationMirror.getElementValues().isEmpty() // There is currently no way to add annotations with parameters from an AnnotationMirror + ) { + annotatable.annotate(annotationClass); + } + } + } + public void callSuperMethod(JMethod superMethod, GeneratedClassHolder holder, JBlock callBlock) { JExpression activitySuper = holder.getGeneratedClass().staticRef("super"); JInvocation superCall = JExpr.invoke(activitySuper, superMethod); diff --git a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/AnnotationHelper.java b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/AnnotationHelper.java index 9d5745ac39..72e3d6f1ed 100644 --- a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/AnnotationHelper.java +++ b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/AnnotationHelper.java @@ -15,29 +15,7 @@ */ package org.androidannotations.helper; -import static org.androidannotations.helper.ModelConstants.VALID_ENHANCED_COMPONENT_ANNOTATIONS; - -import java.lang.annotation.Annotation; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -import javax.annotation.processing.ProcessingEnvironment; -import javax.lang.model.element.AnnotationMirror; -import javax.lang.model.element.AnnotationValue; -import javax.lang.model.element.Element; -import javax.lang.model.element.ExecutableElement; -import javax.lang.model.element.Modifier; -import javax.lang.model.element.NestingKind; -import javax.lang.model.element.TypeElement; -import javax.lang.model.type.DeclaredType; -import javax.lang.model.type.MirroredTypeException; -import javax.lang.model.type.TypeMirror; -import javax.lang.model.util.Elements; -import javax.lang.model.util.Types; - +import com.sun.codemodel.JFieldRef; import org.androidannotations.annotations.OnActivityResult; import org.androidannotations.annotations.OptionsItem; import org.androidannotations.annotations.ResId; @@ -48,7 +26,19 @@ import org.androidannotations.rclass.IRInnerClass; import org.androidannotations.rclass.RInnerClass; -import com.sun.codemodel.JFieldRef; +import javax.annotation.processing.ProcessingEnvironment; +import javax.lang.model.element.*; +import javax.lang.model.type.DeclaredType; +import javax.lang.model.type.MirroredTypeException; +import javax.lang.model.type.TypeMirror; +import javax.lang.model.util.Elements; +import javax.lang.model.util.Types; +import java.lang.annotation.Annotation; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; public class AnnotationHelper { @@ -388,11 +378,6 @@ public DeclaredType extractAnnotationClassParameter(Element element, String anno return extractAnnotationClassParameter(element, annotationName, "value"); } - public boolean enclosingElementHasEnhancedComponentAnnotation(Element element) { - Element enclosingElement = element.getEnclosingElement(); - return hasOneOfClassAnnotations(enclosingElement, VALID_ENHANCED_COMPONENT_ANNOTATIONS); - } - public boolean hasOneOfClassAnnotations(Element element, Class validAnnotation) { List> annotations = new ArrayList>(); annotations.add(validAnnotation); diff --git a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/holder/BaseGeneratedClassHolder.java b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/holder/BaseGeneratedClassHolder.java index fc8857f6c8..35a290366e 100644 --- a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/holder/BaseGeneratedClassHolder.java +++ b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/holder/BaseGeneratedClassHolder.java @@ -15,21 +15,20 @@ */ package org.androidannotations.holder; -import static com.sun.codemodel.JMod.FINAL; -import static com.sun.codemodel.JMod.PUBLIC; +import com.sun.codemodel.ClassType; +import com.sun.codemodel.JClass; +import com.sun.codemodel.JCodeModel; +import com.sun.codemodel.JDefinedClass; +import org.androidannotations.helper.APTCodeModelHelper; +import org.androidannotations.helper.ModelConstants; +import org.androidannotations.process.ProcessHolder; import javax.annotation.processing.ProcessingEnvironment; import javax.lang.model.element.TypeElement; import javax.lang.model.element.TypeParameterElement; -import org.androidannotations.helper.APTCodeModelHelper; -import org.androidannotations.helper.ModelConstants; -import org.androidannotations.process.ProcessHolder; - -import com.sun.codemodel.ClassType; -import com.sun.codemodel.JClass; -import com.sun.codemodel.JCodeModel; -import com.sun.codemodel.JDefinedClass; +import static com.sun.codemodel.JMod.FINAL; +import static com.sun.codemodel.JMod.PUBLIC; public abstract class BaseGeneratedClassHolder implements GeneratedClassHolder { @@ -56,6 +55,7 @@ protected void setGeneratedClass() throws Exception { generatedClass.generify(typeParam.getSimpleName().toString(), bound); } generatedClass._extends(annotatedComponent); + codeModelHelper.addNonAAAnotations(generatedClass, annotatedElement.getAnnotationMirrors(), this); } @Override From e6721660f3b3f8a5f3e84c86e8e7b7a3be64bced Mon Sep 17 00:00:00 2001 From: yDelouis Date: Mon, 28 Apr 2014 19:17:44 +0200 Subject: [PATCH 2/5] Add tests for copy of non AA annotations --- .../functional-test-1-5/pom.xml | 10 +++++++ .../main/java/com/squareup/otto/Produce.java | 27 ------------------- .../java/com/squareup/otto/Subscribe.java | 27 ------------------- .../OttoBean.java => OthersAnnotations.java} | 14 ++++++---- .../androidannotations/test15/otto/Event.java | 20 -------------- 5 files changed, 19 insertions(+), 79 deletions(-) delete mode 100644 AndroidAnnotations/functional-test-1-5/src/main/java/com/squareup/otto/Produce.java delete mode 100644 AndroidAnnotations/functional-test-1-5/src/main/java/com/squareup/otto/Subscribe.java rename AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/{otto/OttoBean.java => OthersAnnotations.java} (81%) delete mode 100644 AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/otto/Event.java diff --git a/AndroidAnnotations/functional-test-1-5/pom.xml b/AndroidAnnotations/functional-test-1-5/pom.xml index a5334fcf41..8683259fb5 100644 --- a/AndroidAnnotations/functional-test-1-5/pom.xml +++ b/AndroidAnnotations/functional-test-1-5/pom.xml @@ -51,6 +51,16 @@ ormlite-android 4.41 + + com.google.code.findbugs + jsr305 + 1.3.9 + + + com.squareup + otto + 1.3.4 + org.springframework.android diff --git a/AndroidAnnotations/functional-test-1-5/src/main/java/com/squareup/otto/Produce.java b/AndroidAnnotations/functional-test-1-5/src/main/java/com/squareup/otto/Produce.java deleted file mode 100644 index e83b4dc152..0000000000 --- a/AndroidAnnotations/functional-test-1-5/src/main/java/com/squareup/otto/Produce.java +++ /dev/null @@ -1,27 +0,0 @@ -/** - * Copyright (C) 2010-2014 eBusiness Information, Excilys Group - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed To in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.squareup.otto; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.METHOD) -public @interface Produce { - -} diff --git a/AndroidAnnotations/functional-test-1-5/src/main/java/com/squareup/otto/Subscribe.java b/AndroidAnnotations/functional-test-1-5/src/main/java/com/squareup/otto/Subscribe.java deleted file mode 100644 index c791f81e8b..0000000000 --- a/AndroidAnnotations/functional-test-1-5/src/main/java/com/squareup/otto/Subscribe.java +++ /dev/null @@ -1,27 +0,0 @@ -/** - * Copyright (C) 2010-2014 eBusiness Information, Excilys Group - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed To in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.squareup.otto; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.METHOD) -public @interface Subscribe { - -} diff --git a/AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/otto/OttoBean.java b/AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/OthersAnnotations.java similarity index 81% rename from AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/otto/OttoBean.java rename to AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/OthersAnnotations.java index b3e74dfa43..6a3e14b7c1 100644 --- a/AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/otto/OttoBean.java +++ b/AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/OthersAnnotations.java @@ -13,22 +13,25 @@ * License for the specific language governing permissions and limitations under * the License. */ -package org.androidannotations.test15.otto; +package org.androidannotations.test15; +import com.squareup.otto.Produce; +import com.squareup.otto.Subscribe; import org.androidannotations.annotations.Background; import org.androidannotations.annotations.EBean; import org.androidannotations.annotations.Trace; -import com.squareup.otto.Produce; -import com.squareup.otto.Subscribe; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; @EBean -public class OttoBean { +@Nullable +public class OthersAnnotations { @Trace @Background @Subscribe - public void onEvent(Event event) { + public void onEvent(@Nonnull Event event) { } @@ -37,4 +40,5 @@ public Event produceEvent() { return new Event(); } + public static class Event {} } diff --git a/AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/otto/Event.java b/AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/otto/Event.java deleted file mode 100644 index 34a76f6431..0000000000 --- a/AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/otto/Event.java +++ /dev/null @@ -1,20 +0,0 @@ -/** - * Copyright (C) 2010-2014 eBusiness Information, Excilys Group - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed To in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package org.androidannotations.test15.otto; - -public class Event { - -} From 5b31f202b1a936c291d97d823fb3bb30937b4bec Mon Sep 17 00:00:00 2001 From: WonderCsabo Date: Fri, 2 May 2014 00:30:46 +0200 Subject: [PATCH 3/5] Adding parameters to copied annotations We are copying all annotations to overriden classes, methods, method arguments etc, what are not from our package. This commit adds the ability to AA to copy the annotation parameters, too. Note that annotation as an annotation parameter is not supported yet. --- .../helper/APTCodeModelHelper.java | 18 ++- .../helper/AnnotationArrayParamExtractor.java | 100 +++++++++++++++ .../helper/AnnotationParamExtractor.java | 118 ++++++++++++++++++ .../test15/OthersAnnotations.java | 20 ++- 4 files changed, 246 insertions(+), 10 deletions(-) create mode 100644 AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/AnnotationArrayParamExtractor.java create mode 100644 AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/AnnotationParamExtractor.java diff --git a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/APTCodeModelHelper.java b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/APTCodeModelHelper.java index bb158a84f5..239cd6b01a 100644 --- a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/APTCodeModelHelper.java +++ b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/APTCodeModelHelper.java @@ -208,14 +208,22 @@ private void addParamToMethod(JMethod method, VariableElement parameter, int mod public void addNonAAAnotations(JAnnotatable annotatable, List annotationMirrors, GeneratedClassHolder holder) { for (AnnotationMirror annotationMirror : annotationMirrors) { JClass annotationClass = typeMirrorToJClass(annotationMirror.getAnnotationType(), holder); - if (!annotationClass.fullName().startsWith("org.androidannotations") - && annotationMirror.getElementValues().isEmpty() // There is currently no way to add annotations with parameters from an AnnotationMirror - ) { - annotatable.annotate(annotationClass); + if (!annotationClass.fullName().startsWith("org.androidannotations")) { + addAnnotation(annotatable, annotationMirror, holder); } } } + private void addAnnotation(JAnnotatable annotatable, AnnotationMirror annotationMirror, GeneratedClassHolder holder) { + Map parameters = annotationMirror.getElementValues(); + + JAnnotationUse annotate = annotatable.annotate(typeMirrorToJClass(annotationMirror.getAnnotationType(), holder)); + + for (Map.Entry param : parameters.entrySet()) { + param.getValue().accept(new AnnotationParamExtractor(annotate, holder, this), param.getKey().getSimpleName().toString()); + } + } + public void callSuperMethod(JMethod superMethod, GeneratedClassHolder holder, JBlock callBlock) { JExpression activitySuper = holder.getGeneratedClass().staticRef("super"); JInvocation superCall = JExpr.invoke(activitySuper, superMethod); @@ -331,7 +339,7 @@ public JDefinedClass createDelegatingAnonymousRunnableClass(EComponentHolder hol /** * Gets all of the methods of the class and includes the methods of any * implemented interfaces. - * + * * @param typeElement * @return full list of methods. */ diff --git a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/AnnotationArrayParamExtractor.java b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/AnnotationArrayParamExtractor.java new file mode 100644 index 0000000000..2f3a8f9161 --- /dev/null +++ b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/AnnotationArrayParamExtractor.java @@ -0,0 +1,100 @@ +package org.androidannotations.helper; + +import javax.lang.model.element.AnnotationMirror; +import javax.lang.model.element.VariableElement; +import javax.lang.model.type.TypeMirror; +import javax.lang.model.util.SimpleAnnotationValueVisitor6; + +import org.androidannotations.holder.GeneratedClassHolder; + +import com.sun.codemodel.JAnnotationArrayMember; +import com.sun.codemodel.JClass; +import com.sun.codemodel.JExpr; +import com.sun.codemodel.JExpression; + +public class AnnotationArrayParamExtractor extends SimpleAnnotationValueVisitor6 { + + private GeneratedClassHolder holder; + private APTCodeModelHelper helper; + + public AnnotationArrayParamExtractor(GeneratedClassHolder holder, APTCodeModelHelper helper) { + this.holder = holder; + this.helper = helper; + } + + @Override + public Void visitBoolean(boolean b, JAnnotationArrayMember p) { + p.param(b); + return null; + } + + @Override + public Void visitByte(byte b, JAnnotationArrayMember p) { + p.param(b); + return null; + } + + @Override + public Void visitChar(char c, JAnnotationArrayMember p) { + p.param(c); + return null; + } + + @Override + public Void visitDouble(double d, JAnnotationArrayMember p) { + p.param(d); + return null; + } + + @Override + public Void visitFloat(float f, JAnnotationArrayMember p) { + p.param(f); + return null; + } + + @Override + public Void visitInt(int i, JAnnotationArrayMember p) { + p.param(i); + return null; + } + + @Override + public Void visitLong(long i, JAnnotationArrayMember p) { + p.param(i); + return null; + } + + @Override + public Void visitShort(short s, JAnnotationArrayMember p) { + p.param(s); + return null; + } + + @Override + public Void visitString(String s, JAnnotationArrayMember p) { + p.param(s); + return null; + } + + @Override + public Void visitType(TypeMirror t, JAnnotationArrayMember p) { + JClass annotationClass = helper.typeMirrorToJClass(t, holder); + JExpression dotclass = JExpr.dotclass(annotationClass); + p.param(dotclass); + return null; + } + + @Override + public Void visitEnumConstant(VariableElement c, JAnnotationArrayMember p) { + JClass annotationClass = helper.typeMirrorToJClass(c.asType(), holder); + JExpression expression = JExpr.direct(annotationClass.fullName() + "." + c.getSimpleName()); + p.param(expression); + return null; + } + + @Override + public Void visitAnnotation(AnnotationMirror a, JAnnotationArrayMember p) { + // TODO + return null; + } +} diff --git a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/AnnotationParamExtractor.java b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/AnnotationParamExtractor.java new file mode 100644 index 0000000000..85470d435c --- /dev/null +++ b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/AnnotationParamExtractor.java @@ -0,0 +1,118 @@ +package org.androidannotations.helper; + +import java.util.List; + +import javax.lang.model.element.AnnotationMirror; +import javax.lang.model.element.AnnotationValue; +import javax.lang.model.element.VariableElement; +import javax.lang.model.type.TypeMirror; +import javax.lang.model.util.SimpleAnnotationValueVisitor6; + +import org.androidannotations.holder.GeneratedClassHolder; + +import com.sun.codemodel.JAnnotationArrayMember; +import com.sun.codemodel.JAnnotationUse; +import com.sun.codemodel.JClass; +import com.sun.codemodel.JExpr; +import com.sun.codemodel.JExpression; + +public class AnnotationParamExtractor extends SimpleAnnotationValueVisitor6 { + + private JAnnotationUse use; + private GeneratedClassHolder holder; + private APTCodeModelHelper helper; + + public AnnotationParamExtractor(JAnnotationUse use, GeneratedClassHolder holder, APTCodeModelHelper helper) { + this.use = use; + this.holder = holder; + this.helper = helper; + } + + @Override + public Void visitArray(List vals, String p) { + JAnnotationArrayMember paramArray = use.paramArray(p); + + for (AnnotationValue annotationValue : vals) { + annotationValue.accept(new AnnotationArrayParamExtractor(holder, helper), paramArray); + } + + return null; + } + + @Override + public Void visitBoolean(boolean b, String p) { + use.param(p, b); + return null; + } + + @Override + public Void visitByte(byte b, String p) { + use.param(p, b); + return null; + } + + @Override + public Void visitChar(char c, String p) { + use.param(p, c); + return null; + } + + @Override + public Void visitDouble(double d, String p) { + use.param(p, d); + return null; + } + + @Override + public Void visitFloat(float f, String p) { + use.param(p, f); + return null; + } + + @Override + public Void visitInt(int i, String p) { + use.param(p, i); + return null; + } + + @Override + public Void visitLong(long i, String p) { + use.param(p, i); + return null; + } + + @Override + public Void visitShort(short s, String p) { + use.param(p, s); + return null; + } + + @Override + public Void visitString(String s, String p) { + use.param(p, s); + return null; + } + + @Override + public Void visitEnumConstant(VariableElement c, String p) { + JClass annotationClass = helper.typeMirrorToJClass(c.asType(), holder); + JExpression expression = JExpr.direct(annotationClass.fullName() + "." + c.getSimpleName()); + use.param(p, expression); + return null; + } + + @Override + public Void visitType(TypeMirror t, String p) { + JClass annotationClass = helper.typeMirrorToJClass(t, holder); + JExpression dotclass = JExpr.dotclass(annotationClass); + use.param(p, dotclass); + return null; + } + + @Override + public Void visitAnnotation(AnnotationMirror a, String p) { + // TODO + // use.annotationParam(name, value); + return null; + } +}; diff --git a/AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/OthersAnnotations.java b/AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/OthersAnnotations.java index 6a3e14b7c1..b5666e365d 100644 --- a/AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/OthersAnnotations.java +++ b/AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/OthersAnnotations.java @@ -15,27 +15,37 @@ */ package org.androidannotations.test15; -import com.squareup.otto.Produce; -import com.squareup.otto.Subscribe; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import javax.annotation.meta.When; + import org.androidannotations.annotations.Background; import org.androidannotations.annotations.EBean; import org.androidannotations.annotations.Trace; +import org.simpleframework.xml.Attribute; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; +import com.squareup.otto.Produce; +import com.squareup.otto.Subscribe; + +import dalvik.annotation.TestTargetClass; @EBean @Nullable +@TestTargetClass(String.class) public class OthersAnnotations { @Trace @Background + @SuppressWarnings(value = { "", "hi" }) + @Attribute(name = "2") + @Nonnull(when = When.MAYBE) @Subscribe - public void onEvent(@Nonnull Event event) { + public void onEvent(@Nonnull(when = When.UNKNOWN) Event event) { } @Produce + @Attribute(name = "2") public Event produceEvent() { return new Event(); } From 051fc57e035d959ac17d0931d7fbc41f1b737cc0 Mon Sep 17 00:00:00 2001 From: WonderCsabo Date: Fri, 2 May 2014 12:32:54 +0200 Subject: [PATCH 4/5] Adding license headers to new files --- .../helper/AnnotationArrayParamExtractor.java | 15 +++++++++++++++ .../helper/AnnotationParamExtractor.java | 15 +++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/AnnotationArrayParamExtractor.java b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/AnnotationArrayParamExtractor.java index 2f3a8f9161..e90df529b9 100644 --- a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/AnnotationArrayParamExtractor.java +++ b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/AnnotationArrayParamExtractor.java @@ -1,3 +1,18 @@ +/** + * Copyright (C) 2010-2014 eBusiness Information, Excilys Group + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed To in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ package org.androidannotations.helper; import javax.lang.model.element.AnnotationMirror; diff --git a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/AnnotationParamExtractor.java b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/AnnotationParamExtractor.java index 85470d435c..d5d75eae01 100644 --- a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/AnnotationParamExtractor.java +++ b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/AnnotationParamExtractor.java @@ -1,3 +1,18 @@ +/** + * Copyright (C) 2010-2014 eBusiness Information, Excilys Group + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed To in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ package org.androidannotations.helper; import java.util.List; From 9b06ae03159cc5ed3976df73b3adba8ebf595d3f Mon Sep 17 00:00:00 2001 From: yDelouis Date: Fri, 2 May 2014 16:03:29 +0200 Subject: [PATCH 5/5] Handle annotation parameter --- .../helper/APTCodeModelHelper.java | 2 +- .../helper/AnnotationArrayParamExtractor.java | 15 ++++---- .../helper/AnnotationParamExtractor.java | 35 +++++++++++++------ .../main/java/com/test/ComplexAnnotation.java | 7 ++++ .../main/java/com/test/SimpleAnnotation.java | 5 +++ .../test15/OthersAnnotations.java | 17 ++++----- 6 files changed, 53 insertions(+), 28 deletions(-) create mode 100644 AndroidAnnotations/functional-test-1-5/src/main/java/com/test/ComplexAnnotation.java create mode 100644 AndroidAnnotations/functional-test-1-5/src/main/java/com/test/SimpleAnnotation.java diff --git a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/APTCodeModelHelper.java b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/APTCodeModelHelper.java index 239cd6b01a..85aa335ef2 100644 --- a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/APTCodeModelHelper.java +++ b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/APTCodeModelHelper.java @@ -214,7 +214,7 @@ public void addNonAAAnotations(JAnnotatable annotatable, List parameters = annotationMirror.getElementValues(); JAnnotationUse annotate = annotatable.annotate(typeMirrorToJClass(annotationMirror.getAnnotationType(), holder)); diff --git a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/AnnotationArrayParamExtractor.java b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/AnnotationArrayParamExtractor.java index e90df529b9..61ab1f4e10 100644 --- a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/AnnotationArrayParamExtractor.java +++ b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/AnnotationArrayParamExtractor.java @@ -15,17 +15,16 @@ */ package org.androidannotations.helper; -import javax.lang.model.element.AnnotationMirror; -import javax.lang.model.element.VariableElement; -import javax.lang.model.type.TypeMirror; -import javax.lang.model.util.SimpleAnnotationValueVisitor6; - -import org.androidannotations.holder.GeneratedClassHolder; - import com.sun.codemodel.JAnnotationArrayMember; import com.sun.codemodel.JClass; import com.sun.codemodel.JExpr; import com.sun.codemodel.JExpression; +import org.androidannotations.holder.GeneratedClassHolder; + +import javax.lang.model.element.AnnotationMirror; +import javax.lang.model.element.VariableElement; +import javax.lang.model.type.TypeMirror; +import javax.lang.model.util.SimpleAnnotationValueVisitor6; public class AnnotationArrayParamExtractor extends SimpleAnnotationValueVisitor6 { @@ -109,7 +108,7 @@ public Void visitEnumConstant(VariableElement c, JAnnotationArrayMember p) { @Override public Void visitAnnotation(AnnotationMirror a, JAnnotationArrayMember p) { - // TODO + helper.addAnnotation(p, a, holder); return null; } } diff --git a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/AnnotationParamExtractor.java b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/AnnotationParamExtractor.java index d5d75eae01..48f24dd936 100644 --- a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/AnnotationParamExtractor.java +++ b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/AnnotationParamExtractor.java @@ -15,21 +15,19 @@ */ package org.androidannotations.helper; -import java.util.List; +import com.sun.codemodel.*; +import org.androidannotations.holder.GeneratedClassHolder; import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.AnnotationValue; +import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.VariableElement; import javax.lang.model.type.TypeMirror; import javax.lang.model.util.SimpleAnnotationValueVisitor6; - -import org.androidannotations.holder.GeneratedClassHolder; - -import com.sun.codemodel.JAnnotationArrayMember; -import com.sun.codemodel.JAnnotationUse; -import com.sun.codemodel.JClass; -import com.sun.codemodel.JExpr; -import com.sun.codemodel.JExpression; +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import java.util.List; +import java.util.Map; public class AnnotationParamExtractor extends SimpleAnnotationValueVisitor6 { @@ -125,9 +123,24 @@ public Void visitType(TypeMirror t, String p) { } @Override + @SuppressWarnings("unchecked") public Void visitAnnotation(AnnotationMirror a, String p) { - // TODO - // use.annotationParam(name, value); + try { + JClass annotationJClass = helper.typeMirrorToJClass(a.getAnnotationType(), holder); + Constructor constructor = JAnnotationUse.class.getDeclaredConstructor(JClass.class); + constructor.setAccessible(true); + JAnnotationUse paramAnnotation = constructor.newInstance(annotationJClass); + Map parameters = a.getElementValues(); + for (Map.Entry param : parameters.entrySet()) { + param.getValue().accept(new AnnotationParamExtractor(paramAnnotation, holder, helper), param.getKey().getSimpleName().toString()); + } + + Method addValueMethod = use.getClass().getDeclaredMethod("addValue", String.class, JAnnotationValue.class); + addValueMethod.setAccessible(true); + addValueMethod.invoke(use, p, paramAnnotation); + } catch (Exception e) { + throw new RuntimeException(e); + } return null; } }; diff --git a/AndroidAnnotations/functional-test-1-5/src/main/java/com/test/ComplexAnnotation.java b/AndroidAnnotations/functional-test-1-5/src/main/java/com/test/ComplexAnnotation.java new file mode 100644 index 0000000000..5d82b5f124 --- /dev/null +++ b/AndroidAnnotations/functional-test-1-5/src/main/java/com/test/ComplexAnnotation.java @@ -0,0 +1,7 @@ +package com.test; + +public @interface ComplexAnnotation { + SimpleAnnotation value(); + SimpleAnnotation[] array() default {}; +} + diff --git a/AndroidAnnotations/functional-test-1-5/src/main/java/com/test/SimpleAnnotation.java b/AndroidAnnotations/functional-test-1-5/src/main/java/com/test/SimpleAnnotation.java new file mode 100644 index 0000000000..5a70bda58b --- /dev/null +++ b/AndroidAnnotations/functional-test-1-5/src/main/java/com/test/SimpleAnnotation.java @@ -0,0 +1,5 @@ +package com.test; + +public @interface SimpleAnnotation { + String value(); +} diff --git a/AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/OthersAnnotations.java b/AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/OthersAnnotations.java index b5666e365d..86394c65f2 100644 --- a/AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/OthersAnnotations.java +++ b/AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/OthersAnnotations.java @@ -15,23 +15,24 @@ */ package org.androidannotations.test15; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import javax.annotation.meta.When; - +import com.squareup.otto.Produce; +import com.squareup.otto.Subscribe; +import com.test.ComplexAnnotation; +import com.test.SimpleAnnotation; +import dalvik.annotation.TestTargetClass; import org.androidannotations.annotations.Background; import org.androidannotations.annotations.EBean; import org.androidannotations.annotations.Trace; import org.simpleframework.xml.Attribute; -import com.squareup.otto.Produce; -import com.squareup.otto.Subscribe; - -import dalvik.annotation.TestTargetClass; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import javax.annotation.meta.When; @EBean @Nullable @TestTargetClass(String.class) +@ComplexAnnotation(value = @SimpleAnnotation("1"), array = {@SimpleAnnotation("2"), @SimpleAnnotation("3")}) public class OthersAnnotations { @Trace