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;
28 import java.security.AccessController;
29 import sun.reflect.LangReflectAccess;
30 import sun.reflect.ReflectionFactory;
33 * The Modifier class provides {@code static} methods and
34 * constants to decode class and member access modifiers. The sets of
35 * modifiers are represented as integers with distinct bit positions
36 * representing different modifiers. The values for the constants
37 * representing the modifiers are taken from the tables in sections 4.1, 4.4, 4.5, and 4.7 of
38 * <cite>The Java™ Virtual Machine Specification</cite>.
40 * @see Class#getModifiers()
41 * @see Member#getModifiers()
43 * @author Nakul Saraiya
44 * @author Kenneth Russell
50 * Bootstrapping protocol between java.lang and java.lang.reflect
54 sun.reflect.ReflectionFactory factory =
55 AccessController.doPrivileged(
56 new ReflectionFactory.GetReflectionFactoryAction());
57 factory.setLangReflectAccess(new java.lang.reflect.ReflectAccess());
61 * Return {@code true} if the integer argument includes the
62 * {@code public} modifier, {@code false} otherwise.
64 * @param mod a set of modifiers
65 * @return {@code true} if {@code mod} includes the
66 * {@code public} modifier; {@code false} otherwise.
68 public static boolean isPublic(int mod) {
69 return (mod & PUBLIC) != 0;
73 * Return {@code true} if the integer argument includes the
74 * {@code private} modifier, {@code false} otherwise.
76 * @param mod a set of modifiers
77 * @return {@code true} if {@code mod} includes the
78 * {@code private} modifier; {@code false} otherwise.
80 public static boolean isPrivate(int mod) {
81 return (mod & PRIVATE) != 0;
85 * Return {@code true} if the integer argument includes the
86 * {@code protected} modifier, {@code false} otherwise.
88 * @param mod a set of modifiers
89 * @return {@code true} if {@code mod} includes the
90 * {@code protected} modifier; {@code false} otherwise.
92 public static boolean isProtected(int mod) {
93 return (mod & PROTECTED) != 0;
97 * Return {@code true} if the integer argument includes the
98 * {@code static} modifier, {@code false} otherwise.
100 * @param mod a set of modifiers
101 * @return {@code true} if {@code mod} includes the
102 * {@code static} modifier; {@code false} otherwise.
104 public static boolean isStatic(int mod) {
105 return (mod & STATIC) != 0;
109 * Return {@code true} if the integer argument includes the
110 * {@code final} modifier, {@code false} otherwise.
112 * @param mod a set of modifiers
113 * @return {@code true} if {@code mod} includes the
114 * {@code final} modifier; {@code false} otherwise.
116 public static boolean isFinal(int mod) {
117 return (mod & FINAL) != 0;
121 * Return {@code true} if the integer argument includes the
122 * {@code synchronized} modifier, {@code false} otherwise.
124 * @param mod a set of modifiers
125 * @return {@code true} if {@code mod} includes the
126 * {@code synchronized} modifier; {@code false} otherwise.
128 public static boolean isSynchronized(int mod) {
129 return (mod & SYNCHRONIZED) != 0;
133 * Return {@code true} if the integer argument includes the
134 * {@code volatile} modifier, {@code false} otherwise.
136 * @param mod a set of modifiers
137 * @return {@code true} if {@code mod} includes the
138 * {@code volatile} modifier; {@code false} otherwise.
140 public static boolean isVolatile(int mod) {
141 return (mod & VOLATILE) != 0;
145 * Return {@code true} if the integer argument includes the
146 * {@code transient} modifier, {@code false} otherwise.
148 * @param mod a set of modifiers
149 * @return {@code true} if {@code mod} includes the
150 * {@code transient} modifier; {@code false} otherwise.
152 public static boolean isTransient(int mod) {
153 return (mod & TRANSIENT) != 0;
157 * Return {@code true} if the integer argument includes the
158 * {@code native} modifier, {@code false} otherwise.
160 * @param mod a set of modifiers
161 * @return {@code true} if {@code mod} includes the
162 * {@code native} modifier; {@code false} otherwise.
164 public static boolean isNative(int mod) {
165 return (mod & NATIVE) != 0;
169 * Return {@code true} if the integer argument includes the
170 * {@code interface} modifier, {@code false} otherwise.
172 * @param mod a set of modifiers
173 * @return {@code true} if {@code mod} includes the
174 * {@code interface} modifier; {@code false} otherwise.
176 public static boolean isInterface(int mod) {
177 return (mod & INTERFACE) != 0;
181 * Return {@code true} if the integer argument includes the
182 * {@code abstract} modifier, {@code false} otherwise.
184 * @param mod a set of modifiers
185 * @return {@code true} if {@code mod} includes the
186 * {@code abstract} modifier; {@code false} otherwise.
188 public static boolean isAbstract(int mod) {
189 return (mod & ABSTRACT) != 0;
193 * Return {@code true} if the integer argument includes the
194 * {@code strictfp} modifier, {@code false} otherwise.
196 * @param mod a set of modifiers
197 * @return {@code true} if {@code mod} includes the
198 * {@code strictfp} modifier; {@code false} otherwise.
200 public static boolean isStrict(int mod) {
201 return (mod & STRICT) != 0;
205 * Return a string describing the access modifier flags in
206 * the specified modifier. For example:
208 * public final synchronized strictfp
209 * </pre></blockquote>
210 * The modifier names are returned in an order consistent with the
211 * suggested modifier orderings given in sections 8.1.1, 8.3.1, 8.4.3, 8.8.3, and 9.1.1 of
212 * <cite>The Java™ Language Specification</cite>.
213 * The full modifier ordering used by this method is:
214 * <blockquote> {@code
215 * public protected private abstract static final transient
216 * volatile synchronized native strictfp
217 * interface } </blockquote>
218 * The {@code interface} modifier discussed in this class is
219 * not a true modifier in the Java language and it appears after
220 * all other modifiers listed by this method. This method may
221 * return a string of modifiers that are not valid modifiers of a
222 * Java entity; in other words, no checking is done on the
223 * possible validity of the combination of modifiers represented
226 * Note that to perform such checking for a known kind of entity,
227 * such as a constructor or method, first AND the argument of
228 * {@code toString} with the appropriate mask from a method like
229 * {@link #constructorModifiers} or {@link #methodModifiers}.
231 * @param mod a set of modifiers
232 * @return a string representation of the set of modifiers
233 * represented by {@code mod}
235 public static String toString(int mod) {
236 StringBuffer sb = new StringBuffer();
239 if ((mod & PUBLIC) != 0) sb.append("public ");
240 if ((mod & PROTECTED) != 0) sb.append("protected ");
241 if ((mod & PRIVATE) != 0) sb.append("private ");
243 /* Canonical order */
244 if ((mod & ABSTRACT) != 0) sb.append("abstract ");
245 if ((mod & STATIC) != 0) sb.append("static ");
246 if ((mod & FINAL) != 0) sb.append("final ");
247 if ((mod & TRANSIENT) != 0) sb.append("transient ");
248 if ((mod & VOLATILE) != 0) sb.append("volatile ");
249 if ((mod & SYNCHRONIZED) != 0) sb.append("synchronized ");
250 if ((mod & NATIVE) != 0) sb.append("native ");
251 if ((mod & STRICT) != 0) sb.append("strictfp ");
252 if ((mod & INTERFACE) != 0) sb.append("interface ");
254 if ((len = sb.length()) > 0) /* trim trailing space */
255 return sb.toString().substring(0, len-1);
260 * Access modifier flag constants from tables 4.1, 4.4, 4.5, and 4.7 of
261 * <cite>The Java™ Virtual Machine Specification</cite>
265 * The {@code int} value representing the {@code public}
268 public static final int PUBLIC = 0x00000001;
271 * The {@code int} value representing the {@code private}
274 public static final int PRIVATE = 0x00000002;
277 * The {@code int} value representing the {@code protected}
280 public static final int PROTECTED = 0x00000004;
283 * The {@code int} value representing the {@code static}
286 public static final int STATIC = 0x00000008;
289 * The {@code int} value representing the {@code final}
292 public static final int FINAL = 0x00000010;
295 * The {@code int} value representing the {@code synchronized}
298 public static final int SYNCHRONIZED = 0x00000020;
301 * The {@code int} value representing the {@code volatile}
304 public static final int VOLATILE = 0x00000040;
307 * The {@code int} value representing the {@code transient}
310 public static final int TRANSIENT = 0x00000080;
313 * The {@code int} value representing the {@code native}
316 public static final int NATIVE = 0x00000100;
319 * The {@code int} value representing the {@code interface}
322 public static final int INTERFACE = 0x00000200;
325 * The {@code int} value representing the {@code abstract}
328 public static final int ABSTRACT = 0x00000400;
331 * The {@code int} value representing the {@code strictfp}
334 public static final int STRICT = 0x00000800;
336 // Bits not (yet) exposed in the public API either because they
337 // have different meanings for fields and methods and there is no
338 // way to distinguish between the two in this class, or because
339 // they are not Java programming language keywords
340 static final int BRIDGE = 0x00000040;
341 static final int VARARGS = 0x00000080;
342 static final int SYNTHETIC = 0x00001000;
343 static final int ANNOTATION= 0x00002000;
344 static final int ENUM = 0x00004000;
345 static boolean isSynthetic(int mod) {
346 return (mod & SYNTHETIC) != 0;
350 * See JLSv3 section 8.1.1.
352 private static final int CLASS_MODIFIERS =
353 Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE |
354 Modifier.ABSTRACT | Modifier.STATIC | Modifier.FINAL |
358 * See JLSv3 section 9.1.1.
360 private static final int INTERFACE_MODIFIERS =
361 Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE |
362 Modifier.ABSTRACT | Modifier.STATIC | Modifier.STRICT;
366 * See JLSv3 section 8.8.3.
368 private static final int CONSTRUCTOR_MODIFIERS =
369 Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE;
372 * See JLSv3 section 8.4.3.
374 private static final int METHOD_MODIFIERS =
375 Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE |
376 Modifier.ABSTRACT | Modifier.STATIC | Modifier.FINAL |
377 Modifier.SYNCHRONIZED | Modifier.NATIVE | Modifier.STRICT;
380 * See JLSv3 section 8.3.1.
382 private static final int FIELD_MODIFIERS =
383 Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE |
384 Modifier.STATIC | Modifier.FINAL | Modifier.TRANSIENT |
388 * Return an {@code int} value OR-ing together the source language
389 * modifiers that can be applied to a class.
390 * @return an {@code int} value OR-ing together the source language
391 * modifiers that can be applied to a class.
393 * @jls 8.1.1 Class Modifiers
396 public static int classModifiers() {
397 return CLASS_MODIFIERS;
401 * Return an {@code int} value OR-ing together the source language
402 * modifiers that can be applied to an interface.
403 * @return an {@code int} value OR-ing together the source language
404 * modifiers that can be applied to an inteface.
406 * @jls 9.1.1 Interface Modifiers
409 public static int interfaceModifiers() {
410 return INTERFACE_MODIFIERS;
414 * Return an {@code int} value OR-ing together the source language
415 * modifiers that can be applied to a constructor.
416 * @return an {@code int} value OR-ing together the source language
417 * modifiers that can be applied to a constructor.
419 * @jls 8.8.3 Constructor Modifiers
422 public static int constructorModifiers() {
423 return CONSTRUCTOR_MODIFIERS;
427 * Return an {@code int} value OR-ing together the source language
428 * modifiers that can be applied to a method.
429 * @return an {@code int} value OR-ing together the source language
430 * modifiers that can be applied to a method.
432 * @jls 8.4.3 Method Modifiers
435 public static int methodModifiers() {
436 return METHOD_MODIFIERS;
441 * Return an {@code int} value OR-ing together the source language
442 * modifiers that can be applied to a field.
443 * @return an {@code int} value OR-ing together the source language
444 * modifiers that can be applied to a field.
446 * @jls 8.3.1 Field Modifiers
449 public static int fieldModifiers() {
450 return FIELD_MODIFIERS;