|
1 /* |
|
2 * Copyright 2008-2009 Sun Microsystems, Inc. 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. Sun designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 * have any questions. |
|
24 */ |
|
25 |
|
26 package com.sun.tools.javac.code; |
|
27 |
|
28 import static com.sun.tools.javac.code.TargetType.TargetAttribute.*; |
|
29 |
|
30 import java.util.EnumSet; |
|
31 import java.util.Set; |
|
32 |
|
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 API supported by Sun Microsystems. If |
|
43 * 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 { |
|
48 |
|
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 // |
|
54 |
|
55 /** For annotations on typecasts. */ |
|
56 TYPECAST(0x00), |
|
57 |
|
58 /** For annotations on a type argument or nested array of a typecast. */ |
|
59 TYPECAST_GENERIC_OR_ARRAY(0x01, HasLocation), |
|
60 |
|
61 /** For annotations on type tests. */ |
|
62 INSTANCEOF(0x02), |
|
63 |
|
64 /** For annotations on a type argument or nested array of a type test. */ |
|
65 INSTANCEOF_GENERIC_OR_ARRAY(0x03, HasLocation), |
|
66 |
|
67 /** For annotations on object creation expressions. */ |
|
68 NEW(0x04), |
|
69 |
|
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), |
|
75 |
|
76 |
|
77 /** For annotations on the method receiver. */ |
|
78 METHOD_RECEIVER(0x06), |
|
79 |
|
80 // invalid location |
|
81 //@Deprecated METHOD_RECEIVER_GENERIC_OR_ARRAY(0x07, HasLocation), |
|
82 |
|
83 /** For annotations on local variables. */ |
|
84 LOCAL_VARIABLE(0x08), |
|
85 |
|
86 /** For annotations on a type argument or nested array of a local. */ |
|
87 LOCAL_VARIABLE_GENERIC_OR_ARRAY(0x09, HasLocation), |
|
88 |
|
89 // handled by regular annotations |
|
90 //@Deprecated METHOD_RETURN(0x0A), |
|
91 |
|
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), |
|
97 |
|
98 // handled by regular annotations |
|
99 //@Deprecated METHOD_PARAMETER(0x0C), |
|
100 |
|
101 /** For annotations on a type argument or nested array of a method parameter. */ |
|
102 METHOD_PARAMETER_GENERIC_OR_ARRAY(0x0D, HasLocation), |
|
103 |
|
104 // handled by regular annotations |
|
105 //@Deprecated FIELD(0x0E), |
|
106 |
|
107 /** For annotations on a type argument or nested array of a field. */ |
|
108 FIELD_GENERIC_OR_ARRAY(0x0F, HasLocation), |
|
109 |
|
110 /** For annotations on a bound of a type parameter of a class. */ |
|
111 CLASS_TYPE_PARAMETER_BOUND(0x10, HasBound, HasParameter), |
|
112 |
|
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), |
|
118 |
|
119 /** For annotations on a bound of a type parameter of a method. */ |
|
120 METHOD_TYPE_PARAMETER_BOUND(0x12, HasBound, HasParameter), |
|
121 |
|
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), |
|
127 |
|
128 /** For annotations on the type of an "extends" or "implements" clause. */ |
|
129 CLASS_EXTENDS(0x14), |
|
130 |
|
131 /** For annotations on the inner type of an "extends" or "implements" clause. */ |
|
132 CLASS_EXTENDS_GENERIC_OR_ARRAY(0x15, HasLocation), |
|
133 |
|
134 /** For annotations on a throws clause in a method declaration. */ |
|
135 THROWS(0x16), |
|
136 |
|
137 // invalid location |
|
138 //@Deprecated THROWS_GENERIC_OR_ARRAY(0x17, HasLocation), |
|
139 |
|
140 /** For annotations in type arguments of object creation expressions. */ |
|
141 NEW_TYPE_ARGUMENT(0x18), |
|
142 NEW_TYPE_ARGUMENT_GENERIC_OR_ARRAY(0x19, HasLocation), |
|
143 |
|
144 METHOD_TYPE_ARGUMENT(0x1A), |
|
145 METHOD_TYPE_ARGUMENT_GENERIC_OR_ARRAY(0x1B, HasLocation), |
|
146 |
|
147 WILDCARD_BOUND(0x1C, HasBound), |
|
148 WILDCARD_BOUND_GENERIC_OR_ARRAY(0x1D, HasBound, HasLocation), |
|
149 |
|
150 CLASS_LITERAL(0x1E), |
|
151 CLASS_LITERAL_GENERIC_OR_ARRAY(0x1F, HasLocation), |
|
152 |
|
153 METHOD_TYPE_PARAMETER(0x20, HasParameter), |
|
154 |
|
155 // invalid location |
|
156 //@Deprecated METHOD_TYPE_PARAMETER_GENERIC_OR_ARRAY(0x21, HasLocation, HasParameter), |
|
157 |
|
158 CLASS_TYPE_PARAMETER(0x22, HasParameter), |
|
159 |
|
160 // invalid location |
|
161 //@Deprecated CLASS_TYPE_PARAMETER_GENERIC_OR_ARRAY(0x23, HasLocation, HasParameter), |
|
162 |
|
163 /** For annotations with an unknown target. */ |
|
164 UNKNOWN(-1); |
|
165 |
|
166 static final int MAXIMUM_TARGET_TYPE_VALUE = 0x22; |
|
167 |
|
168 private final int targetTypeValue; |
|
169 private Set<TargetAttribute> flags; |
|
170 |
|
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 } |
|
180 |
|
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 } |
|
191 |
|
192 public TargetType getGenericComplement() { |
|
193 if (hasLocation()) |
|
194 return this; |
|
195 else |
|
196 return fromTargetTypeValue(targetTypeValue() + 1); |
|
197 } |
|
198 |
|
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 } |
|
209 |
|
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 } |
|
220 |
|
221 public int targetTypeValue() { |
|
222 return this.targetTypeValue; |
|
223 } |
|
224 |
|
225 private static TargetType[] targets = null; |
|
226 |
|
227 private static TargetType[] buildTargets() { |
|
228 TargetType[] targets = new TargetType[MAXIMUM_TARGET_TYPE_VALUE + 1]; |
|
229 TargetType[] alltargets = values(); |
|
230 for (TargetType target : alltargets) { |
|
231 if (target.targetTypeValue >= 0) |
|
232 targets[target.targetTypeValue] = target; |
|
233 } |
|
234 for (int i = 0; i <= MAXIMUM_TARGET_TYPE_VALUE; ++i) { |
|
235 if (targets[i] == null) |
|
236 targets[i] = UNKNOWN; |
|
237 } |
|
238 return targets; |
|
239 } |
|
240 |
|
241 public static boolean isValidTargetTypeValue(int tag) { |
|
242 if (targets == null) |
|
243 targets = buildTargets(); |
|
244 |
|
245 if (((byte)tag) == ((byte)UNKNOWN.targetTypeValue)) |
|
246 return true; |
|
247 |
|
248 return (tag >= 0 && tag < targets.length); |
|
249 } |
|
250 |
|
251 public static TargetType fromTargetTypeValue(int tag) { |
|
252 if (targets == null) |
|
253 targets = buildTargets(); |
|
254 |
|
255 if (((byte)tag) == ((byte)UNKNOWN.targetTypeValue)) |
|
256 return UNKNOWN; |
|
257 |
|
258 if (tag < 0 || tag >= targets.length) |
|
259 throw new IllegalArgumentException("Unknown TargetType: " + tag); |
|
260 return targets[tag]; |
|
261 } |
|
262 |
|
263 static enum TargetAttribute { |
|
264 HasLocation, HasParameter, HasBound; |
|
265 } |
|
266 } |