< 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 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.

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 

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