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