Mon, 17 Dec 2012 07:47:05 -0800
8004832: Add new doclint package
Reviewed-by: mcimadamore
1 /*
2 * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
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
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
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 *
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.
24 */
26 package com.sun.tools.javac.code;
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.*;
37 /** An annotation value.
38 *
39 * <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 * This code and its internal interfaces are subject to change or
42 * deletion without notice.</b>
43 */
44 public abstract class Attribute implements AnnotationValue {
46 /** The type of the annotation element. */
47 public Type type;
49 public Attribute(Type type) {
50 this.type = type;
51 }
53 public abstract void accept(Visitor v);
55 public Object getValue() {
56 throw new UnsupportedOperationException();
57 }
59 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
60 throw new UnsupportedOperationException();
61 }
64 /** The value for an annotation element of primitive type or String. */
65 public static class Constant extends Attribute {
66 public final Object value;
67 public void accept(Visitor v) { v.visitConstant(this); }
68 public Constant(Type type, Object value) {
69 super(type);
70 this.value = value;
71 }
72 public String toString() {
73 return Constants.format(value, type);
74 }
75 public Object getValue() {
76 return Constants.decode(value, type);
77 }
78 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
79 if (value instanceof String)
80 return v.visitString((String) value, p);
81 if (value instanceof Integer) {
82 int i = (Integer) value;
83 switch (type.tag) {
84 case BOOLEAN: return v.visitBoolean(i != 0, p);
85 case CHAR: return v.visitChar((char) i, p);
86 case BYTE: return v.visitByte((byte) i, p);
87 case SHORT: return v.visitShort((short) i, p);
88 case INT: return v.visitInt(i, p);
89 }
90 }
91 switch (type.tag) {
92 case LONG: return v.visitLong((Long) value, p);
93 case FLOAT: return v.visitFloat((Float) value, p);
94 case DOUBLE: return v.visitDouble((Double) value, p);
95 }
96 throw new AssertionError("Bad annotation element value: " + value);
97 }
98 }
100 /** The value for an annotation element of type java.lang.Class,
101 * represented as a ClassSymbol.
102 */
103 public static class Class extends Attribute {
104 public final Type classType;
105 public void accept(Visitor v) { v.visitClass(this); }
106 public Class(Types types, Type type) {
107 super(makeClassType(types, type));
108 this.classType = type;
109 }
110 static Type makeClassType(Types types, Type type) {
111 Type arg = type.isPrimitive()
112 ? types.boxedClass(type).type
113 : types.erasure(type);
114 return new Type.ClassType(types.syms.classType.getEnclosingType(),
115 List.of(arg),
116 types.syms.classType.tsym);
117 }
118 public String toString() {
119 return classType + ".class";
120 }
121 public Type getValue() {
122 return classType;
123 }
124 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
125 return v.visitType(classType, p);
126 }
127 }
129 /** A compound annotation element value, the type of which is an
130 * attribute interface.
131 */
132 public static class Compound extends Attribute implements AnnotationMirror {
133 /** The attributes values, as pairs. Each pair contains a
134 * reference to the accessing method in the attribute interface
135 * and the value to be returned when that method is called to
136 * access this attribute.
137 */
138 public final List<Pair<MethodSymbol,Attribute>> values;
139 public Compound(Type type,
140 List<Pair<MethodSymbol,Attribute>> values) {
141 super(type);
142 this.values = values;
143 }
144 public void accept(Visitor v) { v.visitCompound(this); }
146 /**
147 * Returns a string representation of this annotation.
148 * String is of one of the forms:
149 * @com.example.foo(name1=val1, name2=val2)
150 * @com.example.foo(val)
151 * @com.example.foo
152 * Omit parens for marker annotations, and omit "value=" when allowed.
153 */
154 public String toString() {
155 StringBuilder buf = new StringBuilder();
156 buf.append("@");
157 buf.append(type);
158 int len = values.length();
159 if (len > 0) {
160 buf.append('(');
161 boolean first = true;
162 for (Pair<MethodSymbol, Attribute> value : values) {
163 if (!first) buf.append(", ");
164 first = false;
166 Name name = value.fst.name;
167 if (len > 1 || name != name.table.names.value) {
168 buf.append(name);
169 buf.append('=');
170 }
171 buf.append(value.snd);
172 }
173 buf.append(')');
174 }
175 return buf.toString();
176 }
178 public Attribute member(Name member) {
179 for (Pair<MethodSymbol,Attribute> pair : values)
180 if (pair.fst.name == member) return pair.snd;
181 return null;
182 }
184 public Attribute.Compound getValue() {
185 return this;
186 }
188 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
189 return v.visitAnnotation(this, p);
190 }
192 public DeclaredType getAnnotationType() {
193 return (DeclaredType) type;
194 }
196 public Map<MethodSymbol, Attribute> getElementValues() {
197 Map<MethodSymbol, Attribute> valmap =
198 new LinkedHashMap<MethodSymbol, Attribute>();
199 for (Pair<MethodSymbol, Attribute> value : values)
200 valmap.put(value.fst, value.snd);
201 return valmap;
202 }
203 }
205 /** The value for an annotation element of an array type.
206 */
207 public static class Array extends Attribute {
208 public final Attribute[] values;
209 public Array(Type type, Attribute[] values) {
210 super(type);
211 this.values = values;
212 }
214 public Array(Type type, List<Attribute> values) {
215 super(type);
216 this.values = values.toArray(new Attribute[values.size()]);
217 }
219 public void accept(Visitor v) { v.visitArray(this); }
220 public String toString() {
221 StringBuilder buf = new StringBuilder();
222 buf.append('{');
223 boolean first = true;
224 for (Attribute value : values) {
225 if (!first)
226 buf.append(", ");
227 first = false;
228 buf.append(value);
229 }
230 buf.append('}');
231 return buf.toString();
232 }
233 public List<Attribute> getValue() {
234 return List.from(values);
235 }
236 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
237 return v.visitArray(getValue(), p);
238 }
239 }
241 /** The value for an annotation element of an enum type.
242 */
243 public static class Enum extends Attribute {
244 public VarSymbol value;
245 public Enum(Type type, VarSymbol value) {
246 super(type);
247 this.value = Assert.checkNonNull(value);
248 }
249 public void accept(Visitor v) { v.visitEnum(this); }
250 public String toString() {
251 return value.enclClass() + "." + value; // qualified name
252 }
253 public VarSymbol getValue() {
254 return value;
255 }
256 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
257 return v.visitEnumConstant(value, p);
258 }
259 }
261 public static class Error extends Attribute {
262 public Error(Type type) {
263 super(type);
264 }
265 public void accept(Visitor v) { v.visitError(this); }
266 public String toString() {
267 return "<error>";
268 }
269 public String getValue() {
270 return toString();
271 }
272 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
273 return v.visitString(toString(), p);
274 }
275 }
277 /** A visitor type for dynamic dispatch on the kind of attribute value. */
278 public static interface Visitor {
279 void visitConstant(Attribute.Constant value);
280 void visitClass(Attribute.Class clazz);
281 void visitCompound(Attribute.Compound compound);
282 void visitArray(Attribute.Array array);
283 void visitEnum(Attribute.Enum e);
284 void visitError(Attribute.Error e);
285 }
287 /** A mirror of java.lang.annotation.RetentionPolicy. */
288 public static enum RetentionPolicy {
289 SOURCE,
290 CLASS,
291 RUNTIME
292 }
293 }