Mon, 17 Dec 2012 07:47:05 -0800
8004832: Add new doclint package
Reviewed-by: mcimadamore
1 /*
2 * Copyright (c) 2008, 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.EnumSet;
29 import java.util.Set;
31 import static com.sun.tools.javac.code.TargetType.TargetAttribute.*;
33 /**
34 * Describes the type of program element an extended annotation (or extended
35 * compound attribute) targets.
36 *
37 * By comparison, a Tree.Kind has enum values for all elements in the AST, and
38 * it does not provide enough resolution for type arguments (i.e., whether an
39 * annotation targets a type argument in a local variable, method return type,
40 * or a typecast).
41 *
42 * <p><b>This is NOT part of any supported API.
43 * If you write code that depends on this, you do so at your own risk.
44 * This code and its internal interfaces are subject to change or
45 * deletion without notice.</b>
46 */
47 public enum TargetType {
49 //
50 // Some target types are commented out, because Java doesn't permit such
51 // targets. They are included here to confirm that their omission is
52 // intentional omission not an accidental omission.
53 //
55 /** For annotations on typecasts. */
56 TYPECAST(0x00, IsLocal),
58 /** For annotations on a type argument or nested array of a typecast. */
59 TYPECAST_GENERIC_OR_ARRAY(0x01, HasLocation, IsLocal),
61 /** For annotations on type tests. */
62 INSTANCEOF(0x02, IsLocal),
64 /** For annotations on a type argument or nested array of a type test. */
65 INSTANCEOF_GENERIC_OR_ARRAY(0x03, HasLocation, IsLocal),
67 /** For annotations on object creation expressions. */
68 NEW(0x04, IsLocal),
70 /**
71 * For annotations on a type argument or nested array of an object creation
72 * expression.
73 */
74 NEW_GENERIC_OR_ARRAY(0x05, HasLocation, IsLocal),
77 /** For annotations on the method receiver. */
78 METHOD_RECEIVER(0x06),
80 // invalid location
81 //@Deprecated METHOD_RECEIVER_GENERIC_OR_ARRAY(0x07, HasLocation),
83 /** For annotations on local variables. */
84 LOCAL_VARIABLE(0x08, IsLocal),
86 /** For annotations on a type argument or nested array of a local. */
87 LOCAL_VARIABLE_GENERIC_OR_ARRAY(0x09, HasLocation, IsLocal),
89 // handled by regular annotations
90 //@Deprecated METHOD_RETURN(0x0A),
92 /**
93 * For annotations on a type argument or nested array of a method return
94 * type.
95 */
96 METHOD_RETURN_GENERIC_OR_ARRAY(0x0B, HasLocation),
98 // handled by regular annotations
99 //@Deprecated METHOD_PARAMETER(0x0C),
101 /** For annotations on a type argument or nested array of a method parameter. */
102 METHOD_PARAMETER_GENERIC_OR_ARRAY(0x0D, HasLocation),
104 // handled by regular annotations
105 //@Deprecated FIELD(0x0E),
107 /** For annotations on a type argument or nested array of a field. */
108 FIELD_GENERIC_OR_ARRAY(0x0F, HasLocation),
110 /** For annotations on a bound of a type parameter of a class. */
111 CLASS_TYPE_PARAMETER_BOUND(0x10, HasBound, HasParameter),
113 /**
114 * For annotations on a type argument or nested array of a bound of a type
115 * parameter of a class.
116 */
117 CLASS_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY(0x11, HasBound, HasLocation, HasParameter),
119 /** For annotations on a bound of a type parameter of a method. */
120 METHOD_TYPE_PARAMETER_BOUND(0x12, HasBound, HasParameter),
122 /**
123 * For annotations on a type argument or nested array of a bound of a type
124 * parameter of a method.
125 */
126 METHOD_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY(0x13, HasBound, HasLocation, HasParameter),
128 /** For annotations on the type of an "extends" or "implements" clause. */
129 CLASS_EXTENDS(0x14),
131 /** For annotations on the inner type of an "extends" or "implements" clause. */
132 CLASS_EXTENDS_GENERIC_OR_ARRAY(0x15, HasLocation),
134 /** For annotations on a throws clause in a method declaration. */
135 THROWS(0x16),
137 // invalid location
138 //@Deprecated THROWS_GENERIC_OR_ARRAY(0x17, HasLocation),
140 /** For annotations in type arguments of object creation expressions. */
141 NEW_TYPE_ARGUMENT(0x18, IsLocal),
142 NEW_TYPE_ARGUMENT_GENERIC_OR_ARRAY(0x19, HasLocation, IsLocal),
144 METHOD_TYPE_ARGUMENT(0x1A, IsLocal),
145 METHOD_TYPE_ARGUMENT_GENERIC_OR_ARRAY(0x1B, HasLocation, IsLocal),
147 WILDCARD_BOUND(0x1C, HasBound),
148 WILDCARD_BOUND_GENERIC_OR_ARRAY(0x1D, HasBound, HasLocation),
150 CLASS_LITERAL(0x1E, IsLocal),
151 CLASS_LITERAL_GENERIC_OR_ARRAY(0x1F, HasLocation, IsLocal),
153 METHOD_TYPE_PARAMETER(0x20, HasParameter),
155 // invalid location
156 //@Deprecated METHOD_TYPE_PARAMETER_GENERIC_OR_ARRAY(0x21, HasLocation, HasParameter),
158 CLASS_TYPE_PARAMETER(0x22, HasParameter),
160 // invalid location
161 //@Deprecated CLASS_TYPE_PARAMETER_GENERIC_OR_ARRAY(0x23, HasLocation, HasParameter),
163 /** For annotations with an unknown target. */
164 UNKNOWN(-1);
166 static final int MAXIMUM_TARGET_TYPE_VALUE = 0x22;
168 private final int targetTypeValue;
169 private final Set<TargetAttribute> flags;
171 TargetType(int targetTypeValue, TargetAttribute... attributes) {
172 if (targetTypeValue < Byte.MIN_VALUE
173 || targetTypeValue > Byte.MAX_VALUE)
174 throw new AssertionError("attribute type value needs to be a byte: " + targetTypeValue);
175 this.targetTypeValue = (byte)targetTypeValue;
176 flags = EnumSet.noneOf(TargetAttribute.class);
177 for (TargetAttribute attr : attributes)
178 flags.add(attr);
179 }
181 /**
182 * Returns whether or not this TargetType represents an annotation whose
183 * target is an inner type of a generic or array type.
184 *
185 * @return true if this TargetType represents an annotation on an inner
186 * type, false otherwise
187 */
188 public boolean hasLocation() {
189 return flags.contains(HasLocation);
190 }
192 public TargetType getGenericComplement() {
193 if (hasLocation())
194 return this;
195 else
196 return fromTargetTypeValue(targetTypeValue() + 1);
197 }
199 /**
200 * Returns whether or not this TargetType represents an annotation whose
201 * target has a parameter index.
202 *
203 * @return true if this TargetType has a parameter index,
204 * false otherwise
205 */
206 public boolean hasParameter() {
207 return flags.contains(HasParameter);
208 }
210 /**
211 * Returns whether or not this TargetType represents an annotation whose
212 * target is a type parameter bound.
213 *
214 * @return true if this TargetType represents an type parameter bound
215 * annotation, false otherwise
216 */
217 public boolean hasBound() {
218 return flags.contains(HasBound);
219 }
221 /**
222 * Returns whether or not this TargetType represents an annotation whose
223 * target is exclusively a tree in a method body
224 *
225 * Note: wildcard bound targets could target a local tree and a class
226 * member declaration signature tree
227 */
228 public boolean isLocal() {
229 return flags.contains(IsLocal);
230 }
232 public int targetTypeValue() {
233 return this.targetTypeValue;
234 }
236 private static final TargetType[] targets;
238 static {
239 targets = new TargetType[MAXIMUM_TARGET_TYPE_VALUE + 1];
240 TargetType[] alltargets = values();
241 for (TargetType target : alltargets) {
242 if (target.targetTypeValue >= 0)
243 targets[target.targetTypeValue] = target;
244 }
245 for (int i = 0; i <= MAXIMUM_TARGET_TYPE_VALUE; ++i) {
246 if (targets[i] == null)
247 targets[i] = UNKNOWN;
248 }
249 }
251 public static boolean isValidTargetTypeValue(int tag) {
252 if (((byte)tag) == ((byte)UNKNOWN.targetTypeValue))
253 return true;
255 return (tag >= 0 && tag < targets.length);
256 }
258 public static TargetType fromTargetTypeValue(int tag) {
259 if (((byte)tag) == ((byte)UNKNOWN.targetTypeValue))
260 return UNKNOWN;
262 if (tag < 0 || tag >= targets.length)
263 throw new IllegalArgumentException("Unknown TargetType: " + tag);
264 return targets[tag];
265 }
267 static enum TargetAttribute {
268 HasLocation, HasParameter, HasBound, IsLocal;
269 }
270 }