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 java.util.StringJoiner;
29
30 /**
31 * The Modifier class provides {@code static} methods and
32 * constants to decode class and member access modifiers.
33 * The {@link AccessFlag} class should be used instead of this class.
34 * The sets of modifiers are represented as integers with non-distinct bit positions
35 * representing different modifiers. The values for the constants
36 * representing the modifiers are taken from the tables in sections
37 * {@jvms 4.1}, {@jvms 4.4}, {@jvms 4.5}, and {@jvms 4.7} of
38 * <cite>The Java Virtual Machine Specification</cite>.
39 *
40 * @apiNote
41 * Not all modifiers that are syntactic Java language modifiers are
42 * represented in this class, only those modifiers that <em>also</em>
43 * have a corresponding JVM {@linkplain AccessFlag access flag} are
44 * included. In particular, the {@code default} method modifier (JLS
45 * {@jls 9.4.3}) and the {@code value}, {@code sealed} and {@code non-sealed} class
46 * (JLS {@jls 8.1.1.2}) and interface (JLS {@jls 9.1.1.4}) modifiers
47 * are <em>not</em> represented in this class.
48 *
49 * @see Class#getModifiers()
50 * @see Member#getModifiers()
51 *
52 * @author Nakul Saraiya
53 * @author Kenneth Russell
54 * @since 1.1
55 */
56 public final class Modifier {
57 /**
58 * Do not call.
59 */
60 private Modifier() {throw new AssertionError();}
61
62
63 /**
64 * Return {@code true} if the integer argument includes the
65 * {@code public} modifier, {@code false} otherwise.
66 *
67 * @param mod a set of modifiers
68 * @return {@code true} if {@code mod} includes the
69 * {@code public} modifier; {@code false} otherwise.
70 */
71 public static boolean isPublic(int mod) {
72 return (mod & PUBLIC) != 0;
73 }
74
75 /**
76 * Return {@code true} if the integer argument includes the
77 * {@code private} modifier, {@code false} otherwise.
78 *
79 * @param mod a set of modifiers
80 * @return {@code true} if {@code mod} includes the
81 * {@code private} modifier; {@code false} otherwise.
82 */
83 public static boolean isPrivate(int mod) {
84 return (mod & PRIVATE) != 0;
85 }
86
87 /**
88 * Return {@code true} if the integer argument includes the
89 * {@code protected} modifier, {@code false} otherwise.
90 *
91 * @param mod a set of modifiers
92 * @return {@code true} if {@code mod} includes the
93 * {@code protected} modifier; {@code false} otherwise.
94 */
95 public static boolean isProtected(int mod) {
96 return (mod & PROTECTED) != 0;
97 }
98
99 /**
100 * Return {@code true} if the integer argument includes the
101 * {@code static} modifier, {@code false} otherwise.
102 *
103 * @param mod a set of modifiers
104 * @return {@code true} if {@code mod} includes the
105 * {@code static} modifier; {@code false} otherwise.
106 */
107 public static boolean isStatic(int mod) {
108 return (mod & STATIC) != 0;
109 }
110
111 /**
112 * Return {@code true} if the integer argument includes the
113 * {@code final} modifier, {@code false} otherwise.
114 *
115 * @param mod a set of modifiers
116 * @return {@code true} if {@code mod} includes the
117 * {@code final} modifier; {@code false} otherwise.
118 */
119 public static boolean isFinal(int mod) {
120 return (mod & FINAL) != 0;
121 }
122
123 /**
124 * Return {@code true} if the integer argument includes the
125 * {@code synchronized} modifier, {@code false} otherwise.
126 *
127 * @apiNote {@code isSynchronized} should only be called with the modifiers
128 * of a {@linkplain Method#getModifiers() method}.
129 *
130 * @param mod a set of modifiers
131 * @return {@code true} if {@code mod} includes the
132 * {@code synchronized} modifier; {@code false} otherwise.
133 */
134 public static boolean isSynchronized(int mod) {
135 return (mod & SYNCHRONIZED) != 0;
136 }
137
138 /**
139 * Return {@code true} if the integer argument includes the
140 * {@code volatile} modifier, {@code false} otherwise.
141 *
142 * @param mod a set of modifiers
143 * @return {@code true} if {@code mod} includes the
144 * {@code volatile} modifier; {@code false} otherwise.
145 */
146 public static boolean isVolatile(int mod) {
147 return (mod & VOLATILE) != 0;
148 }
149
150 /**
151 * Return {@code true} if the integer argument includes the
152 * {@code transient} modifier, {@code false} otherwise.
153 *
154 * @param mod a set of modifiers
155 * @return {@code true} if {@code mod} includes the
156 * {@code transient} modifier; {@code false} otherwise.
157 */
158 public static boolean isTransient(int mod) {
159 return (mod & TRANSIENT) != 0;
160 }
161
162 /**
163 * Return {@code true} if the integer argument includes the
164 * {@code native} modifier, {@code false} otherwise.
165 *
166 * @param mod a set of modifiers
167 * @return {@code true} if {@code mod} includes the
168 * {@code native} modifier; {@code false} otherwise.
169 */
170 public static boolean isNative(int mod) {
171 return (mod & NATIVE) != 0;
172 }
173
174 /**
175 * Return {@code true} if the integer argument includes the
176 * {@code interface} modifier, {@code false} otherwise.
177 *
178 * @param mod a set of modifiers
179 * @return {@code true} if {@code mod} includes the
180 * {@code interface} modifier; {@code false} otherwise.
181 */
182 public static boolean isInterface(int mod) {
183 return (mod & INTERFACE) != 0;
184 }
185
186 /**
187 * Return {@code true} if the integer argument includes the
188 * {@code abstract} modifier, {@code false} otherwise.
189 *
190 * @param mod a set of modifiers
191 * @return {@code true} if {@code mod} includes the
192 * {@code abstract} modifier; {@code false} otherwise.
193 */
194 public static boolean isAbstract(int mod) {
195 return (mod & ABSTRACT) != 0;
196 }
197
198 /**
199 * Return {@code true} if the integer argument includes the
200 * {@code strictfp} modifier, {@code false} otherwise.
201 *
202 * @param mod a set of modifiers
203 * @return {@code true} if {@code mod} includes the
204 * {@code strictfp} modifier; {@code false} otherwise.
205 */
206 public static boolean isStrict(int mod) {
207 return (mod & STRICT) != 0;
208 }
209
210 /**
211 * Return a string describing the access modifier flags in
212 * the specified modifier. For example:
213 * <blockquote><pre>
214 * public final synchronized
215 * </pre></blockquote>
216 * The modifier names are returned in an order consistent with the
217 * suggested modifier orderings given in sections 8.1.1, 8.3.1, 8.4.3, 8.8.3, and 9.1.1 of
218 * <cite>The Java Language Specification</cite>.
219 * The full modifier ordering used by this method is:
220 * <blockquote> {@code
221 * public protected private abstract static final transient
222 * volatile synchronized native strictfp
223 * interface } </blockquote>
224 *
225 * The {@code interface} modifier discussed in this class is
226 * not a true modifier in the Java language and it appears after
227 * all other modifiers listed by this method. This method may
228 * return a string of modifiers that are not valid modifiers of a
229 * Java entity; in other words, no checking is done on the
230 * possible validity of the combination of modifiers represented
231 * by the input.
232 *
233 * Note that to perform such checking for a known kind of entity,
234 * such as a constructor or method, first AND the argument of
235 * {@code toString} with the appropriate mask from a method like
236 * {@link #constructorModifiers} or {@link #methodModifiers}.
237 *
238 * @apiNote
239 * To make a high-fidelity representation of the Java source
240 * modifiers of a class or member, source-level modifiers that do
241 * <em>not</em> have a constant in this class should be included
242 * and appear in an order consistent with the full recommended
243 * ordering for that kind of declaration as given in <cite>The
244 * Java Language Specification</cite>. For example, for a
245 * {@linkplain Method#toGenericString() method} the "{@link
246 * Method#isDefault() default}" modifier is ordered immediately
247 * before "{@code static}" (JLS {@jls 9.4}). For a {@linkplain
248 * Class#toGenericString() class object}, the "{@link
249 * Class#isSealed() sealed}" or {@code "non-sealed"} modifier is
250 * ordered immediately after "{@code final}" for a class (JLS
251 * {@jls 8.1.1}) and immediately after "{@code static}" for an
252 * interface (JLS {@jls 9.1.1}).
253 *
254 * @param mod a set of modifiers
255 * @return a string representation of the set of modifiers
256 * represented by {@code mod}
257 */
258 public static String toString(int mod) {
259 StringJoiner sj = new StringJoiner(" ");
260
261 if ((mod & PUBLIC) != 0) sj.add("public");
262 if ((mod & PROTECTED) != 0) sj.add("protected");
263 if ((mod & PRIVATE) != 0) sj.add("private");
264
265 /* Canonical order */
266 if ((mod & ABSTRACT) != 0) sj.add("abstract");
267 if ((mod & STATIC) != 0) sj.add("static");
268 if ((mod & FINAL) != 0) sj.add("final");
269 if ((mod & TRANSIENT) != 0) sj.add("transient");
270 if ((mod & VOLATILE) != 0) sj.add("volatile");
271 if ((mod & SYNCHRONIZED) != 0) sj.add("synchronized");
272 if ((mod & NATIVE) != 0) sj.add("native");
273 if ((mod & STRICT) != 0) sj.add("strictfp");
274 if ((mod & INTERFACE) != 0) sj.add("interface");
275
276 return sj.toString();
277 }
278
279 /*
280 * Access modifier flag constants from tables 4.1, 4.4, 4.5, and 4.7 of
281 * <cite>The Java Virtual Machine Specification</cite>
282 */
283
284 /**
285 * The {@code int} value representing the {@code public}
286 * modifier.
287 * @see AccessFlag#PUBLIC
288 */
289 public static final int PUBLIC = 0x00000001;
290
291 /**
292 * The {@code int} value representing the {@code private}
293 * modifier.
294 * @see AccessFlag#PRIVATE
295 */
296 public static final int PRIVATE = 0x00000002;
297
298 /**
299 * The {@code int} value representing the {@code protected}
300 * modifier.
301 * @see AccessFlag#PROTECTED
302 */
303 public static final int PROTECTED = 0x00000004;
304
305 /**
306 * The {@code int} value representing the {@code static}
307 * modifier.
308 * @see AccessFlag#STATIC
309 */
310 public static final int STATIC = 0x00000008;
311
312 /**
313 * The {@code int} value representing the {@code final}
314 * modifier.
315 * @see AccessFlag#FINAL
316 */
317 public static final int FINAL = 0x00000010;
318
319 /**
320 * The {@code int} value representing the {@code synchronized}
321 * modifier.
322 * @see AccessFlag#SYNCHRONIZED
323 */
324 public static final int SYNCHRONIZED = 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;
346
347 /**
348 * The {@code int} value representing the {@code interface}
349 * modifier.
350 * @see AccessFlag#INTERFACE
351 */
352 public static final int INTERFACE = 0x00000200;
353
354 /**
355 * The {@code int} value representing the {@code abstract}
356 * modifier.
357 * @see AccessFlag#ABSTRACT
358 */
359 public static final int ABSTRACT = 0x00000400;
360
361 /**
362 * The {@code int} value representing the {@code strictfp}
363 * modifier.
364 * @see AccessFlag#STRICT
365 */
366 public static final int STRICT = 0x00000800;
367
368 // Bits not (yet) exposed in the public API either because they
369 // have different meanings for fields and methods and there is no
370 // way to distinguish between the two in this class, or because
371 // they are not Java programming language keywords
372 static final int BRIDGE = 0x00000040;
373 static final int VARARGS = 0x00000080;
374 static final int SYNTHETIC = 0x00001000;
375 static final int ANNOTATION = 0x00002000;
376 static final int ENUM = 0x00004000;
377 static final int MANDATED = 0x00008000;
378 static boolean isSynthetic(int mod) {
379 return (mod & SYNTHETIC) != 0;
380 }
381
382 static boolean isMandated(int mod) {
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.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
424 */
425 private static final int METHOD_MODIFIERS =
426 Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE |
427 Modifier.ABSTRACT | Modifier.STATIC | Modifier.FINAL |
428 Modifier.SYNCHRONIZED | Modifier.NATIVE | Modifier.STRICT;
429
430 /**
431 * The Java source modifiers that can be applied to a field.
432 * @jls 8.3.1 Field Modifiers
433 */
434 private static final int FIELD_MODIFIERS =
435 Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE |
436 Modifier.STATIC | Modifier.FINAL | Modifier.TRANSIENT |
437 Modifier.VOLATILE;
438
439 /**
440 * The Java source modifiers that can be applied to a method or constructor parameter.
441 * @jls 8.4.1 Formal Parameters
442 */
443 private static final int PARAMETER_MODIFIERS =
444 Modifier.FINAL;
445
446 static final int ACCESS_MODIFIERS =
447 Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE;
448
449 /**
450 * Return an {@code int} value OR-ing together the source language
451 * modifiers that can be applied to a class.
452 * @return an {@code int} value OR-ing together the source language
453 * modifiers that can be applied to a class.
454 *
455 * @see AccessFlag.Location#CLASS
456 * @see AccessFlag.Location#INNER_CLASS
457 * @jls 8.1.1 Class Modifiers
458 * @since 1.7
459 */
460 public static int classModifiers() {
461 return CLASS_MODIFIERS;
462 }
463
464 /**
465 * Return an {@code int} value OR-ing together the source language
466 * modifiers that can be applied to an interface.
467 * @return an {@code int} value OR-ing together the source language
468 * modifiers that can be applied to an interface.
469 *
470 * @see AccessFlag.Location#CLASS
471 * @see AccessFlag.Location#INNER_CLASS
472 * @jls 9.1.1 Interface Modifiers
473 * @since 1.7
474 */
475 public static int interfaceModifiers() {
476 return INTERFACE_MODIFIERS;
477 }
478
479 /**
480 * Return an {@code int} value OR-ing together the source language
481 * modifiers that can be applied to a constructor.
482 * @return an {@code int} value OR-ing together the source language
483 * modifiers that can be applied to a constructor.
484 *
485 * @see AccessFlag.Location#METHOD
486 * @jls 8.8.3 Constructor Modifiers
487 * @since 1.7
488 */
489 public static int constructorModifiers() {
490 return CONSTRUCTOR_MODIFIERS;
491 }
492
493 /**
494 * Return an {@code int} value OR-ing together the source language
495 * modifiers that can be applied to a method.
496 * @return an {@code int} value OR-ing together the source language
497 * modifiers that can be applied to a method.
498 *
499 * @see AccessFlag.Location#METHOD
500 * @jls 8.4.3 Method Modifiers
501 * @since 1.7
502 */
503 public static int methodModifiers() {
504 return METHOD_MODIFIERS;
505 }
506
507 /**
508 * Return an {@code int} value OR-ing together the source language
509 * modifiers that can be applied to a field.
510 * @return an {@code int} value OR-ing together the source language
511 * modifiers that can be applied to a field.
512 *
513 * @see AccessFlag.Location#FIELD
514 * @jls 8.3.1 Field Modifiers
515 * @since 1.7
516 */
517 public static int fieldModifiers() {
518 return FIELD_MODIFIERS;
519 }
520
521 /**
522 * Return an {@code int} value OR-ing together the source language
523 * modifiers that can be applied to a parameter.
524 * @return an {@code int} value OR-ing together the source language
525 * modifiers that can be applied to a parameter.
526 *
527 * @see AccessFlag.Location#METHOD_PARAMETER
528 * @jls 8.4.1 Formal Parameters
529 * @since 1.8
530 */
531 public static int parameterModifiers() {
532 return PARAMETER_MODIFIERS;
533 }
534 }