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

Sat, 01 May 2010 15:05:39 -0700

author
jrose
date
Sat, 01 May 2010 15:05:39 -0700
changeset 571
f0e3ec1f9d9f
parent 482
699ecefbdd4e
child 573
005bec70ca27
permissions
-rw-r--r--

6939134: JSR 292 adjustments to method handle invocation
Summary: split MethodHandle.invoke into invokeExact and invokeGeneric
Reviewed-by: twisti

     1 /*
     2  * Copyright 1999-2008 Sun Microsystems, Inc.  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.  Sun designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
    23  * have any questions.
    24  */
    26 package com.sun.tools.javac.code;
    28 import java.util.EnumSet;
    29 import java.util.Collections;
    30 import java.util.Map;
    31 import java.util.Set;
    32 import javax.lang.model.element.Modifier;
    34 /** Access flags and other modifiers for Java classes and members.
    35  *
    36  *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
    37  *  you write code that depends on this, you do so at your own risk.
    38  *  This code and its internal interfaces are subject to change or
    39  *  deletion without notice.</b>
    40  */
    41 public class Flags {
    43     private Flags() {} // uninstantiable
    45     public static String toString(long flags) {
    46         StringBuffer buf = new StringBuffer();
    47         String sep = "";
    48         for (Flag s : asFlagSet(flags)) {
    49             buf.append(sep);
    50             buf.append(s);
    51             sep = " ";
    52         }
    53         return buf.toString();
    54     }
    56     public static EnumSet<Flag> asFlagSet(long mask) {
    57         EnumSet<Flag> flags = EnumSet.noneOf(Flag.class);
    58         if ((mask&PUBLIC) != 0) flags.add(Flag.PUBLIC);
    59         if ((mask&PRIVATE) != 0) flags.add(Flag.PRIVATE);
    60         if ((mask&PROTECTED) != 0) flags.add(Flag.PROTECTED);
    61         if ((mask&STATIC) != 0) flags.add(Flag.STATIC);
    62         if ((mask&FINAL) != 0) flags.add(Flag.FINAL);
    63         if ((mask&SYNCHRONIZED) != 0) flags.add(Flag.SYNCHRONIZED);
    64         if ((mask&VOLATILE) != 0) flags.add(Flag.VOLATILE);
    65         if ((mask&TRANSIENT) != 0) flags.add(Flag.TRANSIENT);
    66         if ((mask&NATIVE) != 0) flags.add(Flag.NATIVE);
    67         if ((mask&INTERFACE) != 0) flags.add(Flag.INTERFACE);
    68         if ((mask&ABSTRACT) != 0) flags.add(Flag.ABSTRACT);
    69         if ((mask&STRICTFP) != 0) flags.add(Flag.STRICTFP);
    70         if ((mask&BRIDGE) != 0) flags.add(Flag.BRIDGE);
    71         if ((mask&SYNTHETIC) != 0) flags.add(Flag.SYNTHETIC);
    72         if ((mask&DEPRECATED) != 0) flags.add(Flag.DEPRECATED);
    73         if ((mask&HASINIT) != 0) flags.add(Flag.HASINIT);
    74         if ((mask&ENUM) != 0) flags.add(Flag.ENUM);
    75         if ((mask&IPROXY) != 0) flags.add(Flag.IPROXY);
    76         if ((mask&NOOUTERTHIS) != 0) flags.add(Flag.NOOUTERTHIS);
    77         if ((mask&EXISTS) != 0) flags.add(Flag.EXISTS);
    78         if ((mask&COMPOUND) != 0) flags.add(Flag.COMPOUND);
    79         if ((mask&CLASS_SEEN) != 0) flags.add(Flag.CLASS_SEEN);
    80         if ((mask&SOURCE_SEEN) != 0) flags.add(Flag.SOURCE_SEEN);
    81         if ((mask&LOCKED) != 0) flags.add(Flag.LOCKED);
    82         if ((mask&UNATTRIBUTED) != 0) flags.add(Flag.UNATTRIBUTED);
    83         if ((mask&ANONCONSTR) != 0) flags.add(Flag.ANONCONSTR);
    84         if ((mask&ACYCLIC) != 0) flags.add(Flag.ACYCLIC);
    85         if ((mask&PARAMETER) != 0) flags.add(Flag.PARAMETER);
    86         if ((mask&VARARGS) != 0) flags.add(Flag.VARARGS);
    87         return flags;
    88     }
    90     /* Standard Java flags.
    91      */
    92     public static final int PUBLIC       = 1<<0;
    93     public static final int PRIVATE      = 1<<1;
    94     public static final int PROTECTED    = 1<<2;
    95     public static final int STATIC       = 1<<3;
    96     public static final int FINAL        = 1<<4;
    97     public static final int SYNCHRONIZED = 1<<5;
    98     public static final int VOLATILE     = 1<<6;
    99     public static final int TRANSIENT    = 1<<7;
   100     public static final int NATIVE       = 1<<8;
   101     public static final int INTERFACE    = 1<<9;
   102     public static final int ABSTRACT     = 1<<10;
   103     public static final int STRICTFP     = 1<<11;
   105     /* Flag that marks a symbol synthetic, added in classfile v49.0. */
   106     public static final int SYNTHETIC    = 1<<12;
   108     /** Flag that marks attribute interfaces, added in classfile v49.0. */
   109     public static final int ANNOTATION   = 1<<13;
   111     /** An enumeration type or an enumeration constant, added in
   112      *  classfile v49.0. */
   113     public static final int ENUM         = 1<<14;
   115     public static final int StandardFlags = 0x0fff;
   116     public static final int ModifierFlags = StandardFlags & ~INTERFACE;
   118     // Because the following access flags are overloaded with other
   119     // bit positions, we translate them when reading and writing class
   120     // files into unique bits positions: ACC_SYNTHETIC <-> SYNTHETIC,
   121     // for example.
   122     public static final int ACC_SUPER    = 0x0020;
   123     public static final int ACC_BRIDGE   = 0x0040;
   124     public static final int ACC_VARARGS  = 0x0080;
   126     /*****************************************
   127      * Internal compiler flags (no bits in the lower 16).
   128      *****************************************/
   130     /** Flag is set if symbol is deprecated.
   131      */
   132     public static final int DEPRECATED   = 1<<17;
   134     /** Flag is set for a variable symbol if the variable's definition
   135      *  has an initializer part.
   136      */
   137     public static final int HASINIT          = 1<<18;
   139     /** Flag is set for compiler-generated anonymous method symbols
   140      *  that `own' an initializer block.
   141      */
   142     public static final int BLOCK            = 1<<20;
   144     /** Flag is set for compiler-generated abstract methods that implement
   145      *  an interface method (Miranda methods).
   146      */
   147     public static final int IPROXY           = 1<<21;
   149     /** Flag is set for nested classes that do not access instance members
   150      *  or `this' of an outer class and therefore don't need to be passed
   151      *  a this$n reference.  This flag is currently set only for anonymous
   152      *  classes in superclass constructor calls and only for pre 1.4 targets.
   153      *  todo: use this flag for optimizing away this$n parameters in
   154      *  other cases.
   155      */
   156     public static final int NOOUTERTHIS  = 1<<22;
   158     /** Flag is set for package symbols if a package has a member or
   159      *  directory and therefore exists.
   160      */
   161     public static final int EXISTS           = 1<<23;
   163     /** Flag is set for compiler-generated compound classes
   164      *  representing multiple variable bounds
   165      */
   166     public static final int COMPOUND     = 1<<24;
   168     /** Flag is set for class symbols if a class file was found for this class.
   169      */
   170     public static final int CLASS_SEEN   = 1<<25;
   172     /** Flag is set for class symbols if a source file was found for this
   173      *  class.
   174      */
   175     public static final int SOURCE_SEEN  = 1<<26;
   177     /* State flags (are reset during compilation).
   178      */
   180     /** Flag for class symbols is set and later re-set as a lock in
   181      *  Enter to detect cycles in the superclass/superinterface
   182      *  relations.  Similarly for constructor call cycle detection in
   183      *  Attr.
   184      */
   185     public static final int LOCKED           = 1<<27;
   187     /** Flag for class symbols is set and later re-set to indicate that a class
   188      *  has been entered but has not yet been attributed.
   189      */
   190     public static final int UNATTRIBUTED = 1<<28;
   192     /** Flag for synthesized default constructors of anonymous classes.
   193      */
   194     public static final int ANONCONSTR   = 1<<29;
   196     /** Flag for class symbols to indicate it has been checked and found
   197      *  acyclic.
   198      */
   199     public static final int ACYCLIC          = 1<<30;
   201     /** Flag that marks bridge methods.
   202      */
   203     public static final long BRIDGE          = 1L<<31;
   205     /** Flag that marks formal parameters.
   206      */
   207     public static final long PARAMETER   = 1L<<33;
   209     /** Flag that marks varargs methods.
   210      */
   211     public static final long VARARGS   = 1L<<34;
   213     /** Flag for annotation type symbols to indicate it has been
   214      *  checked and found acyclic.
   215      */
   216     public static final long ACYCLIC_ANN      = 1L<<35;
   218     /** Flag that marks a generated default constructor.
   219      */
   220     public static final long GENERATEDCONSTR   = 1L<<36;
   222     /** Flag that marks a hypothetical method that need not really be
   223      *  generated in the binary, but is present in the symbol table to
   224      *  simplify checking for erasure clashes.
   225      */
   226     public static final long HYPOTHETICAL   = 1L<<37;
   228     /**
   229      * Flag that marks a Sun proprietary class.
   230      */
   231     public static final long PROPRIETARY = 1L<<38;
   233     /**
   234      * Flag that marks a signature-polymorphic invoke method.
   235      * (These occur inside java.dyn.MethodHandle.)
   236      */
   237     public static final long POLYMORPHIC_SIGNATURE = 1L<<39;
   239     /** Modifier masks.
   240      */
   241     public static final int
   242         AccessFlags           = PUBLIC | PROTECTED | PRIVATE,
   243         LocalClassFlags       = FINAL | ABSTRACT | STRICTFP | ENUM | SYNTHETIC,
   244         MemberClassFlags      = LocalClassFlags | INTERFACE | AccessFlags,
   245         ClassFlags            = LocalClassFlags | INTERFACE | PUBLIC | ANNOTATION,
   246         InterfaceVarFlags     = FINAL | STATIC | PUBLIC,
   247         VarFlags              = AccessFlags | FINAL | STATIC |
   248                                 VOLATILE | TRANSIENT | ENUM,
   249         ConstructorFlags      = AccessFlags,
   250         InterfaceMethodFlags  = ABSTRACT | PUBLIC,
   251         MethodFlags           = AccessFlags | ABSTRACT | STATIC | NATIVE |
   252                                 SYNCHRONIZED | FINAL | STRICTFP;
   253     public static final long
   254         LocalVarFlags         = FINAL | PARAMETER;
   256     public static Set<Modifier> asModifierSet(long flags) {
   257         Set<Modifier> modifiers = modifierSets.get(flags);
   258         if (modifiers == null) {
   259             modifiers = java.util.EnumSet.noneOf(Modifier.class);
   260             if (0 != (flags & PUBLIC))    modifiers.add(Modifier.PUBLIC);
   261             if (0 != (flags & PROTECTED)) modifiers.add(Modifier.PROTECTED);
   262             if (0 != (flags & PRIVATE))   modifiers.add(Modifier.PRIVATE);
   263             if (0 != (flags & ABSTRACT))  modifiers.add(Modifier.ABSTRACT);
   264             if (0 != (flags & STATIC))    modifiers.add(Modifier.STATIC);
   265             if (0 != (flags & FINAL))     modifiers.add(Modifier.FINAL);
   266             if (0 != (flags & TRANSIENT)) modifiers.add(Modifier.TRANSIENT);
   267             if (0 != (flags & VOLATILE))  modifiers.add(Modifier.VOLATILE);
   268             if (0 != (flags & SYNCHRONIZED))
   269                                           modifiers.add(Modifier.SYNCHRONIZED);
   270             if (0 != (flags & NATIVE))    modifiers.add(Modifier.NATIVE);
   271             if (0 != (flags & STRICTFP))  modifiers.add(Modifier.STRICTFP);
   272             modifiers = Collections.unmodifiableSet(modifiers);
   273             modifierSets.put(flags, modifiers);
   274         }
   275         return modifiers;
   276     }
   278     // Cache of modifier sets.
   279     private static Map<Long, Set<Modifier>> modifierSets =
   280         new java.util.concurrent.ConcurrentHashMap<Long, Set<Modifier>>(64);
   282     public static boolean isStatic(Symbol symbol) {
   283         return (symbol.flags() & STATIC) != 0;
   284     }
   286     public static boolean isEnum(Symbol symbol) {
   287         return (symbol.flags() & ENUM) != 0;
   288     }
   290     public static boolean isConstant(Symbol.VarSymbol symbol) {
   291         return symbol.getConstValue() != null;
   292     }
   294     public enum Flag {
   296         PUBLIC("public"),
   297         PRIVATE("private"),
   298         PROTECTED("protected"),
   299         STATIC("static"),
   300         FINAL("final"),
   301         SYNCHRONIZED("synchronized"),
   302         VOLATILE("volatile"),
   303         TRANSIENT("transient"),
   304         NATIVE("native"),
   305         INTERFACE("interface"),
   306         ABSTRACT("abstract"),
   307         STRICTFP("strictfp"),
   308         BRIDGE("bridge"),
   309         SYNTHETIC("synthetic"),
   310         DEPRECATED("deprecated"),
   311         HASINIT("hasinit"),
   312         ENUM("enum"),
   313         IPROXY("iproxy"),
   314         NOOUTERTHIS("noouterthis"),
   315         EXISTS("exists"),
   316         COMPOUND("compound"),
   317         CLASS_SEEN("class_seen"),
   318         SOURCE_SEEN("source_seen"),
   319         LOCKED("locked"),
   320         UNATTRIBUTED("unattributed"),
   321         ANONCONSTR("anonconstr"),
   322         ACYCLIC("acyclic"),
   323         PARAMETER("parameter"),
   324         VARARGS("varargs"),
   325         PACKAGE("package");
   327         String name;
   329         Flag(String name) {
   330             this.name = name;
   331         }
   333         public String toString() {
   334             return name;
   335         }
   336     }
   337 }

mercurial