KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > lang > reflect > Modifier


1 /*
2  * @(#)Modifier.java 1.27 04/02/17
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.lang.reflect;
9
10 import java.security.AccessController JavaDoc;
11 import sun.reflect.LangReflectAccess;
12 import sun.reflect.ReflectionFactory;
13
14 /**
15  * The Modifier class provides <code>static</code> methods and
16  * constants to decode class and member access modifiers. The sets of
17  * modifiers are represented as integers with distinct bit positions
18  * representing different modifiers. The values for the constants
19  * representing the modifiers are taken from <a
20  * HREF="http://java.sun.com/docs/books/vmspec/2nd-edition/html/VMSpecTOC.doc.html"><i>The
21  * Java</i><sup><small>TM</small></sup> <i>Virtual Machine Specification, Second
22  * edition</i></a> tables
23  * <a HREF="http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#75734">4.1</a>,
24  * <a HREF="http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#88358">4.4</a>,
25  * <a HREF="http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#75568">4.5</a>, and
26  * <a HREF="http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#88478">4.7</a>.
27  *
28  * @see Class#getModifiers()
29  * @see Member#getModifiers()
30  *
31  * @author Nakul Saraiya
32  * @author Kenneth Russell
33  */

34 public
35 class Modifier {
36
37     /*
38      * Bootstrapping protocol between java.lang and java.lang.reflect
39      * packages
40      */

41     static {
42         sun.reflect.ReflectionFactory factory =
43             (sun.reflect.ReflectionFactory) AccessController.doPrivileged(
44                 new ReflectionFactory.GetReflectionFactoryAction()
45             );
46         factory.setLangReflectAccess(new java.lang.reflect.ReflectAccess JavaDoc());
47     }
48
49     /**
50      * Return <tt>true</tt> if the integer argument includes the
51      * <tt>public</tt> modifier, <tt>false</tt> otherwise.
52      *
53      * @param mod a set of modifiers
54      * @return <tt>true</tt> if <code>mod</code> includes the
55      * <tt>public</tt> modifier; <tt>false</tt> otherwise.
56      */

57     public static boolean isPublic(int mod) {
58     return (mod & PUBLIC) != 0;
59     }
60
61     /**
62      * Return <tt>true</tt> if the integer argument includes the
63      * <tt>private</tt> modifier, <tt>false</tt> otherwise.
64      *
65      * @param mod a set of modifiers
66      * @return <tt>true</tt> if <code>mod</code> includes the
67      * <tt>private</tt> modifier; <tt>false</tt> otherwise.
68      */

69     public static boolean isPrivate(int mod) {
70     return (mod & PRIVATE) != 0;
71     }
72
73     /**
74      * Return <tt>true</tt> if the integer argument includes the
75      * <tt>protected</tt> modifier, <tt>false</tt> otherwise.
76      *
77      * @param mod a set of modifiers
78      * @return <tt>true</tt> if <code>mod</code> includes the
79      * <tt>protected</tt> modifier; <tt>false</tt> otherwise.
80      */

81     public static boolean isProtected(int mod) {
82     return (mod & PROTECTED) != 0;
83     }
84
85     /**
86      * Return <tt>true</tt> if the integer argument includes the
87      * <tt>static</tt> modifier, <tt>false</tt> otherwise.
88      *
89      * @param mod a set of modifiers
90      * @return <tt>true</tt> if <code>mod</code> includes the
91      * <tt>static</tt> modifier; <tt>false</tt> otherwise.
92      */

93     public static boolean isStatic(int mod) {
94     return (mod & STATIC) != 0;
95     }
96
97     /**
98      * Return <tt>true</tt> if the integer argument includes the
99      * <tt>final</tt> modifier, <tt>false</tt> otherwise.
100      *
101      * @param mod a set of modifiers
102      * @return <tt>true</tt> if <code>mod</code> includes the
103      * <tt>final</tt> modifier; <tt>false</tt> otherwise.
104      */

105     public static boolean isFinal(int mod) {
106     return (mod & FINAL) != 0;
107     }
108
109     /**
110      * Return <tt>true</tt> if the integer argument includes the
111      * <tt>synchronized</tt> modifier, <tt>false</tt> otherwise.
112      *
113      * @param mod a set of modifiers
114      * @return <tt>true</tt> if <code>mod</code> includes the
115      * <tt>synchronized</tt> modifier; <tt>false</tt> otherwise.
116      */

117     public static boolean isSynchronized(int mod) {
118     return (mod & SYNCHRONIZED) != 0;
119     }
120
121     /**
122      * Return <tt>true</tt> if the integer argument includes the
123      * <tt>volatile</tt> modifier, <tt>false</tt> otherwise.
124      *
125      * @param mod a set of modifiers
126      * @return <tt>true</tt> if <code>mod</code> includes the
127      * <tt>volatile</tt> modifier; <tt>false</tt> otherwise.
128      */

129     public static boolean isVolatile(int mod) {
130     return (mod & VOLATILE) != 0;
131     }
132
133     /**
134      * Return <tt>true</tt> if the integer argument includes the
135      * <tt>transient</tt> modifier, <tt>false</tt> otherwise.
136      *
137      * @param mod a set of modifiers
138      * @return <tt>true</tt> if <code>mod</code> includes the
139      * <tt>transient</tt> modifier; <tt>false</tt> otherwise.
140      */

141     public static boolean isTransient(int mod) {
142     return (mod & TRANSIENT) != 0;
143     }
144
145     /**
146      * Return <tt>true</tt> if the integer argument includes the
147      * <tt>native</tt> modifier, <tt>false</tt> otherwise.
148      *
149      * @param mod a set of modifiers
150      * @return <tt>true</tt> if <code>mod</code> includes the
151      * <tt>native</tt> modifier; <tt>false</tt> otherwise.
152      */

153     public static boolean isNative(int mod) {
154     return (mod & NATIVE) != 0;
155     }
156
157     /**
158      * Return <tt>true</tt> if the integer argument includes the
159      * <tt>interface</tt> modifier, <tt>false</tt> otherwise.
160      *
161      * @param mod a set of modifiers
162      * @return <tt>true</tt> if <code>mod</code> includes the
163      * <tt>interface</tt> modifier; <tt>false</tt> otherwise.
164      */

165     public static boolean isInterface(int mod) {
166     return (mod & INTERFACE) != 0;
167     }
168
169     /**
170      * Return <tt>true</tt> if the integer argument includes the
171      * <tt>abstract</tt> modifier, <tt>false</tt> otherwise.
172      *
173      * @param mod a set of modifiers
174      * @return <tt>true</tt> if <code>mod</code> includes the
175      * <tt>abstract</tt> modifier; <tt>false</tt> otherwise.
176      */

177     public static boolean isAbstract(int mod) {
178     return (mod & ABSTRACT) != 0;
179     }
180
181     /**
182      * Return <tt>true</tt> if the integer argument includes the
183      * <tt>strictfp</tt> modifier, <tt>false</tt> otherwise.
184      *
185      * @param mod a set of modifiers
186      * @return <tt>true</tt> if <code>mod</code> includes the
187      * <tt>strictfp</tt> modifier; <tt>false</tt> otherwise.
188      */

189     public static boolean isStrict(int mod) {
190     return (mod & STRICT) != 0;
191     }
192
193     /**
194      * Return a string describing the access modifier flags in
195      * the specified modifier. For example:
196      * <blockquote><pre>
197      * public final synchronized strictfp
198      * </pre></blockquote>
199      * The modifier names are returned in an order consistent with the
200      * suggested modifier orderings given in <a
201      * HREF="http://java.sun.com/docs/books/jls/second_edition/html/j.title.doc.html"><em>The
202      * Java Language Specification, Second Edition</em></a> sections
203      * <a HREF="http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#21613">&sect;8.1.1</a>,
204      * <a HREF="http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#78091">&sect;8.3.1</a>,
205      * <a HREF="http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#78188">&sect;8.4.3</a>,
206      * <a HREF="http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#42018">&sect;8.8.3</a>, and
207      * <a HREF="http://java.sun.com/docs/books/jls/second_edition/html/interfaces.doc.html#235947">&sect;9.1.1</a>.
208      * The full modifier ordering used by this method is:
209      * <blockquote> <code>
210      * public protected private abstract static final transient
211      * volatile synchronized native strictfp
212      * interface </code> </blockquote>
213      * The <code>interface</code> modifier discussed in this class is
214      * not a true modifier in the Java language and it appears after
215      * all other modifiers listed by this method. This method may
216      * return a string of modifiers that are not valid modifiers of a
217      * Java entity; in other words, no checking is done on the
218      * possible validity of the combination of modifiers represented
219      * by the input.
220      *
221      * @param mod a set of modifiers
222      * @return a string representation of the set of modifiers
223      * represented by <code>mod</code>
224      */

225     public static String JavaDoc toString(int mod) {
226     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
227     int len;
228
229     if ((mod & PUBLIC) != 0) sb.append("public ");
230     if ((mod & PROTECTED) != 0) sb.append("protected ");
231     if ((mod & PRIVATE) != 0) sb.append("private ");
232
233     /* Canonical order */
234     if ((mod & ABSTRACT) != 0) sb.append("abstract ");
235     if ((mod & STATIC) != 0) sb.append("static ");
236     if ((mod & FINAL) != 0) sb.append("final ");
237     if ((mod & TRANSIENT) != 0) sb.append("transient ");
238     if ((mod & VOLATILE) != 0) sb.append("volatile ");
239     if ((mod & SYNCHRONIZED) != 0) sb.append("synchronized ");
240     if ((mod & NATIVE) != 0) sb.append("native ");
241     if ((mod & STRICT) != 0) sb.append("strictfp ");
242     if ((mod & INTERFACE) != 0) sb.append("interface ");
243
244     if ((len = sb.length()) > 0) /* trim trailing space */
245         return sb.toString().substring(0, len-1);
246     return "";
247     }
248
249     /*
250      * Access modifier flag constants from <em>The Java Virtual
251      * Machine Specification, Second Edition</em>, tables 4.1, 4.4,
252      * 4.5, and 4.7.
253      */

254
255     /**
256      * The <code>int</code> value representing the <code>public</code>
257      * modifier.
258      */

259     public static final int PUBLIC = 0x00000001;
260
261     /**
262      * The <code>int</code> value representing the <code>private</code>
263      * modifier.
264      */

265     public static final int PRIVATE = 0x00000002;
266
267     /**
268      * The <code>int</code> value representing the <code>protected</code>
269      * modifier.
270      */

271     public static final int PROTECTED = 0x00000004;
272
273     /**
274      * The <code>int</code> value representing the <code>static</code>
275      * modifier.
276      */

277     public static final int STATIC = 0x00000008;
278
279     /**
280      * The <code>int</code> value representing the <code>final</code>
281      * modifier.
282      */

283     public static final int FINAL = 0x00000010;
284
285     /**
286      * The <code>int</code> value representing the <code>synchronized</code>
287      * modifier.
288      */

289     public static final int SYNCHRONIZED = 0x00000020;
290
291     /**
292      * The <code>int</code> value representing the <code>volatile</code>
293      * modifier.
294      */

295     public static final int VOLATILE = 0x00000040;
296
297     /**
298      * The <code>int</code> value representing the <code>transient</code>
299      * modifier.
300      */

301     public static final int TRANSIENT = 0x00000080;
302
303     /**
304      * The <code>int</code> value representing the <code>native</code>
305      * modifier.
306      */

307     public static final int NATIVE = 0x00000100;
308
309     /**
310      * The <code>int</code> value representing the <code>interface</code>
311      * modifier.
312      */

313     public static final int INTERFACE = 0x00000200;
314
315     /**
316      * The <code>int</code> value representing the <code>abstract</code>
317      * modifier.
318      */

319     public static final int ABSTRACT = 0x00000400;
320
321     /**
322      * The <code>int</code> value representing the <code>strictfp</code>
323      * modifier.
324      */

325     public static final int STRICT = 0x00000800;
326
327     // Bits not (yet) exposed in the public API either because they
328
// have different meanings for fields and methods and there is no
329
// way to distinguish between the two in this class, or because
330
// they are not Java programming language keywords
331
static final int BRIDGE = 0x00000040;
332     static final int VARARGS = 0x00000080;
333     static final int SYNTHETIC = 0x00001000;
334     static final int ANNOTATION= 0x00002000;
335     static final int ENUM = 0x00004000;
336     static boolean isSynthetic(int mod) {
337       return (mod & SYNTHETIC) != 0;
338     }
339 }
340
Popular Tags