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 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.
 65      *
 66      * @param   mod a set of modifiers
 67      * @return {@code true} if {@code mod} includes the
 68      * {@code public} modifier; {@code false} otherwise.
 69      */
 70     public static boolean isPublic(int mod) {
 71         return (mod & PUBLIC) != 0;
 72     }
 73 
 74     /**
 75      * Return {@code true} if the integer argument includes the
 76      * {@code private} modifier, {@code false} otherwise.
 77      *
 78      * @param   mod a set of modifiers
 79      * @return {@code true} if {@code mod} includes the
 80      * {@code private} modifier; {@code false} otherwise.
 81      */
 82     public static boolean isPrivate(int mod) {
 83         return (mod & PRIVATE) != 0;
 84     }
 85 
 86     /**
 87      * Return {@code true} if the integer argument includes the
 88      * {@code protected} modifier, {@code false} otherwise.
 89      *
 90      * @param   mod a set of modifiers
 91      * @return {@code true} if {@code mod} includes the
 92      * {@code protected} modifier; {@code false} otherwise.
 93      */
 94     public static boolean isProtected(int mod) {
 95         return (mod & PROTECTED) != 0;
 96     }
 97 
 98     /**
 99      * Return {@code true} if the integer argument includes the
100      * {@code static} modifier, {@code false} otherwise.
101      *
102      * @param   mod a set of modifiers
103      * @return {@code true} if {@code mod} includes the
104      * {@code static} modifier; {@code false} otherwise.
105      */
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      */
154     public static boolean isTransient(int mod) {
155         return (mod & TRANSIENT) != 0;
156     }
157 
158     /**
159      * Return {@code true} if the integer argument includes the
160      * {@code native} modifier, {@code false} otherwise.
161      *
162      * @param   mod a set of modifiers
163      * @return {@code true} if {@code mod} includes the
164      * {@code native} modifier; {@code false} otherwise.
165      */
166     public static boolean isNative(int mod) {
167         return (mod & NATIVE) != 0;
168     }
169 
170     /**
171      * Return {@code true} if the integer argument includes the
172      * {@code interface} modifier, {@code false} otherwise.
173      *
174      * @param   mod a set of modifiers
175      * @return {@code true} if {@code mod} includes the
176      * {@code interface} modifier; {@code false} otherwise.
177      */
178     public static boolean isInterface(int mod) {
179         return (mod & INTERFACE) != 0;
180     }
181 
182     /**
183      * Return {@code true} if the integer argument includes the
184      * {@code abstract} modifier, {@code false} otherwise.
185      *
186      * @param   mod a set of modifiers
187      * @return {@code true} if {@code mod} includes the
188      * {@code abstract} modifier; {@code false} otherwise.
189      */
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
231      * {@code toString} with the appropriate mask from a method like
232      * {@link #constructorModifiers} or {@link #methodModifiers}.
233      *
234      * @apiNote
235      * To make a high-fidelity representation of the Java source
236      * modifiers of a class or member, source-level modifiers that do
237      * <em>not</em> have a constant in this class should be included
238      * and appear in an order consistent with the full recommended
239      * ordering for that kind of declaration as given in <cite>The
240      * Java Language Specification</cite>. For example, for a
241      * {@linkplain Method#toGenericString() method} the "{@link
242      * Method#isDefault() default}" modifier is ordered immediately
243      * before "{@code static}" (JLS {@jls 9.4}). For a {@linkplain
244      * Class#toGenericString() class object}, the "{@link
245      * Class#isSealed() sealed}" or {@code "non-sealed"} modifier is
246      * ordered immediately after "{@code final}" for a class (JLS
247      * {@jls 8.1.1}) and immediately after "{@code static}" for an
248      * interface (JLS {@jls 9.1.1}).
249      *
250      * @param   mod a set of modifiers
251      * @return  a string representation of the set of modifiers
252      * represented by {@code mod}
253      */
254     public static String toString(int mod) {
255         StringJoiner sj = new StringJoiner(" ");
256 
257         if ((mod & PUBLIC) != 0)        sj.add("public");
258         if ((mod & PROTECTED) != 0)     sj.add("protected");
259         if ((mod & PRIVATE) != 0)       sj.add("private");
260 
261         /* Canonical order */
262         if ((mod & ABSTRACT) != 0)      sj.add("abstract");
263         if ((mod & STATIC) != 0)        sj.add("static");
264         if ((mod & FINAL) != 0)         sj.add("final");
265         if ((mod & TRANSIENT) != 0)     sj.add("transient");
266         if ((mod & VOLATILE) != 0)      sj.add("volatile");
267         if ((mod & SYNCHRONIZED) != 0)  sj.add("synchronized");
268         if ((mod & NATIVE) != 0)        sj.add("native");
269         if ((mod & STRICT) != 0)        sj.add("strictfp");
270         if ((mod & INTERFACE) != 0)     sj.add("interface");
271 
272         return sj.toString();
273     }
274 
275     /*
276      * Access modifier flag constants from tables 4.1, 4.4, 4.5, and 4.7 of
277      * <cite>The Java Virtual Machine Specification</cite>
278      */
279 
280     /**
281      * The {@code int} value representing the {@code public}
282      * modifier.
283      * @see AccessFlag#PUBLIC
284      */
285     public static final int PUBLIC           = 0x00000001;
286 
287     /**
288      * The {@code int} value representing the {@code private}
289      * modifier.
290      * @see AccessFlag#PRIVATE
291      */
292     public static final int PRIVATE          = 0x00000002;
293 
294     /**
295      * The {@code int} value representing the {@code protected}
296      * modifier.
297      * @see AccessFlag#PROTECTED
298      */
299     public static final int PROTECTED        = 0x00000004;
300 
301     /**
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      */
454     public static int classModifiers() {
455         return CLASS_MODIFIERS;
456     }
457 
458     /**
459      * Return an {@code int} value OR-ing together the source language
460      * modifiers that can be applied to an interface.
461      * @return an {@code int} value OR-ing together the source language
462      * modifiers that can be applied to an interface.
463      *
464      * @jls 9.1.1 Interface Modifiers
465      * @since 1.7
466      */
467     public static int interfaceModifiers() {
468         return INTERFACE_MODIFIERS;
469     }
470 
471     /**
472      * Return an {@code int} value OR-ing together the source language
473      * modifiers that can be applied to a constructor.
474      * @return an {@code int} value OR-ing together the source language
475      * modifiers that can be applied to a constructor.
476      *
477      * @jls 8.8.3 Constructor Modifiers
478      * @since 1.7
479      */
480     public static int constructorModifiers() {
481         return CONSTRUCTOR_MODIFIERS;
482     }
483 
484     /**
485      * Return an {@code int} value OR-ing together the source language
486      * modifiers that can be applied to a method.
487      * @return an {@code int} value OR-ing together the source language
488      * modifiers that can be applied to a method.
489      *
490      * @jls 8.4.3 Method Modifiers
491      * @since 1.7
492      */
493     public static int methodModifiers() {
494         return METHOD_MODIFIERS;
495     }
496 
497     /**
498      * Return an {@code int} value OR-ing together the source language
499      * modifiers that can be applied to a field.
500      * @return an {@code int} value OR-ing together the source language
501      * modifiers that can be applied to a field.
502      *
503      * @jls 8.3.1 Field Modifiers
504      * @since 1.7
505      */
506     public static int fieldModifiers() {
507         return FIELD_MODIFIERS;
508     }
509 
510     /**
511      * Return an {@code int} value OR-ing together the source language
512      * modifiers that can be applied to a parameter.
513      * @return an {@code int} value OR-ing together the source language
514      * modifiers that can be applied to a parameter.
515      *
516      * @jls 8.4.1 Formal Parameters
517      * @since 1.8
518      */
519     public static int parameterModifiers() {
520         return PARAMETER_MODIFIERS;
521     }
522 }