author | briangoetz |
Wed, 18 Dec 2013 16:05:18 -0500 | |
changeset 22163 | 3651128c74eb |
parent 19507 | 323f001d6be1 |
child 25443 | 9187d77f2c64 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
16557
67a3ae363f03
8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
14258
diff
changeset
|
2 |
* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved. |
10 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5520 | 7 |
* published by the Free Software Foundation. Oracle designates this |
10 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
10 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5520 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
10 | 24 |
*/ |
25 |
||
26 |
package com.sun.tools.javac.model; |
|
27 |
||
14258 | 28 |
import java.io.IOException; |
5003
fd0b30cdbe5c
6929645: Address various findbugs warnings in langtools
darcy
parents:
1789
diff
changeset
|
29 |
import java.io.ObjectInputStream; |
10 | 30 |
import java.lang.annotation.*; |
31 |
import java.lang.reflect.Array; |
|
32 |
import java.lang.reflect.Method; |
|
33 |
import java.util.LinkedHashMap; |
|
34 |
import java.util.Map; |
|
35 |
import sun.reflect.annotation.*; |
|
36 |
||
37 |
import javax.lang.model.type.MirroredTypeException; |
|
38 |
import javax.lang.model.type.MirroredTypesException; |
|
14258 | 39 |
import javax.lang.model.type.TypeMirror; |
40 |
||
10 | 41 |
import com.sun.tools.javac.code.*; |
42 |
import com.sun.tools.javac.code.Symbol.*; |
|
43 |
import com.sun.tools.javac.code.Type.ArrayType; |
|
14258 | 44 |
import com.sun.tools.javac.util.*; |
10 | 45 |
|
46 |
||
47 |
/** |
|
48 |
* A generator of dynamic proxy implementations of |
|
49 |
* java.lang.annotation.Annotation. |
|
50 |
* |
|
51 |
* <p> The "dynamic proxy return form" of an annotation element value is |
|
52 |
* the form used by sun.reflect.annotation.AnnotationInvocationHandler. |
|
53 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5843
diff
changeset
|
54 |
* <p><b>This is NOT part of any supported API. |
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5843
diff
changeset
|
55 |
* If you write code that depends on this, you do so at your own risk. |
10 | 56 |
* This code and its internal interfaces are subject to change or |
57 |
* deletion without notice.</b> |
|
58 |
*/ |
|
59 |
||
60 |
public class AnnotationProxyMaker { |
|
61 |
||
62 |
private final Attribute.Compound anno; |
|
63 |
private final Class<? extends Annotation> annoType; |
|
64 |
||
65 |
||
66 |
private AnnotationProxyMaker(Attribute.Compound anno, |
|
67 |
Class<? extends Annotation> annoType) { |
|
68 |
this.anno = anno; |
|
69 |
this.annoType = annoType; |
|
70 |
} |
|
71 |
||
72 |
||
73 |
/** |
|
74 |
* Returns a dynamic proxy for an annotation mirror. |
|
75 |
*/ |
|
76 |
public static <A extends Annotation> A generateAnnotation( |
|
77 |
Attribute.Compound anno, Class<A> annoType) { |
|
78 |
AnnotationProxyMaker apm = new AnnotationProxyMaker(anno, annoType); |
|
79 |
return annoType.cast(apm.generateAnnotation()); |
|
80 |
} |
|
81 |
||
82 |
||
83 |
/** |
|
84 |
* Returns a dynamic proxy for an annotation mirror. |
|
85 |
*/ |
|
86 |
private Annotation generateAnnotation() { |
|
87 |
return AnnotationParser.annotationForMap(annoType, |
|
88 |
getAllReflectedValues()); |
|
89 |
} |
|
90 |
||
91 |
/** |
|
92 |
* Returns a map from element names to their values in "dynamic |
|
93 |
* proxy return form". Includes all elements, whether explicit or |
|
94 |
* defaulted. |
|
95 |
*/ |
|
96 |
private Map<String, Object> getAllReflectedValues() { |
|
22163 | 97 |
Map<String, Object> res = new LinkedHashMap<>(); |
10 | 98 |
|
99 |
for (Map.Entry<MethodSymbol, Attribute> entry : |
|
100 |
getAllValues().entrySet()) { |
|
101 |
MethodSymbol meth = entry.getKey(); |
|
102 |
Object value = generateValue(meth, entry.getValue()); |
|
103 |
if (value != null) { |
|
104 |
res.put(meth.name.toString(), value); |
|
105 |
} else { |
|
106 |
// Ignore this element. May (properly) lead to |
|
107 |
// IncompleteAnnotationException somewhere down the line. |
|
108 |
} |
|
109 |
} |
|
110 |
return res; |
|
111 |
} |
|
112 |
||
113 |
/** |
|
114 |
* Returns a map from element symbols to their values. |
|
115 |
* Includes all elements, whether explicit or defaulted. |
|
116 |
*/ |
|
117 |
private Map<MethodSymbol, Attribute> getAllValues() { |
|
22163 | 118 |
Map<MethodSymbol, Attribute> res = new LinkedHashMap<>(); |
10 | 119 |
|
120 |
// First find the default values. |
|
121 |
ClassSymbol sym = (ClassSymbol) anno.type.tsym; |
|
122 |
for (Scope.Entry e = sym.members().elems; e != null; e = e.sibling) { |
|
123 |
if (e.sym.kind == Kinds.MTH) { |
|
124 |
MethodSymbol m = (MethodSymbol) e.sym; |
|
125 |
Attribute def = m.getDefaultValue(); |
|
126 |
if (def != null) |
|
127 |
res.put(m, def); |
|
128 |
} |
|
129 |
} |
|
130 |
// Next find the explicit values, possibly overriding defaults. |
|
131 |
for (Pair<MethodSymbol, Attribute> p : anno.values) |
|
132 |
res.put(p.fst, p.snd); |
|
133 |
return res; |
|
134 |
} |
|
135 |
||
136 |
/** |
|
137 |
* Converts an element value to its "dynamic proxy return form". |
|
138 |
* Returns an exception proxy on some errors, but may return null if |
|
139 |
* a useful exception cannot or should not be generated at this point. |
|
140 |
*/ |
|
141 |
private Object generateValue(MethodSymbol meth, Attribute attr) { |
|
142 |
ValueVisitor vv = new ValueVisitor(meth); |
|
143 |
return vv.getValue(attr); |
|
144 |
} |
|
145 |
||
146 |
||
147 |
private class ValueVisitor implements Attribute.Visitor { |
|
148 |
||
149 |
private MethodSymbol meth; // annotation element being visited |
|
150 |
private Class<?> returnClass; // return type of annotation element |
|
151 |
private Object value; // value in "dynamic proxy return form" |
|
152 |
||
153 |
ValueVisitor(MethodSymbol meth) { |
|
154 |
this.meth = meth; |
|
155 |
} |
|
156 |
||
157 |
Object getValue(Attribute attr) { |
|
158 |
Method method; // runtime method of annotation element |
|
159 |
try { |
|
160 |
method = annoType.getMethod(meth.name.toString()); |
|
161 |
} catch (NoSuchMethodException e) { |
|
162 |
return null; |
|
163 |
} |
|
164 |
returnClass = method.getReturnType(); |
|
165 |
attr.accept(this); |
|
166 |
if (!(value instanceof ExceptionProxy) && |
|
167 |
!AnnotationType.invocationHandlerReturnType(returnClass) |
|
168 |
.isInstance(value)) { |
|
169 |
typeMismatch(method, attr); |
|
170 |
} |
|
171 |
return value; |
|
172 |
} |
|
173 |
||
174 |
||
175 |
public void visitConstant(Attribute.Constant c) { |
|
176 |
value = c.getValue(); |
|
177 |
} |
|
178 |
||
179 |
public void visitClass(Attribute.Class c) { |
|
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
7681
diff
changeset
|
180 |
value = new MirroredTypeExceptionProxy(c.classType); |
10 | 181 |
} |
182 |
||
183 |
public void visitArray(Attribute.Array a) { |
|
5843
e659a8974c9c
6519115: MirroredTypeException thrown but should be MirroredTypesException
darcy
parents:
5520
diff
changeset
|
184 |
Name elemName = ((ArrayType) a.type).elemtype.tsym.getQualifiedName(); |
10 | 185 |
|
5843
e659a8974c9c
6519115: MirroredTypeException thrown but should be MirroredTypesException
darcy
parents:
5520
diff
changeset
|
186 |
if (elemName.equals(elemName.table.names.java_lang_Class)) { // Class[] |
10 | 187 |
// Construct a proxy for a MirroredTypesException |
22163 | 188 |
ListBuffer<TypeMirror> elems = new ListBuffer<>(); |
10 | 189 |
for (Attribute value : a.values) { |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
7681
diff
changeset
|
190 |
Type elem = ((Attribute.Class) value).classType; |
5843
e659a8974c9c
6519115: MirroredTypeException thrown but should be MirroredTypesException
darcy
parents:
5520
diff
changeset
|
191 |
elems.append(elem); |
10 | 192 |
} |
5843
e659a8974c9c
6519115: MirroredTypeException thrown but should be MirroredTypesException
darcy
parents:
5520
diff
changeset
|
193 |
value = new MirroredTypesExceptionProxy(elems.toList()); |
10 | 194 |
|
195 |
} else { |
|
196 |
int len = a.values.length; |
|
197 |
Class<?> returnClassSaved = returnClass; |
|
198 |
returnClass = returnClass.getComponentType(); |
|
199 |
try { |
|
200 |
Object res = Array.newInstance(returnClass, len); |
|
201 |
for (int i = 0; i < len; i++) { |
|
202 |
a.values[i].accept(this); |
|
203 |
if (value == null || value instanceof ExceptionProxy) { |
|
204 |
return; |
|
205 |
} |
|
206 |
try { |
|
207 |
Array.set(res, i, value); |
|
208 |
} catch (IllegalArgumentException e) { |
|
209 |
value = null; // indicates a type mismatch |
|
210 |
return; |
|
211 |
} |
|
212 |
} |
|
213 |
value = res; |
|
214 |
} finally { |
|
215 |
returnClass = returnClassSaved; |
|
216 |
} |
|
217 |
} |
|
218 |
} |
|
219 |
||
1789
7ac8c0815000
6765045: Remove rawtypes warnings from langtools
mcimadamore
parents:
1264
diff
changeset
|
220 |
@SuppressWarnings({"unchecked", "rawtypes"}) |
10 | 221 |
public void visitEnum(Attribute.Enum e) { |
222 |
if (returnClass.isEnum()) { |
|
223 |
String constName = e.value.toString(); |
|
224 |
try { |
|
1789
7ac8c0815000
6765045: Remove rawtypes warnings from langtools
mcimadamore
parents:
1264
diff
changeset
|
225 |
value = Enum.valueOf((Class)returnClass, constName); |
10 | 226 |
} catch (IllegalArgumentException ex) { |
227 |
value = new EnumConstantNotPresentExceptionProxy( |
|
1789
7ac8c0815000
6765045: Remove rawtypes warnings from langtools
mcimadamore
parents:
1264
diff
changeset
|
228 |
(Class<Enum<?>>) returnClass, constName); |
10 | 229 |
} |
230 |
} else { |
|
231 |
value = null; // indicates a type mismatch |
|
232 |
} |
|
233 |
} |
|
234 |
||
235 |
public void visitCompound(Attribute.Compound c) { |
|
236 |
try { |
|
237 |
Class<? extends Annotation> nested = |
|
238 |
returnClass.asSubclass(Annotation.class); |
|
239 |
value = generateAnnotation(c, nested); |
|
240 |
} catch (ClassCastException ex) { |
|
241 |
value = null; // indicates a type mismatch |
|
242 |
} |
|
243 |
} |
|
244 |
||
245 |
public void visitError(Attribute.Error e) { |
|
19507
323f001d6be1
8019243: AnnotationTypeMismatchException instead of MirroredTypeException
jfranck
parents:
16557
diff
changeset
|
246 |
if (e instanceof Attribute.UnresolvedClass) |
323f001d6be1
8019243: AnnotationTypeMismatchException instead of MirroredTypeException
jfranck
parents:
16557
diff
changeset
|
247 |
value = new MirroredTypeExceptionProxy(((Attribute.UnresolvedClass)e).classType); |
323f001d6be1
8019243: AnnotationTypeMismatchException instead of MirroredTypeException
jfranck
parents:
16557
diff
changeset
|
248 |
else |
323f001d6be1
8019243: AnnotationTypeMismatchException instead of MirroredTypeException
jfranck
parents:
16557
diff
changeset
|
249 |
value = null; // indicates a type mismatch |
10 | 250 |
} |
251 |
||
252 |
||
253 |
/** |
|
254 |
* Sets "value" to an ExceptionProxy indicating a type mismatch. |
|
255 |
*/ |
|
6931
d6339142c51f
6990133: AnnotationProxyMaker.ValueVisitor$1 contains non-transient non-serializable field
jjg
parents:
5847
diff
changeset
|
256 |
private void typeMismatch(Method method, final Attribute attr) { |
d6339142c51f
6990133: AnnotationProxyMaker.ValueVisitor$1 contains non-transient non-serializable field
jjg
parents:
5847
diff
changeset
|
257 |
class AnnotationTypeMismatchExceptionProxy extends ExceptionProxy { |
10 | 258 |
static final long serialVersionUID = 269; |
6931
d6339142c51f
6990133: AnnotationProxyMaker.ValueVisitor$1 contains non-transient non-serializable field
jjg
parents:
5847
diff
changeset
|
259 |
transient final Method method; |
d6339142c51f
6990133: AnnotationProxyMaker.ValueVisitor$1 contains non-transient non-serializable field
jjg
parents:
5847
diff
changeset
|
260 |
AnnotationTypeMismatchExceptionProxy(Method method) { |
d6339142c51f
6990133: AnnotationProxyMaker.ValueVisitor$1 contains non-transient non-serializable field
jjg
parents:
5847
diff
changeset
|
261 |
this.method = method; |
d6339142c51f
6990133: AnnotationProxyMaker.ValueVisitor$1 contains non-transient non-serializable field
jjg
parents:
5847
diff
changeset
|
262 |
} |
10 | 263 |
public String toString() { |
264 |
return "<error>"; // eg: @Anno(value=<error>) |
|
265 |
} |
|
266 |
protected RuntimeException generateException() { |
|
267 |
return new AnnotationTypeMismatchException(method, |
|
268 |
attr.type.toString()); |
|
269 |
} |
|
6931
d6339142c51f
6990133: AnnotationProxyMaker.ValueVisitor$1 contains non-transient non-serializable field
jjg
parents:
5847
diff
changeset
|
270 |
} |
d6339142c51f
6990133: AnnotationProxyMaker.ValueVisitor$1 contains non-transient non-serializable field
jjg
parents:
5847
diff
changeset
|
271 |
value = new AnnotationTypeMismatchExceptionProxy(method); |
10 | 272 |
} |
273 |
} |
|
274 |
||
275 |
||
276 |
/** |
|
277 |
* ExceptionProxy for MirroredTypeException. |
|
16557
67a3ae363f03
8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
14258
diff
changeset
|
278 |
* The toString, hashCode, and equals methods forward to the underlying |
10 | 279 |
* type. |
280 |
*/ |
|
5003
fd0b30cdbe5c
6929645: Address various findbugs warnings in langtools
darcy
parents:
1789
diff
changeset
|
281 |
private static final class MirroredTypeExceptionProxy extends ExceptionProxy { |
10 | 282 |
static final long serialVersionUID = 269; |
283 |
||
5003
fd0b30cdbe5c
6929645: Address various findbugs warnings in langtools
darcy
parents:
1789
diff
changeset
|
284 |
private transient TypeMirror type; |
10 | 285 |
private final String typeString; |
286 |
||
287 |
MirroredTypeExceptionProxy(TypeMirror t) { |
|
288 |
type = t; |
|
289 |
typeString = t.toString(); |
|
290 |
} |
|
291 |
||
292 |
public String toString() { |
|
293 |
return typeString; |
|
294 |
} |
|
295 |
||
296 |
public int hashCode() { |
|
297 |
return (type != null ? type : typeString).hashCode(); |
|
298 |
} |
|
299 |
||
300 |
public boolean equals(Object obj) { |
|
301 |
return type != null && |
|
302 |
obj instanceof MirroredTypeExceptionProxy && |
|
303 |
type.equals(((MirroredTypeExceptionProxy) obj).type); |
|
304 |
} |
|
305 |
||
306 |
protected RuntimeException generateException() { |
|
307 |
return new MirroredTypeException(type); |
|
308 |
} |
|
5003
fd0b30cdbe5c
6929645: Address various findbugs warnings in langtools
darcy
parents:
1789
diff
changeset
|
309 |
|
fd0b30cdbe5c
6929645: Address various findbugs warnings in langtools
darcy
parents:
1789
diff
changeset
|
310 |
// Explicitly set all transient fields. |
fd0b30cdbe5c
6929645: Address various findbugs warnings in langtools
darcy
parents:
1789
diff
changeset
|
311 |
private void readObject(ObjectInputStream s) |
fd0b30cdbe5c
6929645: Address various findbugs warnings in langtools
darcy
parents:
1789
diff
changeset
|
312 |
throws IOException, ClassNotFoundException { |
fd0b30cdbe5c
6929645: Address various findbugs warnings in langtools
darcy
parents:
1789
diff
changeset
|
313 |
s.defaultReadObject(); |
fd0b30cdbe5c
6929645: Address various findbugs warnings in langtools
darcy
parents:
1789
diff
changeset
|
314 |
type = null; |
fd0b30cdbe5c
6929645: Address various findbugs warnings in langtools
darcy
parents:
1789
diff
changeset
|
315 |
} |
10 | 316 |
} |
317 |
||
318 |
||
319 |
/** |
|
320 |
* ExceptionProxy for MirroredTypesException. |
|
321 |
* The toString, hashCode, and equals methods foward to the underlying |
|
322 |
* types. |
|
323 |
*/ |
|
5003
fd0b30cdbe5c
6929645: Address various findbugs warnings in langtools
darcy
parents:
1789
diff
changeset
|
324 |
private static final class MirroredTypesExceptionProxy extends ExceptionProxy { |
10 | 325 |
static final long serialVersionUID = 269; |
326 |
||
5003
fd0b30cdbe5c
6929645: Address various findbugs warnings in langtools
darcy
parents:
1789
diff
changeset
|
327 |
private transient List<TypeMirror> types; |
10 | 328 |
private final String typeStrings; |
329 |
||
330 |
MirroredTypesExceptionProxy(List<TypeMirror> ts) { |
|
331 |
types = ts; |
|
332 |
typeStrings = ts.toString(); |
|
333 |
} |
|
334 |
||
335 |
public String toString() { |
|
336 |
return typeStrings; |
|
337 |
} |
|
338 |
||
339 |
public int hashCode() { |
|
340 |
return (types != null ? types : typeStrings).hashCode(); |
|
341 |
} |
|
342 |
||
343 |
public boolean equals(Object obj) { |
|
344 |
return types != null && |
|
345 |
obj instanceof MirroredTypesExceptionProxy && |
|
346 |
types.equals( |
|
347 |
((MirroredTypesExceptionProxy) obj).types); |
|
348 |
} |
|
349 |
||
350 |
protected RuntimeException generateException() { |
|
351 |
return new MirroredTypesException(types); |
|
352 |
} |
|
5003
fd0b30cdbe5c
6929645: Address various findbugs warnings in langtools
darcy
parents:
1789
diff
changeset
|
353 |
|
fd0b30cdbe5c
6929645: Address various findbugs warnings in langtools
darcy
parents:
1789
diff
changeset
|
354 |
// Explicitly set all transient fields. |
fd0b30cdbe5c
6929645: Address various findbugs warnings in langtools
darcy
parents:
1789
diff
changeset
|
355 |
private void readObject(ObjectInputStream s) |
fd0b30cdbe5c
6929645: Address various findbugs warnings in langtools
darcy
parents:
1789
diff
changeset
|
356 |
throws IOException, ClassNotFoundException { |
fd0b30cdbe5c
6929645: Address various findbugs warnings in langtools
darcy
parents:
1789
diff
changeset
|
357 |
s.defaultReadObject(); |
fd0b30cdbe5c
6929645: Address various findbugs warnings in langtools
darcy
parents:
1789
diff
changeset
|
358 |
types = null; |
fd0b30cdbe5c
6929645: Address various findbugs warnings in langtools
darcy
parents:
1789
diff
changeset
|
359 |
} |
10 | 360 |
} |
361 |
} |