src/share/classes/com/sun/tools/javac/code/TargetType.java

changeset 1521
71f35e4b93a5
parent 1442
fcf89720ae71
child 1563
bc456436c613
equal deleted inserted replaced
1520:5c956be64b9e 1521:71f35e4b93a5
1 /* 1 /*
2 * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved. 2 * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * 4 *
5 * This code is free software; you can redistribute it and/or modify it 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 6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this 7 * published by the Free Software Foundation. Oracle designates this
23 * questions. 23 * questions.
24 */ 24 */
25 25
26 package com.sun.tools.javac.code; 26 package com.sun.tools.javac.code;
27 27
28 import java.util.EnumSet; 28 import com.sun.tools.javac.util.Assert;
29 import java.util.Set;
30
31 import static com.sun.tools.javac.code.TargetType.TargetAttribute.*;
32 29
33 /** 30 /**
34 * Describes the type of program element an extended annotation (or extended 31 * Describes the type of program element an extended annotation (or extended
35 * compound attribute) targets. 32 * compound attribute) targets.
36 * 33 *
42 * <p><b>This is NOT part of any supported API. 39 * <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. 40 * 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 41 * This code and its internal interfaces are subject to change or
45 * deletion without notice.</b> 42 * deletion without notice.</b>
46 */ 43 */
44 // Code duplicated in com.sun.tools.classfile.TypeAnnotation.TargetType
47 public enum TargetType { 45 public enum TargetType {
46 /** For annotations on a class type parameter declaration. */
47 CLASS_TYPE_PARAMETER(0x00),
48 48
49 // 49 /** For annotations on a method type parameter declaration. */
50 // Some target types are commented out, because Java doesn't permit such 50 METHOD_TYPE_PARAMETER(0x01),
51 // targets. They are included here to confirm that their omission is
52 // intentional omission not an accidental omission.
53 //
54 51
55 /** For annotations on typecasts. */ 52 /** For annotations on the type of an "extends" or "implements" clause. */
56 TYPECAST(0x00, IsLocal), 53 CLASS_EXTENDS(0x10),
57 54
58 /** For annotations on a type argument or nested array of a typecast. */ 55 /** For annotations on a bound of a type parameter of a class. */
59 TYPECAST_GENERIC_OR_ARRAY(0x01, HasLocation, IsLocal), 56 CLASS_TYPE_PARAMETER_BOUND(0x11),
60 57
61 /** For annotations on type tests. */ 58 /** For annotations on a bound of a type parameter of a method. */
62 INSTANCEOF(0x02, IsLocal), 59 METHOD_TYPE_PARAMETER_BOUND(0x12),
63 60
64 /** For annotations on a type argument or nested array of a type test. */ 61 /** For annotations on a field. */
65 INSTANCEOF_GENERIC_OR_ARRAY(0x03, HasLocation, IsLocal), 62 FIELD(0x13),
66 63
67 /** For annotations on object creation expressions. */ 64 /** For annotations on a method return type. */
68 NEW(0x04, IsLocal), 65 METHOD_RETURN(0x14),
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, IsLocal),
75
76 66
77 /** For annotations on the method receiver. */ 67 /** For annotations on the method receiver. */
78 METHOD_RECEIVER(0x06), 68 METHOD_RECEIVER(0x15),
79 69
80 // invalid location 70 /** For annotations on a method parameter. */
81 //@Deprecated METHOD_RECEIVER_GENERIC_OR_ARRAY(0x07, HasLocation), 71 METHOD_FORMAL_PARAMETER(0x16),
82
83 /** For annotations on local variables. */
84 LOCAL_VARIABLE(0x08, IsLocal),
85
86 /** For annotations on a type argument or nested array of a local. */
87 LOCAL_VARIABLE_GENERIC_OR_ARRAY(0x09, HasLocation, IsLocal),
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 72
134 /** For annotations on a throws clause in a method declaration. */ 73 /** For annotations on a throws clause in a method declaration. */
135 THROWS(0x16), 74 THROWS(0x17),
136 75
137 // invalid location 76 /** For annotations on a local variable. */
138 //@Deprecated THROWS_GENERIC_OR_ARRAY(0x17, HasLocation), 77 LOCAL_VARIABLE(0x40, true),
139 78
140 /** For annotations in type arguments of object creation expressions. */ 79 /** For annotations on a resource variable. */
141 NEW_TYPE_ARGUMENT(0x18, IsLocal), 80 RESOURCE_VARIABLE(0x41, true),
142 NEW_TYPE_ARGUMENT_GENERIC_OR_ARRAY(0x19, HasLocation, IsLocal),
143 81
144 METHOD_TYPE_ARGUMENT(0x1A, IsLocal), 82 /** For annotations on an exception parameter. */
145 METHOD_TYPE_ARGUMENT_GENERIC_OR_ARRAY(0x1B, HasLocation, IsLocal), 83 EXCEPTION_PARAMETER(0x42, true),
146 84
147 WILDCARD_BOUND(0x1C, HasBound), 85 /** For annotations on a typecast. */
148 WILDCARD_BOUND_GENERIC_OR_ARRAY(0x1D, HasBound, HasLocation), 86 CAST(0x43, true),
149 87
150 CLASS_LITERAL(0x1E, IsLocal), 88 /** For annotations on a type test. */
151 CLASS_LITERAL_GENERIC_OR_ARRAY(0x1F, HasLocation, IsLocal), 89 INSTANCEOF(0x44, true),
152 90
153 METHOD_TYPE_PARAMETER(0x20, HasParameter), 91 /** For annotations on an object creation expression. */
92 NEW(0x45, true),
154 93
155 // invalid location 94 /** For annotations on a type argument of an object creation expression. */
156 //@Deprecated METHOD_TYPE_PARAMETER_GENERIC_OR_ARRAY(0x21, HasLocation, HasParameter), 95 CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT(0x46, true),
157 96
158 CLASS_TYPE_PARAMETER(0x22, HasParameter), 97 /** For annotations on a type argument of a method call. */
98 METHOD_INVOCATION_TYPE_ARGUMENT(0x47, true),
159 99
160 // invalid location 100 /** For annotations on a lambda parameter type. */
161 //@Deprecated CLASS_TYPE_PARAMETER_GENERIC_OR_ARRAY(0x23, HasLocation, HasParameter), 101 LAMBDA_FORMAL_PARAMETER(0x48, true),
102
103 /** For annotations on a method reference. */
104 METHOD_REFERENCE(0x49, true),
105
106 /** For annotations on a type argument of a method reference. */
107 METHOD_REFERENCE_TYPE_ARGUMENT(0x50, true),
162 108
163 /** For annotations with an unknown target. */ 109 /** For annotations with an unknown target. */
164 UNKNOWN(-1); 110 UNKNOWN(0xFF);
165 111
166 static final int MAXIMUM_TARGET_TYPE_VALUE = 0x22; 112 private static final int MAXIMUM_TARGET_TYPE_VALUE = 0x92;
167 113
168 private final int targetTypeValue; 114 private final int targetTypeValue;
169 private final Set<TargetAttribute> flags; 115 private final boolean isLocal;
170 116
171 TargetType(int targetTypeValue, TargetAttribute... attributes) { 117 private TargetType(int targetTypeValue) {
172 if (targetTypeValue < Byte.MIN_VALUE 118 this(targetTypeValue, false);
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 } 119 }
180 120
181 /** 121 private TargetType(int targetTypeValue, boolean isLocal) {
182 * Returns whether or not this TargetType represents an annotation whose 122 if (targetTypeValue < 0
183 * target is an inner type of a generic or array type. 123 || targetTypeValue > 255)
184 * 124 Assert.error("Attribute type value needs to be an unsigned byte: " + String.format("0x%02X", targetTypeValue));
185 * @return true if this TargetType represents an annotation on an inner 125 this.targetTypeValue = targetTypeValue;
186 * type, false otherwise 126 this.isLocal = isLocal;
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 } 127 }
220 128
221 /** 129 /**
222 * Returns whether or not this TargetType represents an annotation whose 130 * Returns whether or not this TargetType represents an annotation whose
223 * target is exclusively a tree in a method body 131 * target is exclusively a tree in a method body
224 * 132 *
225 * Note: wildcard bound targets could target a local tree and a class 133 * Note: wildcard bound targets could target a local tree and a class
226 * member declaration signature tree 134 * member declaration signature tree
227 */ 135 */
228 public boolean isLocal() { 136 public boolean isLocal() {
229 return flags.contains(IsLocal); 137 return isLocal;
230 } 138 }
231 139
232 public int targetTypeValue() { 140 public int targetTypeValue() {
233 return this.targetTypeValue; 141 return this.targetTypeValue;
234 } 142 }
237 145
238 static { 146 static {
239 targets = new TargetType[MAXIMUM_TARGET_TYPE_VALUE + 1]; 147 targets = new TargetType[MAXIMUM_TARGET_TYPE_VALUE + 1];
240 TargetType[] alltargets = values(); 148 TargetType[] alltargets = values();
241 for (TargetType target : alltargets) { 149 for (TargetType target : alltargets) {
242 if (target.targetTypeValue >= 0) 150 if (target.targetTypeValue != UNKNOWN.targetTypeValue)
243 targets[target.targetTypeValue] = target; 151 targets[target.targetTypeValue] = target;
244 } 152 }
245 for (int i = 0; i <= MAXIMUM_TARGET_TYPE_VALUE; ++i) { 153 for (int i = 0; i <= MAXIMUM_TARGET_TYPE_VALUE; ++i) {
246 if (targets[i] == null) 154 if (targets[i] == null)
247 targets[i] = UNKNOWN; 155 targets[i] = UNKNOWN;
248 } 156 }
249 } 157 }
250 158
251 public static boolean isValidTargetTypeValue(int tag) { 159 public static boolean isValidTargetTypeValue(int tag) {
252 if (((byte)tag) == ((byte)UNKNOWN.targetTypeValue)) 160 if (tag == UNKNOWN.targetTypeValue)
253 return true; 161 return true;
254 162
255 return (tag >= 0 && tag < targets.length); 163 return (tag >= 0 && tag < targets.length);
256 } 164 }
257 165
258 public static TargetType fromTargetTypeValue(int tag) { 166 public static TargetType fromTargetTypeValue(int tag) {
259 if (((byte)tag) == ((byte)UNKNOWN.targetTypeValue)) 167 if (tag == UNKNOWN.targetTypeValue)
260 return UNKNOWN; 168 return UNKNOWN;
261 169
262 if (tag < 0 || tag >= targets.length) 170 if (tag < 0 || tag >= targets.length)
263 throw new IllegalArgumentException("Unknown TargetType: " + tag); 171 Assert.error("Unknown TargetType: " + tag);
264 return targets[tag]; 172 return targets[tag];
265 } 173 }
266
267 static enum TargetAttribute {
268 HasLocation, HasParameter, HasBound, IsLocal;
269 }
270 } 174 }

mercurial