< prev index next >

src/java.base/share/classes/java/lang/reflect/Modifier.java

Print this page

  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 package java.lang.reflect;
 27 


 28 import java.util.StringJoiner;
 29 
 30 /**
 31  * The Modifier class provides {@code static} methods and
 32  * constants to decode class and member access modifiers.  The sets of
 33  * modifiers are represented as integers with distinct bit positions

 34  * representing different modifiers.  The values for the constants
 35  * representing the modifiers are taken from the tables in sections
 36  * {@jvms 4.1}, {@jvms 4.4}, {@jvms 4.5}, and {@jvms 4.7} of
 37  * <cite>The Java Virtual Machine Specification</cite>.
 38  *
 39  * @apiNote
 40  * Not all modifiers that are syntactic Java language modifiers are
 41  * represented in this class, only those modifiers that <em>also</em>
 42  * have a corresponding JVM {@linkplain AccessFlag access flag} are
 43  * included. In particular the {@code default} method modifier (JLS
 44  * {@jls 9.4.3}) and the {@code sealed} and {@code non-sealed} class
 45  * (JLS {@jls 8.1.1.2}) and interface (JLS {@jls 9.1.1.4}) modifiers
 46  * are <em>not</em> represented in this class.
 47  *
 48  * @see Class#getModifiers()
 49  * @see Member#getModifiers()
 50  *
 51  * @author Nakul Saraiya
 52  * @author Kenneth Russell
 53  * @since 1.1
 54  */
 55 public class Modifier {
 56     /**
 57      * Do not call.
 58      */
 59     private Modifier() {throw new AssertionError();}
 60 
 61 
 62     /**
 63      * Return {@code true} if the integer argument includes the
 64      * {@code public} modifier, {@code false} otherwise.

106     public static boolean isStatic(int mod) {
107         return (mod & STATIC) != 0;
108     }
109 
110     /**
111      * Return {@code true} if the integer argument includes the
112      * {@code final} modifier, {@code false} otherwise.
113      *
114      * @param   mod a set of modifiers
115      * @return {@code true} if {@code mod} includes the
116      * {@code final} modifier; {@code false} otherwise.
117      */
118     public static boolean isFinal(int mod) {
119         return (mod & FINAL) != 0;
120     }
121 
122     /**
123      * Return {@code true} if the integer argument includes the
124      * {@code synchronized} modifier, {@code false} otherwise.
125      *



126      * @param   mod a set of modifiers
127      * @return {@code true} if {@code mod} includes the
128      * {@code synchronized} modifier; {@code false} otherwise.
129      */
130     public static boolean isSynchronized(int mod) {
131         return (mod & SYNCHRONIZED) != 0;
132     }
133 


















134     /**
135      * Return {@code true} if the integer argument includes the
136      * {@code volatile} modifier, {@code false} otherwise.
137      *
138      * @param   mod a set of modifiers
139      * @return {@code true} if {@code mod} includes the
140      * {@code volatile} modifier; {@code false} otherwise.
141      */
142     public static boolean isVolatile(int mod) {
143         return (mod & VOLATILE) != 0;
144     }
145 
146     /**
147      * Return {@code true} if the integer argument includes the
148      * {@code transient} modifier, {@code false} otherwise.
149      *
150      * @param   mod a set of modifiers
151      * @return {@code true} if {@code mod} includes the
152      * {@code transient} modifier; {@code false} otherwise.
153      */

190     public static boolean isAbstract(int mod) {
191         return (mod & ABSTRACT) != 0;
192     }
193 
194     /**
195      * Return {@code true} if the integer argument includes the
196      * {@code strictfp} modifier, {@code false} otherwise.
197      *
198      * @param   mod a set of modifiers
199      * @return {@code true} if {@code mod} includes the
200      * {@code strictfp} modifier; {@code false} otherwise.
201      */
202     public static boolean isStrict(int mod) {
203         return (mod & STRICT) != 0;
204     }
205 
206     /**
207      * Return a string describing the access modifier flags in
208      * the specified modifier. For example:
209      * <blockquote><pre>
210      *    public final synchronized strictfp
211      * </pre></blockquote>
212      * The modifier names are returned in an order consistent with the
213      * suggested modifier orderings given in sections 8.1.1, 8.3.1, 8.4.3, 8.8.3, and 9.1.1 of
214      * <cite>The Java Language Specification</cite>.
215      * The full modifier ordering used by this method is:
216      * <blockquote> {@code
217      * public protected private abstract static final transient
218      * volatile synchronized native strictfp
219      * interface } </blockquote>
220      *
221      * The {@code interface} modifier discussed in this class is
222      * not a true modifier in the Java language and it appears after
223      * all other modifiers listed by this method.  This method may
224      * return a string of modifiers that are not valid modifiers of a
225      * Java entity; in other words, no checking is done on the
226      * possible validity of the combination of modifiers represented
227      * by the input.
228      *
229      * Note that to perform such checking for a known kind of entity,
230      * such as a constructor or method, first AND the argument of

302      * The {@code int} value representing the {@code static}
303      * modifier.
304      * @see AccessFlag#STATIC
305      */
306     public static final int STATIC           = 0x00000008;
307 
308     /**
309      * The {@code int} value representing the {@code final}
310      * modifier.
311      * @see AccessFlag#FINAL
312      */
313     public static final int FINAL            = 0x00000010;
314 
315     /**
316      * The {@code int} value representing the {@code synchronized}
317      * modifier.
318      * @see AccessFlag#SYNCHRONIZED
319      */
320     public static final int SYNCHRONIZED     = 0x00000020;
321 









322     /**
323      * The {@code int} value representing the {@code volatile}
324      * modifier.
325      * @see AccessFlag#VOLATILE
326      */
327     public static final int VOLATILE         = 0x00000040;
328 
329     /**
330      * The {@code int} value representing the {@code transient}
331      * modifier.
332      * @see AccessFlag#TRANSIENT
333      */
334     public static final int TRANSIENT        = 0x00000080;
335 
336     /**
337      * The {@code int} value representing the {@code native}
338      * modifier.
339      * @see AccessFlag#NATIVE
340      */
341     public static final int NATIVE           = 0x00000100;
342 
343     /**
344      * The {@code int} value representing the {@code interface}
345      * modifier.
346      * @see AccessFlag#INTERFACE
347      */
348     public static final int INTERFACE        = 0x00000200;
349 
350     /**
351      * The {@code int} value representing the {@code abstract}
352      * modifier.
353      * @see AccessFlag#ABSTRACT
354      */
355     public static final int ABSTRACT         = 0x00000400;
356 
357     /**
358      * The {@code int} value representing the {@code strictfp}
359      * modifier.
360      * @see AccessFlag#STRICT
361      */
362     public static final int STRICT           = 0x00000800;
363 
364     // Bits not (yet) exposed in the public API either because they
365     // have different meanings for fields and methods and there is no
366     // way to distinguish between the two in this class, or because
367     // they are not Java programming language keywords
368     static final int BRIDGE    = 0x00000040;
369     static final int VARARGS   = 0x00000080;
370     static final int SYNTHETIC = 0x00001000;
371     static final int ANNOTATION  = 0x00002000;
372     static final int ENUM      = 0x00004000;
373     static final int MANDATED  = 0x00008000;
374     static boolean isSynthetic(int mod) {
375       return (mod & SYNTHETIC) != 0;
376     }
377 
378     static boolean isMandated(int mod) {
379       return (mod & MANDATED) != 0;
380     }
381 
382     // Note on the FOO_MODIFIERS fields and fooModifiers() methods:
383     // the sets of modifiers are not guaranteed to be constants
384     // across time and Java SE releases. Therefore, it would not be
385     // appropriate to expose an external interface to this information
386     // that would allow the values to be treated as Java-level
387     // constants since the values could be constant folded and updates
388     // to the sets of modifiers missed. Thus, the fooModifiers()
389     // methods return an unchanging values for a given release, but a
390     // value that can potentially change over time.
391 
392     /**
393      * The Java source modifiers that can be applied to a class.
394      * @jls 8.1.1 Class Modifiers
395      */
396     private static final int CLASS_MODIFIERS =
397         Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE |
398         Modifier.ABSTRACT       | Modifier.STATIC       | Modifier.FINAL   |
399         Modifier.STRICT;
400 
401     /**
402      * The Java source modifiers that can be applied to an interface.
403      * @jls 9.1.1 Interface Modifiers
404      */
405     private static final int INTERFACE_MODIFIERS =
406         Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE |
407         Modifier.ABSTRACT       | Modifier.STATIC       | Modifier.STRICT;
408 
409 
410     /**
411      * The Java source modifiers that can be applied to a constructor.
412      * @jls 8.8.3 Constructor Modifiers
413      */
414     private static final int CONSTRUCTOR_MODIFIERS =
415         Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE;
416 
417     /**
418      * The Java source modifiers that can be applied to a method.
419      * @jls 8.4.3  Method Modifiers
420      */
421     private static final int METHOD_MODIFIERS =
422         Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE |
423         Modifier.ABSTRACT       | Modifier.STATIC       | Modifier.FINAL   |
424         Modifier.SYNCHRONIZED   | Modifier.NATIVE       | Modifier.STRICT;
425 
426     /**
427      * The Java source modifiers that can be applied to a field.
428      * @jls 8.3.1 Field Modifiers
429      */
430     private static final int FIELD_MODIFIERS =
431         Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE |
432         Modifier.STATIC         | Modifier.FINAL        | Modifier.TRANSIENT |
433         Modifier.VOLATILE;
434 
435     /**
436      * The Java source modifiers that can be applied to a method or constructor parameter.
437      * @jls 8.4.1 Formal Parameters
438      */
439     private static final int PARAMETER_MODIFIERS =
440         Modifier.FINAL;
441 
442     static final int ACCESS_MODIFIERS =
443         Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE;
444 
445     /**
446      * Return an {@code int} value OR-ing together the source language
447      * modifiers that can be applied to a class.
448      * @return an {@code int} value OR-ing together the source language
449      * modifiers that can be applied to a class.
450      *
451      * @jls 8.1.1 Class Modifiers
452      * @since 1.7
453      */

  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 package java.lang.reflect;
 27 
 28 import jdk.internal.javac.PreviewFeature;
 29 
 30 import java.util.StringJoiner;
 31 
 32 /**
 33  * The Modifier class provides {@code static} methods and
 34  * constants to decode class and member access modifiers.
 35  * The {@link AccessFlag} class should be used instead of this class.
 36  * The sets of modifiers are represented as integers with non-distinct bit positions
 37  * representing different modifiers.  The values for the constants
 38  * representing the modifiers are taken from the tables in sections
 39  * {@jvms 4.1}, {@jvms 4.4}, {@jvms 4.5}, and {@jvms 4.7} of
 40  * <cite>The Java Virtual Machine Specification</cite>.
 41  *
 42  * @apiNote
 43  * Not all modifiers that are syntactic Java language modifiers are
 44  * represented in this class, only those modifiers that <em>also</em>
 45  * have a corresponding JVM {@linkplain AccessFlag access flag} are
 46  * included. In particular, the {@code default} method modifier (JLS
 47  * {@jls 9.4.3}) and the {@code value}, {@code sealed} and {@code non-sealed} class
 48  * (JLS {@jls 8.1.1.2}) and interface (JLS {@jls 9.1.1.4}) modifiers
 49  * are <em>not</em> represented in this class.
 50  *
 51  * @see Class#getModifiers()
 52  * @see Member#getModifiers()
 53  *
 54  * @author Nakul Saraiya
 55  * @author Kenneth Russell
 56  * @since 1.1
 57  */
 58 public class Modifier {
 59     /**
 60      * Do not call.
 61      */
 62     private Modifier() {throw new AssertionError();}
 63 
 64 
 65     /**
 66      * Return {@code true} if the integer argument includes the
 67      * {@code public} modifier, {@code false} otherwise.

109     public static boolean isStatic(int mod) {
110         return (mod & STATIC) != 0;
111     }
112 
113     /**
114      * Return {@code true} if the integer argument includes the
115      * {@code final} modifier, {@code false} otherwise.
116      *
117      * @param   mod a set of modifiers
118      * @return {@code true} if {@code mod} includes the
119      * {@code final} modifier; {@code false} otherwise.
120      */
121     public static boolean isFinal(int mod) {
122         return (mod & FINAL) != 0;
123     }
124 
125     /**
126      * Return {@code true} if the integer argument includes the
127      * {@code synchronized} modifier, {@code false} otherwise.
128      *
129      * @apiNote {@code isSynchronized} should only be called with the modifiers
130      * of a {@linkplain Method#getModifiers() method}.
131      *
132      * @param   mod a set of modifiers
133      * @return {@code true} if {@code mod} includes the
134      * {@code synchronized} modifier; {@code false} otherwise.
135      */
136     public static boolean isSynchronized(int mod) {
137         return (mod & SYNCHRONIZED) != 0;
138     }
139 
140     /**
141      * Return {@code true} if the integer argument includes the
142      * {@code identity} modifier, {@code false} otherwise.
143      *
144      * @apiNote {@code isIdentity} should only be called with the modifiers
145      * of a {@linkplain Class#getModifiers() class}.
146      *
147      * @param   mod a set of modifiers
148      * @return {@code true} if {@code mod} includes the
149      * {@code identity} modifier; {@code false} otherwise.
150      *
151      * @since Valhalla
152      */
153     @PreviewFeature(feature = PreviewFeature.Feature.VALUE_OBJECTS, reflective=true)
154     public static boolean isIdentity(int mod) {
155         return (mod & IDENTITY) != 0;
156     }
157 
158     /**
159      * Return {@code true} if the integer argument includes the
160      * {@code volatile} modifier, {@code false} otherwise.
161      *
162      * @param   mod a set of modifiers
163      * @return {@code true} if {@code mod} includes the
164      * {@code volatile} modifier; {@code false} otherwise.
165      */
166     public static boolean isVolatile(int mod) {
167         return (mod & VOLATILE) != 0;
168     }
169 
170     /**
171      * Return {@code true} if the integer argument includes the
172      * {@code transient} modifier, {@code false} otherwise.
173      *
174      * @param   mod a set of modifiers
175      * @return {@code true} if {@code mod} includes the
176      * {@code transient} modifier; {@code false} otherwise.
177      */

214     public static boolean isAbstract(int mod) {
215         return (mod & ABSTRACT) != 0;
216     }
217 
218     /**
219      * Return {@code true} if the integer argument includes the
220      * {@code strictfp} modifier, {@code false} otherwise.
221      *
222      * @param   mod a set of modifiers
223      * @return {@code true} if {@code mod} includes the
224      * {@code strictfp} modifier; {@code false} otherwise.
225      */
226     public static boolean isStrict(int mod) {
227         return (mod & STRICT) != 0;
228     }
229 
230     /**
231      * Return a string describing the access modifier flags in
232      * the specified modifier. For example:
233      * <blockquote><pre>
234      *    public final synchronized
235      * </pre></blockquote>
236      * The modifier names are returned in an order consistent with the
237      * suggested modifier orderings given in sections 8.1.1, 8.3.1, 8.4.3, 8.8.3, and 9.1.1 of
238      * <cite>The Java Language Specification</cite>.
239      * The full modifier ordering used by this method is:
240      * <blockquote> {@code
241      * public protected private abstract static final transient
242      * volatile synchronized native strictfp
243      * interface } </blockquote>
244      *
245      * The {@code interface} modifier discussed in this class is
246      * not a true modifier in the Java language and it appears after
247      * all other modifiers listed by this method.  This method may
248      * return a string of modifiers that are not valid modifiers of a
249      * Java entity; in other words, no checking is done on the
250      * possible validity of the combination of modifiers represented
251      * by the input.
252      *
253      * Note that to perform such checking for a known kind of entity,
254      * such as a constructor or method, first AND the argument of

326      * The {@code int} value representing the {@code static}
327      * modifier.
328      * @see AccessFlag#STATIC
329      */
330     public static final int STATIC           = 0x00000008;
331 
332     /**
333      * The {@code int} value representing the {@code final}
334      * modifier.
335      * @see AccessFlag#FINAL
336      */
337     public static final int FINAL            = 0x00000010;
338 
339     /**
340      * The {@code int} value representing the {@code synchronized}
341      * modifier.
342      * @see AccessFlag#SYNCHRONIZED
343      */
344     public static final int SYNCHRONIZED     = 0x00000020;
345 
346     /**
347      * The {@code int} value representing the {@code ACC_IDENTITY}
348      * modifier.
349      *
350      * @since Valhalla
351      */
352     @PreviewFeature(feature = PreviewFeature.Feature.VALUE_OBJECTS, reflective=true)
353     public static final int IDENTITY         = 0x00000020;
354 
355     /**
356      * The {@code int} value representing the {@code volatile}
357      * modifier.
358      * @see AccessFlag#VOLATILE
359      */
360     public static final int VOLATILE         = 0x00000040;
361 
362     /**
363      * The {@code int} value representing the {@code transient}
364      * modifier.
365      * @see AccessFlag#TRANSIENT
366      */
367     public static final int TRANSIENT        = 0x00000080;
368 
369     /**
370      * The {@code int} value representing the {@code native}
371      * modifier.
372      * @see AccessFlag#NATIVE
373      */
374     public static final int NATIVE           = 0x00000100;
375 
376     /**
377      * The {@code int} value representing the {@code interface}
378      * modifier.
379      * @see AccessFlag#INTERFACE
380      */
381     public static final int INTERFACE        = 0x00000200;
382 
383     /**
384      * The {@code int} value representing the {@code abstract}
385      * modifier.
386      * @see AccessFlag#ABSTRACT
387      */
388     public static final int ABSTRACT         = 0x00000400;
389 
390     /**
391      * The {@code int} value representing the {@code strictfp}
392      * modifier.
393      * @see AccessFlag#STRICT and AccessFlag#STRICT_FIELD
394      */
395     public static final int STRICT           = 0x00000800;
396 
397     // Bits not (yet) exposed in the public API either because they
398     // have different meanings for fields and methods and there is no
399     // way to distinguish between the two in this class, or because
400     // they are not Java programming language keywords
401     static final int BRIDGE    = 0x00000040;
402     static final int VARARGS   = 0x00000080;
403     static final int SYNTHETIC = 0x00001000;
404     static final int ANNOTATION  = 0x00002000;
405     static final int ENUM      = 0x00004000;
406     static final int MANDATED  = 0x00008000;
407     static boolean isSynthetic(int mod) {
408       return (mod & SYNTHETIC) != 0;
409     }
410 
411     static boolean isMandated(int mod) {
412       return (mod & MANDATED) != 0;
413     }
414 
415     // Note on the FOO_MODIFIERS fields and fooModifiers() methods:
416     // the sets of modifiers are not guaranteed to be constants
417     // across time and Java SE releases. Therefore, it would not be
418     // appropriate to expose an external interface to this information
419     // that would allow the values to be treated as Java-level
420     // constants since the values could be constant folded and updates
421     // to the sets of modifiers missed. Thus, the fooModifiers()
422     // methods return an unchanging values for a given release, but a
423     // value that can potentially change over time.
424 
425     /**
426      * The Java source modifiers that can be applied to a class.
427      * @jls 8.1.1 Class Modifiers
428      */
429     private static final int CLASS_MODIFIERS =
430         Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE |
431         Modifier.ABSTRACT       | Modifier.STATIC       | Modifier.FINAL   |
432         Modifier.IDENTITY       | Modifier.STRICT;
433 
434     /**
435      * The Java source modifiers that can be applied to an interface.
436      * @jls 9.1.1 Interface Modifiers
437      */
438     private static final int INTERFACE_MODIFIERS =
439         Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE |
440         Modifier.ABSTRACT       | Modifier.STATIC       | Modifier.STRICT;
441 
442 
443     /**
444      * The Java source modifiers that can be applied to a constructor.
445      * @jls 8.8.3 Constructor Modifiers
446      */
447     private static final int CONSTRUCTOR_MODIFIERS =
448         Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE;
449 
450     /**
451      * The Java source modifiers that can be applied to a method.
452      * @jls 8.4.3  Method Modifiers
453      */
454     private static final int METHOD_MODIFIERS =
455         Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE |
456         Modifier.ABSTRACT       | Modifier.STATIC       | Modifier.FINAL   |
457         Modifier.SYNCHRONIZED   | Modifier.NATIVE       | Modifier.STRICT;
458 
459     /**
460      * The Java source modifiers that can be applied to a field.
461      * @jls 8.3.1 Field Modifiers
462      */
463     private static final int FIELD_MODIFIERS =
464         Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE |
465         Modifier.STATIC         | Modifier.FINAL        | Modifier.TRANSIENT |
466         Modifier.VOLATILE       | Modifier.STRICT;
467 
468     /**
469      * The Java source modifiers that can be applied to a method or constructor parameter.
470      * @jls 8.4.1 Formal Parameters
471      */
472     private static final int PARAMETER_MODIFIERS =
473         Modifier.FINAL;
474 
475     static final int ACCESS_MODIFIERS =
476         Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE;
477 
478     /**
479      * Return an {@code int} value OR-ing together the source language
480      * modifiers that can be applied to a class.
481      * @return an {@code int} value OR-ing together the source language
482      * modifiers that can be applied to a class.
483      *
484      * @jls 8.1.1 Class Modifiers
485      * @since 1.7
486      */
< prev index next >