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