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
 59      * {@code public} modifier; {@code false} otherwise.
 60      */
 61     public static boolean isPublic(int mod) {
 62         return (mod & PUBLIC) != 0;
 63     }
 64 
 65     /**
 66      * Return {@code true} if the integer argument includes the
 67      * {@code private} modifier, {@code false} otherwise.
 68      *
 69      * @param   mod a set of modifiers
 70      * @return {@code true} if {@code mod} includes the
 71      * {@code private} modifier; {@code false} otherwise.
 72      */
 73     public static boolean isPrivate(int mod) {
 74         return (mod & PRIVATE) != 0;
 75     }
 76 
 77     /**
 78      * Return {@code true} if the integer argument includes the
 79      * {@code protected} modifier, {@code false} otherwise.
 80      *
 81      * @param   mod a set of modifiers
 82      * @return {@code true} if {@code mod} includes the
 83      * {@code protected} modifier; {@code false} otherwise.
 84      */
 85     public static boolean isProtected(int mod) {
 86         return (mod & PROTECTED) != 0;
 87     }
 88 
 89     /**
 90      * Return {@code true} if the integer argument includes the
 91      * {@code static} modifier, {@code false} otherwise.
 92      *
 93      * @param   mod a set of modifiers
 94      * @return {@code true} if {@code mod} includes the
 95      * {@code static} modifier; {@code false} otherwise.
 96      */
 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      */
145     public static boolean isTransient(int mod) {
146         return (mod & TRANSIENT) != 0;
147     }
148 
149     /**
150      * Return {@code true} if the integer argument includes the
151      * {@code native} modifier, {@code false} otherwise.
152      *
153      * @param   mod a set of modifiers
154      * @return {@code true} if {@code mod} includes the
155      * {@code native} modifier; {@code false} otherwise.
156      */
157     public static boolean isNative(int mod) {
158         return (mod & NATIVE) != 0;
159     }
160 
161     /**
162      * Return {@code true} if the integer argument includes the
163      * {@code interface} modifier, {@code false} otherwise.
164      *
165      * @param   mod a set of modifiers
166      * @return {@code true} if {@code mod} includes the
167      * {@code interface} modifier; {@code false} otherwise.
168      */
169     public static boolean isInterface(int mod) {
170         return (mod & INTERFACE) != 0;
171     }
172 
173     /**
174      * Return {@code true} if the integer argument includes the
175      * {@code abstract} modifier, {@code false} otherwise.
176      *
177      * @param   mod a set of modifiers
178      * @return {@code true} if {@code mod} includes the
179      * {@code abstract} modifier; {@code false} otherwise.
180      */
181     public static boolean isAbstract(int mod) {
182         return (mod & ABSTRACT) != 0;
183     }
184 
185     /**
186      * Return {@code true} if the integer argument includes the
187      * {@code strictfp} modifier, {@code false} otherwise.
188      *
189      * @param   mod a set of modifiers
190      * @return {@code true} if {@code mod} includes the
191      * {@code strictfp} modifier; {@code false} otherwise.
192      */
193     public static boolean isStrict(int mod) {
194         return (mod & STRICT) != 0;
195     }
196 
197     /**
198      * Return a string describing the access modifier flags in
199      * the specified modifier. For example:
200      * <blockquote><pre>
201      *    public final synchronized strictfp
202      * </pre></blockquote>
203      * The modifier names are returned in an order consistent with the
204      * suggested modifier orderings given in sections 8.1.1, 8.3.1, 8.4.3, 8.8.3, and 9.1.1 of
205      * <cite>The Java Language Specification</cite>.
206      * The full modifier ordering used by this method is:
207      * <blockquote> {@code
208      * public protected private abstract static final transient
209      * volatile synchronized native strictfp
210      * interface } </blockquote>
211      * The {@code interface} modifier discussed in this class is
212      * not a true modifier in the Java language and it appears after
213      * all other modifiers listed by this method.  This method may
214      * return a string of modifiers that are not valid modifiers of a
215      * Java entity; in other words, no checking is done on the
216      * possible validity of the combination of modifiers represented
217      * by the input.
218      *
219      * Note that to perform such checking for a known kind of entity,
220      * such as a constructor or method, first AND the argument of
221      * {@code toString} with the appropriate mask from a method like
222      * {@link #constructorModifiers} or {@link #methodModifiers}.
223      *
224      * @param   mod a set of modifiers
225      * @return  a string representation of the set of modifiers
226      * represented by {@code mod}
227      */
228     public static String toString(int mod) {
229         StringJoiner sj = new StringJoiner(" ");
230 
231         if ((mod & PUBLIC) != 0)        sj.add("public");
232         if ((mod & PROTECTED) != 0)     sj.add("protected");
233         if ((mod & PRIVATE) != 0)       sj.add("private");
234 
235         /* Canonical order */
236         if ((mod & ABSTRACT) != 0)      sj.add("abstract");
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 final int FLATTENED   = 0x00008000;      // HotSpot-specific bit
337     static boolean isSynthetic(int mod) {
338       return (mod & SYNTHETIC) != 0;
339     }
340 
341     static boolean isMandated(int mod) {
342       return (mod & MANDATED) != 0;
343     }
344 
345     // Note on the FOO_MODIFIERS fields and fooModifiers() methods:
346     // the sets of modifiers are not guaranteed to be constants
347     // across time and Java SE releases. Therefore, it would not be
348     // appropriate to expose an external interface to this information
349     // that would allow the values to be treated as Java-level
350     // constants since the values could be constant folded and updates
351     // to the sets of modifiers missed. Thus, the fooModifiers()
352     // methods return an unchanging values for a given release, but a
353     // value that can potentially change over time.
354 
355     /**
356      * The Java source modifiers that can be applied to a class.
357      * @jls 8.1.1 Class Modifiers
358      */
359     private static final int CLASS_MODIFIERS =
360         Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE |
361         Modifier.ABSTRACT       | Modifier.STATIC       | Modifier.FINAL   |
362         Modifier.STRICT;
363 
364     /**
365      * The Java source modifiers that can be applied to an interface.
366      * @jls 9.1.1 Interface Modifiers
367      */
368     private static final int INTERFACE_MODIFIERS =
369         Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE |
370         Modifier.ABSTRACT       | Modifier.STATIC       | Modifier.STRICT;
371 
372 
373     /**
374      * The Java source modifiers that can be applied to a constructor.
375      * @jls 8.8.3 Constructor Modifiers
376      */
377     private static final int CONSTRUCTOR_MODIFIERS =
378         Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE;
379 
380     /**
381      * The Java source modifiers that can be applied to a method.
382      * @jls 8.4.3  Method Modifiers
383      */
384     private static final int METHOD_MODIFIERS =
385         Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE |
386         Modifier.ABSTRACT       | Modifier.STATIC       | Modifier.FINAL   |
387         Modifier.SYNCHRONIZED   | Modifier.NATIVE       | Modifier.STRICT;
388 
389     /**
390      * The Java source modifiers that can be applied to a field.
391      * @jls 8.3.1 Field Modifiers
392      */
393     private static final int FIELD_MODIFIERS =
394         Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE |
395         Modifier.STATIC         | Modifier.FINAL        | Modifier.TRANSIENT |
396         Modifier.VOLATILE;
397 
398     /**
399      * The Java source modifiers that can be applied to a method or constructor parameter.
400      * @jls 8.4.1 Formal Parameters
401      */
402     private static final int PARAMETER_MODIFIERS =
403         Modifier.FINAL;
404 
405     static final int ACCESS_MODIFIERS =
406         Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE;
407 
408     /**
409      * Return an {@code int} value OR-ing together the source language
410      * modifiers that can be applied to a class.
411      * @return an {@code int} value OR-ing together the source language
412      * modifiers that can be applied to a class.
413      *
414      * @jls 8.1.1 Class Modifiers
415      * @since 1.7
416      */
417     public static int classModifiers() {
418         return CLASS_MODIFIERS;
419     }
420 
421     /**
422      * Return an {@code int} value OR-ing together the source language
423      * modifiers that can be applied to an interface.
424      * @return an {@code int} value OR-ing together the source language
425      * modifiers that can be applied to an interface.
426      *
427      * @jls 9.1.1 Interface Modifiers
428      * @since 1.7
429      */
430     public static int interfaceModifiers() {
431         return INTERFACE_MODIFIERS;
432     }
433 
434     /**
435      * Return an {@code int} value OR-ing together the source language
436      * modifiers that can be applied to a constructor.
437      * @return an {@code int} value OR-ing together the source language
438      * modifiers that can be applied to a constructor.
439      *
440      * @jls 8.8.3 Constructor Modifiers
441      * @since 1.7
442      */
443     public static int constructorModifiers() {
444         return CONSTRUCTOR_MODIFIERS;
445     }
446 
447     /**
448      * Return an {@code int} value OR-ing together the source language
449      * modifiers that can be applied to a method.
450      * @return an {@code int} value OR-ing together the source language
451      * modifiers that can be applied to a method.
452      *
453      * @jls 8.4.3 Method Modifiers
454      * @since 1.7
455      */
456     public static int methodModifiers() {
457         return METHOD_MODIFIERS;
458     }
459 
460     /**
461      * Return an {@code int} value OR-ing together the source language
462      * modifiers that can be applied to a field.
463      * @return an {@code int} value OR-ing together the source language
464      * modifiers that can be applied to a field.
465      *
466      * @jls 8.3.1 Field Modifiers
467      * @since 1.7
468      */
469     public static int fieldModifiers() {
470         return FIELD_MODIFIERS;
471     }
472 
473     /**
474      * Return an {@code int} value OR-ing together the source language
475      * modifiers that can be applied to a parameter.
476      * @return an {@code int} value OR-ing together the source language
477      * modifiers that can be applied to a parameter.
478      *
479      * @jls 8.4.1 Formal Parameters
480      * @since 1.8
481      */
482     public static int parameterModifiers() {
483         return PARAMETER_MODIFIERS;
484     }
485 }