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

Mon, 21 Jan 2013 20:19:53 +0000

author
mcimadamore
date
Mon, 21 Jan 2013 20:19:53 +0000
changeset 1513
cf84b07a82db
parent 1512
b12ffdfa1341
child 1521
71f35e4b93a5
permissions
-rw-r--r--

8005166: Add support for static interface methods
Summary: Support public static interface methods
Reviewed-by: jjg

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

mercurial