001 /*
002 * Copyright 1996-2004 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.lang.reflect;
027
028 import java.security.AccessController;
029 import sun.reflect.LangReflectAccess;
030 import sun.reflect.ReflectionFactory;
031
032 /**
033 * The Modifier class provides {@code static} methods and
034 * constants to decode class and member access modifiers. The sets of
035 * modifiers are represented as integers with distinct bit positions
036 * representing different modifiers. The values for the constants
037 * representing the modifiers are taken from <a
038 * href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/VMSpecTOC.doc.html"><i>The
039 * Java</i><sup><small>TM</small></sup> <i>Virtual Machine Specification, Second
040 * edition</i></a> tables
041 * <a href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#75734">4.1</a>,
042 * <a href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#88358">4.4</a>,
043 * <a href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#75568">4.5</a>, and
044 * <a href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#88478">4.7</a>.
045 *
046 * @see Class#getModifiers()
047 * @see Member#getModifiers()
048 *
049 * @author Nakul Saraiya
050 * @author Kenneth Russell
051 */
052 public class Modifier {
053
054 /*
055 * Bootstrapping protocol between java.lang and java.lang.reflect
056 * packages
057 */
058 static {
059 sun.reflect.ReflectionFactory factory = (sun.reflect.ReflectionFactory) AccessController
060 .doPrivileged(new ReflectionFactory.GetReflectionFactoryAction());
061 factory
062 .setLangReflectAccess(new java.lang.reflect.ReflectAccess());
063 }
064
065 /**
066 * Return {@code true} if the integer argument includes the
067 * {@code public} modifier, {@code false} otherwise.
068 *
069 * @param mod a set of modifiers
070 * @return {@code true} if {@code mod} includes the
071 * {@code public} modifier; {@code false} otherwise.
072 */
073 public static boolean isPublic(int mod) {
074 return (mod & PUBLIC) != 0;
075 }
076
077 /**
078 * Return {@code true} if the integer argument includes the
079 * {@code private} modifier, {@code false} otherwise.
080 *
081 * @param mod a set of modifiers
082 * @return {@code true} if {@code mod} includes the
083 * {@code private} modifier; {@code false} otherwise.
084 */
085 public static boolean isPrivate(int mod) {
086 return (mod & PRIVATE) != 0;
087 }
088
089 /**
090 * Return {@code true} if the integer argument includes the
091 * {@code protected} modifier, {@code false} otherwise.
092 *
093 * @param mod a set of modifiers
094 * @return {@code true} if {@code mod} includes the
095 * {@code protected} modifier; {@code false} otherwise.
096 */
097 public static boolean isProtected(int mod) {
098 return (mod & PROTECTED) != 0;
099 }
100
101 /**
102 * Return {@code true} if the integer argument includes the
103 * {@code static} modifier, {@code false} otherwise.
104 *
105 * @param mod a set of modifiers
106 * @return {@code true} if {@code mod} includes the
107 * {@code static} modifier; {@code false} otherwise.
108 */
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 * @param mod a set of modifiers
130 * @return {@code true} if {@code mod} includes the
131 * {@code synchronized} modifier; {@code false} otherwise.
132 */
133 public static boolean isSynchronized(int mod) {
134 return (mod & SYNCHRONIZED) != 0;
135 }
136
137 /**
138 * Return {@code true} if the integer argument includes the
139 * {@code volatile} modifier, {@code false} otherwise.
140 *
141 * @param mod a set of modifiers
142 * @return {@code true} if {@code mod} includes the
143 * {@code volatile} modifier; {@code false} otherwise.
144 */
145 public static boolean isVolatile(int mod) {
146 return (mod & VOLATILE) != 0;
147 }
148
149 /**
150 * Return {@code true} if the integer argument includes the
151 * {@code transient} modifier, {@code false} otherwise.
152 *
153 * @param mod a set of modifiers
154 * @return {@code true} if {@code mod} includes the
155 * {@code transient} modifier; {@code false} otherwise.
156 */
157 public static boolean isTransient(int mod) {
158 return (mod & TRANSIENT) != 0;
159 }
160
161 /**
162 * Return {@code true} if the integer argument includes the
163 * {@code native} modifier, {@code false} otherwise.
164 *
165 * @param mod a set of modifiers
166 * @return {@code true} if {@code mod} includes the
167 * {@code native} modifier; {@code false} otherwise.
168 */
169 public static boolean isNative(int mod) {
170 return (mod & NATIVE) != 0;
171 }
172
173 /**
174 * Return {@code true} if the integer argument includes the
175 * {@code interface} modifier, {@code false} otherwise.
176 *
177 * @param mod a set of modifiers
178 * @return {@code true} if {@code mod} includes the
179 * {@code interface} modifier; {@code false} otherwise.
180 */
181 public static boolean isInterface(int mod) {
182 return (mod & INTERFACE) != 0;
183 }
184
185 /**
186 * Return {@code true} if the integer argument includes the
187 * {@code abstract} modifier, {@code false} otherwise.
188 *
189 * @param mod a set of modifiers
190 * @return {@code true} if {@code mod} includes the
191 * {@code abstract} modifier; {@code false} otherwise.
192 */
193 public static boolean isAbstract(int mod) {
194 return (mod & ABSTRACT) != 0;
195 }
196
197 /**
198 * Return {@code true} if the integer argument includes the
199 * {@code strictfp} modifier, {@code false} otherwise.
200 *
201 * @param mod a set of modifiers
202 * @return {@code true} if {@code mod} includes the
203 * {@code strictfp} modifier; {@code false} otherwise.
204 */
205 public static boolean isStrict(int mod) {
206 return (mod & STRICT) != 0;
207 }
208
209 /**
210 * Return a string describing the access modifier flags in
211 * the specified modifier. For example:
212 * <blockquote><pre>
213 * public final synchronized strictfp
214 * </pre></blockquote>
215 * The modifier names are returned in an order consistent with the
216 * suggested modifier orderings given in <a
217 * href="http://java.sun.com/docs/books/jls/second_edition/html/j.title.doc.html"><em>The
218 * Java Language Specification, Second Edition</em></a> sections
219 * <a href="http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#21613">§8.1.1</a>,
220 * <a href="http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#78091">§8.3.1</a>,
221 * <a href="http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#78188">§8.4.3</a>,
222 * <a href="http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#42018">§8.8.3</a>, and
223 * <a href="http://java.sun.com/docs/books/jls/second_edition/html/interfaces.doc.html#235947">§9.1.1</a>.
224 * The full modifier ordering used by this method is:
225 * <blockquote> {@code
226 * public protected private abstract static final transient
227 * volatile synchronized native strictfp
228 * interface } </blockquote>
229 * The {@code interface} modifier discussed in this class is
230 * not a true modifier in the Java language and it appears after
231 * all other modifiers listed by this method. This method may
232 * return a string of modifiers that are not valid modifiers of a
233 * Java entity; in other words, no checking is done on the
234 * possible validity of the combination of modifiers represented
235 * by the input.
236 *
237 * @param mod a set of modifiers
238 * @return a string representation of the set of modifiers
239 * represented by {@code mod}
240 */
241 public static String toString(int mod) {
242 StringBuffer sb = new StringBuffer();
243 int len;
244
245 if ((mod & PUBLIC) != 0)
246 sb.append("public ");
247 if ((mod & PROTECTED) != 0)
248 sb.append("protected ");
249 if ((mod & PRIVATE) != 0)
250 sb.append("private ");
251
252 /* Canonical order */
253 if ((mod & ABSTRACT) != 0)
254 sb.append("abstract ");
255 if ((mod & STATIC) != 0)
256 sb.append("static ");
257 if ((mod & FINAL) != 0)
258 sb.append("final ");
259 if ((mod & TRANSIENT) != 0)
260 sb.append("transient ");
261 if ((mod & VOLATILE) != 0)
262 sb.append("volatile ");
263 if ((mod & SYNCHRONIZED) != 0)
264 sb.append("synchronized ");
265 if ((mod & NATIVE) != 0)
266 sb.append("native ");
267 if ((mod & STRICT) != 0)
268 sb.append("strictfp ");
269 if ((mod & INTERFACE) != 0)
270 sb.append("interface ");
271
272 if ((len = sb.length()) > 0) /* trim trailing space */
273 return sb.toString().substring(0, len - 1);
274 return "";
275 }
276
277 /*
278 * Access modifier flag constants from <em>The Java Virtual
279 * Machine Specification, Second Edition</em>, tables 4.1, 4.4,
280 * 4.5, and 4.7.
281 */
282
283 /**
284 * The {@code int} value representing the {@code public}
285 * modifier.
286 */
287 public static final int PUBLIC = 0x00000001;
288
289 /**
290 * The {@code int} value representing the {@code private}
291 * modifier.
292 */
293 public static final int PRIVATE = 0x00000002;
294
295 /**
296 * The {@code int} value representing the {@code protected}
297 * modifier.
298 */
299 public static final int PROTECTED = 0x00000004;
300
301 /**
302 * The {@code int} value representing the {@code static}
303 * modifier.
304 */
305 public static final int STATIC = 0x00000008;
306
307 /**
308 * The {@code int} value representing the {@code final}
309 * modifier.
310 */
311 public static final int FINAL = 0x00000010;
312
313 /**
314 * The {@code int} value representing the {@code synchronized}
315 * modifier.
316 */
317 public static final int SYNCHRONIZED = 0x00000020;
318
319 /**
320 * The {@code int} value representing the {@code volatile}
321 * modifier.
322 */
323 public static final int VOLATILE = 0x00000040;
324
325 /**
326 * The {@code int} value representing the {@code transient}
327 * modifier.
328 */
329 public static final int TRANSIENT = 0x00000080;
330
331 /**
332 * The {@code int} value representing the {@code native}
333 * modifier.
334 */
335 public static final int NATIVE = 0x00000100;
336
337 /**
338 * The {@code int} value representing the {@code interface}
339 * modifier.
340 */
341 public static final int INTERFACE = 0x00000200;
342
343 /**
344 * The {@code int} value representing the {@code abstract}
345 * modifier.
346 */
347 public static final int ABSTRACT = 0x00000400;
348
349 /**
350 * The {@code int} value representing the {@code strictfp}
351 * modifier.
352 */
353 public static final int STRICT = 0x00000800;
354
355 // Bits not (yet) exposed in the public API either because they
356 // have different meanings for fields and methods and there is no
357 // way to distinguish between the two in this class, or because
358 // they are not Java programming language keywords
359 static final int BRIDGE = 0x00000040;
360 static final int VARARGS = 0x00000080;
361 static final int SYNTHETIC = 0x00001000;
362 static final int ANNOTATION = 0x00002000;
363 static final int ENUM = 0x00004000;
364
365 static boolean isSynthetic(int mod) {
366 return (mod & SYNTHETIC) != 0;
367 }
368 }
|