< prev index next >

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

Print this page

  1 /*
  2  * Copyright (c) 1996, 2020, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.  Oracle designates this
  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

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












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

237         if ((mod & STATIC) != 0)        sj.add("static");
238         if ((mod & FINAL) != 0)         sj.add("final");
239         if ((mod & TRANSIENT) != 0)     sj.add("transient");
240         if ((mod & VOLATILE) != 0)      sj.add("volatile");
241         if ((mod & SYNCHRONIZED) != 0)  sj.add("synchronized");
242         if ((mod & NATIVE) != 0)        sj.add("native");
243         if ((mod & STRICT) != 0)        sj.add("strictfp");
244         if ((mod & INTERFACE) != 0)     sj.add("interface");
245 
246         return sj.toString();
247     }
248 
249     /*
250      * Access modifier flag constants from tables 4.1, 4.4, 4.5, and 4.7 of
251      * <cite>The Java Virtual Machine Specification</cite>
252      */
253 
254     /**
255      * The {@code int} value representing the {@code public}
256      * modifier.

257      */
258     public static final int PUBLIC           = 0x00000001;
259 
260     /**
261      * The {@code int} value representing the {@code private}
262      * modifier.

263      */
264     public static final int PRIVATE          = 0x00000002;
265 
266     /**
267      * The {@code int} value representing the {@code protected}
268      * modifier.

269      */
270     public static final int PROTECTED        = 0x00000004;
271 
272     /**
273      * The {@code int} value representing the {@code static}
274      * modifier.

275      */
276     public static final int STATIC           = 0x00000008;
277 
278     /**
279      * The {@code int} value representing the {@code final}
280      * modifier.

281      */
282     public static final int FINAL            = 0x00000010;
283 
284     /**
285      * The {@code int} value representing the {@code synchronized}
286      * modifier.

287      */
288     public static final int SYNCHRONIZED     = 0x00000020;
289 






290     /**
291      * The {@code int} value representing the {@code volatile}
292      * modifier.

293      */
294     public static final int VOLATILE         = 0x00000040;
295 
296     /**
297      * The {@code int} value representing the {@code transient}
298      * modifier.

299      */
300     public static final int TRANSIENT        = 0x00000080;
301 
302     /**
303      * The {@code int} value representing the {@code native}
304      * modifier.

305      */
306     public static final int NATIVE           = 0x00000100;
307 
308     /**
309      * The {@code int} value representing the {@code interface}
310      * modifier.

311      */
312     public static final int INTERFACE        = 0x00000200;
313 
314     /**
315      * The {@code int} value representing the {@code abstract}
316      * modifier.

317      */
318     public static final int ABSTRACT         = 0x00000400;
319 
320     /**
321      * The {@code int} value representing the {@code strictfp}
322      * modifier.
323      */
324     public static final int STRICT           = 0x00000800;
325 
326     // Bits not (yet) exposed in the public API either because they
327     // have different meanings for fields and methods and there is no
328     // way to distinguish between the two in this class, or because
329     // they are not Java programming language keywords
330     static final int BRIDGE    = 0x00000040;
331     static final int VARARGS   = 0x00000080;
332     static final int SYNTHETIC = 0x00001000;
333     static final int ANNOTATION  = 0x00002000;
334     static final int ENUM      = 0x00004000;
335     static final int MANDATED  = 0x00008000;
336     static boolean isSynthetic(int mod) {
337       return (mod & SYNTHETIC) != 0;
338     }
339 
340     static boolean isMandated(int mod) {
341       return (mod & MANDATED) != 0;
342     }
343 
344     // Note on the FOO_MODIFIERS fields and fooModifiers() methods:
345     // the sets of modifiers are not guaranteed to be constants
346     // across time and Java SE releases. Therefore, it would not be
347     // appropriate to expose an external interface to this information
348     // that would allow the values to be treated as Java-level
349     // constants since the values could be constant folded and updates
350     // to the sets of modifiers missed. Thus, the fooModifiers()
351     // methods return an unchanging values for a given release, but a
352     // value that can potentially change over time.
353 
354     /**
355      * The Java source modifiers that can be applied to a class.
356      * @jls 8.1.1 Class Modifiers
357      */
358     private static final int CLASS_MODIFIERS =
359         Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE |
360         Modifier.ABSTRACT       | Modifier.STATIC       | Modifier.FINAL   |
361         Modifier.STRICT;
362 
363     /**
364      * The Java source modifiers that can be applied to an interface.
365      * @jls 9.1.1 Interface Modifiers
366      */
367     private static final int INTERFACE_MODIFIERS =
368         Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE |
369         Modifier.ABSTRACT       | Modifier.STATIC       | Modifier.STRICT;
370 
371 
372     /**
373      * The Java source modifiers that can be applied to a constructor.
374      * @jls 8.8.3 Constructor Modifiers
375      */
376     private static final int CONSTRUCTOR_MODIFIERS =
377         Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE;
378 
379     /**
380      * The Java source modifiers that can be applied to a method.
381      * @jls 8.4.3  Method Modifiers

  1 /*
  2  * Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.  Oracle designates this
  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

105      * @param   mod a set of modifiers
106      * @return {@code true} if {@code mod} includes the
107      * {@code final} modifier; {@code false} otherwise.
108      */
109     public static boolean isFinal(int mod) {
110         return (mod & FINAL) != 0;
111     }
112 
113     /**
114      * Return {@code true} if the integer argument includes the
115      * {@code synchronized} modifier, {@code false} otherwise.
116      *
117      * @param   mod a set of modifiers
118      * @return {@code true} if {@code mod} includes the
119      * {@code synchronized} modifier; {@code false} otherwise.
120      */
121     public static boolean isSynchronized(int mod) {
122         return (mod & SYNCHRONIZED) != 0;
123     }
124 
125     /**
126      * Return {@code true} if the integer argument includes the
127      * {@code permitsValue} modifier, {@code false} otherwise.
128      *
129      * @param   mod a set of modifiers
130      * @return {@code true} if {@code mod} includes the
131      * {@code permitsValue} modifier; {@code false} otherwise.
132      */
133     public static boolean isPermitsValue(int mod) {
134         return (mod & PERMITS_VALUE) != 0;
135     }
136 
137     /**
138      * Return {@code true} if the integer argument includes the
139      * {@code volatile} modifier, {@code false} otherwise.
140      *
141      * @param   mod a set of modifiers
142      * @return {@code true} if {@code mod} includes the
143      * {@code volatile} modifier; {@code false} otherwise.
144      */
145     public static boolean isVolatile(int mod) {
146         return (mod & VOLATILE) != 0;
147     }
148 
149     /**
150      * Return {@code true} if the integer argument includes the
151      * {@code transient} modifier, {@code false} otherwise.
152      *
153      * @param   mod a set of modifiers
154      * @return {@code true} if {@code mod} includes the
155      * {@code transient} modifier; {@code false} otherwise.
156      */

249         if ((mod & STATIC) != 0)        sj.add("static");
250         if ((mod & FINAL) != 0)         sj.add("final");
251         if ((mod & TRANSIENT) != 0)     sj.add("transient");
252         if ((mod & VOLATILE) != 0)      sj.add("volatile");
253         if ((mod & SYNCHRONIZED) != 0)  sj.add("synchronized");
254         if ((mod & NATIVE) != 0)        sj.add("native");
255         if ((mod & STRICT) != 0)        sj.add("strictfp");
256         if ((mod & INTERFACE) != 0)     sj.add("interface");
257 
258         return sj.toString();
259     }
260 
261     /*
262      * Access modifier flag constants from tables 4.1, 4.4, 4.5, and 4.7 of
263      * <cite>The Java Virtual Machine Specification</cite>
264      */
265 
266     /**
267      * The {@code int} value representing the {@code public}
268      * modifier.
269      * @see AccessFlag#PUBLIC
270      */
271     public static final int PUBLIC           = 0x00000001;
272 
273     /**
274      * The {@code int} value representing the {@code private}
275      * modifier.
276      * @see AccessFlag#PRIVATE
277      */
278     public static final int PRIVATE          = 0x00000002;
279 
280     /**
281      * The {@code int} value representing the {@code protected}
282      * modifier.
283      * @see AccessFlag#PROTECTED
284      */
285     public static final int PROTECTED        = 0x00000004;
286 
287     /**
288      * The {@code int} value representing the {@code static}
289      * modifier.
290      * @see AccessFlag#STATIC
291      */
292     public static final int STATIC           = 0x00000008;
293 
294     /**
295      * The {@code int} value representing the {@code final}
296      * modifier.
297      * @see AccessFlag#FINAL
298      */
299     public static final int FINAL            = 0x00000010;
300 
301     /**
302      * The {@code int} value representing the {@code synchronized}
303      * modifier.
304      * @see AccessFlag#SYNCHRONIZED
305      */
306     public static final int SYNCHRONIZED     = 0x00000020;
307 
308     /**
309      * The {@code int} value representing the {@code permits_value}
310      * modifier.
311      */
312     public static final int PERMITS_VALUE    = 0x00000040;
313 
314     /**
315      * The {@code int} value representing the {@code volatile}
316      * modifier.
317      * @see AccessFlag#VOLATILE
318      */
319     public static final int VOLATILE         = 0x00000040;
320 
321     /**
322      * The {@code int} value representing the {@code transient}
323      * modifier.
324      * @see AccessFlag#TRANSIENT
325      */
326     public static final int TRANSIENT        = 0x00000080;
327 
328     /**
329      * The {@code int} value representing the {@code native}
330      * modifier.
331      * @see AccessFlag#NATIVE
332      */
333     public static final int NATIVE           = 0x00000100;
334 
335     /**
336      * The {@code int} value representing the {@code interface}
337      * modifier.
338      * @see AccessFlag#INTERFACE
339      */
340     public static final int INTERFACE        = 0x00000200;
341 
342     /**
343      * The {@code int} value representing the {@code abstract}
344      * modifier.
345      * @see AccessFlag#ABSTRACT
346      */
347     public static final int ABSTRACT         = 0x00000400;
348 
349     /**
350      * The {@code int} value representing the {@code strictfp}
351      * modifier.
352      */
353     public static final int STRICT           = 0x00000800;
354 
355     // Bits not (yet) exposed in the public API either because they
356     // have different meanings for fields and methods and there is no
357     // way to distinguish between the two in this class, or because
358     // they are not Java programming language keywords
359     static final int BRIDGE      = 0x00000040;
360     static final int VARARGS     = 0x00000080;
361     static final int SYNTHETIC   = 0x00001000;
362     static final int ANNOTATION  = 0x00002000;
363     static final int ENUM        = 0x00004000;
364     static final int MANDATED    = 0x00008000;
365     static boolean isSynthetic(int mod) {
366       return (mod & SYNTHETIC) != 0;
367     }
368 
369     static boolean isMandated(int mod) {
370       return (mod & MANDATED) != 0;
371     }
372 
373     // Note on the FOO_MODIFIERS fields and fooModifiers() methods:
374     // the sets of modifiers are not guaranteed to be constants
375     // across time and Java SE releases. Therefore, it would not be
376     // appropriate to expose an external interface to this information
377     // that would allow the values to be treated as Java-level
378     // constants since the values could be constant folded and updates
379     // to the sets of modifiers missed. Thus, the fooModifiers()
380     // methods return an unchanging values for a given release, but a
381     // value that can potentially change over time.
382 
383     /**
384      * The Java source modifiers that can be applied to a class.
385      * @jls 8.1.1 Class Modifiers
386      */
387     private static final int CLASS_MODIFIERS =
388         Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE |
389         Modifier.ABSTRACT       | Modifier.STATIC       | Modifier.FINAL   |
390         Modifier.STRICT         | Modifier.PERMITS_VALUE;
391 
392     /**
393      * The Java source modifiers that can be applied to an interface.
394      * @jls 9.1.1 Interface Modifiers
395      */
396     private static final int INTERFACE_MODIFIERS =
397         Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE |
398         Modifier.ABSTRACT       | Modifier.STATIC       | Modifier.STRICT;
399 
400 
401     /**
402      * The Java source modifiers that can be applied to a constructor.
403      * @jls 8.8.3 Constructor Modifiers
404      */
405     private static final int CONSTRUCTOR_MODIFIERS =
406         Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE;
407 
408     /**
409      * The Java source modifiers that can be applied to a method.
410      * @jls 8.4.3  Method Modifiers
< prev index next >