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

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/javac/code/Flags.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,411 @@
     1.4 +/*
     1.5 + * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.javac.code;
    1.30 +
    1.31 +import java.util.Collections;
    1.32 +import java.util.EnumSet;
    1.33 +import java.util.Map;
    1.34 +import java.util.Set;
    1.35 +
    1.36 +import javax.lang.model.element.Modifier;
    1.37 +
    1.38 +import com.sun.tools.javac.util.Assert;
    1.39 +import com.sun.tools.javac.util.StringUtils;
    1.40 +
    1.41 +/** Access flags and other modifiers for Java classes and members.
    1.42 + *
    1.43 + *  <p><b>This is NOT part of any supported API.
    1.44 + *  If you write code that depends on this, you do so at your own risk.
    1.45 + *  This code and its internal interfaces are subject to change or
    1.46 + *  deletion without notice.</b>
    1.47 + */
    1.48 +public class Flags {
    1.49 +
    1.50 +    private Flags() {} // uninstantiable
    1.51 +
    1.52 +    public static String toString(long flags) {
    1.53 +        StringBuilder buf = new StringBuilder();
    1.54 +        String sep = "";
    1.55 +        for (Flag flag : asFlagSet(flags)) {
    1.56 +            buf.append(sep);
    1.57 +            buf.append(flag);
    1.58 +            sep = " ";
    1.59 +        }
    1.60 +        return buf.toString();
    1.61 +    }
    1.62 +
    1.63 +    public static EnumSet<Flag> asFlagSet(long flags) {
    1.64 +        EnumSet<Flag> flagSet = EnumSet.noneOf(Flag.class);
    1.65 +        for (Flag flag : Flag.values()) {
    1.66 +            if ((flags & flag.value) != 0) {
    1.67 +                flagSet.add(flag);
    1.68 +                flags &= ~flag.value;
    1.69 +            }
    1.70 +        }
    1.71 +        Assert.check(flags == 0, "Flags parameter contains unknown flags " + flags);
    1.72 +        return flagSet;
    1.73 +    }
    1.74 +
    1.75 +    /* Standard Java flags.
    1.76 +     */
    1.77 +    public static final int PUBLIC       = 1;
    1.78 +    public static final int PRIVATE      = 1<<1;
    1.79 +    public static final int PROTECTED    = 1<<2;
    1.80 +    public static final int STATIC       = 1<<3;
    1.81 +    public static final int FINAL        = 1<<4;
    1.82 +    public static final int SYNCHRONIZED = 1<<5;
    1.83 +    public static final int VOLATILE     = 1<<6;
    1.84 +    public static final int TRANSIENT    = 1<<7;
    1.85 +    public static final int NATIVE       = 1<<8;
    1.86 +    public static final int INTERFACE    = 1<<9;
    1.87 +    public static final int ABSTRACT     = 1<<10;
    1.88 +    public static final int STRICTFP     = 1<<11;
    1.89 +
    1.90 +    /* Flag that marks a symbol synthetic, added in classfile v49.0. */
    1.91 +    public static final int SYNTHETIC    = 1<<12;
    1.92 +
    1.93 +    /** Flag that marks attribute interfaces, added in classfile v49.0. */
    1.94 +    public static final int ANNOTATION   = 1<<13;
    1.95 +
    1.96 +    /** An enumeration type or an enumeration constant, added in
    1.97 +     *  classfile v49.0. */
    1.98 +    public static final int ENUM         = 1<<14;
    1.99 +
   1.100 +    /** Added in SE8, represents constructs implicitly declared in source. */
   1.101 +    public static final int MANDATED     = 1<<15;
   1.102 +
   1.103 +    public static final int StandardFlags = 0x0fff;
   1.104 +
   1.105 +    // Because the following access flags are overloaded with other
   1.106 +    // bit positions, we translate them when reading and writing class
   1.107 +    // files into unique bits positions: ACC_SYNTHETIC <-> SYNTHETIC,
   1.108 +    // for example.
   1.109 +    public static final int ACC_SUPER    = 0x0020;
   1.110 +    public static final int ACC_BRIDGE   = 0x0040;
   1.111 +    public static final int ACC_VARARGS  = 0x0080;
   1.112 +
   1.113 +    /*****************************************
   1.114 +     * Internal compiler flags (no bits in the lower 16).
   1.115 +     *****************************************/
   1.116 +
   1.117 +    /** Flag is set if symbol is deprecated.
   1.118 +     */
   1.119 +    public static final int DEPRECATED   = 1<<17;
   1.120 +
   1.121 +    /** Flag is set for a variable symbol if the variable's definition
   1.122 +     *  has an initializer part.
   1.123 +     */
   1.124 +    public static final int HASINIT          = 1<<18;
   1.125 +
   1.126 +    /** Flag is set for compiler-generated anonymous method symbols
   1.127 +     *  that `own' an initializer block.
   1.128 +     */
   1.129 +    public static final int BLOCK            = 1<<20;
   1.130 +
   1.131 +    /** Flag is set for compiler-generated abstract methods that implement
   1.132 +     *  an interface method (Miranda methods).
   1.133 +     */
   1.134 +    public static final int IPROXY           = 1<<21;
   1.135 +
   1.136 +    /** Flag is set for nested classes that do not access instance members
   1.137 +     *  or `this' of an outer class and therefore don't need to be passed
   1.138 +     *  a this$n reference.  This value is currently set only for anonymous
   1.139 +     *  classes in superclass constructor calls and only for pre 1.4 targets.
   1.140 +     *  todo: use this value for optimizing away this$n parameters in
   1.141 +     *  other cases.
   1.142 +     */
   1.143 +    public static final int NOOUTERTHIS  = 1<<22;
   1.144 +
   1.145 +    /** Flag is set for package symbols if a package has a member or
   1.146 +     *  directory and therefore exists.
   1.147 +     */
   1.148 +    public static final int EXISTS           = 1<<23;
   1.149 +
   1.150 +    /** Flag is set for compiler-generated compound classes
   1.151 +     *  representing multiple variable bounds
   1.152 +     */
   1.153 +    public static final int COMPOUND     = 1<<24;
   1.154 +
   1.155 +    /** Flag is set for class symbols if a class file was found for this class.
   1.156 +     */
   1.157 +    public static final int CLASS_SEEN   = 1<<25;
   1.158 +
   1.159 +    /** Flag is set for class symbols if a source file was found for this
   1.160 +     *  class.
   1.161 +     */
   1.162 +    public static final int SOURCE_SEEN  = 1<<26;
   1.163 +
   1.164 +    /* State flags (are reset during compilation).
   1.165 +     */
   1.166 +
   1.167 +    /** Flag for class symbols is set and later re-set as a lock in
   1.168 +     *  Enter to detect cycles in the superclass/superinterface
   1.169 +     *  relations.  Similarly for constructor call cycle detection in
   1.170 +     *  Attr.
   1.171 +     */
   1.172 +    public static final int LOCKED           = 1<<27;
   1.173 +
   1.174 +    /** Flag for class symbols is set and later re-set to indicate that a class
   1.175 +     *  has been entered but has not yet been attributed.
   1.176 +     */
   1.177 +    public static final int UNATTRIBUTED = 1<<28;
   1.178 +
   1.179 +    /** Flag for synthesized default constructors of anonymous classes.
   1.180 +     */
   1.181 +    public static final int ANONCONSTR   = 1<<29;
   1.182 +
   1.183 +    /** Flag for class symbols to indicate it has been checked and found
   1.184 +     *  acyclic.
   1.185 +     */
   1.186 +    public static final int ACYCLIC          = 1<<30;
   1.187 +
   1.188 +    /** Flag that marks bridge methods.
   1.189 +     */
   1.190 +    public static final long BRIDGE          = 1L<<31;
   1.191 +
   1.192 +    /** Flag that marks formal parameters.
   1.193 +     */
   1.194 +    public static final long PARAMETER   = 1L<<33;
   1.195 +
   1.196 +    /** Flag that marks varargs methods.
   1.197 +     */
   1.198 +    public static final long VARARGS   = 1L<<34;
   1.199 +
   1.200 +    /** Flag for annotation type symbols to indicate it has been
   1.201 +     *  checked and found acyclic.
   1.202 +     */
   1.203 +    public static final long ACYCLIC_ANN      = 1L<<35;
   1.204 +
   1.205 +    /** Flag that marks a generated default constructor.
   1.206 +     */
   1.207 +    public static final long GENERATEDCONSTR   = 1L<<36;
   1.208 +
   1.209 +    /** Flag that marks a hypothetical method that need not really be
   1.210 +     *  generated in the binary, but is present in the symbol table to
   1.211 +     *  simplify checking for erasure clashes - also used for 292 poly sig methods.
   1.212 +     */
   1.213 +    public static final long HYPOTHETICAL   = 1L<<37;
   1.214 +
   1.215 +    /**
   1.216 +     * Flag that marks an internal proprietary class.
   1.217 +     */
   1.218 +    public static final long PROPRIETARY = 1L<<38;
   1.219 +
   1.220 +    /**
   1.221 +     * Flag that marks a multi-catch parameter.
   1.222 +     */
   1.223 +    public static final long UNION = 1L<<39;
   1.224 +
   1.225 +    /**
   1.226 +     * Flag that marks a special kind of bridge method (the ones that
   1.227 +     * come from restricted supertype bounds).
   1.228 +     */
   1.229 +    public static final long OVERRIDE_BRIDGE = 1L<<40;
   1.230 +
   1.231 +    /**
   1.232 +     * Flag that marks an 'effectively final' local variable.
   1.233 +     */
   1.234 +    public static final long EFFECTIVELY_FINAL = 1L<<41;
   1.235 +
   1.236 +    /**
   1.237 +     * Flag that marks non-override equivalent methods with the same signature.
   1.238 +     */
   1.239 +    public static final long CLASH = 1L<<42;
   1.240 +
   1.241 +    /**
   1.242 +     * Flag that marks either a default method or an interface containing default methods.
   1.243 +     */
   1.244 +    public static final long DEFAULT = 1L<<43;
   1.245 +
   1.246 +    /**
   1.247 +     * Flag that marks class as auxiliary, ie a non-public class following
   1.248 +     * the public class in a source file, that could block implicit compilation.
   1.249 +     */
   1.250 +    public static final long AUXILIARY = 1L<<44;
   1.251 +
   1.252 +    /**
   1.253 +     * Flag that marks that a symbol is not available in the current profile
   1.254 +     */
   1.255 +    public static final long NOT_IN_PROFILE = 1L<<45;
   1.256 +
   1.257 +    /**
   1.258 +     * Flag that indicates that an override error has been detected by Check.
   1.259 +     */
   1.260 +    public static final long BAD_OVERRIDE = 1L<<45;
   1.261 +
   1.262 +    /**
   1.263 +     * Flag that indicates a signature polymorphic method (292).
   1.264 +     */
   1.265 +    public static final long SIGNATURE_POLYMORPHIC = 1L<<46;
   1.266 +
   1.267 +    /**
   1.268 +     * Flag that indicates that an inference variable is used in a 'throws' clause.
   1.269 +     */
   1.270 +    public static final long THROWS = 1L<<47;
   1.271 +
   1.272 +    /**
   1.273 +     * Flag that marks potentially ambiguous overloads
   1.274 +     */
   1.275 +    public static final long POTENTIALLY_AMBIGUOUS = 1L<<48;
   1.276 +
   1.277 +    /**
   1.278 +     * Flag that marks a synthetic method body for a lambda expression
   1.279 +     */
   1.280 +    public static final long LAMBDA_METHOD = 1L<<49;
   1.281 +
   1.282 +    /**
   1.283 +     * Flag to control recursion in TransTypes
   1.284 +     */
   1.285 +    public static final long TYPE_TRANSLATED = 1L<<50;
   1.286 +
   1.287 +    /** Modifier masks.
   1.288 +     */
   1.289 +    public static final int
   1.290 +        AccessFlags           = PUBLIC | PROTECTED | PRIVATE,
   1.291 +        LocalClassFlags       = FINAL | ABSTRACT | STRICTFP | ENUM | SYNTHETIC,
   1.292 +        MemberClassFlags      = LocalClassFlags | INTERFACE | AccessFlags,
   1.293 +        ClassFlags            = LocalClassFlags | INTERFACE | PUBLIC | ANNOTATION,
   1.294 +        InterfaceVarFlags     = FINAL | STATIC | PUBLIC,
   1.295 +        VarFlags              = AccessFlags | FINAL | STATIC |
   1.296 +                                VOLATILE | TRANSIENT | ENUM,
   1.297 +        ConstructorFlags      = AccessFlags,
   1.298 +        InterfaceMethodFlags  = ABSTRACT | PUBLIC,
   1.299 +        MethodFlags           = AccessFlags | ABSTRACT | STATIC | NATIVE |
   1.300 +                                SYNCHRONIZED | FINAL | STRICTFP;
   1.301 +    public static final long
   1.302 +        ExtendedStandardFlags       = (long)StandardFlags | DEFAULT,
   1.303 +        ModifierFlags               = ((long)StandardFlags & ~INTERFACE) | DEFAULT,
   1.304 +        InterfaceMethodMask         = ABSTRACT | STATIC | PUBLIC | STRICTFP | DEFAULT,
   1.305 +        AnnotationTypeElementMask   = ABSTRACT | PUBLIC,
   1.306 +        LocalVarFlags               = FINAL | PARAMETER,
   1.307 +        ReceiverParamFlags          = PARAMETER;
   1.308 +
   1.309 +
   1.310 +    public static Set<Modifier> asModifierSet(long flags) {
   1.311 +        Set<Modifier> modifiers = modifierSets.get(flags);
   1.312 +        if (modifiers == null) {
   1.313 +            modifiers = java.util.EnumSet.noneOf(Modifier.class);
   1.314 +            if (0 != (flags & PUBLIC))    modifiers.add(Modifier.PUBLIC);
   1.315 +            if (0 != (flags & PROTECTED)) modifiers.add(Modifier.PROTECTED);
   1.316 +            if (0 != (flags & PRIVATE))   modifiers.add(Modifier.PRIVATE);
   1.317 +            if (0 != (flags & ABSTRACT))  modifiers.add(Modifier.ABSTRACT);
   1.318 +            if (0 != (flags & STATIC))    modifiers.add(Modifier.STATIC);
   1.319 +            if (0 != (flags & FINAL))     modifiers.add(Modifier.FINAL);
   1.320 +            if (0 != (flags & TRANSIENT)) modifiers.add(Modifier.TRANSIENT);
   1.321 +            if (0 != (flags & VOLATILE))  modifiers.add(Modifier.VOLATILE);
   1.322 +            if (0 != (flags & SYNCHRONIZED))
   1.323 +                                          modifiers.add(Modifier.SYNCHRONIZED);
   1.324 +            if (0 != (flags & NATIVE))    modifiers.add(Modifier.NATIVE);
   1.325 +            if (0 != (flags & STRICTFP))  modifiers.add(Modifier.STRICTFP);
   1.326 +            if (0 != (flags & DEFAULT))   modifiers.add(Modifier.DEFAULT);
   1.327 +            modifiers = Collections.unmodifiableSet(modifiers);
   1.328 +            modifierSets.put(flags, modifiers);
   1.329 +        }
   1.330 +        return modifiers;
   1.331 +    }
   1.332 +
   1.333 +    // Cache of modifier sets.
   1.334 +    private static final Map<Long, Set<Modifier>> modifierSets =
   1.335 +        new java.util.concurrent.ConcurrentHashMap<Long, Set<Modifier>>(64);
   1.336 +
   1.337 +    public static boolean isStatic(Symbol symbol) {
   1.338 +        return (symbol.flags() & STATIC) != 0;
   1.339 +    }
   1.340 +
   1.341 +    public static boolean isEnum(Symbol symbol) {
   1.342 +        return (symbol.flags() & ENUM) != 0;
   1.343 +    }
   1.344 +
   1.345 +    public static boolean isConstant(Symbol.VarSymbol symbol) {
   1.346 +        return symbol.getConstValue() != null;
   1.347 +    }
   1.348 +
   1.349 +
   1.350 +    public enum Flag {
   1.351 +        PUBLIC(Flags.PUBLIC),
   1.352 +        PRIVATE(Flags.PRIVATE),
   1.353 +        PROTECTED(Flags.PROTECTED),
   1.354 +        STATIC(Flags.STATIC),
   1.355 +        FINAL(Flags.FINAL),
   1.356 +        SYNCHRONIZED(Flags.SYNCHRONIZED),
   1.357 +        VOLATILE(Flags.VOLATILE),
   1.358 +        TRANSIENT(Flags.TRANSIENT),
   1.359 +        NATIVE(Flags.NATIVE),
   1.360 +        INTERFACE(Flags.INTERFACE),
   1.361 +        ABSTRACT(Flags.ABSTRACT),
   1.362 +        DEFAULT(Flags.DEFAULT),
   1.363 +        STRICTFP(Flags.STRICTFP),
   1.364 +        BRIDGE(Flags.BRIDGE),
   1.365 +        SYNTHETIC(Flags.SYNTHETIC),
   1.366 +        ANNOTATION(Flags.ANNOTATION),
   1.367 +        DEPRECATED(Flags.DEPRECATED),
   1.368 +        HASINIT(Flags.HASINIT),
   1.369 +        BLOCK(Flags.BLOCK),
   1.370 +        ENUM(Flags.ENUM),
   1.371 +        MANDATED(Flags.MANDATED),
   1.372 +        IPROXY(Flags.IPROXY),
   1.373 +        NOOUTERTHIS(Flags.NOOUTERTHIS),
   1.374 +        EXISTS(Flags.EXISTS),
   1.375 +        COMPOUND(Flags.COMPOUND),
   1.376 +        CLASS_SEEN(Flags.CLASS_SEEN),
   1.377 +        SOURCE_SEEN(Flags.SOURCE_SEEN),
   1.378 +        LOCKED(Flags.LOCKED),
   1.379 +        UNATTRIBUTED(Flags.UNATTRIBUTED),
   1.380 +        ANONCONSTR(Flags.ANONCONSTR),
   1.381 +        ACYCLIC(Flags.ACYCLIC),
   1.382 +        PARAMETER(Flags.PARAMETER),
   1.383 +        VARARGS(Flags.VARARGS),
   1.384 +        ACYCLIC_ANN(Flags.ACYCLIC_ANN),
   1.385 +        GENERATEDCONSTR(Flags.GENERATEDCONSTR),
   1.386 +        HYPOTHETICAL(Flags.HYPOTHETICAL),
   1.387 +        PROPRIETARY(Flags.PROPRIETARY),
   1.388 +        UNION(Flags.UNION),
   1.389 +        OVERRIDE_BRIDGE(Flags.OVERRIDE_BRIDGE),
   1.390 +        EFFECTIVELY_FINAL(Flags.EFFECTIVELY_FINAL),
   1.391 +        CLASH(Flags.CLASH),
   1.392 +        AUXILIARY(Flags.AUXILIARY),
   1.393 +        NOT_IN_PROFILE(Flags.NOT_IN_PROFILE),
   1.394 +        BAD_OVERRIDE(Flags.BAD_OVERRIDE),
   1.395 +        SIGNATURE_POLYMORPHIC(Flags.SIGNATURE_POLYMORPHIC),
   1.396 +        THROWS(Flags.THROWS),
   1.397 +        LAMBDA_METHOD(Flags.LAMBDA_METHOD),
   1.398 +        TYPE_TRANSLATED(Flags.TYPE_TRANSLATED);
   1.399 +
   1.400 +        Flag(long flag) {
   1.401 +            this.value = flag;
   1.402 +            this.lowercaseName = StringUtils.toLowerCase(name());
   1.403 +        }
   1.404 +
   1.405 +        @Override
   1.406 +        public String toString() {
   1.407 +            return lowercaseName;
   1.408 +        }
   1.409 +
   1.410 +        final long value;
   1.411 +        final String lowercaseName;
   1.412 +    }
   1.413 +
   1.414 +}

mercurial