001: /*
002: * Copyright 1999-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.javac.code;
027:
028: import com.sun.tools.javac.util.Version;
029: import java.util.Collections;
030: import java.util.Map;
031: import java.util.Set;
032: import javax.lang.model.element.Modifier;
033:
034: /** Access flags and other modifiers for Java classes and members.
035: *
036: * <p><b>This is NOT part of any API supported by Sun Microsystems. If
037: * you write code that depends on this, you do so at your own risk.
038: * This code and its internal interfaces are subject to change or
039: * deletion without notice.</b>
040: */
041: @Version("@(#)Flags.java 1.51 07/05/05")
042: public class Flags {
043:
044: private Flags() {
045: } // uninstantiable
046:
047: public static String toString(long flags) {
048: StringBuffer buf = new StringBuffer();
049: if ((flags & PUBLIC) != 0)
050: buf.append("public ");
051: if ((flags & PRIVATE) != 0)
052: buf.append("private ");
053: if ((flags & PROTECTED) != 0)
054: buf.append("protected ");
055: if ((flags & STATIC) != 0)
056: buf.append("static ");
057: if ((flags & FINAL) != 0)
058: buf.append("final ");
059: if ((flags & SYNCHRONIZED) != 0)
060: buf.append("synchronized ");
061: if ((flags & VOLATILE) != 0)
062: buf.append("volatile ");
063: if ((flags & TRANSIENT) != 0)
064: buf.append("transient ");
065: if ((flags & NATIVE) != 0)
066: buf.append("native ");
067: if ((flags & INTERFACE) != 0)
068: buf.append("interface ");
069: if ((flags & ABSTRACT) != 0)
070: buf.append("abstract ");
071: if ((flags & STRICTFP) != 0)
072: buf.append("strictfp ");
073: if ((flags & BRIDGE) != 0)
074: buf.append("bridge ");
075: if ((flags & SYNTHETIC) != 0)
076: buf.append("synthetic ");
077: if ((flags & DEPRECATED) != 0)
078: buf.append("deprecated ");
079: if ((flags & HASINIT) != 0)
080: buf.append("hasinit ");
081: if ((flags & ENUM) != 0)
082: buf.append("enum ");
083: if ((flags & IPROXY) != 0)
084: buf.append("iproxy ");
085: if ((flags & NOOUTERTHIS) != 0)
086: buf.append("noouterthis ");
087: if ((flags & EXISTS) != 0)
088: buf.append("exists ");
089: if ((flags & COMPOUND) != 0)
090: buf.append("compound ");
091: if ((flags & CLASS_SEEN) != 0)
092: buf.append("class_seen ");
093: if ((flags & SOURCE_SEEN) != 0)
094: buf.append("source_seen ");
095: if ((flags & LOCKED) != 0)
096: buf.append("locked ");
097: if ((flags & UNATTRIBUTED) != 0)
098: buf.append("unattributed ");
099: if ((flags & ANONCONSTR) != 0)
100: buf.append("anonconstr ");
101: if ((flags & ACYCLIC) != 0)
102: buf.append("acyclic ");
103: if ((flags & PARAMETER) != 0)
104: buf.append("parameter ");
105: if ((flags & VARARGS) != 0)
106: buf.append("varargs ");
107: return buf.toString();
108: }
109:
110: /* Standard Java flags.
111: */
112: public static final int PUBLIC = 1 << 0;
113: public static final int PRIVATE = 1 << 1;
114: public static final int PROTECTED = 1 << 2;
115: public static final int STATIC = 1 << 3;
116: public static final int FINAL = 1 << 4;
117: public static final int SYNCHRONIZED = 1 << 5;
118: public static final int VOLATILE = 1 << 6;
119: public static final int TRANSIENT = 1 << 7;
120: public static final int NATIVE = 1 << 8;
121: public static final int INTERFACE = 1 << 9;
122: public static final int ABSTRACT = 1 << 10;
123: public static final int STRICTFP = 1 << 11;
124:
125: /* Flag that marks a symbol synthetic, added in classfile v49.0. */
126: public static final int SYNTHETIC = 1 << 12;
127:
128: /** Flag that marks attribute interfaces, added in classfile v49.0. */
129: public static final int ANNOTATION = 1 << 13;
130:
131: /** An enumeration type or an enumeration constant, added in
132: * classfile v49.0. */
133: public static final int ENUM = 1 << 14;
134:
135: public static final int StandardFlags = 0x0fff;
136:
137: // Because the following access flags are overloaded with other
138: // bit positions, we translate them when reading and writing class
139: // files into unique bits positions: ACC_SYNTHETIC <-> SYNTHETIC,
140: // for example.
141: public static final int ACC_SUPER = 0x0020;
142: public static final int ACC_BRIDGE = 0x0040;
143: public static final int ACC_VARARGS = 0x0080;
144:
145: /*****************************************
146: * Internal compiler flags (no bits in the lower 16).
147: *****************************************/
148:
149: /** Flag is set if symbol is deprecated.
150: */
151: public static final int DEPRECATED = 1 << 17;
152:
153: /** Flag is set for a variable symbol if the variable's definition
154: * has an initializer part.
155: */
156: public static final int HASINIT = 1 << 18;
157:
158: /** Flag is set for compiler-generated anonymous method symbols
159: * that `own' an initializer block.
160: */
161: public static final int BLOCK = 1 << 20;
162:
163: /** Flag is set for compiler-generated abstract methods that implement
164: * an interface method (Miranda methods).
165: */
166: public static final int IPROXY = 1 << 21;
167:
168: /** Flag is set for nested classes that do not access instance members
169: * or `this' of an outer class and therefore don't need to be passed
170: * a this$n reference. This flag is currently set only for anonymous
171: * classes in superclass constructor calls and only for pre 1.4 targets.
172: * todo: use this flag for optimizing away this$n parameters in
173: * other cases.
174: */
175: public static final int NOOUTERTHIS = 1 << 22;
176:
177: /** Flag is set for package symbols if a package has a member or
178: * directory and therefore exists.
179: */
180: public static final int EXISTS = 1 << 23;
181:
182: /** Flag is set for compiler-generated compound classes
183: * representing multiple variable bounds
184: */
185: public static final int COMPOUND = 1 << 24;
186:
187: /** Flag is set for class symbols if a class file was found for this class.
188: */
189: public static final int CLASS_SEEN = 1 << 25;
190:
191: /** Flag is set for class symbols if a source file was found for this
192: * class.
193: */
194: public static final int SOURCE_SEEN = 1 << 26;
195:
196: /* State flags (are reset during compilation).
197: */
198:
199: /** Flag for class symbols is set and later re-set as a lock in
200: * Enter to detect cycles in the superclass/superinterface
201: * relations. Similarly for constructor call cycle detection in
202: * Attr.
203: */
204: public static final int LOCKED = 1 << 27;
205:
206: /** Flag for class symbols is set and later re-set to indicate that a class
207: * has been entered but has not yet been attributed.
208: */
209: public static final int UNATTRIBUTED = 1 << 28;
210:
211: /** Flag for synthesized default constructors of anonymous classes.
212: */
213: public static final int ANONCONSTR = 1 << 29;
214:
215: /** Flag for class symbols to indicate it has been checked and found
216: * acyclic.
217: */
218: public static final int ACYCLIC = 1 << 30;
219:
220: /** Flag that marks bridge methods.
221: */
222: public static final long BRIDGE = 1L << 31;
223:
224: /** Flag that marks formal parameters.
225: */
226: public static final long PARAMETER = 1L << 33;
227:
228: /** Flag that marks varargs methods.
229: */
230: public static final long VARARGS = 1L << 34;
231:
232: /** Flag for annotation type symbols to indicate it has been
233: * checked and found acyclic.
234: */
235: public static final long ACYCLIC_ANN = 1L << 35;
236:
237: /** Flag that marks a generated default constructor.
238: */
239: public static final long GENERATEDCONSTR = 1L << 36;
240:
241: /** Flag that marks a hypothetical method that need not really be
242: * generated in the binary, but is present in the symbol table to
243: * simplify checking for erasure clashes.
244: */
245: public static final long HYPOTHETICAL = 1L << 37;
246:
247: /**
248: * Flag that marks a Sun proprietary class.
249: */
250: public static final long PROPRIETARY = 1L << 38;
251:
252: /** Modifier masks.
253: */
254: public static final int AccessFlags = PUBLIC | PROTECTED | PRIVATE,
255: LocalClassFlags = FINAL | ABSTRACT | STRICTFP | ENUM
256: | SYNTHETIC, MemberClassFlags = LocalClassFlags
257: | INTERFACE | AccessFlags,
258: ClassFlags = LocalClassFlags | INTERFACE | PUBLIC
259: | ANNOTATION, InterfaceVarFlags = FINAL | STATIC
260: | PUBLIC, VarFlags = AccessFlags | FINAL | STATIC
261: | VOLATILE | TRANSIENT | ENUM,
262: ConstructorFlags = AccessFlags,
263: InterfaceMethodFlags = ABSTRACT | PUBLIC,
264: MethodFlags = AccessFlags | ABSTRACT | STATIC | NATIVE
265: | SYNCHRONIZED | FINAL | STRICTFP;
266: public static final long LocalVarFlags = FINAL | PARAMETER;
267:
268: public static Set<Modifier> asModifierSet(long flags) {
269: Set<Modifier> modifiers = modifierSets.get(flags);
270: if (modifiers == null) {
271: modifiers = java.util.EnumSet.noneOf(Modifier.class);
272: if (0 != (flags & PUBLIC))
273: modifiers.add(Modifier.PUBLIC);
274: if (0 != (flags & PROTECTED))
275: modifiers.add(Modifier.PROTECTED);
276: if (0 != (flags & PRIVATE))
277: modifiers.add(Modifier.PRIVATE);
278: if (0 != (flags & ABSTRACT))
279: modifiers.add(Modifier.ABSTRACT);
280: if (0 != (flags & STATIC))
281: modifiers.add(Modifier.STATIC);
282: if (0 != (flags & FINAL))
283: modifiers.add(Modifier.FINAL);
284: if (0 != (flags & TRANSIENT))
285: modifiers.add(Modifier.TRANSIENT);
286: if (0 != (flags & VOLATILE))
287: modifiers.add(Modifier.VOLATILE);
288: if (0 != (flags & SYNCHRONIZED))
289: modifiers.add(Modifier.SYNCHRONIZED);
290: if (0 != (flags & NATIVE))
291: modifiers.add(Modifier.NATIVE);
292: if (0 != (flags & STRICTFP))
293: modifiers.add(Modifier.STRICTFP);
294: modifiers = Collections.unmodifiableSet(modifiers);
295: modifierSets.put(flags, modifiers);
296: }
297: return modifiers;
298: }
299:
300: // Cache of modifier sets.
301: private static Map<Long, Set<Modifier>> modifierSets = new java.util.concurrent.ConcurrentHashMap<Long, Set<Modifier>>(
302: 64);
303:
304: public static boolean isStatic(Symbol symbol) {
305: return (symbol.flags() & STATIC) != 0;
306: }
307:
308: public static boolean isEnum(Symbol symbol) {
309: return (symbol.flags() & ENUM) != 0;
310: }
311:
312: public static boolean isConstant(Symbol.VarSymbol symbol) {
313: return symbol.getConstValue() != null;
314: }
315: }
|