31 import javax.lang.model.element.AnnotationValue; |
31 import javax.lang.model.element.AnnotationValue; |
32 import javax.lang.model.element.AnnotationValueVisitor; |
32 import javax.lang.model.element.AnnotationValueVisitor; |
33 import javax.lang.model.type.DeclaredType; |
33 import javax.lang.model.type.DeclaredType; |
34 import com.sun.tools.javac.code.Symbol.*; |
34 import com.sun.tools.javac.code.Symbol.*; |
35 import com.sun.tools.javac.util.*; |
35 import com.sun.tools.javac.util.*; |
|
36 import com.sun.tools.javac.util.DefinedBy.Api; |
36 |
37 |
37 /** An annotation value. |
38 /** An annotation value. |
38 * |
39 * |
39 * <p><b>This is NOT part of any supported API. |
40 * <p><b>This is NOT part of any supported API. |
40 * If you write code that depends on this, you do so at your own risk. |
41 * If you write code that depends on this, you do so at your own risk. |
50 this.type = type; |
51 this.type = type; |
51 } |
52 } |
52 |
53 |
53 public abstract void accept(Visitor v); |
54 public abstract void accept(Visitor v); |
54 |
55 |
|
56 @DefinedBy(Api.LANGUAGE_MODEL) |
55 public Object getValue() { |
57 public Object getValue() { |
56 throw new UnsupportedOperationException(); |
58 throw new UnsupportedOperationException(); |
57 } |
59 } |
58 |
60 |
|
61 @DefinedBy(Api.LANGUAGE_MODEL) |
59 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) { |
62 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) { |
60 throw new UnsupportedOperationException(); |
63 throw new UnsupportedOperationException(); |
61 } |
64 } |
62 |
65 |
63 public boolean isSynthesized() { |
66 public boolean isSynthesized() { |
72 public void accept(Visitor v) { v.visitConstant(this); } |
75 public void accept(Visitor v) { v.visitConstant(this); } |
73 public Constant(Type type, Object value) { |
76 public Constant(Type type, Object value) { |
74 super(type); |
77 super(type); |
75 this.value = value; |
78 this.value = value; |
76 } |
79 } |
|
80 @DefinedBy(Api.LANGUAGE_MODEL) |
77 public String toString() { |
81 public String toString() { |
78 return Constants.format(value, type); |
82 return Constants.format(value, type); |
79 } |
83 } |
|
84 @DefinedBy(Api.LANGUAGE_MODEL) |
80 public Object getValue() { |
85 public Object getValue() { |
81 return Constants.decode(value, type); |
86 return Constants.decode(value, type); |
82 } |
87 } |
|
88 @DefinedBy(Api.LANGUAGE_MODEL) |
83 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) { |
89 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) { |
84 if (value instanceof String) |
90 if (value instanceof String) |
85 return v.visitString((String) value, p); |
91 return v.visitString((String) value, p); |
86 if (value instanceof Integer) { |
92 if (value instanceof Integer) { |
87 int i = (Integer) value; |
93 int i = (Integer) value; |
119 return new Type.ClassType(types.syms.classType.getEnclosingType(), |
125 return new Type.ClassType(types.syms.classType.getEnclosingType(), |
120 List.of(arg), |
126 List.of(arg), |
121 types.syms.classType.tsym, |
127 types.syms.classType.tsym, |
122 Type.noAnnotations); |
128 Type.noAnnotations); |
123 } |
129 } |
|
130 @DefinedBy(Api.LANGUAGE_MODEL) |
124 public String toString() { |
131 public String toString() { |
125 return classType + ".class"; |
132 return classType + ".class"; |
126 } |
133 } |
|
134 @DefinedBy(Api.LANGUAGE_MODEL) |
127 public Type getValue() { |
135 public Type getValue() { |
128 return classType; |
136 return classType; |
129 } |
137 } |
|
138 @DefinedBy(Api.LANGUAGE_MODEL) |
130 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) { |
139 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) { |
131 return v.visitType(classType, p); |
140 return v.visitType(classType, p); |
132 } |
141 } |
133 } |
142 } |
134 |
143 |
181 * @com.example.foo(name1=val1, name2=val2) |
190 * @com.example.foo(name1=val1, name2=val2) |
182 * @com.example.foo(val) |
191 * @com.example.foo(val) |
183 * @com.example.foo |
192 * @com.example.foo |
184 * Omit parens for marker annotations, and omit "value=" when allowed. |
193 * Omit parens for marker annotations, and omit "value=" when allowed. |
185 */ |
194 */ |
|
195 @DefinedBy(Api.LANGUAGE_MODEL) |
186 public String toString() { |
196 public String toString() { |
187 StringBuilder buf = new StringBuilder(); |
197 StringBuilder buf = new StringBuilder(); |
188 buf.append("@"); |
198 buf.append("@"); |
189 buf.append(type); |
199 buf.append(type); |
190 int len = values.length(); |
200 int len = values.length(); |
216 for (Pair<MethodSymbol,Attribute> pair : values) |
226 for (Pair<MethodSymbol,Attribute> pair : values) |
217 if (pair.fst.name == member) return pair; |
227 if (pair.fst.name == member) return pair; |
218 return null; |
228 return null; |
219 } |
229 } |
220 |
230 |
|
231 @DefinedBy(Api.LANGUAGE_MODEL) |
221 public Attribute.Compound getValue() { |
232 public Attribute.Compound getValue() { |
222 return this; |
233 return this; |
223 } |
234 } |
224 |
235 |
|
236 @DefinedBy(Api.LANGUAGE_MODEL) |
225 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) { |
237 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) { |
226 return v.visitAnnotation(this, p); |
238 return v.visitAnnotation(this, p); |
227 } |
239 } |
228 |
240 |
|
241 @DefinedBy(Api.LANGUAGE_MODEL) |
229 public DeclaredType getAnnotationType() { |
242 public DeclaredType getAnnotationType() { |
230 return (DeclaredType) type; |
243 return (DeclaredType) type; |
231 } |
244 } |
232 |
245 |
|
246 @DefinedBy(Api.LANGUAGE_MODEL) |
233 public Map<MethodSymbol, Attribute> getElementValues() { |
247 public Map<MethodSymbol, Attribute> getElementValues() { |
234 Map<MethodSymbol, Attribute> valmap = new LinkedHashMap<>(); |
248 Map<MethodSymbol, Attribute> valmap = new LinkedHashMap<>(); |
235 for (Pair<MethodSymbol, Attribute> value : values) |
249 for (Pair<MethodSymbol, Attribute> value : values) |
236 valmap.put(value.fst, value.snd); |
250 valmap.put(value.fst, value.snd); |
237 return valmap; |
251 return valmap; |
270 super(type); |
284 super(type); |
271 this.values = values.toArray(new Attribute[values.size()]); |
285 this.values = values.toArray(new Attribute[values.size()]); |
272 } |
286 } |
273 |
287 |
274 public void accept(Visitor v) { v.visitArray(this); } |
288 public void accept(Visitor v) { v.visitArray(this); } |
|
289 @DefinedBy(Api.LANGUAGE_MODEL) |
275 public String toString() { |
290 public String toString() { |
276 StringBuilder buf = new StringBuilder(); |
291 StringBuilder buf = new StringBuilder(); |
277 buf.append('{'); |
292 buf.append('{'); |
278 boolean first = true; |
293 boolean first = true; |
279 for (Attribute value : values) { |
294 for (Attribute value : values) { |
283 buf.append(value); |
298 buf.append(value); |
284 } |
299 } |
285 buf.append('}'); |
300 buf.append('}'); |
286 return buf.toString(); |
301 return buf.toString(); |
287 } |
302 } |
|
303 @DefinedBy(Api.LANGUAGE_MODEL) |
288 public List<Attribute> getValue() { |
304 public List<Attribute> getValue() { |
289 return List.from(values); |
305 return List.from(values); |
290 } |
306 } |
|
307 @DefinedBy(Api.LANGUAGE_MODEL) |
291 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) { |
308 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) { |
292 return v.visitArray(getValue(), p); |
309 return v.visitArray(getValue(), p); |
293 } |
310 } |
294 |
311 |
295 @Override |
312 @Override |
308 public Enum(Type type, VarSymbol value) { |
325 public Enum(Type type, VarSymbol value) { |
309 super(type); |
326 super(type); |
310 this.value = Assert.checkNonNull(value); |
327 this.value = Assert.checkNonNull(value); |
311 } |
328 } |
312 public void accept(Visitor v) { v.visitEnum(this); } |
329 public void accept(Visitor v) { v.visitEnum(this); } |
|
330 @DefinedBy(Api.LANGUAGE_MODEL) |
313 public String toString() { |
331 public String toString() { |
314 return value.enclClass() + "." + value; // qualified name |
332 return value.enclClass() + "." + value; // qualified name |
315 } |
333 } |
|
334 @DefinedBy(Api.LANGUAGE_MODEL) |
316 public VarSymbol getValue() { |
335 public VarSymbol getValue() { |
317 return value; |
336 return value; |
318 } |
337 } |
|
338 @DefinedBy(Api.LANGUAGE_MODEL) |
319 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) { |
339 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) { |
320 return v.visitEnumConstant(value, p); |
340 return v.visitEnumConstant(value, p); |
321 } |
341 } |
322 } |
342 } |
323 |
343 |
324 public static class Error extends Attribute { |
344 public static class Error extends Attribute { |
325 public Error(Type type) { |
345 public Error(Type type) { |
326 super(type); |
346 super(type); |
327 } |
347 } |
328 public void accept(Visitor v) { v.visitError(this); } |
348 public void accept(Visitor v) { v.visitError(this); } |
|
349 @DefinedBy(Api.LANGUAGE_MODEL) |
329 public String toString() { |
350 public String toString() { |
330 return "<error>"; |
351 return "<error>"; |
331 } |
352 } |
|
353 @DefinedBy(Api.LANGUAGE_MODEL) |
332 public String getValue() { |
354 public String getValue() { |
333 return toString(); |
355 return toString(); |
334 } |
356 } |
|
357 @DefinedBy(Api.LANGUAGE_MODEL) |
335 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) { |
358 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) { |
336 return v.visitString(toString(), p); |
359 return v.visitString(toString(), p); |
337 } |
360 } |
338 } |
361 } |
339 |
362 |