31 |
31 |
32 import com.sun.tools.javac.code.Attribute; |
32 import com.sun.tools.javac.code.Attribute; |
33 import com.sun.tools.javac.code.Kinds; |
33 import com.sun.tools.javac.code.Kinds; |
34 import com.sun.tools.javac.code.Symbol; |
34 import com.sun.tools.javac.code.Symbol; |
35 import com.sun.tools.javac.code.Symbol.ClassSymbol; |
35 import com.sun.tools.javac.code.Symbol.ClassSymbol; |
|
36 import com.sun.tools.javac.code.Symbol.TypeVariableSymbol; |
|
37 import com.sun.tools.javac.code.TargetType; |
36 import com.sun.tools.javac.code.Type; |
38 import com.sun.tools.javac.code.Type; |
37 import com.sun.tools.javac.code.Type.AnnotatedType; |
39 import com.sun.tools.javac.code.Type.AnnotatedType; |
38 import com.sun.tools.javac.util.ListBuffer; |
40 import com.sun.tools.javac.util.ListBuffer; |
39 import static com.sun.tools.javac.code.TypeTag.CLASS; |
41 import static com.sun.tools.javac.code.TypeTag.CLASS; |
|
42 import com.sun.tools.javac.util.List; |
40 |
43 |
41 /** |
44 /** |
42 * Utility methods for operating on annotated constructs. |
45 * Utility methods for operating on annotated constructs. |
43 * |
46 * |
44 * <p><b>This is NOT part of any supported API. |
47 * <p><b>This is NOT part of any supported API. |
59 Class<A> annoType) { |
62 Class<A> annoType) { |
60 if (!annoType.isAnnotation()) |
63 if (!annoType.isAnnotation()) |
61 throw new IllegalArgumentException("Not an annotation type: " |
64 throw new IllegalArgumentException("Not an annotation type: " |
62 + annoType); |
65 + annoType); |
63 Attribute.Compound c; |
66 Attribute.Compound c; |
64 if (annotated.kind == Kinds.TYP && annotated instanceof ClassSymbol) { |
67 if (annotated.kind == Kinds.TYP && |
|
68 annotated instanceof ClassSymbol) { |
65 c = getAttributeOnClass((ClassSymbol)annotated, annoType); |
69 c = getAttributeOnClass((ClassSymbol)annotated, annoType); |
|
70 } else if (annotated.kind == Kinds.TYP && |
|
71 annotated instanceof TypeVariableSymbol) { |
|
72 c = getAttributeOnTypeVariable((TypeVariableSymbol)annotated, annoType); |
66 } else { |
73 } else { |
67 c = getAttribute(annotated, annoType); |
74 c = getAttribute(annotated, annoType); |
68 } |
75 } |
69 return c == null ? null : AnnotationProxyMaker.generateAnnotation(c, annoType); |
76 return c == null ? null : AnnotationProxyMaker.generateAnnotation(c, annoType); |
70 } |
77 } |
76 |
83 |
77 for (Attribute.Compound anno : annotated.getRawAttributes()) { |
84 for (Attribute.Compound anno : annotated.getRawAttributes()) { |
78 if (name.equals(anno.type.tsym.flatName().toString())) |
85 if (name.equals(anno.type.tsym.flatName().toString())) |
79 return anno; |
86 return anno; |
80 } |
87 } |
|
88 |
|
89 return null; |
|
90 } |
|
91 |
|
92 // Helper to getAnnotation[s] |
|
93 private static <A extends Annotation> Attribute.Compound |
|
94 getAttributeOnTypeVariable(TypeVariableSymbol annotated, Class<A> annoType) { |
|
95 String name = annoType.getName(); |
|
96 |
|
97 // Declaration annotations on type variables are stored in type attributes |
|
98 // on the owner of the TypeVariableSymbol |
|
99 List<Attribute.Compound> res = List.nil(); |
|
100 List<Attribute.TypeCompound> candidates = annotated.owner.getRawTypeAttributes(); |
|
101 for (Attribute.TypeCompound anno : candidates) |
|
102 if (anno.position.type == TargetType.CLASS_TYPE_PARAMETER || |
|
103 anno.position.type == TargetType.METHOD_TYPE_PARAMETER) |
|
104 if (name.equals(anno.type.tsym.flatName().toString())) |
|
105 return anno; |
81 |
106 |
82 return null; |
107 return null; |
83 } |
108 } |
84 |
109 |
85 // Helper to getAnnotation[s] |
110 // Helper to getAnnotation[s] |