Flags.java revision 2571:10fc81ac75b4
1251652Sgjb/*
2251652Sgjb * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
3251652Sgjb * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4251652Sgjb *
5251652Sgjb * This code is free software; you can redistribute it and/or modify it
6251652Sgjb * under the terms of the GNU General Public License version 2 only, as
7251652Sgjb * published by the Free Software Foundation.  Oracle designates this
8251652Sgjb * particular file as subject to the "Classpath" exception as provided
9251652Sgjb * by Oracle in the LICENSE file that accompanied this code.
10251652Sgjb *
11251652Sgjb * This code is distributed in the hope that it will be useful, but WITHOUT
12251652Sgjb * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13251652Sgjb * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14251652Sgjb * version 2 for more details (a copy is included in the LICENSE file that
15251652Sgjb * accompanied this code).
16251652Sgjb *
17251652Sgjb * You should have received a copy of the GNU General Public License version
18251652Sgjb * 2 along with this work; if not, write to the Free Software Foundation,
19251652Sgjb * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20251652Sgjb *
21251652Sgjb * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22251652Sgjb * or visit www.oracle.com if you need additional information or have any
23251652Sgjb * questions.
24251652Sgjb */
25251652Sgjb
26251652Sgjbpackage com.sun.tools.javac.code;
27251652Sgjb
28251652Sgjbimport java.util.Collections;
29251652Sgjbimport java.util.EnumSet;
30251652Sgjbimport java.util.Map;
31251652Sgjbimport java.util.Set;
32251652Sgjbimport java.util.concurrent.ConcurrentHashMap;
33251652Sgjb
34251652Sgjbimport javax.lang.model.element.Modifier;
35251652Sgjb
36251652Sgjbimport com.sun.tools.javac.util.Assert;
37251652Sgjbimport com.sun.tools.javac.util.StringUtils;
38251652Sgjb
39251652Sgjb/** Access flags and other modifiers for Java classes and members.
40251652Sgjb *
41251652Sgjb *  <p><b>This is NOT part of any supported API.
42251652Sgjb *  If you write code that depends on this, you do so at your own risk.
43251652Sgjb *  This code and its internal interfaces are subject to change or
44254293Sgjb *  deletion without notice.</b>
45254293Sgjb */
46254293Sgjbpublic class Flags {
47251652Sgjb
48252846Sgjb    private Flags() {} // uninstantiable
49252846Sgjb
50252846Sgjb    public static String toString(long flags) {
51252846Sgjb        StringBuilder buf = new StringBuilder();
52251652Sgjb        String sep = "";
53251652Sgjb        for (Flag flag : asFlagSet(flags)) {
54251652Sgjb            buf.append(sep);
55251652Sgjb            buf.append(flag);
56251652Sgjb            sep = " ";
57251652Sgjb        }
58251652Sgjb        return buf.toString();
59251652Sgjb    }
60254293Sgjb
61254293Sgjb    public static EnumSet<Flag> asFlagSet(long flags) {
62254293Sgjb        EnumSet<Flag> flagSet = EnumSet.noneOf(Flag.class);
63254293Sgjb        for (Flag flag : Flag.values()) {
64254293Sgjb            if ((flags & flag.value) != 0) {
65251652Sgjb                flagSet.add(flag);
66251652Sgjb                flags &= ~flag.value;
67251652Sgjb            }
68251652Sgjb        }
69251652Sgjb        Assert.check(flags == 0);
70251652Sgjb        return flagSet;
71251652Sgjb    }
72251652Sgjb
73251652Sgjb    /* Standard Java flags.
74251652Sgjb     */
75251652Sgjb    public static final int PUBLIC       = 1;
76251652Sgjb    public static final int PRIVATE      = 1<<1;
77251652Sgjb    public static final int PROTECTED    = 1<<2;
78251652Sgjb    public static final int STATIC       = 1<<3;
79251652Sgjb    public static final int FINAL        = 1<<4;
80251652Sgjb    public static final int SYNCHRONIZED = 1<<5;
81251652Sgjb    public static final int VOLATILE     = 1<<6;
82251652Sgjb    public static final int TRANSIENT    = 1<<7;
83251652Sgjb    public static final int NATIVE       = 1<<8;
84251652Sgjb    public static final int INTERFACE    = 1<<9;
85251652Sgjb    public static final int ABSTRACT     = 1<<10;
86251652Sgjb    public static final int STRICTFP     = 1<<11;
87251652Sgjb
88251652Sgjb    /* Flag that marks a symbol synthetic, added in classfile v49.0. */
89251652Sgjb    public static final int SYNTHETIC    = 1<<12;
90251652Sgjb
91251652Sgjb    /** Flag that marks attribute interfaces, added in classfile v49.0. */
92251652Sgjb    public static final int ANNOTATION   = 1<<13;
93251652Sgjb
94251652Sgjb    /** An enumeration type or an enumeration constant, added in
95251652Sgjb     *  classfile v49.0. */
96251652Sgjb    public static final int ENUM         = 1<<14;
97251652Sgjb
98252846Sgjb    /** Added in SE8, represents constructs implicitly declared in source. */
99252846Sgjb    public static final int MANDATED     = 1<<15;
100252846Sgjb
101252846Sgjb    public static final int StandardFlags = 0x0fff;
102252846Sgjb
103252846Sgjb    // Because the following access flags are overloaded with other
104252846Sgjb    // bit positions, we translate them when reading and writing class
105252846Sgjb    // files into unique bits positions: ACC_SYNTHETIC <-> SYNTHETIC,
106252846Sgjb    // for example.
107252846Sgjb    public static final int ACC_SUPER    = 0x0020;
108252846Sgjb    public static final int ACC_BRIDGE   = 0x0040;
109252846Sgjb    public static final int ACC_VARARGS  = 0x0080;
110252846Sgjb
111252846Sgjb    /*****************************************
112252846Sgjb     * Internal compiler flags (no bits in the lower 16).
113252846Sgjb     *****************************************/
114252846Sgjb
115252846Sgjb    /** Flag is set if symbol is deprecated.
116252846Sgjb     */
117251652Sgjb    public static final int DEPRECATED   = 1<<17;
118251652Sgjb
119251652Sgjb    /** Flag is set for a variable symbol if the variable's definition
120251652Sgjb     *  has an initializer part.
121254293Sgjb     */
122254293Sgjb    public static final int HASINIT          = 1<<18;
123254293Sgjb
124254293Sgjb    /** Flag is set for compiler-generated anonymous method symbols
125254293Sgjb     *  that `own' an initializer block.
126251652Sgjb     */
127251652Sgjb    public static final int BLOCK            = 1<<20;
128251652Sgjb
129251652Sgjb    /** Flag is set for compiler-generated abstract methods that implement
130252846Sgjb     *  an interface method (Miranda methods).
131252846Sgjb     */
132252846Sgjb    public static final int IPROXY           = 1<<21;
133251652Sgjb
134252846Sgjb    /** Flag is set for nested classes that do not access instance members
135252846Sgjb     *  or `this' of an outer class and therefore don't need to be passed
136252846Sgjb     *  a this$n reference.  This value is currently set only for anonymous
137252846Sgjb     *  classes in superclass constructor calls and only for pre 1.4 targets.
138251652Sgjb     *  todo: use this value for optimizing away this$n parameters in
139251652Sgjb     *  other cases.
140251652Sgjb     */
141251652Sgjb    public static final int NOOUTERTHIS  = 1<<22;
142251652Sgjb
143251652Sgjb    /** Flag is set for package symbols if a package has a member or
144251652Sgjb     *  directory and therefore exists.
145251652Sgjb     */
146251652Sgjb    public static final int EXISTS           = 1<<23;
147251652Sgjb
148251652Sgjb    /** Flag is set for compiler-generated compound classes
149251652Sgjb     *  representing multiple variable bounds
150251652Sgjb     */
151251652Sgjb    public static final int COMPOUND     = 1<<24;
152251652Sgjb
153251652Sgjb    /** Flag is set for class symbols if a class file was found for this class.
154254293Sgjb     */
155251652Sgjb    public static final int CLASS_SEEN   = 1<<25;
156254293Sgjb
157251652Sgjb    /** Flag is set for class symbols if a source file was found for this
158251652Sgjb     *  class.
159254293Sgjb     */
160251652Sgjb    public static final int SOURCE_SEEN  = 1<<26;
161251652Sgjb
162251652Sgjb    /* State flags (are reset during compilation).
163251652Sgjb     */
164251652Sgjb
165251652Sgjb    /** Flag for class symbols is set and later re-set as a lock in
166251652Sgjb     *  Enter to detect cycles in the superclass/superinterface
167251652Sgjb     *  relations.  Similarly for constructor call cycle detection in
168251652Sgjb     *  Attr.
169251652Sgjb     */
170257776Sgjb    public static final int LOCKED           = 1<<27;
171257776Sgjb
172257776Sgjb    /** Flag for class symbols is set and later re-set to indicate that a class
173257776Sgjb     *  has been entered but has not yet been attributed.
174251652Sgjb     */
175251652Sgjb    public static final int UNATTRIBUTED = 1<<28;
176252846Sgjb
177258261Sgjb    /** Flag for synthesized default constructors of anonymous classes.
178258261Sgjb     */
179251652Sgjb    public static final int ANONCONSTR   = 1<<29;
180252846Sgjb
181251652Sgjb    /** Flag for class symbols to indicate it has been checked and found
182251652Sgjb     *  acyclic.
183251652Sgjb     */
184252101Sgjb    public static final int ACYCLIC          = 1<<30;
185252101Sgjb
186252101Sgjb    /** Flag that marks bridge methods.
187252101Sgjb     */
188252101Sgjb    public static final long BRIDGE          = 1L<<31;
189252101Sgjb
190252101Sgjb    /** Flag that marks formal parameters.
191252101Sgjb     */
192252101Sgjb    public static final long PARAMETER   = 1L<<33;
193252101Sgjb
194252101Sgjb    /** Flag that marks varargs methods.
195251652Sgjb     */
196251652Sgjb    public static final long VARARGS   = 1L<<34;
197251652Sgjb
198251652Sgjb    /** Flag for annotation type symbols to indicate it has been
199251652Sgjb     *  checked and found acyclic.
200251652Sgjb     */
201251652Sgjb    public static final long ACYCLIC_ANN      = 1L<<35;
202251652Sgjb
203251652Sgjb    /** Flag that marks a generated default constructor.
204252846Sgjb     */
205252846Sgjb    public static final long GENERATEDCONSTR   = 1L<<36;
206252846Sgjb
207251652Sgjb    /** Flag that marks a hypothetical method that need not really be
208252846Sgjb     *  generated in the binary, but is present in the symbol table to
209251652Sgjb     *  simplify checking for erasure clashes - also used for 292 poly sig methods.
210     */
211    public static final long HYPOTHETICAL   = 1L<<37;
212
213    /**
214     * Flag that marks an internal proprietary class.
215     */
216    public static final long PROPRIETARY = 1L<<38;
217
218    /**
219     * Flag that marks a multi-catch parameter.
220     */
221    public static final long UNION = 1L<<39;
222
223    /**
224     * Flag that marks a special kind of bridge method (the ones that
225     * come from restricted supertype bounds).
226     */
227    public static final long OVERRIDE_BRIDGE = 1L<<40;
228
229    /**
230     * Flag that marks an 'effectively final' local variable.
231     */
232    public static final long EFFECTIVELY_FINAL = 1L<<41;
233
234    /**
235     * Flag that marks non-override equivalent methods with the same signature.
236     */
237    public static final long CLASH = 1L<<42;
238
239    /**
240     * Flag that marks either a default method or an interface containing default methods.
241     */
242    public static final long DEFAULT = 1L<<43;
243
244    /**
245     * Flag that marks class as auxiliary, ie a non-public class following
246     * the public class in a source file, that could block implicit compilation.
247     */
248    public static final long AUXILIARY = 1L<<44;
249
250    /**
251     * Flag that marks that a symbol is not available in the current profile
252     */
253    public static final long NOT_IN_PROFILE = 1L<<45;
254
255    /**
256     * Flag that indicates that an override error has been detected by Check.
257     */
258    public static final long BAD_OVERRIDE = 1L<<45;
259
260    /**
261     * Flag that indicates a signature polymorphic method (292).
262     */
263    public static final long SIGNATURE_POLYMORPHIC = 1L<<46;
264
265    /**
266     * Flag that indicates that an inference variable is used in a 'throws' clause.
267     */
268    public static final long THROWS = 1L<<47;
269
270    /**
271     * Flag that marks potentially ambiguous overloads
272     */
273    public static final long POTENTIALLY_AMBIGUOUS = 1L<<48;
274
275    /**
276     * Flag that marks a synthetic method body for a lambda expression
277     */
278    public static final long LAMBDA_METHOD = 1L<<49;
279
280    /**
281     * Flag to control recursion in TransTypes
282     */
283    public static final long TYPE_TRANSLATED = 1L<<50;
284
285    /** Modifier masks.
286     */
287    public static final int
288        AccessFlags           = PUBLIC | PROTECTED | PRIVATE,
289        LocalClassFlags       = FINAL | ABSTRACT | STRICTFP | ENUM | SYNTHETIC,
290        MemberClassFlags      = LocalClassFlags | INTERFACE | AccessFlags,
291        ClassFlags            = LocalClassFlags | INTERFACE | PUBLIC | ANNOTATION,
292        InterfaceVarFlags     = FINAL | STATIC | PUBLIC,
293        VarFlags              = AccessFlags | FINAL | STATIC |
294                                VOLATILE | TRANSIENT | ENUM,
295        ConstructorFlags      = AccessFlags,
296        InterfaceMethodFlags  = ABSTRACT | PUBLIC,
297        MethodFlags           = AccessFlags | ABSTRACT | STATIC | NATIVE |
298                                SYNCHRONIZED | FINAL | STRICTFP;
299    public static final long
300        ExtendedStandardFlags       = (long)StandardFlags | DEFAULT,
301        ModifierFlags               = ((long)StandardFlags & ~INTERFACE) | DEFAULT,
302        InterfaceMethodMask         = ABSTRACT | STATIC | PUBLIC | STRICTFP | DEFAULT,
303        AnnotationTypeElementMask   = ABSTRACT | PUBLIC,
304        LocalVarFlags               = FINAL | PARAMETER,
305        ReceiverParamFlags          = PARAMETER;
306
307
308    public static Set<Modifier> asModifierSet(long flags) {
309        Set<Modifier> modifiers = modifierSets.get(flags);
310        if (modifiers == null) {
311            modifiers = java.util.EnumSet.noneOf(Modifier.class);
312            if (0 != (flags & PUBLIC))    modifiers.add(Modifier.PUBLIC);
313            if (0 != (flags & PROTECTED)) modifiers.add(Modifier.PROTECTED);
314            if (0 != (flags & PRIVATE))   modifiers.add(Modifier.PRIVATE);
315            if (0 != (flags & ABSTRACT))  modifiers.add(Modifier.ABSTRACT);
316            if (0 != (flags & STATIC))    modifiers.add(Modifier.STATIC);
317            if (0 != (flags & FINAL))     modifiers.add(Modifier.FINAL);
318            if (0 != (flags & TRANSIENT)) modifiers.add(Modifier.TRANSIENT);
319            if (0 != (flags & VOLATILE))  modifiers.add(Modifier.VOLATILE);
320            if (0 != (flags & SYNCHRONIZED))
321                                          modifiers.add(Modifier.SYNCHRONIZED);
322            if (0 != (flags & NATIVE))    modifiers.add(Modifier.NATIVE);
323            if (0 != (flags & STRICTFP))  modifiers.add(Modifier.STRICTFP);
324            if (0 != (flags & DEFAULT))   modifiers.add(Modifier.DEFAULT);
325            modifiers = Collections.unmodifiableSet(modifiers);
326            modifierSets.put(flags, modifiers);
327        }
328        return modifiers;
329    }
330
331    // Cache of modifier sets.
332    private static final Map<Long, Set<Modifier>> modifierSets = new ConcurrentHashMap<>(64);
333
334    public static boolean isStatic(Symbol symbol) {
335        return (symbol.flags() & STATIC) != 0;
336    }
337
338    public static boolean isEnum(Symbol symbol) {
339        return (symbol.flags() & ENUM) != 0;
340    }
341
342    public static boolean isConstant(Symbol.VarSymbol symbol) {
343        return symbol.getConstValue() != null;
344    }
345
346
347    public enum Flag {
348        PUBLIC(Flags.PUBLIC),
349        PRIVATE(Flags.PRIVATE),
350        PROTECTED(Flags.PROTECTED),
351        STATIC(Flags.STATIC),
352        FINAL(Flags.FINAL),
353        SYNCHRONIZED(Flags.SYNCHRONIZED),
354        VOLATILE(Flags.VOLATILE),
355        TRANSIENT(Flags.TRANSIENT),
356        NATIVE(Flags.NATIVE),
357        INTERFACE(Flags.INTERFACE),
358        ABSTRACT(Flags.ABSTRACT),
359        DEFAULT(Flags.DEFAULT),
360        STRICTFP(Flags.STRICTFP),
361        BRIDGE(Flags.BRIDGE),
362        SYNTHETIC(Flags.SYNTHETIC),
363        ANNOTATION(Flags.ANNOTATION),
364        DEPRECATED(Flags.DEPRECATED),
365        HASINIT(Flags.HASINIT),
366        BLOCK(Flags.BLOCK),
367        ENUM(Flags.ENUM),
368        MANDATED(Flags.MANDATED),
369        IPROXY(Flags.IPROXY),
370        NOOUTERTHIS(Flags.NOOUTERTHIS),
371        EXISTS(Flags.EXISTS),
372        COMPOUND(Flags.COMPOUND),
373        CLASS_SEEN(Flags.CLASS_SEEN),
374        SOURCE_SEEN(Flags.SOURCE_SEEN),
375        LOCKED(Flags.LOCKED),
376        UNATTRIBUTED(Flags.UNATTRIBUTED),
377        ANONCONSTR(Flags.ANONCONSTR),
378        ACYCLIC(Flags.ACYCLIC),
379        PARAMETER(Flags.PARAMETER),
380        VARARGS(Flags.VARARGS),
381        ACYCLIC_ANN(Flags.ACYCLIC_ANN),
382        GENERATEDCONSTR(Flags.GENERATEDCONSTR),
383        HYPOTHETICAL(Flags.HYPOTHETICAL),
384        PROPRIETARY(Flags.PROPRIETARY),
385        UNION(Flags.UNION),
386        OVERRIDE_BRIDGE(Flags.OVERRIDE_BRIDGE),
387        EFFECTIVELY_FINAL(Flags.EFFECTIVELY_FINAL),
388        CLASH(Flags.CLASH),
389        AUXILIARY(Flags.AUXILIARY),
390        NOT_IN_PROFILE(Flags.NOT_IN_PROFILE),
391        BAD_OVERRIDE(Flags.BAD_OVERRIDE),
392        SIGNATURE_POLYMORPHIC(Flags.SIGNATURE_POLYMORPHIC),
393        THROWS(Flags.THROWS),
394        LAMBDA_METHOD(Flags.LAMBDA_METHOD),
395        TYPE_TRANSLATED(Flags.TYPE_TRANSLATED);
396
397        Flag(long flag) {
398            this.value = flag;
399            this.lowercaseName = StringUtils.toLowerCase(name());
400        }
401
402        @Override
403        public String toString() {
404            return lowercaseName;
405        }
406
407        final long value;
408        final String lowercaseName;
409    }
410
411}
412