aoqi@0: /*
aoqi@0: * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0: *
aoqi@0: * This code is free software; you can redistribute it and/or modify it
aoqi@0: * under the terms of the GNU General Public License version 2 only, as
aoqi@0: * published by the Free Software Foundation. Oracle designates this
aoqi@0: * particular file as subject to the "Classpath" exception as provided
aoqi@0: * by Oracle in the LICENSE file that accompanied this code.
aoqi@0: *
aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0: * accompanied this code).
aoqi@0: *
aoqi@0: * You should have received a copy of the GNU General Public License version
aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0: *
aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0: * or visit www.oracle.com if you need additional information or have any
aoqi@0: * questions.
aoqi@0: */
aoqi@0:
aoqi@0: package com.sun.tools.javac.code;
aoqi@0:
aoqi@0: import java.util.Collections;
aoqi@0: import java.util.EnumSet;
aoqi@0: import java.util.Map;
aoqi@0: import java.util.Set;
aoqi@0:
aoqi@0: import javax.lang.model.element.Modifier;
aoqi@0:
aoqi@0: import com.sun.tools.javac.util.Assert;
aoqi@0: import com.sun.tools.javac.util.StringUtils;
aoqi@0:
aoqi@0: /** Access flags and other modifiers for Java classes and members.
aoqi@0: *
aoqi@0: *
This is NOT part of any supported API.
aoqi@0: * If you write code that depends on this, you do so at your own risk.
aoqi@0: * This code and its internal interfaces are subject to change or
aoqi@0: * deletion without notice.
aoqi@0: */
aoqi@0: public class Flags {
aoqi@0:
aoqi@0: private Flags() {} // uninstantiable
aoqi@0:
aoqi@0: public static String toString(long flags) {
aoqi@0: StringBuilder buf = new StringBuilder();
aoqi@0: String sep = "";
aoqi@0: for (Flag flag : asFlagSet(flags)) {
aoqi@0: buf.append(sep);
aoqi@0: buf.append(flag);
aoqi@0: sep = " ";
aoqi@0: }
aoqi@0: return buf.toString();
aoqi@0: }
aoqi@0:
aoqi@0: public static EnumSet asFlagSet(long flags) {
aoqi@0: EnumSet flagSet = EnumSet.noneOf(Flag.class);
aoqi@0: for (Flag flag : Flag.values()) {
aoqi@0: if ((flags & flag.value) != 0) {
aoqi@0: flagSet.add(flag);
aoqi@0: flags &= ~flag.value;
aoqi@0: }
aoqi@0: }
aoqi@0: Assert.check(flags == 0, "Flags parameter contains unknown flags " + flags);
aoqi@0: return flagSet;
aoqi@0: }
aoqi@0:
aoqi@0: /* Standard Java flags.
aoqi@0: */
aoqi@0: public static final int PUBLIC = 1;
aoqi@0: public static final int PRIVATE = 1<<1;
aoqi@0: public static final int PROTECTED = 1<<2;
aoqi@0: public static final int STATIC = 1<<3;
aoqi@0: public static final int FINAL = 1<<4;
aoqi@0: public static final int SYNCHRONIZED = 1<<5;
aoqi@0: public static final int VOLATILE = 1<<6;
aoqi@0: public static final int TRANSIENT = 1<<7;
aoqi@0: public static final int NATIVE = 1<<8;
aoqi@0: public static final int INTERFACE = 1<<9;
aoqi@0: public static final int ABSTRACT = 1<<10;
aoqi@0: public static final int STRICTFP = 1<<11;
aoqi@0:
aoqi@0: /* Flag that marks a symbol synthetic, added in classfile v49.0. */
aoqi@0: public static final int SYNTHETIC = 1<<12;
aoqi@0:
aoqi@0: /** Flag that marks attribute interfaces, added in classfile v49.0. */
aoqi@0: public static final int ANNOTATION = 1<<13;
aoqi@0:
aoqi@0: /** An enumeration type or an enumeration constant, added in
aoqi@0: * classfile v49.0. */
aoqi@0: public static final int ENUM = 1<<14;
aoqi@0:
aoqi@0: /** Added in SE8, represents constructs implicitly declared in source. */
aoqi@0: public static final int MANDATED = 1<<15;
aoqi@0:
aoqi@0: public static final int StandardFlags = 0x0fff;
aoqi@0:
aoqi@0: // Because the following access flags are overloaded with other
aoqi@0: // bit positions, we translate them when reading and writing class
aoqi@0: // files into unique bits positions: ACC_SYNTHETIC <-> SYNTHETIC,
aoqi@0: // for example.
aoqi@0: public static final int ACC_SUPER = 0x0020;
aoqi@0: public static final int ACC_BRIDGE = 0x0040;
aoqi@0: public static final int ACC_VARARGS = 0x0080;
aoqi@0:
aoqi@0: /*****************************************
aoqi@0: * Internal compiler flags (no bits in the lower 16).
aoqi@0: *****************************************/
aoqi@0:
aoqi@0: /** Flag is set if symbol is deprecated.
aoqi@0: */
aoqi@0: public static final int DEPRECATED = 1<<17;
aoqi@0:
aoqi@0: /** Flag is set for a variable symbol if the variable's definition
aoqi@0: * has an initializer part.
aoqi@0: */
aoqi@0: public static final int HASINIT = 1<<18;
aoqi@0:
aoqi@0: /** Flag is set for compiler-generated anonymous method symbols
aoqi@0: * that `own' an initializer block.
aoqi@0: */
aoqi@0: public static final int BLOCK = 1<<20;
aoqi@0:
aoqi@0: /** Flag is set for compiler-generated abstract methods that implement
aoqi@0: * an interface method (Miranda methods).
aoqi@0: */
aoqi@0: public static final int IPROXY = 1<<21;
aoqi@0:
aoqi@0: /** Flag is set for nested classes that do not access instance members
aoqi@0: * or `this' of an outer class and therefore don't need to be passed
aoqi@0: * a this$n reference. This value is currently set only for anonymous
aoqi@0: * classes in superclass constructor calls and only for pre 1.4 targets.
aoqi@0: * todo: use this value for optimizing away this$n parameters in
aoqi@0: * other cases.
aoqi@0: */
aoqi@0: public static final int NOOUTERTHIS = 1<<22;
aoqi@0:
aoqi@0: /** Flag is set for package symbols if a package has a member or
aoqi@0: * directory and therefore exists.
aoqi@0: */
aoqi@0: public static final int EXISTS = 1<<23;
aoqi@0:
aoqi@0: /** Flag is set for compiler-generated compound classes
aoqi@0: * representing multiple variable bounds
aoqi@0: */
aoqi@0: public static final int COMPOUND = 1<<24;
aoqi@0:
aoqi@0: /** Flag is set for class symbols if a class file was found for this class.
aoqi@0: */
aoqi@0: public static final int CLASS_SEEN = 1<<25;
aoqi@0:
aoqi@0: /** Flag is set for class symbols if a source file was found for this
aoqi@0: * class.
aoqi@0: */
aoqi@0: public static final int SOURCE_SEEN = 1<<26;
aoqi@0:
aoqi@0: /* State flags (are reset during compilation).
aoqi@0: */
aoqi@0:
aoqi@0: /** Flag for class symbols is set and later re-set as a lock in
aoqi@0: * Enter to detect cycles in the superclass/superinterface
aoqi@0: * relations. Similarly for constructor call cycle detection in
aoqi@0: * Attr.
aoqi@0: */
aoqi@0: public static final int LOCKED = 1<<27;
aoqi@0:
aoqi@0: /** Flag for class symbols is set and later re-set to indicate that a class
aoqi@0: * has been entered but has not yet been attributed.
aoqi@0: */
aoqi@0: public static final int UNATTRIBUTED = 1<<28;
aoqi@0:
aoqi@0: /** Flag for synthesized default constructors of anonymous classes.
aoqi@0: */
aoqi@0: public static final int ANONCONSTR = 1<<29;
aoqi@0:
aoqi@0: /** Flag for class symbols to indicate it has been checked and found
aoqi@0: * acyclic.
aoqi@0: */
aoqi@0: public static final int ACYCLIC = 1<<30;
aoqi@0:
aoqi@0: /** Flag that marks bridge methods.
aoqi@0: */
aoqi@0: public static final long BRIDGE = 1L<<31;
aoqi@0:
aoqi@0: /** Flag that marks formal parameters.
aoqi@0: */
aoqi@0: public static final long PARAMETER = 1L<<33;
aoqi@0:
aoqi@0: /** Flag that marks varargs methods.
aoqi@0: */
aoqi@0: public static final long VARARGS = 1L<<34;
aoqi@0:
aoqi@0: /** Flag for annotation type symbols to indicate it has been
aoqi@0: * checked and found acyclic.
aoqi@0: */
aoqi@0: public static final long ACYCLIC_ANN = 1L<<35;
aoqi@0:
aoqi@0: /** Flag that marks a generated default constructor.
aoqi@0: */
aoqi@0: public static final long GENERATEDCONSTR = 1L<<36;
aoqi@0:
aoqi@0: /** Flag that marks a hypothetical method that need not really be
aoqi@0: * generated in the binary, but is present in the symbol table to
aoqi@0: * simplify checking for erasure clashes - also used for 292 poly sig methods.
aoqi@0: */
aoqi@0: public static final long HYPOTHETICAL = 1L<<37;
aoqi@0:
aoqi@0: /**
aoqi@0: * Flag that marks an internal proprietary class.
aoqi@0: */
aoqi@0: public static final long PROPRIETARY = 1L<<38;
aoqi@0:
aoqi@0: /**
aoqi@0: * Flag that marks a multi-catch parameter.
aoqi@0: */
aoqi@0: public static final long UNION = 1L<<39;
aoqi@0:
aoqi@0: /**
aoqi@0: * Flag that marks a special kind of bridge method (the ones that
aoqi@0: * come from restricted supertype bounds).
aoqi@0: */
aoqi@0: public static final long OVERRIDE_BRIDGE = 1L<<40;
aoqi@0:
aoqi@0: /**
aoqi@0: * Flag that marks an 'effectively final' local variable.
aoqi@0: */
aoqi@0: public static final long EFFECTIVELY_FINAL = 1L<<41;
aoqi@0:
aoqi@0: /**
aoqi@0: * Flag that marks non-override equivalent methods with the same signature.
aoqi@0: */
aoqi@0: public static final long CLASH = 1L<<42;
aoqi@0:
aoqi@0: /**
aoqi@0: * Flag that marks either a default method or an interface containing default methods.
aoqi@0: */
aoqi@0: public static final long DEFAULT = 1L<<43;
aoqi@0:
aoqi@0: /**
aoqi@0: * Flag that marks class as auxiliary, ie a non-public class following
aoqi@0: * the public class in a source file, that could block implicit compilation.
aoqi@0: */
aoqi@0: public static final long AUXILIARY = 1L<<44;
aoqi@0:
aoqi@0: /**
aoqi@0: * Flag that marks that a symbol is not available in the current profile
aoqi@0: */
aoqi@0: public static final long NOT_IN_PROFILE = 1L<<45;
aoqi@0:
aoqi@0: /**
aoqi@0: * Flag that indicates that an override error has been detected by Check.
aoqi@0: */
aoqi@0: public static final long BAD_OVERRIDE = 1L<<45;
aoqi@0:
aoqi@0: /**
aoqi@0: * Flag that indicates a signature polymorphic method (292).
aoqi@0: */
aoqi@0: public static final long SIGNATURE_POLYMORPHIC = 1L<<46;
aoqi@0:
aoqi@0: /**
aoqi@0: * Flag that indicates that an inference variable is used in a 'throws' clause.
aoqi@0: */
aoqi@0: public static final long THROWS = 1L<<47;
aoqi@0:
aoqi@0: /**
aoqi@0: * Flag that marks potentially ambiguous overloads
aoqi@0: */
aoqi@0: public static final long POTENTIALLY_AMBIGUOUS = 1L<<48;
aoqi@0:
aoqi@0: /**
aoqi@0: * Flag that marks a synthetic method body for a lambda expression
aoqi@0: */
aoqi@0: public static final long LAMBDA_METHOD = 1L<<49;
aoqi@0:
aoqi@0: /**
aoqi@0: * Flag to control recursion in TransTypes
aoqi@0: */
aoqi@0: public static final long TYPE_TRANSLATED = 1L<<50;
aoqi@0:
aoqi@0: /** Modifier masks.
aoqi@0: */
aoqi@0: public static final int
aoqi@0: AccessFlags = PUBLIC | PROTECTED | PRIVATE,
aoqi@0: LocalClassFlags = FINAL | ABSTRACT | STRICTFP | ENUM | SYNTHETIC,
aoqi@0: MemberClassFlags = LocalClassFlags | INTERFACE | AccessFlags,
aoqi@0: ClassFlags = LocalClassFlags | INTERFACE | PUBLIC | ANNOTATION,
aoqi@0: InterfaceVarFlags = FINAL | STATIC | PUBLIC,
aoqi@0: VarFlags = AccessFlags | FINAL | STATIC |
aoqi@0: VOLATILE | TRANSIENT | ENUM,
aoqi@0: ConstructorFlags = AccessFlags,
aoqi@0: InterfaceMethodFlags = ABSTRACT | PUBLIC,
aoqi@0: MethodFlags = AccessFlags | ABSTRACT | STATIC | NATIVE |
aoqi@0: SYNCHRONIZED | FINAL | STRICTFP;
aoqi@0: public static final long
aoqi@0: ExtendedStandardFlags = (long)StandardFlags | DEFAULT,
aoqi@0: ModifierFlags = ((long)StandardFlags & ~INTERFACE) | DEFAULT,
aoqi@0: InterfaceMethodMask = ABSTRACT | STATIC | PUBLIC | STRICTFP | DEFAULT,
aoqi@0: AnnotationTypeElementMask = ABSTRACT | PUBLIC,
aoqi@0: LocalVarFlags = FINAL | PARAMETER,
aoqi@0: ReceiverParamFlags = PARAMETER;
aoqi@0:
aoqi@0:
aoqi@0: public static Set asModifierSet(long flags) {
aoqi@0: Set modifiers = modifierSets.get(flags);
aoqi@0: if (modifiers == null) {
aoqi@0: modifiers = java.util.EnumSet.noneOf(Modifier.class);
aoqi@0: if (0 != (flags & PUBLIC)) modifiers.add(Modifier.PUBLIC);
aoqi@0: if (0 != (flags & PROTECTED)) modifiers.add(Modifier.PROTECTED);
aoqi@0: if (0 != (flags & PRIVATE)) modifiers.add(Modifier.PRIVATE);
aoqi@0: if (0 != (flags & ABSTRACT)) modifiers.add(Modifier.ABSTRACT);
aoqi@0: if (0 != (flags & STATIC)) modifiers.add(Modifier.STATIC);
aoqi@0: if (0 != (flags & FINAL)) modifiers.add(Modifier.FINAL);
aoqi@0: if (0 != (flags & TRANSIENT)) modifiers.add(Modifier.TRANSIENT);
aoqi@0: if (0 != (flags & VOLATILE)) modifiers.add(Modifier.VOLATILE);
aoqi@0: if (0 != (flags & SYNCHRONIZED))
aoqi@0: modifiers.add(Modifier.SYNCHRONIZED);
aoqi@0: if (0 != (flags & NATIVE)) modifiers.add(Modifier.NATIVE);
aoqi@0: if (0 != (flags & STRICTFP)) modifiers.add(Modifier.STRICTFP);
aoqi@0: if (0 != (flags & DEFAULT)) modifiers.add(Modifier.DEFAULT);
aoqi@0: modifiers = Collections.unmodifiableSet(modifiers);
aoqi@0: modifierSets.put(flags, modifiers);
aoqi@0: }
aoqi@0: return modifiers;
aoqi@0: }
aoqi@0:
aoqi@0: // Cache of modifier sets.
aoqi@0: private static final Map> modifierSets =
aoqi@0: new java.util.concurrent.ConcurrentHashMap>(64);
aoqi@0:
aoqi@0: public static boolean isStatic(Symbol symbol) {
aoqi@0: return (symbol.flags() & STATIC) != 0;
aoqi@0: }
aoqi@0:
aoqi@0: public static boolean isEnum(Symbol symbol) {
aoqi@0: return (symbol.flags() & ENUM) != 0;
aoqi@0: }
aoqi@0:
aoqi@0: public static boolean isConstant(Symbol.VarSymbol symbol) {
aoqi@0: return symbol.getConstValue() != null;
aoqi@0: }
aoqi@0:
aoqi@0:
aoqi@0: public enum Flag {
aoqi@0: PUBLIC(Flags.PUBLIC),
aoqi@0: PRIVATE(Flags.PRIVATE),
aoqi@0: PROTECTED(Flags.PROTECTED),
aoqi@0: STATIC(Flags.STATIC),
aoqi@0: FINAL(Flags.FINAL),
aoqi@0: SYNCHRONIZED(Flags.SYNCHRONIZED),
aoqi@0: VOLATILE(Flags.VOLATILE),
aoqi@0: TRANSIENT(Flags.TRANSIENT),
aoqi@0: NATIVE(Flags.NATIVE),
aoqi@0: INTERFACE(Flags.INTERFACE),
aoqi@0: ABSTRACT(Flags.ABSTRACT),
aoqi@0: DEFAULT(Flags.DEFAULT),
aoqi@0: STRICTFP(Flags.STRICTFP),
aoqi@0: BRIDGE(Flags.BRIDGE),
aoqi@0: SYNTHETIC(Flags.SYNTHETIC),
aoqi@0: ANNOTATION(Flags.ANNOTATION),
aoqi@0: DEPRECATED(Flags.DEPRECATED),
aoqi@0: HASINIT(Flags.HASINIT),
aoqi@0: BLOCK(Flags.BLOCK),
aoqi@0: ENUM(Flags.ENUM),
aoqi@0: MANDATED(Flags.MANDATED),
aoqi@0: IPROXY(Flags.IPROXY),
aoqi@0: NOOUTERTHIS(Flags.NOOUTERTHIS),
aoqi@0: EXISTS(Flags.EXISTS),
aoqi@0: COMPOUND(Flags.COMPOUND),
aoqi@0: CLASS_SEEN(Flags.CLASS_SEEN),
aoqi@0: SOURCE_SEEN(Flags.SOURCE_SEEN),
aoqi@0: LOCKED(Flags.LOCKED),
aoqi@0: UNATTRIBUTED(Flags.UNATTRIBUTED),
aoqi@0: ANONCONSTR(Flags.ANONCONSTR),
aoqi@0: ACYCLIC(Flags.ACYCLIC),
aoqi@0: PARAMETER(Flags.PARAMETER),
aoqi@0: VARARGS(Flags.VARARGS),
aoqi@0: ACYCLIC_ANN(Flags.ACYCLIC_ANN),
aoqi@0: GENERATEDCONSTR(Flags.GENERATEDCONSTR),
aoqi@0: HYPOTHETICAL(Flags.HYPOTHETICAL),
aoqi@0: PROPRIETARY(Flags.PROPRIETARY),
aoqi@0: UNION(Flags.UNION),
aoqi@0: OVERRIDE_BRIDGE(Flags.OVERRIDE_BRIDGE),
aoqi@0: EFFECTIVELY_FINAL(Flags.EFFECTIVELY_FINAL),
aoqi@0: CLASH(Flags.CLASH),
aoqi@0: AUXILIARY(Flags.AUXILIARY),
aoqi@0: NOT_IN_PROFILE(Flags.NOT_IN_PROFILE),
aoqi@0: BAD_OVERRIDE(Flags.BAD_OVERRIDE),
aoqi@0: SIGNATURE_POLYMORPHIC(Flags.SIGNATURE_POLYMORPHIC),
aoqi@0: THROWS(Flags.THROWS),
aoqi@0: LAMBDA_METHOD(Flags.LAMBDA_METHOD),
aoqi@0: TYPE_TRANSLATED(Flags.TYPE_TRANSLATED);
aoqi@0:
aoqi@0: Flag(long flag) {
aoqi@0: this.value = flag;
aoqi@0: this.lowercaseName = StringUtils.toLowerCase(name());
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public String toString() {
aoqi@0: return lowercaseName;
aoqi@0: }
aoqi@0:
aoqi@0: final long value;
aoqi@0: final String lowercaseName;
aoqi@0: }
aoqi@0:
aoqi@0: }