author | mcimadamore |
Mon, 21 Jan 2013 20:13:56 +0000 | |
changeset 15374 | fb8f6acf09cc |
parent 14961 | e731935052af |
child 15385 | ee1eebe7e210 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
8032
diff
changeset
|
2 |
* Copyright (c) 2003, 2012, 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.code; |
|
27 |
||
28 |
import java.util.LinkedHashMap; |
|
29 |
import java.util.Map; |
|
30 |
import javax.lang.model.element.AnnotationMirror; |
|
31 |
import javax.lang.model.element.AnnotationValue; |
|
32 |
import javax.lang.model.element.AnnotationValueVisitor; |
|
33 |
import javax.lang.model.type.DeclaredType; |
|
34 |
import com.sun.tools.javac.code.Symbol.*; |
|
35 |
import com.sun.tools.javac.util.*; |
|
36 |
||
37 |
/** An annotation value. |
|
38 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
39 |
* <p><b>This is NOT part of any supported API. |
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
40 |
* If you write code that depends on this, you do so at your own risk. |
10 | 41 |
* This code and its internal interfaces are subject to change or |
42 |
* deletion without notice.</b> |
|
43 |
*/ |
|
44 |
public abstract class Attribute implements AnnotationValue { |
|
45 |
||
46 |
/** The type of the annotation element. */ |
|
47 |
public Type type; |
|
48 |
||
49 |
public Attribute(Type type) { |
|
50 |
this.type = type; |
|
51 |
} |
|
52 |
||
53 |
public abstract void accept(Visitor v); |
|
54 |
||
55 |
public Object getValue() { |
|
56 |
throw new UnsupportedOperationException(); |
|
57 |
} |
|
58 |
||
59 |
public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) { |
|
60 |
throw new UnsupportedOperationException(); |
|
61 |
} |
|
62 |
||
14961
e731935052af
8005098: Provide isSynthesized() information on Attribute.Compound
jfranck
parents:
14359
diff
changeset
|
63 |
public boolean isSynthesized() { |
e731935052af
8005098: Provide isSynthesized() information on Attribute.Compound
jfranck
parents:
14359
diff
changeset
|
64 |
return false; |
e731935052af
8005098: Provide isSynthesized() information on Attribute.Compound
jfranck
parents:
14359
diff
changeset
|
65 |
} |
10 | 66 |
|
67 |
/** The value for an annotation element of primitive type or String. */ |
|
68 |
public static class Constant extends Attribute { |
|
69 |
public final Object value; |
|
70 |
public void accept(Visitor v) { v.visitConstant(this); } |
|
71 |
public Constant(Type type, Object value) { |
|
72 |
super(type); |
|
73 |
this.value = value; |
|
74 |
} |
|
75 |
public String toString() { |
|
76 |
return Constants.format(value, type); |
|
77 |
} |
|
78 |
public Object getValue() { |
|
79 |
return Constants.decode(value, type); |
|
80 |
} |
|
81 |
public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) { |
|
82 |
if (value instanceof String) |
|
83 |
return v.visitString((String) value, p); |
|
84 |
if (value instanceof Integer) { |
|
85 |
int i = (Integer) value; |
|
86 |
switch (type.tag) { |
|
87 |
case BOOLEAN: return v.visitBoolean(i != 0, p); |
|
88 |
case CHAR: return v.visitChar((char) i, p); |
|
89 |
case BYTE: return v.visitByte((byte) i, p); |
|
90 |
case SHORT: return v.visitShort((short) i, p); |
|
91 |
case INT: return v.visitInt(i, p); |
|
92 |
} |
|
93 |
} |
|
94 |
switch (type.tag) { |
|
95 |
case LONG: return v.visitLong((Long) value, p); |
|
96 |
case FLOAT: return v.visitFloat((Float) value, p); |
|
97 |
case DOUBLE: return v.visitDouble((Double) value, p); |
|
98 |
} |
|
99 |
throw new AssertionError("Bad annotation element value: " + value); |
|
100 |
} |
|
101 |
} |
|
102 |
||
103 |
/** The value for an annotation element of type java.lang.Class, |
|
104 |
* represented as a ClassSymbol. |
|
105 |
*/ |
|
106 |
public static class Class extends Attribute { |
|
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
8032
diff
changeset
|
107 |
public final Type classType; |
10 | 108 |
public void accept(Visitor v) { v.visitClass(this); } |
109 |
public Class(Types types, Type type) { |
|
110 |
super(makeClassType(types, type)); |
|
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
8032
diff
changeset
|
111 |
this.classType = type; |
10 | 112 |
} |
113 |
static Type makeClassType(Types types, Type type) { |
|
114 |
Type arg = type.isPrimitive() |
|
115 |
? types.boxedClass(type).type |
|
116 |
: types.erasure(type); |
|
117 |
return new Type.ClassType(types.syms.classType.getEnclosingType(), |
|
118 |
List.of(arg), |
|
119 |
types.syms.classType.tsym); |
|
120 |
} |
|
121 |
public String toString() { |
|
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
8032
diff
changeset
|
122 |
return classType + ".class"; |
10 | 123 |
} |
124 |
public Type getValue() { |
|
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
8032
diff
changeset
|
125 |
return classType; |
10 | 126 |
} |
127 |
public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) { |
|
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
8032
diff
changeset
|
128 |
return v.visitType(classType, p); |
10 | 129 |
} |
130 |
} |
|
131 |
||
132 |
/** A compound annotation element value, the type of which is an |
|
133 |
* attribute interface. |
|
134 |
*/ |
|
135 |
public static class Compound extends Attribute implements AnnotationMirror { |
|
136 |
/** The attributes values, as pairs. Each pair contains a |
|
137 |
* reference to the accessing method in the attribute interface |
|
138 |
* and the value to be returned when that method is called to |
|
139 |
* access this attribute. |
|
140 |
*/ |
|
141 |
public final List<Pair<MethodSymbol,Attribute>> values; |
|
14961
e731935052af
8005098: Provide isSynthesized() information on Attribute.Compound
jfranck
parents:
14359
diff
changeset
|
142 |
|
e731935052af
8005098: Provide isSynthesized() information on Attribute.Compound
jfranck
parents:
14359
diff
changeset
|
143 |
private boolean synthesized = false; |
e731935052af
8005098: Provide isSynthesized() information on Attribute.Compound
jfranck
parents:
14359
diff
changeset
|
144 |
|
e731935052af
8005098: Provide isSynthesized() information on Attribute.Compound
jfranck
parents:
14359
diff
changeset
|
145 |
@Override |
e731935052af
8005098: Provide isSynthesized() information on Attribute.Compound
jfranck
parents:
14359
diff
changeset
|
146 |
public boolean isSynthesized() { |
e731935052af
8005098: Provide isSynthesized() information on Attribute.Compound
jfranck
parents:
14359
diff
changeset
|
147 |
return synthesized; |
e731935052af
8005098: Provide isSynthesized() information on Attribute.Compound
jfranck
parents:
14359
diff
changeset
|
148 |
} |
e731935052af
8005098: Provide isSynthesized() information on Attribute.Compound
jfranck
parents:
14359
diff
changeset
|
149 |
|
e731935052af
8005098: Provide isSynthesized() information on Attribute.Compound
jfranck
parents:
14359
diff
changeset
|
150 |
public void setSynthesized(boolean synthesized) { |
e731935052af
8005098: Provide isSynthesized() information on Attribute.Compound
jfranck
parents:
14359
diff
changeset
|
151 |
this.synthesized = synthesized; |
e731935052af
8005098: Provide isSynthesized() information on Attribute.Compound
jfranck
parents:
14359
diff
changeset
|
152 |
} |
e731935052af
8005098: Provide isSynthesized() information on Attribute.Compound
jfranck
parents:
14359
diff
changeset
|
153 |
|
10 | 154 |
public Compound(Type type, |
155 |
List<Pair<MethodSymbol,Attribute>> values) { |
|
156 |
super(type); |
|
157 |
this.values = values; |
|
158 |
} |
|
159 |
public void accept(Visitor v) { v.visitCompound(this); } |
|
160 |
||
161 |
/** |
|
162 |
* Returns a string representation of this annotation. |
|
163 |
* String is of one of the forms: |
|
164 |
* @com.example.foo(name1=val1, name2=val2) |
|
165 |
* @com.example.foo(val) |
|
166 |
* @com.example.foo |
|
167 |
* Omit parens for marker annotations, and omit "value=" when allowed. |
|
168 |
*/ |
|
169 |
public String toString() { |
|
170 |
StringBuilder buf = new StringBuilder(); |
|
171 |
buf.append("@"); |
|
172 |
buf.append(type); |
|
173 |
int len = values.length(); |
|
174 |
if (len > 0) { |
|
175 |
buf.append('('); |
|
176 |
boolean first = true; |
|
177 |
for (Pair<MethodSymbol, Attribute> value : values) { |
|
178 |
if (!first) buf.append(", "); |
|
179 |
first = false; |
|
180 |
||
181 |
Name name = value.fst.name; |
|
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
1206
diff
changeset
|
182 |
if (len > 1 || name != name.table.names.value) { |
10 | 183 |
buf.append(name); |
184 |
buf.append('='); |
|
185 |
} |
|
186 |
buf.append(value.snd); |
|
187 |
} |
|
188 |
buf.append(')'); |
|
189 |
} |
|
190 |
return buf.toString(); |
|
191 |
} |
|
192 |
||
193 |
public Attribute member(Name member) { |
|
194 |
for (Pair<MethodSymbol,Attribute> pair : values) |
|
195 |
if (pair.fst.name == member) return pair.snd; |
|
196 |
return null; |
|
197 |
} |
|
198 |
||
199 |
public Attribute.Compound getValue() { |
|
200 |
return this; |
|
201 |
} |
|
202 |
||
203 |
public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) { |
|
204 |
return v.visitAnnotation(this, p); |
|
205 |
} |
|
206 |
||
207 |
public DeclaredType getAnnotationType() { |
|
208 |
return (DeclaredType) type; |
|
209 |
} |
|
210 |
||
211 |
public Map<MethodSymbol, Attribute> getElementValues() { |
|
212 |
Map<MethodSymbol, Attribute> valmap = |
|
213 |
new LinkedHashMap<MethodSymbol, Attribute>(); |
|
214 |
for (Pair<MethodSymbol, Attribute> value : values) |
|
215 |
valmap.put(value.fst, value.snd); |
|
216 |
return valmap; |
|
217 |
} |
|
218 |
} |
|
219 |
||
220 |
/** The value for an annotation element of an array type. |
|
221 |
*/ |
|
222 |
public static class Array extends Attribute { |
|
223 |
public final Attribute[] values; |
|
224 |
public Array(Type type, Attribute[] values) { |
|
225 |
super(type); |
|
226 |
this.values = values; |
|
227 |
} |
|
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
8032
diff
changeset
|
228 |
|
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
8032
diff
changeset
|
229 |
public Array(Type type, List<Attribute> values) { |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
8032
diff
changeset
|
230 |
super(type); |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
8032
diff
changeset
|
231 |
this.values = values.toArray(new Attribute[values.size()]); |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
8032
diff
changeset
|
232 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
8032
diff
changeset
|
233 |
|
10 | 234 |
public void accept(Visitor v) { v.visitArray(this); } |
235 |
public String toString() { |
|
236 |
StringBuilder buf = new StringBuilder(); |
|
237 |
buf.append('{'); |
|
238 |
boolean first = true; |
|
239 |
for (Attribute value : values) { |
|
240 |
if (!first) |
|
241 |
buf.append(", "); |
|
242 |
first = false; |
|
243 |
buf.append(value); |
|
244 |
} |
|
245 |
buf.append('}'); |
|
246 |
return buf.toString(); |
|
247 |
} |
|
248 |
public List<Attribute> getValue() { |
|
249 |
return List.from(values); |
|
250 |
} |
|
251 |
public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) { |
|
252 |
return v.visitArray(getValue(), p); |
|
253 |
} |
|
254 |
} |
|
255 |
||
256 |
/** The value for an annotation element of an enum type. |
|
257 |
*/ |
|
258 |
public static class Enum extends Attribute { |
|
259 |
public VarSymbol value; |
|
260 |
public Enum(Type type, VarSymbol value) { |
|
261 |
super(type); |
|
8032 | 262 |
this.value = Assert.checkNonNull(value); |
10 | 263 |
} |
264 |
public void accept(Visitor v) { v.visitEnum(this); } |
|
265 |
public String toString() { |
|
266 |
return value.enclClass() + "." + value; // qualified name |
|
267 |
} |
|
268 |
public VarSymbol getValue() { |
|
269 |
return value; |
|
270 |
} |
|
271 |
public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) { |
|
272 |
return v.visitEnumConstant(value, p); |
|
273 |
} |
|
274 |
} |
|
275 |
||
276 |
public static class Error extends Attribute { |
|
277 |
public Error(Type type) { |
|
278 |
super(type); |
|
279 |
} |
|
280 |
public void accept(Visitor v) { v.visitError(this); } |
|
281 |
public String toString() { |
|
282 |
return "<error>"; |
|
283 |
} |
|
284 |
public String getValue() { |
|
285 |
return toString(); |
|
286 |
} |
|
287 |
public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) { |
|
288 |
return v.visitString(toString(), p); |
|
289 |
} |
|
290 |
} |
|
291 |
||
292 |
/** A visitor type for dynamic dispatch on the kind of attribute value. */ |
|
293 |
public static interface Visitor { |
|
294 |
void visitConstant(Attribute.Constant value); |
|
295 |
void visitClass(Attribute.Class clazz); |
|
296 |
void visitCompound(Attribute.Compound compound); |
|
297 |
void visitArray(Attribute.Array array); |
|
298 |
void visitEnum(Attribute.Enum e); |
|
299 |
void visitError(Attribute.Error e); |
|
300 |
} |
|
6575
ae1798028008
6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
5847
diff
changeset
|
301 |
|
ae1798028008
6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
5847
diff
changeset
|
302 |
/** A mirror of java.lang.annotation.RetentionPolicy. */ |
ae1798028008
6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
5847
diff
changeset
|
303 |
public static enum RetentionPolicy { |
ae1798028008
6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
5847
diff
changeset
|
304 |
SOURCE, |
ae1798028008
6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
5847
diff
changeset
|
305 |
CLASS, |
ae1798028008
6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
5847
diff
changeset
|
306 |
RUNTIME |
ae1798028008
6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
5847
diff
changeset
|
307 |
} |
10 | 308 |
} |