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
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 * @see Class#getModifiers()
40 * @see Member#getModifiers()
41 *
42 * @author Nakul Saraiya
43 * @author Kenneth Russell
44 * @since 1.1
45 */
46 public class Modifier {
47 /**
48 * Do not call.
49 */
50 private Modifier() {throw new AssertionError();}
51
52
53 /**
54 * Return {@code true} if the integer argument includes the
55 * {@code public} modifier, {@code false} otherwise.
56 *
57 * @param mod a set of modifiers
58 * @return {@code true} if {@code mod} includes the
97 public static boolean isStatic(int mod) {
98 return (mod & STATIC) != 0;
99 }
100
101 /**
102 * Return {@code true} if the integer argument includes the
103 * {@code final} modifier, {@code false} otherwise.
104 *
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 */
276 * The {@code int} value representing the {@code static}
277 * modifier.
278 * @see AccessFlag#STATIC
279 */
280 public static final int STATIC = 0x00000008;
281
282 /**
283 * The {@code int} value representing the {@code final}
284 * modifier.
285 * @see AccessFlag#FINAL
286 */
287 public static final int FINAL = 0x00000010;
288
289 /**
290 * The {@code int} value representing the {@code synchronized}
291 * modifier.
292 * @see AccessFlag#SYNCHRONIZED
293 */
294 public static final int SYNCHRONIZED = 0x00000020;
295
296 /**
297 * The {@code int} value representing the {@code volatile}
298 * modifier.
299 * @see AccessFlag#VOLATILE
300 */
301 public static final int VOLATILE = 0x00000040;
302
303 /**
304 * The {@code int} value representing the {@code transient}
305 * modifier.
306 * @see AccessFlag#TRANSIENT
307 */
308 public static final int TRANSIENT = 0x00000080;
309
310 /**
311 * The {@code int} value representing the {@code native}
312 * modifier.
313 * @see AccessFlag#NATIVE
314 */
315 public static final int NATIVE = 0x00000100;
353 return (mod & MANDATED) != 0;
354 }
355
356 // Note on the FOO_MODIFIERS fields and fooModifiers() methods:
357 // the sets of modifiers are not guaranteed to be constants
358 // across time and Java SE releases. Therefore, it would not be
359 // appropriate to expose an external interface to this information
360 // that would allow the values to be treated as Java-level
361 // constants since the values could be constant folded and updates
362 // to the sets of modifiers missed. Thus, the fooModifiers()
363 // methods return an unchanging values for a given release, but a
364 // value that can potentially change over time.
365
366 /**
367 * The Java source modifiers that can be applied to a class.
368 * @jls 8.1.1 Class Modifiers
369 */
370 private static final int CLASS_MODIFIERS =
371 Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE |
372 Modifier.ABSTRACT | Modifier.STATIC | Modifier.FINAL |
373 Modifier.STRICT;
374
375 /**
376 * The Java source modifiers that can be applied to an interface.
377 * @jls 9.1.1 Interface Modifiers
378 */
379 private static final int INTERFACE_MODIFIERS =
380 Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE |
381 Modifier.ABSTRACT | Modifier.STATIC | Modifier.STRICT;
382
383
384 /**
385 * The Java source modifiers that can be applied to a constructor.
386 * @jls 8.8.3 Constructor Modifiers
387 */
388 private static final int CONSTRUCTOR_MODIFIERS =
389 Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE;
390
391 /**
392 * The Java source modifiers that can be applied to a method.
393 * @jls 8.4.3 Method Modifiers
|
1 /*
2 * Copyright (c) 1996, 2024, 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
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 * @see Class#accessFlags()
43 * @see Member#accessFlags()
44 * @see Class#getModifiers()
45 * @see Member#getModifiers()
46 *
47 * @author Nakul Saraiya
48 * @author Kenneth Russell
49 * @since 1.1
50 */
51 public class Modifier {
52 /**
53 * Do not call.
54 */
55 private Modifier() {throw new AssertionError();}
56
57
58 /**
59 * Return {@code true} if the integer argument includes the
60 * {@code public} modifier, {@code false} otherwise.
61 *
62 * @param mod a set of modifiers
63 * @return {@code true} if {@code mod} includes the
102 public static boolean isStatic(int mod) {
103 return (mod & STATIC) != 0;
104 }
105
106 /**
107 * Return {@code true} if the integer argument includes the
108 * {@code final} modifier, {@code false} otherwise.
109 *
110 * @param mod a set of modifiers
111 * @return {@code true} if {@code mod} includes the
112 * {@code final} modifier; {@code false} otherwise.
113 */
114 public static boolean isFinal(int mod) {
115 return (mod & FINAL) != 0;
116 }
117
118 /**
119 * Return {@code true} if the integer argument includes the
120 * {@code synchronized} modifier, {@code false} otherwise.
121 *
122 * @apiNote {@code isSynchronized} should only be called with the modifiers
123 * of a {@linkplain Method#getModifiers() method}.
124 *
125 * @param mod a set of modifiers
126 * @return {@code true} if {@code mod} includes the
127 * {@code synchronized} modifier; {@code false} otherwise.
128 */
129 public static boolean isSynchronized(int mod) {
130 return (mod & SYNCHRONIZED) != 0;
131 }
132
133 /**
134 * Return {@code true} if the integer argument includes the
135 * {@code identity} modifier, {@code false} otherwise.
136 *
137 * @apiNote {@code isIdentity} should only be called with the modifiers
138 * of a {@linkplain Class#getModifiers() class}.
139 *
140 * @param mod a set of modifiers
141 * @return {@code true} if {@code mod} includes the
142 * {@code identity} modifier; {@code false} otherwise.
143 */
144 public static boolean isIdentity(int mod) {
145 return (mod & IDENTITY) != 0;
146 }
147
148 /**
149 * Return {@code true} if the integer argument includes the
150 * {@code volatile} modifier, {@code false} otherwise.
151 *
152 * @param mod a set of modifiers
153 * @return {@code true} if {@code mod} includes the
154 * {@code volatile} modifier; {@code false} otherwise.
155 */
156 public static boolean isVolatile(int mod) {
157 return (mod & VOLATILE) != 0;
158 }
159
160 /**
161 * Return {@code true} if the integer argument includes the
162 * {@code transient} modifier, {@code false} otherwise.
163 *
164 * @param mod a set of modifiers
165 * @return {@code true} if {@code mod} includes the
166 * {@code transient} modifier; {@code false} otherwise.
167 */
299 * The {@code int} value representing the {@code static}
300 * modifier.
301 * @see AccessFlag#STATIC
302 */
303 public static final int STATIC = 0x00000008;
304
305 /**
306 * The {@code int} value representing the {@code final}
307 * modifier.
308 * @see AccessFlag#FINAL
309 */
310 public static final int FINAL = 0x00000010;
311
312 /**
313 * The {@code int} value representing the {@code synchronized}
314 * modifier.
315 * @see AccessFlag#SYNCHRONIZED
316 */
317 public static final int SYNCHRONIZED = 0x00000020;
318
319 /**
320 * The {@code int} value representing the {@code ACC_IDENTITY}
321 * modifier.
322 */
323 @PreviewFeature(feature = PreviewFeature.Feature.VALUE_OBJECTS)
324 public static final int IDENTITY = 0x00000020;
325
326 /**
327 * The {@code int} value representing the {@code volatile}
328 * modifier.
329 * @see AccessFlag#VOLATILE
330 */
331 public static final int VOLATILE = 0x00000040;
332
333 /**
334 * The {@code int} value representing the {@code transient}
335 * modifier.
336 * @see AccessFlag#TRANSIENT
337 */
338 public static final int TRANSIENT = 0x00000080;
339
340 /**
341 * The {@code int} value representing the {@code native}
342 * modifier.
343 * @see AccessFlag#NATIVE
344 */
345 public static final int NATIVE = 0x00000100;
383 return (mod & MANDATED) != 0;
384 }
385
386 // Note on the FOO_MODIFIERS fields and fooModifiers() methods:
387 // the sets of modifiers are not guaranteed to be constants
388 // across time and Java SE releases. Therefore, it would not be
389 // appropriate to expose an external interface to this information
390 // that would allow the values to be treated as Java-level
391 // constants since the values could be constant folded and updates
392 // to the sets of modifiers missed. Thus, the fooModifiers()
393 // methods return an unchanging values for a given release, but a
394 // value that can potentially change over time.
395
396 /**
397 * The Java source modifiers that can be applied to a class.
398 * @jls 8.1.1 Class Modifiers
399 */
400 private static final int CLASS_MODIFIERS =
401 Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE |
402 Modifier.ABSTRACT | Modifier.STATIC | Modifier.FINAL |
403 Modifier.IDENTITY | Modifier.STRICT;
404
405 /**
406 * The Java source modifiers that can be applied to an interface.
407 * @jls 9.1.1 Interface Modifiers
408 */
409 private static final int INTERFACE_MODIFIERS =
410 Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE |
411 Modifier.ABSTRACT | Modifier.STATIC | Modifier.STRICT;
412
413
414 /**
415 * The Java source modifiers that can be applied to a constructor.
416 * @jls 8.8.3 Constructor Modifiers
417 */
418 private static final int CONSTRUCTOR_MODIFIERS =
419 Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE;
420
421 /**
422 * The Java source modifiers that can be applied to a method.
423 * @jls 8.4.3 Method Modifiers
|