• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  * Copyright (c) 1996, 2008, Oracle and/or its affiliates. All rights reserved.
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This code is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 only, as
8  * published by the Free Software Foundation.  Oracle designates this
9  * particular file as subject to the "Classpath" exception as provided
10  * by Oracle in the LICENSE file that accompanied this code.
11  *
12  * This code is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15  * version 2 for more details (a copy is included in the LICENSE file that
16  * accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License version
19  * 2 along with this work; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21  *
22  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
23  * or visit www.oracle.com if you need additional information or have any
24  * questions.
25  */
26 
27 package java.lang.reflect;
28 
29 /**
30  * The Modifier class provides {@code static} methods and
31  * constants to decode class and member access modifiers.  The sets of
32  * modifiers are represented as integers with distinct bit positions
33  * representing different modifiers.  The values for the constants
34  * representing the modifiers are taken from the tables in sections 4.1, 4.4, 4.5, and 4.7 of
35  * <cite>The Java&trade; Virtual Machine Specification</cite>.
36  *
37  * @see Class#getModifiers()
38  * @see Member#getModifiers()
39  *
40  * @author Nakul Saraiya
41  * @author Kenneth Russell
42  */
43 public
44 class Modifier {
45 
46     /**
47      * Return {@code true} if the integer argument includes the
48      * {@code public} modifier, {@code false} otherwise.
49      *
50      * @param   mod a set of modifiers
51      * @return {@code true} if {@code mod} includes the
52      * {@code public} modifier; {@code false} otherwise.
53      */
isPublic(int mod)54     public static boolean isPublic(int mod) {
55         return (mod & PUBLIC) != 0;
56     }
57 
58     /**
59      * Return {@code true} if the integer argument includes the
60      * {@code private} modifier, {@code false} otherwise.
61      *
62      * @param   mod a set of modifiers
63      * @return {@code true} if {@code mod} includes the
64      * {@code private} modifier; {@code false} otherwise.
65      */
isPrivate(int mod)66     public static boolean isPrivate(int mod) {
67         return (mod & PRIVATE) != 0;
68     }
69 
70     /**
71      * Return {@code true} if the integer argument includes the
72      * {@code protected} modifier, {@code false} otherwise.
73      *
74      * @param   mod a set of modifiers
75      * @return {@code true} if {@code mod} includes the
76      * {@code protected} modifier; {@code false} otherwise.
77      */
isProtected(int mod)78     public static boolean isProtected(int mod) {
79         return (mod & PROTECTED) != 0;
80     }
81 
82     /**
83      * Return {@code true} if the integer argument includes the
84      * {@code static} modifier, {@code false} otherwise.
85      *
86      * @param   mod a set of modifiers
87      * @return {@code true} if {@code mod} includes the
88      * {@code static} modifier; {@code false} otherwise.
89      */
isStatic(int mod)90     public static boolean isStatic(int mod) {
91         return (mod & STATIC) != 0;
92     }
93 
94     /**
95      * Return {@code true} if the integer argument includes the
96      * {@code final} modifier, {@code false} otherwise.
97      *
98      * @param   mod a set of modifiers
99      * @return {@code true} if {@code mod} includes the
100      * {@code final} modifier; {@code false} otherwise.
101      */
isFinal(int mod)102     public static boolean isFinal(int mod) {
103         return (mod & FINAL) != 0;
104     }
105 
106     /**
107      * Return {@code true} if the integer argument includes the
108      * {@code synchronized} modifier, {@code false} otherwise.
109      *
110      * @param   mod a set of modifiers
111      * @return {@code true} if {@code mod} includes the
112      * {@code synchronized} modifier; {@code false} otherwise.
113      */
isSynchronized(int mod)114     public static boolean isSynchronized(int mod) {
115         return (mod & SYNCHRONIZED) != 0;
116     }
117 
118     /**
119      * Return {@code true} if the integer argument includes the
120      * {@code volatile} modifier, {@code false} otherwise.
121      *
122      * @param   mod a set of modifiers
123      * @return {@code true} if {@code mod} includes the
124      * {@code volatile} modifier; {@code false} otherwise.
125      */
isVolatile(int mod)126     public static boolean isVolatile(int mod) {
127         return (mod & VOLATILE) != 0;
128     }
129 
130     /**
131      * Returns true if the given modifiers contain {@link Modifier#CONSTRUCTOR}.
132      * @hide
133      */
isConstructor(int modifiers)134     public static boolean isConstructor(int modifiers) {
135         return ((modifiers & Modifier.CONSTRUCTOR) != 0);
136     }
137 
138     /**
139      * Return {@code true} if the integer argument includes the
140      * {@code transient} modifier, {@code false} otherwise.
141      *
142      * @param   mod a set of modifiers
143      * @return {@code true} if {@code mod} includes the
144      * {@code transient} modifier; {@code false} otherwise.
145      */
isTransient(int mod)146     public static boolean isTransient(int mod) {
147         return (mod & TRANSIENT) != 0;
148     }
149 
150     /**
151      * Return {@code true} if the integer argument includes the
152      * {@code native} modifier, {@code false} otherwise.
153      *
154      * @param   mod a set of modifiers
155      * @return {@code true} if {@code mod} includes the
156      * {@code native} modifier; {@code false} otherwise.
157      */
isNative(int mod)158     public static boolean isNative(int mod) {
159         return (mod & NATIVE) != 0;
160     }
161 
162     /**
163      * Return {@code true} if the integer argument includes the
164      * {@code interface} modifier, {@code false} otherwise.
165      *
166      * @param   mod a set of modifiers
167      * @return {@code true} if {@code mod} includes the
168      * {@code interface} modifier; {@code false} otherwise.
169      */
isInterface(int mod)170     public static boolean isInterface(int mod) {
171         return (mod & INTERFACE) != 0;
172     }
173 
174     /**
175      * Return {@code true} if the integer argument includes the
176      * {@code abstract} modifier, {@code false} otherwise.
177      *
178      * @param   mod a set of modifiers
179      * @return {@code true} if {@code mod} includes the
180      * {@code abstract} modifier; {@code false} otherwise.
181      */
isAbstract(int mod)182     public static boolean isAbstract(int mod) {
183         return (mod & ABSTRACT) != 0;
184     }
185 
186     /**
187      * Return {@code true} if the integer argument includes the
188      * {@code strictfp} modifier, {@code false} otherwise.
189      *
190      * @param   mod a set of modifiers
191      * @return {@code true} if {@code mod} includes the
192      * {@code strictfp} modifier; {@code false} otherwise.
193      */
isStrict(int mod)194     public static boolean isStrict(int mod) {
195         return (mod & STRICT) != 0;
196     }
197 
198     /**
199      * Return a string describing the access modifier flags in
200      * the specified modifier. For example:
201      * <blockquote><pre>
202      *    public final synchronized strictfp
203      * </pre></blockquote>
204      * The modifier names are returned in an order consistent with the
205      * suggested modifier orderings given in sections 8.1.1, 8.3.1, 8.4.3, 8.8.3, and 9.1.1 of
206      * <cite>The Java&trade; Language Specification</cite>.
207      * The full modifier ordering used by this method is:
208      * <blockquote> {@code
209      * public protected private abstract static final transient
210      * volatile synchronized native strictfp
211      * interface } </blockquote>
212      * The {@code interface} modifier discussed in this class is
213      * not a true modifier in the Java language and it appears after
214      * all other modifiers listed by this method.  This method may
215      * return a string of modifiers that are not valid modifiers of a
216      * Java entity; in other words, no checking is done on the
217      * possible validity of the combination of modifiers represented
218      * by the input.
219      *
220      * Note that to perform such checking for a known kind of entity,
221      * such as a constructor or method, first AND the argument of
222      * {@code toString} with the appropriate mask from a method like
223      * {@link #constructorModifiers} or {@link #methodModifiers}.
224      *
225      * @param   mod a set of modifiers
226      * @return  a string representation of the set of modifiers
227      * represented by {@code mod}
228      */
toString(int mod)229     public static String toString(int mod) {
230         StringBuffer sb = new StringBuffer();
231         int len;
232 
233         if ((mod & PUBLIC) != 0)        sb.append("public ");
234         if ((mod & PROTECTED) != 0)     sb.append("protected ");
235         if ((mod & PRIVATE) != 0)       sb.append("private ");
236 
237         /* Canonical order */
238         if ((mod & ABSTRACT) != 0)      sb.append("abstract ");
239         if ((mod & STATIC) != 0)        sb.append("static ");
240         if ((mod & FINAL) != 0)         sb.append("final ");
241         if ((mod & TRANSIENT) != 0)     sb.append("transient ");
242         if ((mod & VOLATILE) != 0)      sb.append("volatile ");
243         if ((mod & SYNCHRONIZED) != 0)  sb.append("synchronized ");
244         if ((mod & NATIVE) != 0)        sb.append("native ");
245         if ((mod & STRICT) != 0)        sb.append("strictfp ");
246         if ((mod & INTERFACE) != 0)     sb.append("interface ");
247 
248         if ((len = sb.length()) > 0)    /* trim trailing space */
249             return sb.toString().substring(0, len-1);
250         return "";
251     }
252 
253     /*
254      * Access modifier flag constants from tables 4.1, 4.4, 4.5, and 4.7 of
255      * <cite>The Java&trade; Virtual Machine Specification</cite>
256      */
257 
258     /**
259      * The {@code int} value representing the {@code public}
260      * modifier.
261      */
262     public static final int PUBLIC           = 0x00000001;
263 
264     /**
265      * The {@code int} value representing the {@code private}
266      * modifier.
267      */
268     public static final int PRIVATE          = 0x00000002;
269 
270     /**
271      * The {@code int} value representing the {@code protected}
272      * modifier.
273      */
274     public static final int PROTECTED        = 0x00000004;
275 
276     /**
277      * The {@code int} value representing the {@code static}
278      * modifier.
279      */
280     public static final int STATIC           = 0x00000008;
281 
282     /**
283      * The {@code int} value representing the {@code final}
284      * modifier.
285      */
286     public static final int FINAL            = 0x00000010;
287 
288     /**
289      * The {@code int} value representing the {@code synchronized}
290      * modifier.
291      */
292     public static final int SYNCHRONIZED     = 0x00000020;
293 
294     /**
295      * The {@code int} value representing the {@code volatile}
296      * modifier.
297      */
298     public static final int VOLATILE         = 0x00000040;
299 
300     /**
301      * The {@code int} value representing the {@code transient}
302      * modifier.
303      */
304     public static final int TRANSIENT        = 0x00000080;
305 
306     /**
307      * The {@code int} value representing the {@code native}
308      * modifier.
309      */
310     public static final int NATIVE           = 0x00000100;
311 
312     /**
313      * The {@code int} value representing the {@code interface}
314      * modifier.
315      */
316     public static final int INTERFACE        = 0x00000200;
317 
318     /**
319      * The {@code int} value representing the {@code abstract}
320      * modifier.
321      */
322     public static final int ABSTRACT         = 0x00000400;
323 
324     /**
325      * The {@code int} value representing the {@code strictfp}
326      * modifier.
327      */
328     public static final int STRICT           = 0x00000800;
329 
330     // Bits not (yet) exposed in the public API either because they
331     // have different meanings for fields and methods and there is no
332     // way to distinguish between the two in this class, or because
333     // they are not Java programming language keywords
334     static final int BRIDGE    = 0x00000040;
335     static final int VARARGS   = 0x00000080;
336     /**
337      * @hide
338      */
339     public static final int SYNTHETIC = 0x00001000;
340     static final int ANNOTATION= 0x00002000;
341     static final int ENUM      = 0x00004000;
isSynthetic(int mod)342     static boolean isSynthetic(int mod) {
343       return (mod & SYNTHETIC) != 0;
344     }
345 
346     /**
347      * Dex addition to mark instance constructors and static class
348      * initializer methods.
349      * @hide
350      */
351     public static final int CONSTRUCTOR = 0x10000;
352 
353     /**
354      * Default methods are marked with a synthetic access flag
355      * to speed up class loading and invocation target lookup.
356      * Implies INTERFACE, not-ABSTRACT, and not-STATIC.
357      *
358      * @hide
359      */
360     public static final int DEFAULT = 0x00400000;
361 
362     /**
363      * See JLSv3 section 8.1.1.
364      */
365     private static final int CLASS_MODIFIERS =
366         Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE |
367         Modifier.ABSTRACT       | Modifier.STATIC       | Modifier.FINAL   |
368         Modifier.STRICT;
369 
370     /**
371      * See JLSv3 section 9.1.1.
372      */
373     private static final int INTERFACE_MODIFIERS =
374         Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE |
375         Modifier.ABSTRACT       | Modifier.STATIC       | Modifier.STRICT;
376 
377 
378     /**
379      * See JLSv3 section 8.8.3.
380      */
381     private static final int CONSTRUCTOR_MODIFIERS =
382         Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE;
383 
384     /**
385      * See JLSv3 section 8.4.3.
386      */
387     private static final int METHOD_MODIFIERS =
388         Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE |
389         Modifier.ABSTRACT       | Modifier.STATIC       | Modifier.FINAL   |
390         Modifier.SYNCHRONIZED   | Modifier.NATIVE       | Modifier.STRICT;
391 
392     /**
393      * See JLSv3 section 8.3.1.
394      */
395     private static final int FIELD_MODIFIERS =
396         Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE |
397         Modifier.STATIC         | Modifier.FINAL        | Modifier.TRANSIENT |
398         Modifier.VOLATILE;
399 
400     /**
401      * Return an {@code int} value OR-ing together the source language
402      * modifiers that can be applied to a class.
403      * @return an {@code int} value OR-ing together the source language
404      * modifiers that can be applied to a class.
405      *
406      * @jls 8.1.1 Class Modifiers
407      * @since 1.7
408      */
classModifiers()409     public static int classModifiers() {
410         return CLASS_MODIFIERS;
411     }
412 
413     /**
414      * Return an {@code int} value OR-ing together the source language
415      * modifiers that can be applied to an interface.
416      * @return an {@code int} value OR-ing together the source language
417      * modifiers that can be applied to an inteface.
418      *
419      * @jls 9.1.1 Interface Modifiers
420      * @since 1.7
421      */
interfaceModifiers()422     public static int interfaceModifiers() {
423         return INTERFACE_MODIFIERS;
424     }
425 
426     /**
427      * Return an {@code int} value OR-ing together the source language
428      * modifiers that can be applied to a constructor.
429      * @return an {@code int} value OR-ing together the source language
430      * modifiers that can be applied to a constructor.
431      *
432      * @jls 8.8.3 Constructor Modifiers
433      * @since 1.7
434      */
constructorModifiers()435     public static int constructorModifiers() {
436         return CONSTRUCTOR_MODIFIERS;
437     }
438 
439     /**
440      * Return an {@code int} value OR-ing together the source language
441      * modifiers that can be applied to a method.
442      * @return an {@code int} value OR-ing together the source language
443      * modifiers that can be applied to a method.
444      *
445      * @jls 8.4.3 Method Modifiers
446      * @since 1.7
447      */
methodModifiers()448     public static int methodModifiers() {
449         return METHOD_MODIFIERS;
450     }
451 
452 
453     /**
454      * Return an {@code int} value OR-ing together the source language
455      * modifiers that can be applied to a field.
456      * @return an {@code int} value OR-ing together the source language
457      * modifiers that can be applied to a field.
458      *
459      * @jls 8.3.1 Field Modifiers
460      * @since 1.7
461      */
fieldModifiers()462     public static int fieldModifiers() {
463         return FIELD_MODIFIERS;
464     }
465 }
466