< prev index next >

src/java.base/share/classes/java/lang/reflect/Modifier.java

Print this page

  1 /*
  2  * Copyright (c) 1996, 2025, 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 final 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.

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 

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

  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.

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 

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
< prev index next >