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 Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,274 @@ 1.4 +/* 1.5 + * Copyright 1999-2006 Sun Microsystems, Inc. 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. Sun designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.25 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.26 + * have any 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.Map; 1.33 +import java.util.Set; 1.34 +import javax.lang.model.element.Modifier; 1.35 + 1.36 +/** Access flags and other modifiers for Java classes and members. 1.37 + * 1.38 + * <p><b>This is NOT part of any API supported by Sun Microsystems. If 1.39 + * you write code that depends on this, you do so at your own risk. 1.40 + * This code and its internal interfaces are subject to change or 1.41 + * deletion without notice.</b> 1.42 + */ 1.43 +public class Flags { 1.44 + 1.45 + private Flags() {} // uninstantiable 1.46 + 1.47 + public static String toString(long flags) { 1.48 + StringBuffer buf = new StringBuffer(); 1.49 + if ((flags&PUBLIC) != 0) buf.append("public "); 1.50 + if ((flags&PRIVATE) != 0) buf.append("private "); 1.51 + if ((flags&PROTECTED) != 0) buf.append("protected "); 1.52 + if ((flags&STATIC) != 0) buf.append("static "); 1.53 + if ((flags&FINAL) != 0) buf.append("final "); 1.54 + if ((flags&SYNCHRONIZED) != 0) buf.append("synchronized "); 1.55 + if ((flags&VOLATILE) != 0) buf.append("volatile "); 1.56 + if ((flags&TRANSIENT) != 0) buf.append("transient "); 1.57 + if ((flags&NATIVE) != 0) buf.append("native "); 1.58 + if ((flags&INTERFACE) != 0) buf.append("interface "); 1.59 + if ((flags&ABSTRACT) != 0) buf.append("abstract "); 1.60 + if ((flags&STRICTFP) != 0) buf.append("strictfp "); 1.61 + if ((flags&BRIDGE) != 0) buf.append("bridge "); 1.62 + if ((flags&SYNTHETIC) != 0) buf.append("synthetic "); 1.63 + if ((flags&DEPRECATED) != 0) buf.append("deprecated "); 1.64 + if ((flags&HASINIT) != 0) buf.append("hasinit "); 1.65 + if ((flags&ENUM) != 0) buf.append("enum "); 1.66 + if ((flags&IPROXY) != 0) buf.append("iproxy "); 1.67 + if ((flags&NOOUTERTHIS) != 0) buf.append("noouterthis "); 1.68 + if ((flags&EXISTS) != 0) buf.append("exists "); 1.69 + if ((flags&COMPOUND) != 0) buf.append("compound "); 1.70 + if ((flags&CLASS_SEEN) != 0) buf.append("class_seen "); 1.71 + if ((flags&SOURCE_SEEN) != 0) buf.append("source_seen "); 1.72 + if ((flags&LOCKED) != 0) buf.append("locked "); 1.73 + if ((flags&UNATTRIBUTED) != 0) buf.append("unattributed "); 1.74 + if ((flags&ANONCONSTR) != 0) buf.append("anonconstr "); 1.75 + if ((flags&ACYCLIC) != 0) buf.append("acyclic "); 1.76 + if ((flags&PARAMETER) != 0) buf.append("parameter "); 1.77 + if ((flags&VARARGS) != 0) buf.append("varargs "); 1.78 + return buf.toString(); 1.79 + } 1.80 + 1.81 + /* Standard Java flags. 1.82 + */ 1.83 + public static final int PUBLIC = 1<<0; 1.84 + public static final int PRIVATE = 1<<1; 1.85 + public static final int PROTECTED = 1<<2; 1.86 + public static final int STATIC = 1<<3; 1.87 + public static final int FINAL = 1<<4; 1.88 + public static final int SYNCHRONIZED = 1<<5; 1.89 + public static final int VOLATILE = 1<<6; 1.90 + public static final int TRANSIENT = 1<<7; 1.91 + public static final int NATIVE = 1<<8; 1.92 + public static final int INTERFACE = 1<<9; 1.93 + public static final int ABSTRACT = 1<<10; 1.94 + public static final int STRICTFP = 1<<11; 1.95 + 1.96 + /* Flag that marks a symbol synthetic, added in classfile v49.0. */ 1.97 + public static final int SYNTHETIC = 1<<12; 1.98 + 1.99 + /** Flag that marks attribute interfaces, added in classfile v49.0. */ 1.100 + public static final int ANNOTATION = 1<<13; 1.101 + 1.102 + /** An enumeration type or an enumeration constant, added in 1.103 + * classfile v49.0. */ 1.104 + public static final int ENUM = 1<<14; 1.105 + 1.106 + public static final int StandardFlags = 0x0fff; 1.107 + 1.108 + // Because the following access flags are overloaded with other 1.109 + // bit positions, we translate them when reading and writing class 1.110 + // files into unique bits positions: ACC_SYNTHETIC <-> SYNTHETIC, 1.111 + // for example. 1.112 + public static final int ACC_SUPER = 0x0020; 1.113 + public static final int ACC_BRIDGE = 0x0040; 1.114 + public static final int ACC_VARARGS = 0x0080; 1.115 + 1.116 + /***************************************** 1.117 + * Internal compiler flags (no bits in the lower 16). 1.118 + *****************************************/ 1.119 + 1.120 + /** Flag is set if symbol is deprecated. 1.121 + */ 1.122 + public static final int DEPRECATED = 1<<17; 1.123 + 1.124 + /** Flag is set for a variable symbol if the variable's definition 1.125 + * has an initializer part. 1.126 + */ 1.127 + public static final int HASINIT = 1<<18; 1.128 + 1.129 + /** Flag is set for compiler-generated anonymous method symbols 1.130 + * that `own' an initializer block. 1.131 + */ 1.132 + public static final int BLOCK = 1<<20; 1.133 + 1.134 + /** Flag is set for compiler-generated abstract methods that implement 1.135 + * an interface method (Miranda methods). 1.136 + */ 1.137 + public static final int IPROXY = 1<<21; 1.138 + 1.139 + /** Flag is set for nested classes that do not access instance members 1.140 + * or `this' of an outer class and therefore don't need to be passed 1.141 + * a this$n reference. This flag is currently set only for anonymous 1.142 + * classes in superclass constructor calls and only for pre 1.4 targets. 1.143 + * todo: use this flag for optimizing away this$n parameters in 1.144 + * other cases. 1.145 + */ 1.146 + public static final int NOOUTERTHIS = 1<<22; 1.147 + 1.148 + /** Flag is set for package symbols if a package has a member or 1.149 + * directory and therefore exists. 1.150 + */ 1.151 + public static final int EXISTS = 1<<23; 1.152 + 1.153 + /** Flag is set for compiler-generated compound classes 1.154 + * representing multiple variable bounds 1.155 + */ 1.156 + public static final int COMPOUND = 1<<24; 1.157 + 1.158 + /** Flag is set for class symbols if a class file was found for this class. 1.159 + */ 1.160 + public static final int CLASS_SEEN = 1<<25; 1.161 + 1.162 + /** Flag is set for class symbols if a source file was found for this 1.163 + * class. 1.164 + */ 1.165 + public static final int SOURCE_SEEN = 1<<26; 1.166 + 1.167 + /* State flags (are reset during compilation). 1.168 + */ 1.169 + 1.170 + /** Flag for class symbols is set and later re-set as a lock in 1.171 + * Enter to detect cycles in the superclass/superinterface 1.172 + * relations. Similarly for constructor call cycle detection in 1.173 + * Attr. 1.174 + */ 1.175 + public static final int LOCKED = 1<<27; 1.176 + 1.177 + /** Flag for class symbols is set and later re-set to indicate that a class 1.178 + * has been entered but has not yet been attributed. 1.179 + */ 1.180 + public static final int UNATTRIBUTED = 1<<28; 1.181 + 1.182 + /** Flag for synthesized default constructors of anonymous classes. 1.183 + */ 1.184 + public static final int ANONCONSTR = 1<<29; 1.185 + 1.186 + /** Flag for class symbols to indicate it has been checked and found 1.187 + * acyclic. 1.188 + */ 1.189 + public static final int ACYCLIC = 1<<30; 1.190 + 1.191 + /** Flag that marks bridge methods. 1.192 + */ 1.193 + public static final long BRIDGE = 1L<<31; 1.194 + 1.195 + /** Flag that marks formal parameters. 1.196 + */ 1.197 + public static final long PARAMETER = 1L<<33; 1.198 + 1.199 + /** Flag that marks varargs methods. 1.200 + */ 1.201 + public static final long VARARGS = 1L<<34; 1.202 + 1.203 + /** Flag for annotation type symbols to indicate it has been 1.204 + * checked and found acyclic. 1.205 + */ 1.206 + public static final long ACYCLIC_ANN = 1L<<35; 1.207 + 1.208 + /** Flag that marks a generated default constructor. 1.209 + */ 1.210 + public static final long GENERATEDCONSTR = 1L<<36; 1.211 + 1.212 + /** Flag that marks a hypothetical method that need not really be 1.213 + * generated in the binary, but is present in the symbol table to 1.214 + * simplify checking for erasure clashes. 1.215 + */ 1.216 + public static final long HYPOTHETICAL = 1L<<37; 1.217 + 1.218 + /** 1.219 + * Flag that marks a Sun proprietary class. 1.220 + */ 1.221 + public static final long PROPRIETARY = 1L<<38; 1.222 + 1.223 + /** Modifier masks. 1.224 + */ 1.225 + public static final int 1.226 + AccessFlags = PUBLIC | PROTECTED | PRIVATE, 1.227 + LocalClassFlags = FINAL | ABSTRACT | STRICTFP | ENUM | SYNTHETIC, 1.228 + MemberClassFlags = LocalClassFlags | INTERFACE | AccessFlags, 1.229 + ClassFlags = LocalClassFlags | INTERFACE | PUBLIC | ANNOTATION, 1.230 + InterfaceVarFlags = FINAL | STATIC | PUBLIC, 1.231 + VarFlags = AccessFlags | FINAL | STATIC | 1.232 + VOLATILE | TRANSIENT | ENUM, 1.233 + ConstructorFlags = AccessFlags, 1.234 + InterfaceMethodFlags = ABSTRACT | PUBLIC, 1.235 + MethodFlags = AccessFlags | ABSTRACT | STATIC | NATIVE | 1.236 + SYNCHRONIZED | FINAL | STRICTFP; 1.237 + public static final long 1.238 + LocalVarFlags = FINAL | PARAMETER; 1.239 + 1.240 + public static Set<Modifier> asModifierSet(long flags) { 1.241 + Set<Modifier> modifiers = modifierSets.get(flags); 1.242 + if (modifiers == null) { 1.243 + modifiers = java.util.EnumSet.noneOf(Modifier.class); 1.244 + if (0 != (flags & PUBLIC)) modifiers.add(Modifier.PUBLIC); 1.245 + if (0 != (flags & PROTECTED)) modifiers.add(Modifier.PROTECTED); 1.246 + if (0 != (flags & PRIVATE)) modifiers.add(Modifier.PRIVATE); 1.247 + if (0 != (flags & ABSTRACT)) modifiers.add(Modifier.ABSTRACT); 1.248 + if (0 != (flags & STATIC)) modifiers.add(Modifier.STATIC); 1.249 + if (0 != (flags & FINAL)) modifiers.add(Modifier.FINAL); 1.250 + if (0 != (flags & TRANSIENT)) modifiers.add(Modifier.TRANSIENT); 1.251 + if (0 != (flags & VOLATILE)) modifiers.add(Modifier.VOLATILE); 1.252 + if (0 != (flags & SYNCHRONIZED)) 1.253 + modifiers.add(Modifier.SYNCHRONIZED); 1.254 + if (0 != (flags & NATIVE)) modifiers.add(Modifier.NATIVE); 1.255 + if (0 != (flags & STRICTFP)) modifiers.add(Modifier.STRICTFP); 1.256 + modifiers = Collections.unmodifiableSet(modifiers); 1.257 + modifierSets.put(flags, modifiers); 1.258 + } 1.259 + return modifiers; 1.260 + } 1.261 + 1.262 + // Cache of modifier sets. 1.263 + private static Map<Long, Set<Modifier>> modifierSets = 1.264 + new java.util.concurrent.ConcurrentHashMap<Long, Set<Modifier>>(64); 1.265 + 1.266 + public static boolean isStatic(Symbol symbol) { 1.267 + return (symbol.flags() & STATIC) != 0; 1.268 + } 1.269 + 1.270 + public static boolean isEnum(Symbol symbol) { 1.271 + return (symbol.flags() & ENUM) != 0; 1.272 + } 1.273 + 1.274 + public static boolean isConstant(Symbol.VarSymbol symbol) { 1.275 + return symbol.getConstValue() != null; 1.276 + } 1.277 +}