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

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

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

mercurial