Skip to content

Commit 5b31f20

Browse files
committed
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.
1 parent e672166 commit 5b31f20

4 files changed

Lines changed: 246 additions & 10 deletions

File tree

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/APTCodeModelHelper.java‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,22 @@ private void addParamToMethod(JMethod method, VariableElement parameter, int mod
208208
public void addNonAAAnotations(JAnnotatable annotatable, List<? extends AnnotationMirror> annotationMirrors, GeneratedClassHolder holder) {
209209
for (AnnotationMirror annotationMirror : annotationMirrors) {
210210
JClass annotationClass = typeMirrorToJClass(annotationMirror.getAnnotationType(), holder);
211-
if (!annotationClass.fullName().startsWith("org.androidannotations")
212-
&& annotationMirror.getElementValues().isEmpty() // There is currently no way to add annotations with parameters from an AnnotationMirror
213-
) {
214-
annotatable.annotate(annotationClass);
211+
if (!annotationClass.fullName().startsWith("org.androidannotations")) {
212+
addAnnotation(annotatable, annotationMirror, holder);
215213
}
216214
}
217215
}
218216

217+
private void addAnnotation(JAnnotatable annotatable, AnnotationMirror annotationMirror, GeneratedClassHolder holder) {
218+
Map<? extends ExecutableElement, ? extends AnnotationValue> parameters = annotationMirror.getElementValues();
219+
220+
JAnnotationUse annotate = annotatable.annotate(typeMirrorToJClass(annotationMirror.getAnnotationType(), holder));
221+
222+
for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> param : parameters.entrySet()) {
223+
param.getValue().accept(new AnnotationParamExtractor(annotate, holder, this), param.getKey().getSimpleName().toString());
224+
}
225+
}
226+
219227
public void callSuperMethod(JMethod superMethod, GeneratedClassHolder holder, JBlock callBlock) {
220228
JExpression activitySuper = holder.getGeneratedClass().staticRef("super");
221229
JInvocation superCall = JExpr.invoke(activitySuper, superMethod);
@@ -331,7 +339,7 @@ public JDefinedClass createDelegatingAnonymousRunnableClass(EComponentHolder hol
331339
/**
332340
* Gets all of the methods of the class and includes the methods of any
333341
* implemented interfaces.
334-
*
342+
*
335343
* @param typeElement
336344
* @return full list of methods.
337345
*/
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package org.androidannotations.helper;
2+
3+
import javax.lang.model.element.AnnotationMirror;
4+
import javax.lang.model.element.VariableElement;
5+
import javax.lang.model.type.TypeMirror;
6+
import javax.lang.model.util.SimpleAnnotationValueVisitor6;
7+
8+
import org.androidannotations.holder.GeneratedClassHolder;
9+
10+
import com.sun.codemodel.JAnnotationArrayMember;
11+
import com.sun.codemodel.JClass;
12+
import com.sun.codemodel.JExpr;
13+
import com.sun.codemodel.JExpression;
14+
15+
public class AnnotationArrayParamExtractor extends SimpleAnnotationValueVisitor6<Void, JAnnotationArrayMember> {
16+
17+
private GeneratedClassHolder holder;
18+
private APTCodeModelHelper helper;
19+
20+
public AnnotationArrayParamExtractor(GeneratedClassHolder holder, APTCodeModelHelper helper) {
21+
this.holder = holder;
22+
this.helper = helper;
23+
}
24+
25+
@Override
26+
public Void visitBoolean(boolean b, JAnnotationArrayMember p) {
27+
p.param(b);
28+
return null;
29+
}
30+
31+
@Override
32+
public Void visitByte(byte b, JAnnotationArrayMember p) {
33+
p.param(b);
34+
return null;
35+
}
36+
37+
@Override
38+
public Void visitChar(char c, JAnnotationArrayMember p) {
39+
p.param(c);
40+
return null;
41+
}
42+
43+
@Override
44+
public Void visitDouble(double d, JAnnotationArrayMember p) {
45+
p.param(d);
46+
return null;
47+
}
48+
49+
@Override
50+
public Void visitFloat(float f, JAnnotationArrayMember p) {
51+
p.param(f);
52+
return null;
53+
}
54+
55+
@Override
56+
public Void visitInt(int i, JAnnotationArrayMember p) {
57+
p.param(i);
58+
return null;
59+
}
60+
61+
@Override
62+
public Void visitLong(long i, JAnnotationArrayMember p) {
63+
p.param(i);
64+
return null;
65+
}
66+
67+
@Override
68+
public Void visitShort(short s, JAnnotationArrayMember p) {
69+
p.param(s);
70+
return null;
71+
}
72+
73+
@Override
74+
public Void visitString(String s, JAnnotationArrayMember p) {
75+
p.param(s);
76+
return null;
77+
}
78+
79+
@Override
80+
public Void visitType(TypeMirror t, JAnnotationArrayMember p) {
81+
JClass annotationClass = helper.typeMirrorToJClass(t, holder);
82+
JExpression dotclass = JExpr.dotclass(annotationClass);
83+
p.param(dotclass);
84+
return null;
85+
}
86+
87+
@Override
88+
public Void visitEnumConstant(VariableElement c, JAnnotationArrayMember p) {
89+
JClass annotationClass = helper.typeMirrorToJClass(c.asType(), holder);
90+
JExpression expression = JExpr.direct(annotationClass.fullName() + "." + c.getSimpleName());
91+
p.param(expression);
92+
return null;
93+
}
94+
95+
@Override
96+
public Void visitAnnotation(AnnotationMirror a, JAnnotationArrayMember p) {
97+
// TODO
98+
return null;
99+
}
100+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package org.androidannotations.helper;
2+
3+
import java.util.List;
4+
5+
import javax.lang.model.element.AnnotationMirror;
6+
import javax.lang.model.element.AnnotationValue;
7+
import javax.lang.model.element.VariableElement;
8+
import javax.lang.model.type.TypeMirror;
9+
import javax.lang.model.util.SimpleAnnotationValueVisitor6;
10+
11+
import org.androidannotations.holder.GeneratedClassHolder;
12+
13+
import com.sun.codemodel.JAnnotationArrayMember;
14+
import com.sun.codemodel.JAnnotationUse;
15+
import com.sun.codemodel.JClass;
16+
import com.sun.codemodel.JExpr;
17+
import com.sun.codemodel.JExpression;
18+
19+
public class AnnotationParamExtractor extends SimpleAnnotationValueVisitor6<Void, String> {
20+
21+
private JAnnotationUse use;
22+
private GeneratedClassHolder holder;
23+
private APTCodeModelHelper helper;
24+
25+
public AnnotationParamExtractor(JAnnotationUse use, GeneratedClassHolder holder, APTCodeModelHelper helper) {
26+
this.use = use;
27+
this.holder = holder;
28+
this.helper = helper;
29+
}
30+
31+
@Override
32+
public Void visitArray(List<? extends AnnotationValue> vals, String p) {
33+
JAnnotationArrayMember paramArray = use.paramArray(p);
34+
35+
for (AnnotationValue annotationValue : vals) {
36+
annotationValue.accept(new AnnotationArrayParamExtractor(holder, helper), paramArray);
37+
}
38+
39+
return null;
40+
}
41+
42+
@Override
43+
public Void visitBoolean(boolean b, String p) {
44+
use.param(p, b);
45+
return null;
46+
}
47+
48+
@Override
49+
public Void visitByte(byte b, String p) {
50+
use.param(p, b);
51+
return null;
52+
}
53+
54+
@Override
55+
public Void visitChar(char c, String p) {
56+
use.param(p, c);
57+
return null;
58+
}
59+
60+
@Override
61+
public Void visitDouble(double d, String p) {
62+
use.param(p, d);
63+
return null;
64+
}
65+
66+
@Override
67+
public Void visitFloat(float f, String p) {
68+
use.param(p, f);
69+
return null;
70+
}
71+
72+
@Override
73+
public Void visitInt(int i, String p) {
74+
use.param(p, i);
75+
return null;
76+
}
77+
78+
@Override
79+
public Void visitLong(long i, String p) {
80+
use.param(p, i);
81+
return null;
82+
}
83+
84+
@Override
85+
public Void visitShort(short s, String p) {
86+
use.param(p, s);
87+
return null;
88+
}
89+
90+
@Override
91+
public Void visitString(String s, String p) {
92+
use.param(p, s);
93+
return null;
94+
}
95+
96+
@Override
97+
public Void visitEnumConstant(VariableElement c, String p) {
98+
JClass annotationClass = helper.typeMirrorToJClass(c.asType(), holder);
99+
JExpression expression = JExpr.direct(annotationClass.fullName() + "." + c.getSimpleName());
100+
use.param(p, expression);
101+
return null;
102+
}
103+
104+
@Override
105+
public Void visitType(TypeMirror t, String p) {
106+
JClass annotationClass = helper.typeMirrorToJClass(t, holder);
107+
JExpression dotclass = JExpr.dotclass(annotationClass);
108+
use.param(p, dotclass);
109+
return null;
110+
}
111+
112+
@Override
113+
public Void visitAnnotation(AnnotationMirror a, String p) {
114+
// TODO
115+
// use.annotationParam(name, value);
116+
return null;
117+
}
118+
};

‎AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/OthersAnnotations.java‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,37 @@
1515
*/
1616
package org.androidannotations.test15;
1717

18-
import com.squareup.otto.Produce;
19-
import com.squareup.otto.Subscribe;
18+
import javax.annotation.Nonnull;
19+
import javax.annotation.Nullable;
20+
import javax.annotation.meta.When;
21+
2022
import org.androidannotations.annotations.Background;
2123
import org.androidannotations.annotations.EBean;
2224
import org.androidannotations.annotations.Trace;
25+
import org.simpleframework.xml.Attribute;
2326

24-
import javax.annotation.Nonnull;
25-
import javax.annotation.Nullable;
27+
import com.squareup.otto.Produce;
28+
import com.squareup.otto.Subscribe;
29+
30+
import dalvik.annotation.TestTargetClass;
2631

2732
@EBean
2833
@Nullable
34+
@TestTargetClass(String.class)
2935
public class OthersAnnotations {
3036

3137
@Trace
3238
@Background
39+
@SuppressWarnings(value = { "", "hi" })
40+
@Attribute(name = "2")
41+
@Nonnull(when = When.MAYBE)
3342
@Subscribe
34-
public void onEvent(@Nonnull Event event) {
43+
public void onEvent(@Nonnull(when = When.UNKNOWN) Event event) {
3544

3645
}
3746

3847
@Produce
48+
@Attribute(name = "2")
3949
public Event produceEvent() {
4050
return new Event();
4151
}

0 commit comments

Comments
 (0)