2
|
1 |
/*
|
|
2 |
* Copyright 1996-2004 Sun Microsystems, Inc. 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. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package java.lang.reflect;
|
|
27 |
|
|
28 |
import java.security.AccessController;
|
|
29 |
import sun.reflect.LangReflectAccess;
|
|
30 |
import sun.reflect.ReflectionFactory;
|
|
31 |
|
|
32 |
/**
|
|
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 <a
|
|
38 |
* href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/VMSpecTOC.doc.html"><i>The
|
|
39 |
* Java</i><sup><small>TM</small></sup> <i>Virtual Machine Specification, Second
|
|
40 |
* edition</i></a> tables
|
|
41 |
* <a href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#75734">4.1</a>,
|
|
42 |
* <a href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#88358">4.4</a>,
|
|
43 |
* <a href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#75568">4.5</a>, and
|
|
44 |
* <a href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#88478">4.7</a>.
|
|
45 |
*
|
|
46 |
* @see Class#getModifiers()
|
|
47 |
* @see Member#getModifiers()
|
|
48 |
*
|
|
49 |
* @author Nakul Saraiya
|
|
50 |
* @author Kenneth Russell
|
|
51 |
*/
|
|
52 |
public
|
|
53 |
class Modifier {
|
|
54 |
|
|
55 |
/*
|
|
56 |
* Bootstrapping protocol between java.lang and java.lang.reflect
|
|
57 |
* packages
|
|
58 |
*/
|
|
59 |
static {
|
|
60 |
sun.reflect.ReflectionFactory factory =
|
51
|
61 |
AccessController.doPrivileged(
|
|
62 |
new ReflectionFactory.GetReflectionFactoryAction());
|
2
|
63 |
factory.setLangReflectAccess(new java.lang.reflect.ReflectAccess());
|
|
64 |
}
|
|
65 |
|
|
66 |
/**
|
|
67 |
* Return {@code true} if the integer argument includes the
|
|
68 |
* {@code public} modifier, {@code false} otherwise.
|
|
69 |
*
|
|
70 |
* @param mod a set of modifiers
|
|
71 |
* @return {@code true} if {@code mod} includes the
|
|
72 |
* {@code public} modifier; {@code false} otherwise.
|
|
73 |
*/
|
|
74 |
public static boolean isPublic(int mod) {
|
|
75 |
return (mod & PUBLIC) != 0;
|
|
76 |
}
|
|
77 |
|
|
78 |
/**
|
|
79 |
* Return {@code true} if the integer argument includes the
|
|
80 |
* {@code private} modifier, {@code false} otherwise.
|
|
81 |
*
|
|
82 |
* @param mod a set of modifiers
|
|
83 |
* @return {@code true} if {@code mod} includes the
|
|
84 |
* {@code private} modifier; {@code false} otherwise.
|
|
85 |
*/
|
|
86 |
public static boolean isPrivate(int mod) {
|
|
87 |
return (mod & PRIVATE) != 0;
|
|
88 |
}
|
|
89 |
|
|
90 |
/**
|
|
91 |
* Return {@code true} if the integer argument includes the
|
|
92 |
* {@code protected} modifier, {@code false} otherwise.
|
|
93 |
*
|
|
94 |
* @param mod a set of modifiers
|
|
95 |
* @return {@code true} if {@code mod} includes the
|
|
96 |
* {@code protected} modifier; {@code false} otherwise.
|
|
97 |
*/
|
|
98 |
public static boolean isProtected(int mod) {
|
|
99 |
return (mod & PROTECTED) != 0;
|
|
100 |
}
|
|
101 |
|
|
102 |
/**
|
|
103 |
* Return {@code true} if the integer argument includes the
|
|
104 |
* {@code static} modifier, {@code false} otherwise.
|
|
105 |
*
|
|
106 |
* @param mod a set of modifiers
|
|
107 |
* @return {@code true} if {@code mod} includes the
|
|
108 |
* {@code static} modifier; {@code false} otherwise.
|
|
109 |
*/
|
|
110 |
public static boolean isStatic(int mod) {
|
|
111 |
return (mod & STATIC) != 0;
|
|
112 |
}
|
|
113 |
|
|
114 |
/**
|
|
115 |
* Return {@code true} if the integer argument includes the
|
|
116 |
* {@code final} modifier, {@code false} otherwise.
|
|
117 |
*
|
|
118 |
* @param mod a set of modifiers
|
|
119 |
* @return {@code true} if {@code mod} includes the
|
|
120 |
* {@code final} modifier; {@code false} otherwise.
|
|
121 |
*/
|
|
122 |
public static boolean isFinal(int mod) {
|
|
123 |
return (mod & FINAL) != 0;
|
|
124 |
}
|
|
125 |
|
|
126 |
/**
|
|
127 |
* Return {@code true} if the integer argument includes the
|
|
128 |
* {@code synchronized} modifier, {@code false} otherwise.
|
|
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 |
|
|
150 |
/**
|
|
151 |
* Return {@code true} if the integer argument includes the
|
|
152 |
* {@code transient} modifier, {@code false} otherwise.
|
|
153 |
*
|
|
154 |
* @param mod a set of modifiers
|
|
155 |
* @return {@code true} if {@code mod} includes the
|
|
156 |
* {@code transient} modifier; {@code false} otherwise.
|
|
157 |
*/
|
|
158 |
public static boolean isTransient(int mod) {
|
|
159 |
return (mod & TRANSIENT) != 0;
|
|
160 |
}
|
|
161 |
|
|
162 |
/**
|
|
163 |
* Return {@code true} if the integer argument includes the
|
|
164 |
* {@code native} modifier, {@code false} otherwise.
|
|
165 |
*
|
|
166 |
* @param mod a set of modifiers
|
|
167 |
* @return {@code true} if {@code mod} includes the
|
|
168 |
* {@code native} modifier; {@code false} otherwise.
|
|
169 |
*/
|
|
170 |
public static boolean isNative(int mod) {
|
|
171 |
return (mod & NATIVE) != 0;
|
|
172 |
}
|
|
173 |
|
|
174 |
/**
|
|
175 |
* Return {@code true} if the integer argument includes the
|
|
176 |
* {@code interface} modifier, {@code false} otherwise.
|
|
177 |
*
|
|
178 |
* @param mod a set of modifiers
|
|
179 |
* @return {@code true} if {@code mod} includes the
|
|
180 |
* {@code interface} modifier; {@code false} otherwise.
|
|
181 |
*/
|
|
182 |
public static boolean isInterface(int mod) {
|
|
183 |
return (mod & INTERFACE) != 0;
|
|
184 |
}
|
|
185 |
|
|
186 |
/**
|
|
187 |
* Return {@code true} if the integer argument includes the
|
|
188 |
* {@code abstract} modifier, {@code false} otherwise.
|
|
189 |
*
|
|
190 |
* @param mod a set of modifiers
|
|
191 |
* @return {@code true} if {@code mod} includes the
|
|
192 |
* {@code abstract} modifier; {@code false} otherwise.
|
|
193 |
*/
|
|
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 strictfp
|
|
215 |
* </pre></blockquote>
|
|
216 |
* The modifier names are returned in an order consistent with the
|
|
217 |
* suggested modifier orderings given in <a
|
|
218 |
* href="http://java.sun.com/docs/books/jls/second_edition/html/j.title.doc.html"><em>The
|
|
219 |
* Java Language Specification, Second Edition</em></a> sections
|
|
220 |
* <a href="http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#21613">§8.1.1</a>,
|
|
221 |
* <a href="http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#78091">§8.3.1</a>,
|
|
222 |
* <a href="http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#78188">§8.4.3</a>,
|
|
223 |
* <a href="http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#42018">§8.8.3</a>, and
|
|
224 |
* <a href="http://java.sun.com/docs/books/jls/second_edition/html/interfaces.doc.html#235947">§9.1.1</a>.
|
|
225 |
* The full modifier ordering used by this method is:
|
|
226 |
* <blockquote> {@code
|
|
227 |
* public protected private abstract static final transient
|
|
228 |
* volatile synchronized native strictfp
|
|
229 |
* interface } </blockquote>
|
|
230 |
* The {@code interface} modifier discussed in this class is
|
|
231 |
* not a true modifier in the Java language and it appears after
|
|
232 |
* all other modifiers listed by this method. This method may
|
|
233 |
* return a string of modifiers that are not valid modifiers of a
|
|
234 |
* Java entity; in other words, no checking is done on the
|
|
235 |
* possible validity of the combination of modifiers represented
|
|
236 |
* by the input.
|
|
237 |
*
|
|
238 |
* @param mod a set of modifiers
|
|
239 |
* @return a string representation of the set of modifiers
|
|
240 |
* represented by {@code mod}
|
|
241 |
*/
|
|
242 |
public static String toString(int mod) {
|
|
243 |
StringBuffer sb = new StringBuffer();
|
|
244 |
int len;
|
|
245 |
|
|
246 |
if ((mod & PUBLIC) != 0) sb.append("public ");
|
|
247 |
if ((mod & PROTECTED) != 0) sb.append("protected ");
|
|
248 |
if ((mod & PRIVATE) != 0) sb.append("private ");
|
|
249 |
|
|
250 |
/* Canonical order */
|
|
251 |
if ((mod & ABSTRACT) != 0) sb.append("abstract ");
|
|
252 |
if ((mod & STATIC) != 0) sb.append("static ");
|
|
253 |
if ((mod & FINAL) != 0) sb.append("final ");
|
|
254 |
if ((mod & TRANSIENT) != 0) sb.append("transient ");
|
|
255 |
if ((mod & VOLATILE) != 0) sb.append("volatile ");
|
|
256 |
if ((mod & SYNCHRONIZED) != 0) sb.append("synchronized ");
|
|
257 |
if ((mod & NATIVE) != 0) sb.append("native ");
|
|
258 |
if ((mod & STRICT) != 0) sb.append("strictfp ");
|
|
259 |
if ((mod & INTERFACE) != 0) sb.append("interface ");
|
|
260 |
|
|
261 |
if ((len = sb.length()) > 0) /* trim trailing space */
|
|
262 |
return sb.toString().substring(0, len-1);
|
|
263 |
return "";
|
|
264 |
}
|
|
265 |
|
|
266 |
/*
|
|
267 |
* Access modifier flag constants from <em>The Java Virtual
|
|
268 |
* Machine Specification, Second Edition</em>, tables 4.1, 4.4,
|
|
269 |
* 4.5, and 4.7.
|
|
270 |
*/
|
|
271 |
|
|
272 |
/**
|
|
273 |
* The {@code int} value representing the {@code public}
|
|
274 |
* modifier.
|
|
275 |
*/
|
|
276 |
public static final int PUBLIC = 0x00000001;
|
|
277 |
|
|
278 |
/**
|
|
279 |
* The {@code int} value representing the {@code private}
|
|
280 |
* modifier.
|
|
281 |
*/
|
|
282 |
public static final int PRIVATE = 0x00000002;
|
|
283 |
|
|
284 |
/**
|
|
285 |
* The {@code int} value representing the {@code protected}
|
|
286 |
* modifier.
|
|
287 |
*/
|
|
288 |
public static final int PROTECTED = 0x00000004;
|
|
289 |
|
|
290 |
/**
|
|
291 |
* The {@code int} value representing the {@code static}
|
|
292 |
* modifier.
|
|
293 |
*/
|
|
294 |
public static final int STATIC = 0x00000008;
|
|
295 |
|
|
296 |
/**
|
|
297 |
* The {@code int} value representing the {@code final}
|
|
298 |
* modifier.
|
|
299 |
*/
|
|
300 |
public static final int FINAL = 0x00000010;
|
|
301 |
|
|
302 |
/**
|
|
303 |
* The {@code int} value representing the {@code synchronized}
|
|
304 |
* modifier.
|
|
305 |
*/
|
|
306 |
public static final int SYNCHRONIZED = 0x00000020;
|
|
307 |
|
|
308 |
/**
|
|
309 |
* The {@code int} value representing the {@code volatile}
|
|
310 |
* modifier.
|
|
311 |
*/
|
|
312 |
public static final int VOLATILE = 0x00000040;
|
|
313 |
|
|
314 |
/**
|
|
315 |
* The {@code int} value representing the {@code transient}
|
|
316 |
* modifier.
|
|
317 |
*/
|
|
318 |
public static final int TRANSIENT = 0x00000080;
|
|
319 |
|
|
320 |
/**
|
|
321 |
* The {@code int} value representing the {@code native}
|
|
322 |
* modifier.
|
|
323 |
*/
|
|
324 |
public static final int NATIVE = 0x00000100;
|
|
325 |
|
|
326 |
/**
|
|
327 |
* The {@code int} value representing the {@code interface}
|
|
328 |
* modifier.
|
|
329 |
*/
|
|
330 |
public static final int INTERFACE = 0x00000200;
|
|
331 |
|
|
332 |
/**
|
|
333 |
* The {@code int} value representing the {@code abstract}
|
|
334 |
* modifier.
|
|
335 |
*/
|
|
336 |
public static final int ABSTRACT = 0x00000400;
|
|
337 |
|
|
338 |
/**
|
|
339 |
* The {@code int} value representing the {@code strictfp}
|
|
340 |
* modifier.
|
|
341 |
*/
|
|
342 |
public static final int STRICT = 0x00000800;
|
|
343 |
|
|
344 |
// Bits not (yet) exposed in the public API either because they
|
|
345 |
// have different meanings for fields and methods and there is no
|
|
346 |
// way to distinguish between the two in this class, or because
|
|
347 |
// they are not Java programming language keywords
|
|
348 |
static final int BRIDGE = 0x00000040;
|
|
349 |
static final int VARARGS = 0x00000080;
|
|
350 |
static final int SYNTHETIC = 0x00001000;
|
|
351 |
static final int ANNOTATION= 0x00002000;
|
|
352 |
static final int ENUM = 0x00004000;
|
|
353 |
static boolean isSynthetic(int mod) {
|
|
354 |
return (mod & SYNTHETIC) != 0;
|
|
355 |
}
|
|
356 |
}
|