1 /*
2 * Copyright (c) 1996, 2026, 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 * @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 final 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.
68 *
69 * @param mod a set of modifiers
70 * @return {@code true} if {@code mod} includes the
71 * {@code public} modifier; {@code false} otherwise.
72 */
73 public static boolean isPublic(int mod) {
74 return (mod & PUBLIC) != 0;
75 }
76
77 /**
78 * Return {@code true} if the integer argument includes the
79 * {@code private} modifier, {@code false} otherwise.
80 *
81 * @param mod a set of modifiers
82 * @return {@code true} if {@code mod} includes the
83 * {@code private} modifier; {@code false} otherwise.
84 */
85 public static boolean isPrivate(int mod) {
86 return (mod & PRIVATE) != 0;
87 }
88
89 /**
90 * Return {@code true} if the integer argument includes the
91 * {@code protected} modifier, {@code false} otherwise.
92 *
93 * @param mod a set of modifiers
94 * @return {@code true} if {@code mod} includes the
95 * {@code protected} modifier; {@code false} otherwise.
96 */
97 public static boolean isProtected(int mod) {
98 return (mod & PROTECTED) != 0;
99 }
100
101 /**
102 * Return {@code true} if the integer argument includes the
103 * {@code static} modifier, {@code false} otherwise.
104 *
105 * @param mod a set of modifiers
106 * @return {@code true} if {@code mod} includes the
107 * {@code static} modifier; {@code false} otherwise.
108 */
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 volatile} modifier, {@code false} otherwise.
143 *
144 * @param mod a set of modifiers
145 * @return {@code true} if {@code mod} includes the
146 * {@code volatile} modifier; {@code false} otherwise.
147 */
148 public static boolean isVolatile(int mod) {
149 return (mod & VOLATILE) != 0;
150 }
151
152 /**
153 * Return {@code true} if the integer argument includes the
154 * {@code transient} modifier, {@code false} otherwise.
155 *
156 * @param mod a set of modifiers
157 * @return {@code true} if {@code mod} includes the
158 * {@code transient} modifier; {@code false} otherwise.
159 */
160 public static boolean isTransient(int mod) {
161 return (mod & TRANSIENT) != 0;
162 }
163
164 /**
165 * Return {@code true} if the integer argument includes the
166 * {@code native} modifier, {@code false} otherwise.
167 *
168 * @param mod a set of modifiers
169 * @return {@code true} if {@code mod} includes the
170 * {@code native} modifier; {@code false} otherwise.
171 */
172 public static boolean isNative(int mod) {
173 return (mod & NATIVE) != 0;
174 }
175
176 /**
177 * Return {@code true} if the integer argument includes the
178 * {@code interface} modifier, {@code false} otherwise.
179 *
180 * @param mod a set of modifiers
181 * @return {@code true} if {@code mod} includes the
182 * {@code interface} modifier; {@code false} otherwise.
183 */
184 public static boolean isInterface(int mod) {
185 return (mod & INTERFACE) != 0;
186 }
187
188 /**
189 * Return {@code true} if the integer argument includes the
190 * {@code abstract} modifier, {@code false} otherwise.
191 *
192 * @param mod a set of modifiers
193 * @return {@code true} if {@code mod} includes the
194 * {@code abstract} modifier; {@code false} otherwise.
195 */
196 public static boolean isAbstract(int mod) {
197 return (mod & ABSTRACT) != 0;
198 }
199
200 /**
201 * Return {@code true} if the integer argument includes the
202 * {@code strictfp} modifier, {@code false} otherwise.
203 *
204 * @param mod a set of modifiers
205 * @return {@code true} if {@code mod} includes the
206 * {@code strictfp} modifier; {@code false} otherwise.
207 */
208 public static boolean isStrict(int mod) {
209 return (mod & STRICT) != 0;
210 }
211
212 /**
213 * Return a string describing the access modifier flags in
214 * the specified modifier. For example:
215 * <blockquote><pre>
216 * public final synchronized
217 * </pre></blockquote>
218 * The modifier names are returned in an order consistent with the
219 * suggested modifier orderings given in sections 8.1.1, 8.3.1, 8.4.3, 8.8.3, and 9.1.1 of
220 * <cite>The Java Language Specification</cite>.
221 * The full modifier ordering used by this method is:
222 * <blockquote> {@code
223 * public protected private abstract static final transient
224 * volatile synchronized native strictfp
225 * interface } </blockquote>
226 *
227 * The {@code interface} modifier discussed in this class is
228 * not a true modifier in the Java language and it appears after
229 * all other modifiers listed by this method. This method may
230 * return a string of modifiers that are not valid modifiers of a
231 * Java entity; in other words, no checking is done on the
232 * possible validity of the combination of modifiers represented
233 * by the input.
234 *
235 * Note that to perform such checking for a known kind of entity,
236 * such as a constructor or method, first AND the argument of
237 * {@code toString} with the appropriate mask from a method like
238 * {@link #constructorModifiers} or {@link #methodModifiers}.
239 *
240 * @apiNote
241 * To make a high-fidelity representation of the Java source
242 * modifiers of a class or member, source-level modifiers that do
243 * <em>not</em> have a constant in this class should be included
244 * and appear in an order consistent with the full recommended
245 * ordering for that kind of declaration as given in <cite>The
246 * Java Language Specification</cite>. For example, for a
247 * {@linkplain Method#toGenericString() method} the "{@link
248 * Method#isDefault() default}" modifier is ordered immediately
249 * before "{@code static}" (JLS {@jls 9.4}). For a {@linkplain
250 * Class#toGenericString() class object}, the "{@link
251 * Class#isSealed() sealed}" or {@code "non-sealed"} modifier is
252 * ordered immediately after "{@code final}" for a class (JLS
253 * {@jls 8.1.1}) and immediately after "{@code static}" for an
254 * interface (JLS {@jls 9.1.1}).
255 *
256 * @param mod a set of modifiers
257 * @return a string representation of the set of modifiers
258 * represented by {@code mod}
259 */
260 public static String toString(int mod) {
261 StringJoiner sj = new StringJoiner(" ");
262
263 if ((mod & PUBLIC) != 0) sj.add("public");
264 if ((mod & PROTECTED) != 0) sj.add("protected");
265 if ((mod & PRIVATE) != 0) sj.add("private");
266
267 /* Canonical order */
268 if ((mod & ABSTRACT) != 0) sj.add("abstract");
269 if ((mod & STATIC) != 0) sj.add("static");
270 if ((mod & FINAL) != 0) sj.add("final");
271 if ((mod & TRANSIENT) != 0) sj.add("transient");
272 if ((mod & VOLATILE) != 0) sj.add("volatile");
273 if ((mod & SYNCHRONIZED) != 0) sj.add("synchronized");
274 if ((mod & NATIVE) != 0) sj.add("native");
275 if ((mod & STRICT) != 0) sj.add("strictfp");
276 if ((mod & INTERFACE) != 0) sj.add("interface");
277
278 return sj.toString();
279 }
280
281 /*
282 * Access modifier flag constants from tables 4.1, 4.4, 4.5, and 4.7 of
283 * <cite>The Java Virtual Machine Specification</cite>
284 */
285
286 /**
287 * The {@code int} value representing the {@code public}
288 * modifier.
289 * @see AccessFlag#PUBLIC
290 */
291 public static final int PUBLIC = 0x00000001;
292
293 /**
294 * The {@code int} value representing the {@code private}
295 * modifier.
296 * @see AccessFlag#PRIVATE
297 */
298 public static final int PRIVATE = 0x00000002;
299
300 /**
301 * The {@code int} value representing the {@code protected}
302 * modifier.
303 * @see AccessFlag#PROTECTED
304 */
305 public static final int PROTECTED = 0x00000004;
306
307 /**
308 * The {@code int} value representing the {@code static}
309 * modifier.
310 * @see AccessFlag#STATIC
311 */
312 public static final int STATIC = 0x00000008;
313
314 /**
315 * The {@code int} value representing the {@code final}
316 * modifier.
317 * @see AccessFlag#FINAL
318 */
319 public static final int FINAL = 0x00000010;
320
321 /**
322 * The {@code int} value representing the {@code synchronized}
323 * modifier.
324 * @see AccessFlag#SYNCHRONIZED
325 */
326 public static final int SYNCHRONIZED = 0x00000020;
327
328 /**
329 * The {@code int} value representing the {@code volatile}
330 * modifier.
331 * @see AccessFlag#VOLATILE
332 */
333 public static final int VOLATILE = 0x00000040;
334
335 /**
336 * The {@code int} value representing the {@code transient}
337 * modifier.
338 * @see AccessFlag#TRANSIENT
339 */
340 public static final int TRANSIENT = 0x00000080;
341
342 /**
343 * The {@code int} value representing the {@code native}
344 * modifier.
345 * @see AccessFlag#NATIVE
346 */
347 public static final int NATIVE = 0x00000100;
348
349 /**
350 * The {@code int} value representing the {@code interface}
351 * modifier.
352 * @see AccessFlag#INTERFACE
353 */
354 public static final int INTERFACE = 0x00000200;
355
356 /**
357 * The {@code int} value representing the {@code abstract}
358 * modifier.
359 * @see AccessFlag#ABSTRACT
360 */
361 public static final int ABSTRACT = 0x00000400;
362
363 /**
364 * The {@code int} value representing the {@code strictfp}
365 * modifier.
366 * @see AccessFlag#STRICT
367 */
368 public static final int STRICT = 0x00000800;
369
370 // Bits not (yet) exposed in the public API either because they
371 // have different meanings for fields and methods and there is no
372 // way to distinguish between the two in this class, or because
373 // they are not Java programming language keywords
374 static final int BRIDGE = 0x00000040;
375 static final int VARARGS = 0x00000080;
376 static final int SYNTHETIC = 0x00001000;
377 static final int ANNOTATION = 0x00002000;
378 static final int ENUM = 0x00004000;
379 static final int MANDATED = 0x00008000;
380 static boolean isSynthetic(int mod) {
381 return (mod & SYNTHETIC) != 0;
382 }
383
384 static boolean isMandated(int mod) {
385 return (mod & MANDATED) != 0;
386 }
387
388 // Note on the FOO_MODIFIERS fields and fooModifiers() methods:
389 // the sets of modifiers are not guaranteed to be constants
390 // across time and Java SE releases. Therefore, it would not be
391 // appropriate to expose an external interface to this information
392 // that would allow the values to be treated as Java-level
393 // constants since the values could be constant folded and updates
394 // to the sets of modifiers missed. Thus, the fooModifiers()
395 // methods return an unchanging values for a given release, but a
396 // value that can potentially change over time.
397
398 /**
399 * The Java source modifiers that can be applied to a class.
400 * @jls 8.1.1 Class Modifiers
401 */
402 private static final int CLASS_MODIFIERS =
403 Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE |
404 Modifier.ABSTRACT | Modifier.STATIC | Modifier.FINAL |
405 Modifier.STRICT;
406
407 /**
408 * The Java source modifiers that can be applied to an interface.
409 * @jls 9.1.1 Interface Modifiers
410 */
411 private static final int INTERFACE_MODIFIERS =
412 Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE |
413 Modifier.ABSTRACT | Modifier.STATIC | Modifier.STRICT;
414
415
416 /**
417 * The Java source modifiers that can be applied to a constructor.
418 * @jls 8.8.3 Constructor Modifiers
419 */
420 private static final int CONSTRUCTOR_MODIFIERS =
421 Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE;
422
423 /**
424 * The Java source modifiers that can be applied to a method.
425 * @jls 8.4.3 Method Modifiers
426 */
427 private static final int METHOD_MODIFIERS =
428 Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE |
429 Modifier.ABSTRACT | Modifier.STATIC | Modifier.FINAL |
430 Modifier.SYNCHRONIZED | Modifier.NATIVE | Modifier.STRICT;
431
432 /**
433 * The Java source modifiers that can be applied to a field.
434 * @jls 8.3.1 Field Modifiers
435 */
436 private static final int FIELD_MODIFIERS =
437 Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE |
438 Modifier.STATIC | Modifier.FINAL | Modifier.TRANSIENT |
439 Modifier.VOLATILE;
440
441 /**
442 * The Java source modifiers that can be applied to a method or constructor parameter.
443 * @jls 8.4.1 Formal Parameters
444 */
445 private static final int PARAMETER_MODIFIERS =
446 Modifier.FINAL;
447
448 static final int ACCESS_MODIFIERS =
449 Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE;
450
451 /**
452 * Return an {@code int} value OR-ing together the source language
453 * modifiers that can be applied to a class.
454 * @return an {@code int} value OR-ing together the source language
455 * modifiers that can be applied to a class.
456 *
457 * @see AccessFlag.Location#CLASS
458 * @see AccessFlag.Location#INNER_CLASS
459 * @jls 8.1.1 Class Modifiers
460 * @since 1.7
461 */
462 public static int classModifiers() {
463 return CLASS_MODIFIERS;
464 }
465
466 /**
467 * Return an {@code int} value OR-ing together the source language
468 * modifiers that can be applied to an interface.
469 * @return an {@code int} value OR-ing together the source language
470 * modifiers that can be applied to an interface.
471 *
472 * @see AccessFlag.Location#CLASS
473 * @see AccessFlag.Location#INNER_CLASS
474 * @jls 9.1.1 Interface Modifiers
475 * @since 1.7
476 */
477 public static int interfaceModifiers() {
478 return INTERFACE_MODIFIERS;
479 }
480
481 /**
482 * Return an {@code int} value OR-ing together the source language
483 * modifiers that can be applied to a constructor.
484 * @return an {@code int} value OR-ing together the source language
485 * modifiers that can be applied to a constructor.
486 *
487 * @see AccessFlag.Location#METHOD
488 * @jls 8.8.3 Constructor Modifiers
489 * @since 1.7
490 */
491 public static int constructorModifiers() {
492 return CONSTRUCTOR_MODIFIERS;
493 }
494
495 /**
496 * Return an {@code int} value OR-ing together the source language
497 * modifiers that can be applied to a method.
498 * @return an {@code int} value OR-ing together the source language
499 * modifiers that can be applied to a method.
500 *
501 * @see AccessFlag.Location#METHOD
502 * @jls 8.4.3 Method Modifiers
503 * @since 1.7
504 */
505 public static int methodModifiers() {
506 return METHOD_MODIFIERS;
507 }
508
509 /**
510 * Return an {@code int} value OR-ing together the source language
511 * modifiers that can be applied to a field.
512 * @return an {@code int} value OR-ing together the source language
513 * modifiers that can be applied to a field.
514 *
515 * @see AccessFlag.Location#FIELD
516 * @jls 8.3.1 Field Modifiers
517 * @since 1.7
518 */
519 public static int fieldModifiers() {
520 return FIELD_MODIFIERS;
521 }
522
523 /**
524 * Return an {@code int} value OR-ing together the source language
525 * modifiers that can be applied to a parameter.
526 * @return an {@code int} value OR-ing together the source language
527 * modifiers that can be applied to a parameter.
528 *
529 * @see AccessFlag.Location#METHOD_PARAMETER
530 * @jls 8.4.1 Formal Parameters
531 * @since 1.8
532 */
533 public static int parameterModifiers() {
534 return PARAMETER_MODIFIERS;
535 }
536 }