KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > core > Flags


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  * IBM Corporation - added constant AccDefault
11  * IBM Corporation - added constants AccBridge and AccVarargs for J2SE 1.5
12  *******************************************************************************/

13 package org.eclipse.jdt.core;
14
15 import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
16
17 /**
18  * Utility class for decoding modifier flags in Java elements.
19  * <p>
20  * This class provides static methods only; it is not intended to be
21  * instantiated or subclassed by clients.
22  * </p>
23  * <p>
24  * Note that the numeric values of these flags match the ones for class files
25  * as described in the Java Virtual Machine Specification. The AST class
26  * <code>Modifier</code> provides the same functionality as this class, only in
27  * the <code>org.eclipse.jdt.core.dom</code> package.
28  * </p>
29  *
30  * @see IMember#getFlags()
31  */

32 public final class Flags {
33
34     /**
35      * Constant representing the absence of any flag
36      * @since 3.0
37      */

38     public static final int AccDefault = ClassFileConstants.AccDefault;
39     /**
40      * Public access flag. See The Java Virtual Machine Specification for more details.
41      * @since 2.0
42      */

43     public static final int AccPublic = ClassFileConstants.AccPublic;
44     /**
45      * Private access flag. See The Java Virtual Machine Specification for more details.
46      * @since 2.0
47      */

48     public static final int AccPrivate = ClassFileConstants.AccPrivate;
49     /**
50      * Protected access flag. See The Java Virtual Machine Specification for more details.
51      * @since 2.0
52      */

53     public static final int AccProtected = ClassFileConstants.AccProtected;
54     /**
55      * Static access flag. See The Java Virtual Machine Specification for more details.
56      * @since 2.0
57      */

58     public static final int AccStatic = ClassFileConstants.AccStatic;
59     /**
60      * Final access flag. See The Java Virtual Machine Specification for more details.
61      * @since 2.0
62      */

63     public static final int AccFinal = ClassFileConstants.AccFinal;
64     /**
65      * Synchronized access flag. See The Java Virtual Machine Specification for more details.
66      * @since 2.0
67      */

68     public static final int AccSynchronized = ClassFileConstants.AccSynchronized;
69     /**
70      * Volatile property flag. See The Java Virtual Machine Specification for more details.
71      * @since 2.0
72      */

73     public static final int AccVolatile = ClassFileConstants.AccVolatile;
74     /**
75      * Transient property flag. See The Java Virtual Machine Specification for more details.
76      * @since 2.0
77      */

78     public static final int AccTransient = ClassFileConstants.AccTransient;
79     /**
80      * Native property flag. See The Java Virtual Machine Specification for more details.
81      * @since 2.0
82      */

83     public static final int AccNative = ClassFileConstants.AccNative;
84     /**
85      * Interface property flag. See The Java Virtual Machine Specification for more details.
86      * @since 2.0
87      */

88     public static final int AccInterface = ClassFileConstants.AccInterface;
89     /**
90      * Abstract property flag. See The Java Virtual Machine Specification for more details.
91      * @since 2.0
92      */

93     public static final int AccAbstract = ClassFileConstants.AccAbstract;
94     /**
95      * Strictfp property flag. See The Java Virtual Machine Specification for more details.
96      * @since 2.0
97      */

98     public static final int AccStrictfp = ClassFileConstants.AccStrictfp;
99     /**
100      * Super property flag. See The Java Virtual Machine Specification for more details.
101      * @since 2.0
102      */

103     public static final int AccSuper = ClassFileConstants.AccSuper;
104     /**
105      * Synthetic property flag. See The Java Virtual Machine Specification for more details.
106      * @since 2.0
107      */

108     public static final int AccSynthetic = ClassFileConstants.AccSynthetic;
109     /**
110      * Deprecated property flag. See The Java Virtual Machine Specification for more details.
111      * @since 2.0
112      */

113     public static final int AccDeprecated = ClassFileConstants.AccDeprecated;
114     
115     /**
116      * Bridge method property flag (added in J2SE 1.5). Used to flag a compiler-generated
117      * bridge methods.
118      * See The Java Virtual Machine Specification for more details.
119      * @since 3.0
120      */

121     public static final int AccBridge = ClassFileConstants.AccBridge;
122
123     /**
124      * Varargs method property flag (added in J2SE 1.5).
125      * Used to flag variable arity method declarations.
126      * See The Java Virtual Machine Specification for more details.
127      * @since 3.0
128      */

129     public static final int AccVarargs = ClassFileConstants.AccVarargs;
130
131     /**
132      * Enum property flag (added in J2SE 1.5).
133      * See The Java Virtual Machine Specification for more details.
134      * @since 3.0
135      */

136     public static final int AccEnum = ClassFileConstants.AccEnum;
137
138     /**
139      * Annotation property flag (added in J2SE 1.5).
140      * See The Java Virtual Machine Specification for more details.
141      * @since 3.0
142      */

143     public static final int AccAnnotation = ClassFileConstants.AccAnnotation;
144
145     /**
146      * Not instantiable.
147      */

148     private Flags() {
149         // Not instantiable
150
}
151     /**
152      * Returns whether the given integer includes the <code>abstract</code> modifier.
153      *
154      * @param flags the flags
155      * @return <code>true</code> if the <code>abstract</code> modifier is included
156      */

157     public static boolean isAbstract(int flags) {
158         return (flags & AccAbstract) != 0;
159     }
160     /**
161      * Returns whether the given integer includes the indication that the
162      * element is deprecated (<code>@deprecated</code> tag in Javadoc comment).
163      *
164      * @param flags the flags
165      * @return <code>true</code> if the element is marked as deprecated
166      */

167     public static boolean isDeprecated(int flags) {
168         return (flags & AccDeprecated) != 0;
169     }
170     /**
171      * Returns whether the given integer includes the <code>final</code> modifier.
172      *
173      * @param flags the flags
174      * @return <code>true</code> if the <code>final</code> modifier is included
175      */

176     public static boolean isFinal(int flags) {
177         return (flags & AccFinal) != 0;
178     }
179     /**
180      * Returns whether the given integer includes the <code>interface</code> modifier.
181      *
182      * @param flags the flags
183      * @return <code>true</code> if the <code>interface</code> modifier is included
184      * @since 2.0
185      */

186     public static boolean isInterface(int flags) {
187         return (flags & AccInterface) != 0;
188     }
189     /**
190      * Returns whether the given integer includes the <code>native</code> modifier.
191      *
192      * @param flags the flags
193      * @return <code>true</code> if the <code>native</code> modifier is included
194      */

195     public static boolean isNative(int flags) {
196         return (flags & AccNative) != 0;
197     }
198     /**
199      * Returns whether the given integer does not include one of the
200      * <code>public</code>, <code>private</code>, or <code>protected</code> flags.
201      *
202      * @param flags the flags
203      * @return <code>true</code> if no visibility flag is set
204      * @since 3.2
205      */

206     public static boolean isPackageDefault(int flags) {
207         return (flags & (AccPublic | AccPrivate | AccProtected)) == 0;
208     }
209     /**
210      * Returns whether the given integer includes the <code>private</code> modifier.
211      *
212      * @param flags the flags
213      * @return <code>true</code> if the <code>private</code> modifier is included
214      */

215     public static boolean isPrivate(int flags) {
216         return (flags & AccPrivate) != 0;
217     }
218     /**
219      * Returns whether the given integer includes the <code>protected</code> modifier.
220      *
221      * @param flags the flags
222      * @return <code>true</code> if the <code>protected</code> modifier is included
223      */

224     public static boolean isProtected(int flags) {
225         return (flags & AccProtected) != 0;
226     }
227     /**
228      * Returns whether the given integer includes the <code>public</code> modifier.
229      *
230      * @param flags the flags
231      * @return <code>true</code> if the <code>public</code> modifier is included
232      */

233     public static boolean isPublic(int flags) {
234         return (flags & AccPublic) != 0;
235     }
236     /**
237      * Returns whether the given integer includes the <code>static</code> modifier.
238      *
239      * @param flags the flags
240      * @return <code>true</code> if the <code>static</code> modifier is included
241      */

242     public static boolean isStatic(int flags) {
243         return (flags & AccStatic) != 0;
244     }
245     /**
246      * Returns whether the given integer includes the <code>super</code> modifier.
247      *
248      * @param flags the flags
249      * @return <code>true</code> if the <code>super</code> modifier is included
250      * @since 3.2
251      */

252     public static boolean isSuper(int flags) {
253         return (flags & AccSuper) != 0;
254     }
255     /**
256      * Returns whether the given integer includes the <code>strictfp</code> modifier.
257      *
258      * @param flags the flags
259      * @return <code>true</code> if the <code>strictfp</code> modifier is included
260      */

261     public static boolean isStrictfp(int flags) {
262         return (flags & AccStrictfp) != 0;
263     }
264     /**
265      * Returns whether the given integer includes the <code>synchronized</code> modifier.
266      *
267      * @param flags the flags
268      * @return <code>true</code> if the <code>synchronized</code> modifier is included
269      */

270     public static boolean isSynchronized(int flags) {
271         return (flags & AccSynchronized) != 0;
272     }
273     /**
274      * Returns whether the given integer includes the indication that the
275      * element is synthetic.
276      *
277      * @param flags the flags
278      * @return <code>true</code> if the element is marked synthetic
279      */

280     public static boolean isSynthetic(int flags) {
281         return (flags & AccSynthetic) != 0;
282     }
283     /**
284      * Returns whether the given integer includes the <code>transient</code> modifier.
285      *
286      * @param flags the flags
287      * @return <code>true</code> if the <code>transient</code> modifier is included
288      */

289     public static boolean isTransient(int flags) {
290         return (flags & AccTransient) != 0;
291     }
292     /**
293      * Returns whether the given integer includes the <code>volatile</code> modifier.
294      *
295      * @param flags the flags
296      * @return <code>true</code> if the <code>volatile</code> modifier is included
297      */

298     public static boolean isVolatile(int flags) {
299         return (flags & AccVolatile) != 0;
300     }
301     
302     /**
303      * Returns whether the given integer has the <code>AccBridge</code>
304      * bit set.
305      *
306      * @param flags the flags
307      * @return <code>true</code> if the <code>AccBridge</code> flag is included
308      * @see #AccBridge
309      * @since 3.0
310      */

311     public static boolean isBridge(int flags) {
312         return (flags & AccBridge) != 0;
313     }
314     
315     /**
316      * Returns whether the given integer has the <code>AccVarargs</code>
317      * bit set.
318      *
319      * @param flags the flags
320      * @return <code>true</code> if the <code>AccVarargs</code> flag is included
321      * @see #AccVarargs
322      * @since 3.0
323      */

324     public static boolean isVarargs(int flags) {
325         return (flags & AccVarargs) != 0;
326     }
327     
328     /**
329      * Returns whether the given integer has the <code>AccEnum</code>
330      * bit set.
331      *
332      * @param flags the flags
333      * @return <code>true</code> if the <code>AccEnum</code> flag is included
334      * @see #AccEnum
335      * @since 3.0
336      */

337     public static boolean isEnum(int flags) {
338         return (flags & AccEnum) != 0;
339     }
340     
341     /**
342      * Returns whether the given integer has the <code>AccAnnotation</code>
343      * bit set.
344      *
345      * @param flags the flags
346      * @return <code>true</code> if the <code>AccAnnotation</code> flag is included
347      * @see #AccAnnotation
348      * @since 3.0
349      */

350     public static boolean isAnnotation(int flags) {
351         return (flags & AccAnnotation) != 0;
352     }
353     
354     /**
355      * Returns a standard string describing the given modifier flags.
356      * Only modifier flags are included in the output; deprecated,
357      * synthetic, bridge, etc. flags are ignored.
358      * <p>
359      * The flags are output in the following order:
360      * <pre>
361      * <code>public</code> <code>protected</code> <code>private</code>
362      * <code>static</code>
363      * <code>abstract</code> <code>final</code> <code>native</code> <code>synchronized</code> <code>transient</code> <code>volatile</code> <code>strictfp</code>
364      * </pre>
365      * This is a compromise between the orders specified in sections 8.1.1,
366      * 8.3.1, 8.4.3, 8.8.3, 9.1.1, and 9.3 of <em>The Java Language
367      * Specification, Second Edition</em> (JLS2).
368      * </p>
369      * <p>
370      * Note that the flags of a method can include the AccVarargs flag that has no standard description. Since the AccVarargs flag has the same value as
371      * the AccTransient flag (valid for fields only), attempting to get the description of method modifiers with the AccVarargs flag set would result in an
372      * unexpected description. Clients should ensure that the AccVarargs is not included in the flags of a method as follows:
373      * <pre>
374      * IMethod method = ...
375      * int flags = method.getFlags() & ~Flags.AccVarargs;
376      * return Flags.toString(flags);
377      * </pre>
378      * </p>
379      * <p>
380      * Examples results:
381      * <pre>
382      * <code>"public static final"</code>
383      * <code>"private native"</code>
384      * </pre>
385      * </p>
386      *
387      * @param flags the flags
388      * @return the standard string representation of the given flags
389      */

390     public static String JavaDoc toString(int flags) {
391         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
392
393         if (isPublic(flags))
394             sb.append("public "); //$NON-NLS-1$
395
if (isProtected(flags))
396             sb.append("protected "); //$NON-NLS-1$
397
if (isPrivate(flags))
398             sb.append("private "); //$NON-NLS-1$
399
if (isStatic(flags))
400             sb.append("static "); //$NON-NLS-1$
401
if (isAbstract(flags))
402             sb.append("abstract "); //$NON-NLS-1$
403
if (isFinal(flags))
404             sb.append("final "); //$NON-NLS-1$
405
if (isNative(flags))
406             sb.append("native "); //$NON-NLS-1$
407
if (isSynchronized(flags))
408             sb.append("synchronized "); //$NON-NLS-1$
409
if (isTransient(flags))
410             sb.append("transient "); //$NON-NLS-1$
411
if (isVolatile(flags))
412             sb.append("volatile "); //$NON-NLS-1$
413
if (isStrictfp(flags))
414             sb.append("strictfp "); //$NON-NLS-1$
415

416         int len = sb.length();
417         if (len == 0)
418             return ""; //$NON-NLS-1$
419
sb.setLength(len - 1);
420         return sb.toString();
421     }
422 }
423
Popular Tags