2 * Copyright (c) 1996, 2008, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
26 package java.lang.reflect;
29 * The Modifier class provides {@code static} methods and
30 * constants to decode class and member access modifiers. The sets of
31 * modifiers are represented as integers with distinct bit positions
32 * representing different modifiers. The values for the constants
33 * representing the modifiers are taken from the tables in sections 4.1, 4.4, 4.5, and 4.7 of
34 * <cite>The Java™ Virtual Machine Specification</cite>.
36 * @see Class#getModifiers()
37 * @see Member#getModifiers()
39 * @author Nakul Saraiya
40 * @author Kenneth Russell
46 * Return {@code true} if the integer argument includes the
47 * {@code public} modifier, {@code false} otherwise.
49 * @param mod a set of modifiers
50 * @return {@code true} if {@code mod} includes the
51 * {@code public} modifier; {@code false} otherwise.
53 public static boolean isPublic(int mod) {
54 return (mod & PUBLIC) != 0;
58 * Return {@code true} if the integer argument includes the
59 * {@code private} modifier, {@code false} otherwise.
61 * @param mod a set of modifiers
62 * @return {@code true} if {@code mod} includes the
63 * {@code private} modifier; {@code false} otherwise.
65 public static boolean isPrivate(int mod) {
66 return (mod & PRIVATE) != 0;
70 * Return {@code true} if the integer argument includes the
71 * {@code protected} modifier, {@code false} otherwise.
73 * @param mod a set of modifiers
74 * @return {@code true} if {@code mod} includes the
75 * {@code protected} modifier; {@code false} otherwise.
77 public static boolean isProtected(int mod) {
78 return (mod & PROTECTED) != 0;
82 * Return {@code true} if the integer argument includes the
83 * {@code static} modifier, {@code false} otherwise.
85 * @param mod a set of modifiers
86 * @return {@code true} if {@code mod} includes the
87 * {@code static} modifier; {@code false} otherwise.
89 public static boolean isStatic(int mod) {
90 return (mod & STATIC) != 0;
94 * Return {@code true} if the integer argument includes the
95 * {@code final} modifier, {@code false} otherwise.
97 * @param mod a set of modifiers
98 * @return {@code true} if {@code mod} includes the
99 * {@code final} modifier; {@code false} otherwise.
101 public static boolean isFinal(int mod) {
102 return (mod & FINAL) != 0;
106 * Return {@code true} if the integer argument includes the
107 * {@code synchronized} modifier, {@code false} otherwise.
109 * @param mod a set of modifiers
110 * @return {@code true} if {@code mod} includes the
111 * {@code synchronized} modifier; {@code false} otherwise.
113 public static boolean isSynchronized(int mod) {
114 return (mod & SYNCHRONIZED) != 0;
118 * Return {@code true} if the integer argument includes the
119 * {@code volatile} modifier, {@code false} otherwise.
121 * @param mod a set of modifiers
122 * @return {@code true} if {@code mod} includes the
123 * {@code volatile} modifier; {@code false} otherwise.
125 public static boolean isVolatile(int mod) {
126 return (mod & VOLATILE) != 0;
130 * Return {@code true} if the integer argument includes the
131 * {@code transient} modifier, {@code false} otherwise.
133 * @param mod a set of modifiers
134 * @return {@code true} if {@code mod} includes the
135 * {@code transient} modifier; {@code false} otherwise.
137 public static boolean isTransient(int mod) {
138 return (mod & TRANSIENT) != 0;
142 * Return {@code true} if the integer argument includes the
143 * {@code native} modifier, {@code false} otherwise.
145 * @param mod a set of modifiers
146 * @return {@code true} if {@code mod} includes the
147 * {@code native} modifier; {@code false} otherwise.
149 public static boolean isNative(int mod) {
150 return (mod & NATIVE) != 0;
154 * Return {@code true} if the integer argument includes the
155 * {@code interface} modifier, {@code false} otherwise.
157 * @param mod a set of modifiers
158 * @return {@code true} if {@code mod} includes the
159 * {@code interface} modifier; {@code false} otherwise.
161 public static boolean isInterface(int mod) {
162 return (mod & INTERFACE) != 0;
166 * Return {@code true} if the integer argument includes the
167 * {@code abstract} modifier, {@code false} otherwise.
169 * @param mod a set of modifiers
170 * @return {@code true} if {@code mod} includes the
171 * {@code abstract} modifier; {@code false} otherwise.
173 public static boolean isAbstract(int mod) {
174 return (mod & ABSTRACT) != 0;
178 * Return {@code true} if the integer argument includes the
179 * {@code strictfp} modifier, {@code false} otherwise.
181 * @param mod a set of modifiers
182 * @return {@code true} if {@code mod} includes the
183 * {@code strictfp} modifier; {@code false} otherwise.
185 public static boolean isStrict(int mod) {
186 return (mod & STRICT) != 0;
190 * Return a string describing the access modifier flags in
191 * the specified modifier. For example:
193 * public final synchronized strictfp
194 * </pre></blockquote>
195 * The modifier names are returned in an order consistent with the
196 * suggested modifier orderings given in sections 8.1.1, 8.3.1, 8.4.3, 8.8.3, and 9.1.1 of
197 * <cite>The Java™ Language Specification</cite>.
198 * The full modifier ordering used by this method is:
199 * <blockquote> {@code
200 * public protected private abstract static final transient
201 * volatile synchronized native strictfp
202 * interface } </blockquote>
203 * The {@code interface} modifier discussed in this class is
204 * not a true modifier in the Java language and it appears after
205 * all other modifiers listed by this method. This method may
206 * return a string of modifiers that are not valid modifiers of a
207 * Java entity; in other words, no checking is done on the
208 * possible validity of the combination of modifiers represented
211 * Note that to perform such checking for a known kind of entity,
212 * such as a constructor or method, first AND the argument of
213 * {@code toString} with the appropriate mask from a method like
214 * {@link #constructorModifiers} or {@link #methodModifiers}.
216 * @param mod a set of modifiers
217 * @return a string representation of the set of modifiers
218 * represented by {@code mod}
220 public static String toString(int mod) {
221 StringBuffer sb = new StringBuffer();
224 if ((mod & PUBLIC) != 0) sb.append("public ");
225 if ((mod & PROTECTED) != 0) sb.append("protected ");
226 if ((mod & PRIVATE) != 0) sb.append("private ");
228 /* Canonical order */
229 if ((mod & ABSTRACT) != 0) sb.append("abstract ");
230 if ((mod & STATIC) != 0) sb.append("static ");
231 if ((mod & FINAL) != 0) sb.append("final ");
232 if ((mod & TRANSIENT) != 0) sb.append("transient ");
233 if ((mod & VOLATILE) != 0) sb.append("volatile ");
234 if ((mod & SYNCHRONIZED) != 0) sb.append("synchronized ");
235 if ((mod & NATIVE) != 0) sb.append("native ");
236 if ((mod & STRICT) != 0) sb.append("strictfp ");
237 if ((mod & INTERFACE) != 0) sb.append("interface ");
239 if ((len = sb.length()) > 0) /* trim trailing space */
240 return sb.toString().substring(0, len-1);
245 * Access modifier flag constants from tables 4.1, 4.4, 4.5, and 4.7 of
246 * <cite>The Java™ Virtual Machine Specification</cite>
250 * The {@code int} value representing the {@code public}
253 public static final int PUBLIC = 0x00000001;
256 * The {@code int} value representing the {@code private}
259 public static final int PRIVATE = 0x00000002;
262 * The {@code int} value representing the {@code protected}
265 public static final int PROTECTED = 0x00000004;
268 * The {@code int} value representing the {@code static}
271 public static final int STATIC = 0x00000008;
274 * The {@code int} value representing the {@code final}
277 public static final int FINAL = 0x00000010;
280 * The {@code int} value representing the {@code synchronized}
283 public static final int SYNCHRONIZED = 0x00000020;
286 * The {@code int} value representing the {@code volatile}
289 public static final int VOLATILE = 0x00000040;
292 * The {@code int} value representing the {@code transient}
295 public static final int TRANSIENT = 0x00000080;
298 * The {@code int} value representing the {@code native}
301 public static final int NATIVE = 0x00000100;
304 * The {@code int} value representing the {@code interface}
307 public static final int INTERFACE = 0x00000200;
310 * The {@code int} value representing the {@code abstract}
313 public static final int ABSTRACT = 0x00000400;
316 * The {@code int} value representing the {@code strictfp}
319 public static final int STRICT = 0x00000800;
321 // Bits not (yet) exposed in the public API either because they
322 // have different meanings for fields and methods and there is no
323 // way to distinguish between the two in this class, or because
324 // they are not Java programming language keywords
325 static final int BRIDGE = 0x00000040;
326 static final int VARARGS = 0x00000080;
327 static final int SYNTHETIC = 0x00001000;
328 static final int ANNOTATION= 0x00002000;
329 static final int ENUM = 0x00004000;
330 static boolean isSynthetic(int mod) {
331 return (mod & SYNTHETIC) != 0;
335 * See JLSv3 section 8.1.1.
337 private static final int CLASS_MODIFIERS =
338 Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE |
339 Modifier.ABSTRACT | Modifier.STATIC | Modifier.FINAL |
343 * See JLSv3 section 9.1.1.
345 private static final int INTERFACE_MODIFIERS =
346 Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE |
347 Modifier.ABSTRACT | Modifier.STATIC | Modifier.STRICT;
351 * See JLSv3 section 8.8.3.
353 private static final int CONSTRUCTOR_MODIFIERS =
354 Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE;
357 * See JLSv3 section 8.4.3.
359 private static final int METHOD_MODIFIERS =
360 Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE |
361 Modifier.ABSTRACT | Modifier.STATIC | Modifier.FINAL |
362 Modifier.SYNCHRONIZED | Modifier.NATIVE | Modifier.STRICT;
365 * See JLSv3 section 8.3.1.
367 private static final int FIELD_MODIFIERS =
368 Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE |
369 Modifier.STATIC | Modifier.FINAL | Modifier.TRANSIENT |
373 * Return an {@code int} value OR-ing together the source language
374 * modifiers that can be applied to a class.
375 * @return an {@code int} value OR-ing together the source language
376 * modifiers that can be applied to a class.
378 * @jls 8.1.1 Class Modifiers
381 public static int classModifiers() {
382 return CLASS_MODIFIERS;
386 * Return an {@code int} value OR-ing together the source language
387 * modifiers that can be applied to an interface.
388 * @return an {@code int} value OR-ing together the source language
389 * modifiers that can be applied to an inteface.
391 * @jls 9.1.1 Interface Modifiers
394 public static int interfaceModifiers() {
395 return INTERFACE_MODIFIERS;
399 * Return an {@code int} value OR-ing together the source language
400 * modifiers that can be applied to a constructor.
401 * @return an {@code int} value OR-ing together the source language
402 * modifiers that can be applied to a constructor.
404 * @jls 8.8.3 Constructor Modifiers
407 public static int constructorModifiers() {
408 return CONSTRUCTOR_MODIFIERS;
412 * Return an {@code int} value OR-ing together the source language
413 * modifiers that can be applied to a method.
414 * @return an {@code int} value OR-ing together the source language
415 * modifiers that can be applied to a method.
417 * @jls 8.4.3 Method Modifiers
420 public static int methodModifiers() {
421 return METHOD_MODIFIERS;
426 * Return an {@code int} value OR-ing together the source language
427 * modifiers that can be applied to a field.
428 * @return an {@code int} value OR-ing together the source language
429 * modifiers that can be applied to a field.
431 * @jls 8.3.1 Field Modifiers
434 public static int fieldModifiers() {
435 return FIELD_MODIFIERS;