Thu, 10 Oct 2013 20:12:08 -0400
8008762: Type annotation on inner class in anonymous class show up as regular type annotations
8015257: type annotation with TYPE_USE and FIELD attributed differently if repeated.
8013409: test failures for type annotations
Summary: Fixes to address some problems in type annotations
Reviewed-by: jfranck, jjg
1 /*
2 * Copyright (c) 1999, 2013, 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.Collections;
29 import java.util.EnumSet;
30 import java.util.Map;
31 import java.util.Set;
33 import javax.lang.model.element.Modifier;
35 import com.sun.tools.javac.util.Assert;
37 /** Access flags and other modifiers for Java classes and members.
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 class Flags {
46 private Flags() {} // uninstantiable
48 public static String toString(long flags) {
49 StringBuilder buf = new StringBuilder();
50 String sep = "";
51 for (Flag flag : asFlagSet(flags)) {
52 buf.append(sep);
53 buf.append(flag);
54 sep = " ";
55 }
56 return buf.toString();
57 }
59 public static EnumSet<Flag> asFlagSet(long flags) {
60 EnumSet<Flag> flagSet = EnumSet.noneOf(Flag.class);
61 for (Flag flag : Flag.values()) {
62 if ((flags & flag.value) != 0) {
63 flagSet.add(flag);
64 flags &= ~flag.value;
65 }
66 }
67 Assert.check(flags == 0, "Flags parameter contains unknown flags " + flags);
68 return flagSet;
69 }
71 /* Standard Java flags.
72 */
73 public static final int PUBLIC = 1;
74 public static final int PRIVATE = 1<<1;
75 public static final int PROTECTED = 1<<2;
76 public static final int STATIC = 1<<3;
77 public static final int FINAL = 1<<4;
78 public static final int SYNCHRONIZED = 1<<5;
79 public static final int VOLATILE = 1<<6;
80 public static final int TRANSIENT = 1<<7;
81 public static final int NATIVE = 1<<8;
82 public static final int INTERFACE = 1<<9;
83 public static final int ABSTRACT = 1<<10;
84 public static final int STRICTFP = 1<<11;
86 /* Flag that marks a symbol synthetic, added in classfile v49.0. */
87 public static final int SYNTHETIC = 1<<12;
89 /** Flag that marks attribute interfaces, added in classfile v49.0. */
90 public static final int ANNOTATION = 1<<13;
92 /** An enumeration type or an enumeration constant, added in
93 * classfile v49.0. */
94 public static final int ENUM = 1<<14;
96 /** Added in SE8, represents constructs implicitly declared in source. */
97 public static final int MANDATED = 1<<15;
99 public static final int StandardFlags = 0x0fff;
101 // Because the following access flags are overloaded with other
102 // bit positions, we translate them when reading and writing class
103 // files into unique bits positions: ACC_SYNTHETIC <-> SYNTHETIC,
104 // for example.
105 public static final int ACC_SUPER = 0x0020;
106 public static final int ACC_BRIDGE = 0x0040;
107 public static final int ACC_VARARGS = 0x0080;
109 /*****************************************
110 * Internal compiler flags (no bits in the lower 16).
111 *****************************************/
113 /** Flag is set if symbol is deprecated.
114 */
115 public static final int DEPRECATED = 1<<17;
117 /** Flag is set for a variable symbol if the variable's definition
118 * has an initializer part.
119 */
120 public static final int HASINIT = 1<<18;
122 /** Flag is set for compiler-generated anonymous method symbols
123 * that `own' an initializer block.
124 */
125 public static final int BLOCK = 1<<20;
127 /** Flag is set for compiler-generated abstract methods that implement
128 * an interface method (Miranda methods).
129 */
130 public static final int IPROXY = 1<<21;
132 /** Flag is set for nested classes that do not access instance members
133 * or `this' of an outer class and therefore don't need to be passed
134 * a this$n reference. This value is currently set only for anonymous
135 * classes in superclass constructor calls and only for pre 1.4 targets.
136 * todo: use this value for optimizing away this$n parameters in
137 * other cases.
138 */
139 public static final int NOOUTERTHIS = 1<<22;
141 /** Flag is set for package symbols if a package has a member or
142 * directory and therefore exists.
143 */
144 public static final int EXISTS = 1<<23;
146 /** Flag is set for compiler-generated compound classes
147 * representing multiple variable bounds
148 */
149 public static final int COMPOUND = 1<<24;
151 /** Flag is set for class symbols if a class file was found for this class.
152 */
153 public static final int CLASS_SEEN = 1<<25;
155 /** Flag is set for class symbols if a source file was found for this
156 * class.
157 */
158 public static final int SOURCE_SEEN = 1<<26;
160 /* State flags (are reset during compilation).
161 */
163 /** Flag for class symbols is set and later re-set as a lock in
164 * Enter to detect cycles in the superclass/superinterface
165 * relations. Similarly for constructor call cycle detection in
166 * Attr.
167 */
168 public static final int LOCKED = 1<<27;
170 /** Flag for class symbols is set and later re-set to indicate that a class
171 * has been entered but has not yet been attributed.
172 */
173 public static final int UNATTRIBUTED = 1<<28;
175 /** Flag for synthesized default constructors of anonymous classes.
176 */
177 public static final int ANONCONSTR = 1<<29;
179 /** Flag for class symbols to indicate it has been checked and found
180 * acyclic.
181 */
182 public static final int ACYCLIC = 1<<30;
184 /** Flag that marks bridge methods.
185 */
186 public static final long BRIDGE = 1L<<31;
188 /** Flag that marks formal parameters.
189 */
190 public static final long PARAMETER = 1L<<33;
192 /** Flag that marks varargs methods.
193 */
194 public static final long VARARGS = 1L<<34;
196 /** Flag for annotation type symbols to indicate it has been
197 * checked and found acyclic.
198 */
199 public static final long ACYCLIC_ANN = 1L<<35;
201 /** Flag that marks a generated default constructor.
202 */
203 public static final long GENERATEDCONSTR = 1L<<36;
205 /** Flag that marks a hypothetical method that need not really be
206 * generated in the binary, but is present in the symbol table to
207 * simplify checking for erasure clashes - also used for 292 poly sig methods.
208 */
209 public static final long HYPOTHETICAL = 1L<<37;
211 /**
212 * Flag that marks an internal proprietary class.
213 */
214 public static final long PROPRIETARY = 1L<<38;
216 /**
217 * Flag that marks a multi-catch parameter.
218 */
219 public static final long UNION = 1L<<39;
221 /**
222 * Flag that marks a special kind of bridge method (the ones that
223 * come from restricted supertype bounds).
224 */
225 public static final long OVERRIDE_BRIDGE = 1L<<40;
227 /**
228 * Flag that marks an 'effectively final' local variable.
229 */
230 public static final long EFFECTIVELY_FINAL = 1L<<41;
232 /**
233 * Flag that marks non-override equivalent methods with the same signature.
234 */
235 public static final long CLASH = 1L<<42;
237 /**
238 * Flag that marks either a default method or an interface containing default methods.
239 */
240 public static final long DEFAULT = 1L<<43;
242 /**
243 * Flag that marks class as auxiliary, ie a non-public class following
244 * the public class in a source file, that could block implicit compilation.
245 */
246 public static final long AUXILIARY = 1L<<44;
248 /**
249 * Flag that marks that a symbol is not available in the current profile
250 */
251 public static final long NOT_IN_PROFILE = 1L<<45;
253 /**
254 * Flag that indicates that an override error has been detected by Check.
255 */
256 public static final long BAD_OVERRIDE = 1L<<45;
258 /**
259 * Flag that indicates a signature polymorphic method (292).
260 */
261 public static final long SIGNATURE_POLYMORPHIC = 1L<<46;
263 /**
264 * Flag that indicates that an inference variable is used in a 'throws' clause.
265 */
266 public static final long THROWS = 1L<<47;
268 /**
269 * Flag that marks potentially ambiguous overloads
270 */
271 public static final long POTENTIALLY_AMBIGUOUS = 1L<<48;
273 /** Modifier masks.
274 */
275 public static final int
276 AccessFlags = PUBLIC | PROTECTED | PRIVATE,
277 LocalClassFlags = FINAL | ABSTRACT | STRICTFP | ENUM | SYNTHETIC,
278 MemberClassFlags = LocalClassFlags | INTERFACE | AccessFlags,
279 ClassFlags = LocalClassFlags | INTERFACE | PUBLIC | ANNOTATION,
280 InterfaceVarFlags = FINAL | STATIC | PUBLIC,
281 VarFlags = AccessFlags | FINAL | STATIC |
282 VOLATILE | TRANSIENT | ENUM,
283 ConstructorFlags = AccessFlags,
284 InterfaceMethodFlags = ABSTRACT | PUBLIC,
285 MethodFlags = AccessFlags | ABSTRACT | STATIC | NATIVE |
286 SYNCHRONIZED | FINAL | STRICTFP;
287 public static final long
288 ExtendedStandardFlags = (long)StandardFlags | DEFAULT,
289 ModifierFlags = ((long)StandardFlags & ~INTERFACE) | DEFAULT,
290 InterfaceMethodMask = ABSTRACT | STATIC | PUBLIC | STRICTFP | DEFAULT,
291 AnnotationTypeElementMask = FINAL | ABSTRACT | PUBLIC | STRICTFP,
292 LocalVarFlags = FINAL | PARAMETER;
295 public static Set<Modifier> asModifierSet(long flags) {
296 Set<Modifier> modifiers = modifierSets.get(flags);
297 if (modifiers == null) {
298 modifiers = java.util.EnumSet.noneOf(Modifier.class);
299 if (0 != (flags & PUBLIC)) modifiers.add(Modifier.PUBLIC);
300 if (0 != (flags & PROTECTED)) modifiers.add(Modifier.PROTECTED);
301 if (0 != (flags & PRIVATE)) modifiers.add(Modifier.PRIVATE);
302 if (0 != (flags & ABSTRACT)) modifiers.add(Modifier.ABSTRACT);
303 if (0 != (flags & STATIC)) modifiers.add(Modifier.STATIC);
304 if (0 != (flags & FINAL)) modifiers.add(Modifier.FINAL);
305 if (0 != (flags & TRANSIENT)) modifiers.add(Modifier.TRANSIENT);
306 if (0 != (flags & VOLATILE)) modifiers.add(Modifier.VOLATILE);
307 if (0 != (flags & SYNCHRONIZED))
308 modifiers.add(Modifier.SYNCHRONIZED);
309 if (0 != (flags & NATIVE)) modifiers.add(Modifier.NATIVE);
310 if (0 != (flags & STRICTFP)) modifiers.add(Modifier.STRICTFP);
311 if (0 != (flags & DEFAULT)) modifiers.add(Modifier.DEFAULT);
312 modifiers = Collections.unmodifiableSet(modifiers);
313 modifierSets.put(flags, modifiers);
314 }
315 return modifiers;
316 }
318 // Cache of modifier sets.
319 private static final Map<Long, Set<Modifier>> modifierSets =
320 new java.util.concurrent.ConcurrentHashMap<Long, Set<Modifier>>(64);
322 public static boolean isStatic(Symbol symbol) {
323 return (symbol.flags() & STATIC) != 0;
324 }
326 public static boolean isEnum(Symbol symbol) {
327 return (symbol.flags() & ENUM) != 0;
328 }
330 public static boolean isConstant(Symbol.VarSymbol symbol) {
331 return symbol.getConstValue() != null;
332 }
335 public enum Flag {
336 PUBLIC(Flags.PUBLIC),
337 PRIVATE(Flags.PRIVATE),
338 PROTECTED(Flags.PROTECTED),
339 STATIC(Flags.STATIC),
340 FINAL(Flags.FINAL),
341 SYNCHRONIZED(Flags.SYNCHRONIZED),
342 VOLATILE(Flags.VOLATILE),
343 TRANSIENT(Flags.TRANSIENT),
344 NATIVE(Flags.NATIVE),
345 INTERFACE(Flags.INTERFACE),
346 ABSTRACT(Flags.ABSTRACT),
347 DEFAULT(Flags.DEFAULT),
348 STRICTFP(Flags.STRICTFP),
349 BRIDGE(Flags.BRIDGE),
350 SYNTHETIC(Flags.SYNTHETIC),
351 ANNOTATION(Flags.ANNOTATION),
352 DEPRECATED(Flags.DEPRECATED),
353 HASINIT(Flags.HASINIT),
354 BLOCK(Flags.BLOCK),
355 ENUM(Flags.ENUM),
356 MANDATED(Flags.MANDATED),
357 IPROXY(Flags.IPROXY),
358 NOOUTERTHIS(Flags.NOOUTERTHIS),
359 EXISTS(Flags.EXISTS),
360 COMPOUND(Flags.COMPOUND),
361 CLASS_SEEN(Flags.CLASS_SEEN),
362 SOURCE_SEEN(Flags.SOURCE_SEEN),
363 LOCKED(Flags.LOCKED),
364 UNATTRIBUTED(Flags.UNATTRIBUTED),
365 ANONCONSTR(Flags.ANONCONSTR),
366 ACYCLIC(Flags.ACYCLIC),
367 PARAMETER(Flags.PARAMETER),
368 VARARGS(Flags.VARARGS),
369 ACYCLIC_ANN(Flags.ACYCLIC_ANN),
370 GENERATEDCONSTR(Flags.GENERATEDCONSTR),
371 HYPOTHETICAL(Flags.HYPOTHETICAL),
372 PROPRIETARY(Flags.PROPRIETARY),
373 UNION(Flags.UNION),
374 OVERRIDE_BRIDGE(Flags.OVERRIDE_BRIDGE),
375 EFFECTIVELY_FINAL(Flags.EFFECTIVELY_FINAL),
376 CLASH(Flags.CLASH),
377 AUXILIARY(Flags.AUXILIARY),
378 NOT_IN_PROFILE(Flags.NOT_IN_PROFILE),
379 BAD_OVERRIDE(Flags.BAD_OVERRIDE),
380 SIGNATURE_POLYMORPHIC(Flags.SIGNATURE_POLYMORPHIC),
381 THROWS(Flags.THROWS);
383 Flag(long flag) {
384 this.value = flag;
385 this.lowercaseName = name().toLowerCase();
386 }
388 @Override
389 public String toString() {
390 return lowercaseName;
391 }
393 final long value;
394 final String lowercaseName;
395 }
397 }