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

Tue, 17 Dec 2013 10:55:59 +0100

author
jlahoda
date
Tue, 17 Dec 2013 10:55:59 +0100
changeset 2413
fe033d997ddf
parent 2411
71767cdf52a7
child 2424
7e97c65c373c
permissions
-rw-r--r--

8029800: Flags.java uses String.toLowerCase without specifying Locale
Summary: Introducing StringUtils.toLowerCase/toUpperCase independent on the default locale, converting almost all usages of String.toLowerCase/toUpperCase to use the new methods.
Reviewed-by: jjg, bpatel

     1 /*
     2  * Copyright (c) 1999, 2014, Oracle and/or its affiliates. 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.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    26 package com.sun.tools.javac.code;
    28 import java.util.Collections;
    29 import java.util.EnumSet;
    30 import java.util.Map;
    31 import java.util.Set;
    33 import javax.lang.model.element.Modifier;
    35 import com.sun.tools.javac.util.Assert;
    36 import com.sun.tools.javac.util.StringUtils;
    38 /** Access flags and other modifiers for Java classes and members.
    39  *
    40  *  <p><b>This is NOT part of any supported API.
    41  *  If you write code that depends on this, you do so at your own risk.
    42  *  This code and its internal interfaces are subject to change or
    43  *  deletion without notice.</b>
    44  */
    45 public class Flags {
    47     private Flags() {} // uninstantiable
    49     public static String toString(long flags) {
    50         StringBuilder buf = new StringBuilder();
    51         String sep = "";
    52         for (Flag flag : asFlagSet(flags)) {
    53             buf.append(sep);
    54             buf.append(flag);
    55             sep = " ";
    56         }
    57         return buf.toString();
    58     }
    60     public static EnumSet<Flag> asFlagSet(long flags) {
    61         EnumSet<Flag> flagSet = EnumSet.noneOf(Flag.class);
    62         for (Flag flag : Flag.values()) {
    63             if ((flags & flag.value) != 0) {
    64                 flagSet.add(flag);
    65                 flags &= ~flag.value;
    66             }
    67         }
    68         Assert.check(flags == 0, "Flags parameter contains unknown flags " + flags);
    69         return flagSet;
    70     }
    72     /* Standard Java flags.
    73      */
    74     public static final int PUBLIC       = 1;
    75     public static final int PRIVATE      = 1<<1;
    76     public static final int PROTECTED    = 1<<2;
    77     public static final int STATIC       = 1<<3;
    78     public static final int FINAL        = 1<<4;
    79     public static final int SYNCHRONIZED = 1<<5;
    80     public static final int VOLATILE     = 1<<6;
    81     public static final int TRANSIENT    = 1<<7;
    82     public static final int NATIVE       = 1<<8;
    83     public static final int INTERFACE    = 1<<9;
    84     public static final int ABSTRACT     = 1<<10;
    85     public static final int STRICTFP     = 1<<11;
    87     /* Flag that marks a symbol synthetic, added in classfile v49.0. */
    88     public static final int SYNTHETIC    = 1<<12;
    90     /** Flag that marks attribute interfaces, added in classfile v49.0. */
    91     public static final int ANNOTATION   = 1<<13;
    93     /** An enumeration type or an enumeration constant, added in
    94      *  classfile v49.0. */
    95     public static final int ENUM         = 1<<14;
    97     /** Added in SE8, represents constructs implicitly declared in source. */
    98     public static final int MANDATED     = 1<<15;
   100     public static final int StandardFlags = 0x0fff;
   102     // Because the following access flags are overloaded with other
   103     // bit positions, we translate them when reading and writing class
   104     // files into unique bits positions: ACC_SYNTHETIC <-> SYNTHETIC,
   105     // for example.
   106     public static final int ACC_SUPER    = 0x0020;
   107     public static final int ACC_BRIDGE   = 0x0040;
   108     public static final int ACC_VARARGS  = 0x0080;
   110     /*****************************************
   111      * Internal compiler flags (no bits in the lower 16).
   112      *****************************************/
   114     /** Flag is set if symbol is deprecated.
   115      */
   116     public static final int DEPRECATED   = 1<<17;
   118     /** Flag is set for a variable symbol if the variable's definition
   119      *  has an initializer part.
   120      */
   121     public static final int HASINIT          = 1<<18;
   123     /** Flag is set for compiler-generated anonymous method symbols
   124      *  that `own' an initializer block.
   125      */
   126     public static final int BLOCK            = 1<<20;
   128     /** Flag is set for compiler-generated abstract methods that implement
   129      *  an interface method (Miranda methods).
   130      */
   131     public static final int IPROXY           = 1<<21;
   133     /** Flag is set for nested classes that do not access instance members
   134      *  or `this' of an outer class and therefore don't need to be passed
   135      *  a this$n reference.  This value is currently set only for anonymous
   136      *  classes in superclass constructor calls and only for pre 1.4 targets.
   137      *  todo: use this value for optimizing away this$n parameters in
   138      *  other cases.
   139      */
   140     public static final int NOOUTERTHIS  = 1<<22;
   142     /** Flag is set for package symbols if a package has a member or
   143      *  directory and therefore exists.
   144      */
   145     public static final int EXISTS           = 1<<23;
   147     /** Flag is set for compiler-generated compound classes
   148      *  representing multiple variable bounds
   149      */
   150     public static final int COMPOUND     = 1<<24;
   152     /** Flag is set for class symbols if a class file was found for this class.
   153      */
   154     public static final int CLASS_SEEN   = 1<<25;
   156     /** Flag is set for class symbols if a source file was found for this
   157      *  class.
   158      */
   159     public static final int SOURCE_SEEN  = 1<<26;
   161     /* State flags (are reset during compilation).
   162      */
   164     /** Flag for class symbols is set and later re-set as a lock in
   165      *  Enter to detect cycles in the superclass/superinterface
   166      *  relations.  Similarly for constructor call cycle detection in
   167      *  Attr.
   168      */
   169     public static final int LOCKED           = 1<<27;
   171     /** Flag for class symbols is set and later re-set to indicate that a class
   172      *  has been entered but has not yet been attributed.
   173      */
   174     public static final int UNATTRIBUTED = 1<<28;
   176     /** Flag for synthesized default constructors of anonymous classes.
   177      */
   178     public static final int ANONCONSTR   = 1<<29;
   180     /** Flag for class symbols to indicate it has been checked and found
   181      *  acyclic.
   182      */
   183     public static final int ACYCLIC          = 1<<30;
   185     /** Flag that marks bridge methods.
   186      */
   187     public static final long BRIDGE          = 1L<<31;
   189     /** Flag that marks formal parameters.
   190      */
   191     public static final long PARAMETER   = 1L<<33;
   193     /** Flag that marks varargs methods.
   194      */
   195     public static final long VARARGS   = 1L<<34;
   197     /** Flag for annotation type symbols to indicate it has been
   198      *  checked and found acyclic.
   199      */
   200     public static final long ACYCLIC_ANN      = 1L<<35;
   202     /** Flag that marks a generated default constructor.
   203      */
   204     public static final long GENERATEDCONSTR   = 1L<<36;
   206     /** Flag that marks a hypothetical method that need not really be
   207      *  generated in the binary, but is present in the symbol table to
   208      *  simplify checking for erasure clashes - also used for 292 poly sig methods.
   209      */
   210     public static final long HYPOTHETICAL   = 1L<<37;
   212     /**
   213      * Flag that marks an internal proprietary class.
   214      */
   215     public static final long PROPRIETARY = 1L<<38;
   217     /**
   218      * Flag that marks a multi-catch parameter.
   219      */
   220     public static final long UNION = 1L<<39;
   222     /**
   223      * Flag that marks a special kind of bridge method (the ones that
   224      * come from restricted supertype bounds).
   225      */
   226     public static final long OVERRIDE_BRIDGE = 1L<<40;
   228     /**
   229      * Flag that marks an 'effectively final' local variable.
   230      */
   231     public static final long EFFECTIVELY_FINAL = 1L<<41;
   233     /**
   234      * Flag that marks non-override equivalent methods with the same signature.
   235      */
   236     public static final long CLASH = 1L<<42;
   238     /**
   239      * Flag that marks either a default method or an interface containing default methods.
   240      */
   241     public static final long DEFAULT = 1L<<43;
   243     /**
   244      * Flag that marks class as auxiliary, ie a non-public class following
   245      * the public class in a source file, that could block implicit compilation.
   246      */
   247     public static final long AUXILIARY = 1L<<44;
   249     /**
   250      * Flag that marks that a symbol is not available in the current profile
   251      */
   252     public static final long NOT_IN_PROFILE = 1L<<45;
   254     /**
   255      * Flag that indicates that an override error has been detected by Check.
   256      */
   257     public static final long BAD_OVERRIDE = 1L<<45;
   259     /**
   260      * Flag that indicates a signature polymorphic method (292).
   261      */
   262     public static final long SIGNATURE_POLYMORPHIC = 1L<<46;
   264     /**
   265      * Flag that indicates that an inference variable is used in a 'throws' clause.
   266      */
   267     public static final long THROWS = 1L<<47;
   269     /**
   270      * Flag that marks potentially ambiguous overloads
   271      */
   272     public static final long POTENTIALLY_AMBIGUOUS = 1L<<48;
   274     /**
   275      * Flag that marks a synthetic method body for a lambda expression
   276      */
   277     public static final long LAMBDA_METHOD = 1L<<49;
   279     /** Modifier masks.
   280      */
   281     public static final int
   282         AccessFlags           = PUBLIC | PROTECTED | PRIVATE,
   283         LocalClassFlags       = FINAL | ABSTRACT | STRICTFP | ENUM | SYNTHETIC,
   284         MemberClassFlags      = LocalClassFlags | INTERFACE | AccessFlags,
   285         ClassFlags            = LocalClassFlags | INTERFACE | PUBLIC | ANNOTATION,
   286         InterfaceVarFlags     = FINAL | STATIC | PUBLIC,
   287         VarFlags              = AccessFlags | FINAL | STATIC |
   288                                 VOLATILE | TRANSIENT | ENUM,
   289         ConstructorFlags      = AccessFlags,
   290         InterfaceMethodFlags  = ABSTRACT | PUBLIC,
   291         MethodFlags           = AccessFlags | ABSTRACT | STATIC | NATIVE |
   292                                 SYNCHRONIZED | FINAL | STRICTFP;
   293     public static final long
   294         ExtendedStandardFlags       = (long)StandardFlags | DEFAULT,
   295         ModifierFlags               = ((long)StandardFlags & ~INTERFACE) | DEFAULT,
   296         InterfaceMethodMask         = ABSTRACT | STATIC | PUBLIC | STRICTFP | DEFAULT,
   297         AnnotationTypeElementMask   = ABSTRACT | PUBLIC,
   298         LocalVarFlags               = FINAL | PARAMETER;
   301     public static Set<Modifier> asModifierSet(long flags) {
   302         Set<Modifier> modifiers = modifierSets.get(flags);
   303         if (modifiers == null) {
   304             modifiers = java.util.EnumSet.noneOf(Modifier.class);
   305             if (0 != (flags & PUBLIC))    modifiers.add(Modifier.PUBLIC);
   306             if (0 != (flags & PROTECTED)) modifiers.add(Modifier.PROTECTED);
   307             if (0 != (flags & PRIVATE))   modifiers.add(Modifier.PRIVATE);
   308             if (0 != (flags & ABSTRACT))  modifiers.add(Modifier.ABSTRACT);
   309             if (0 != (flags & STATIC))    modifiers.add(Modifier.STATIC);
   310             if (0 != (flags & FINAL))     modifiers.add(Modifier.FINAL);
   311             if (0 != (flags & TRANSIENT)) modifiers.add(Modifier.TRANSIENT);
   312             if (0 != (flags & VOLATILE))  modifiers.add(Modifier.VOLATILE);
   313             if (0 != (flags & SYNCHRONIZED))
   314                                           modifiers.add(Modifier.SYNCHRONIZED);
   315             if (0 != (flags & NATIVE))    modifiers.add(Modifier.NATIVE);
   316             if (0 != (flags & STRICTFP))  modifiers.add(Modifier.STRICTFP);
   317             if (0 != (flags & DEFAULT))   modifiers.add(Modifier.DEFAULT);
   318             modifiers = Collections.unmodifiableSet(modifiers);
   319             modifierSets.put(flags, modifiers);
   320         }
   321         return modifiers;
   322     }
   324     // Cache of modifier sets.
   325     private static final Map<Long, Set<Modifier>> modifierSets =
   326         new java.util.concurrent.ConcurrentHashMap<Long, Set<Modifier>>(64);
   328     public static boolean isStatic(Symbol symbol) {
   329         return (symbol.flags() & STATIC) != 0;
   330     }
   332     public static boolean isEnum(Symbol symbol) {
   333         return (symbol.flags() & ENUM) != 0;
   334     }
   336     public static boolean isConstant(Symbol.VarSymbol symbol) {
   337         return symbol.getConstValue() != null;
   338     }
   341     public enum Flag {
   342         PUBLIC(Flags.PUBLIC),
   343         PRIVATE(Flags.PRIVATE),
   344         PROTECTED(Flags.PROTECTED),
   345         STATIC(Flags.STATIC),
   346         FINAL(Flags.FINAL),
   347         SYNCHRONIZED(Flags.SYNCHRONIZED),
   348         VOLATILE(Flags.VOLATILE),
   349         TRANSIENT(Flags.TRANSIENT),
   350         NATIVE(Flags.NATIVE),
   351         INTERFACE(Flags.INTERFACE),
   352         ABSTRACT(Flags.ABSTRACT),
   353         DEFAULT(Flags.DEFAULT),
   354         STRICTFP(Flags.STRICTFP),
   355         BRIDGE(Flags.BRIDGE),
   356         SYNTHETIC(Flags.SYNTHETIC),
   357         ANNOTATION(Flags.ANNOTATION),
   358         DEPRECATED(Flags.DEPRECATED),
   359         HASINIT(Flags.HASINIT),
   360         BLOCK(Flags.BLOCK),
   361         ENUM(Flags.ENUM),
   362         MANDATED(Flags.MANDATED),
   363         IPROXY(Flags.IPROXY),
   364         NOOUTERTHIS(Flags.NOOUTERTHIS),
   365         EXISTS(Flags.EXISTS),
   366         COMPOUND(Flags.COMPOUND),
   367         CLASS_SEEN(Flags.CLASS_SEEN),
   368         SOURCE_SEEN(Flags.SOURCE_SEEN),
   369         LOCKED(Flags.LOCKED),
   370         UNATTRIBUTED(Flags.UNATTRIBUTED),
   371         ANONCONSTR(Flags.ANONCONSTR),
   372         ACYCLIC(Flags.ACYCLIC),
   373         PARAMETER(Flags.PARAMETER),
   374         VARARGS(Flags.VARARGS),
   375         ACYCLIC_ANN(Flags.ACYCLIC_ANN),
   376         GENERATEDCONSTR(Flags.GENERATEDCONSTR),
   377         HYPOTHETICAL(Flags.HYPOTHETICAL),
   378         PROPRIETARY(Flags.PROPRIETARY),
   379         UNION(Flags.UNION),
   380         OVERRIDE_BRIDGE(Flags.OVERRIDE_BRIDGE),
   381         EFFECTIVELY_FINAL(Flags.EFFECTIVELY_FINAL),
   382         CLASH(Flags.CLASH),
   383         AUXILIARY(Flags.AUXILIARY),
   384         NOT_IN_PROFILE(Flags.NOT_IN_PROFILE),
   385         BAD_OVERRIDE(Flags.BAD_OVERRIDE),
   386         SIGNATURE_POLYMORPHIC(Flags.SIGNATURE_POLYMORPHIC),
   387         THROWS(Flags.THROWS),
   388         LAMBDA_METHOD(Flags.LAMBDA_METHOD);
   390         Flag(long flag) {
   391             this.value = flag;
   392             this.lowercaseName = StringUtils.toLowerCase(name());
   393         }
   395         @Override
   396         public String toString() {
   397             return lowercaseName;
   398         }
   400         final long value;
   401         final String lowercaseName;
   402     }
   404 }

mercurial