27 |
|
|
28 |
package java.lang.reflect; |
package java.lang.reflect; |
29 |
|
|
|
/* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3 |
|
|
* "The Java Language Specification", ISBN 0-201-63451-1 |
|
|
* plus online API docs for JDK 1.2 beta from http://www.javasoft.com. |
|
|
* Status: Believed complete and correct to version 1.2. |
|
|
*/ |
|
|
|
|
30 |
/** |
/** |
31 |
* Modifier is a helper class with static methods to determine whether an |
* Modifier is a helper class with static methods to determine whether an |
32 |
* int returned from getModifiers() represents static, public, protected, |
* int returned from getModifiers() represents static, public, protected, |
40 |
* |
* |
41 |
* @author John Keiser |
* @author John Keiser |
42 |
* @author Tom Tromey <tromey@cygnus.com> |
* @author Tom Tromey <tromey@cygnus.com> |
43 |
* |
* @author Eric Blake <ebb9@email.byu.edu> |
44 |
* @see Member#getModifiers() |
* @see Member#getModifiers() |
45 |
* @see Method#getModifiers() |
* @see Method#getModifiers() |
46 |
* @see Field#getModifiers() |
* @see Field#getModifiers() |
47 |
* @see Constructor#getModifiers() |
* @see Constructor#getModifiers() |
48 |
* @see Class#getModifiers() |
* @see Class#getModifiers() |
49 |
|
* @since 1.1 |
50 |
*/ |
*/ |
51 |
public class Modifier |
public class Modifier |
52 |
{ |
{ |
55 |
* worthless. However, this function is in the 1.1 spec, so it is added |
* worthless. However, this function is in the 1.1 spec, so it is added |
56 |
* for completeness.</STRONG> |
* for completeness.</STRONG> |
57 |
*/ |
*/ |
58 |
public Modifier() {} |
public Modifier() |
59 |
|
{ |
60 |
|
} |
61 |
|
|
62 |
/** Public: accessible from any other class. **/ |
/** |
63 |
|
* Public: accessible from any other class. |
64 |
|
*/ |
65 |
public static final int PUBLIC = 0x0001; |
public static final int PUBLIC = 0x0001; |
66 |
|
|
67 |
/** Private: accessible only from the declaring class. **/ |
/** |
68 |
|
* Private: accessible only from the same enclosing class. |
69 |
|
*/ |
70 |
public static final int PRIVATE = 0x0002; |
public static final int PRIVATE = 0x0002; |
71 |
|
|
72 |
/** Protected: accessible only to subclasses. **/ |
/** |
73 |
|
* Protected: accessible only to subclasses, or within the package. |
74 |
|
*/ |
75 |
public static final int PROTECTED = 0x0004; |
public static final int PROTECTED = 0x0004; |
76 |
|
|
77 |
/** Static: field or method - can be accessed or invoked without an |
/** |
78 |
instance of the declaring class. **/ |
* Static:<br><ul> |
79 |
|
* <li>Class: no enclosing instance for nested class.</li> |
80 |
|
* <li>Field or Method: can be accessed or invoked without an |
81 |
|
* instance of the declaring class.</li> |
82 |
|
* </ul> |
83 |
|
*/ |
84 |
public static final int STATIC = 0x0008; |
public static final int STATIC = 0x0008; |
85 |
|
|
86 |
/** Final:<BR> |
/** |
87 |
* <UL> |
* Final:<br><ul> |
88 |
* <LI> Class: no subclasses allowed. </LI> |
* <li>Class: no subclasses allowed.</li> |
89 |
* <LI> Field: cannot be changed. </LI> |
* <li>Field: cannot be changed.</li> |
90 |
* <LI> Method: cannot be overriden. </LI> |
* <li>Method: cannot be overriden.</li> |
91 |
* </UL> |
* </ul> |
92 |
*/ |
*/ |
93 |
public static final int FINAL = 0x0010; |
public static final int FINAL = 0x0010; |
94 |
|
|
95 |
/** Synchronized: lock the class while calling this method. **/ |
/** |
96 |
|
* Synchronized: Method: lock the class while calling this method. |
97 |
|
*/ |
98 |
public static final int SYNCHRONIZED = 0x0020; |
public static final int SYNCHRONIZED = 0x0020; |
99 |
|
|
100 |
/** Volatile: cannot be cached.<P> **/ |
/** |
101 |
|
* Volatile: Field: cannot be cached. |
102 |
|
*/ |
103 |
public static final int VOLATILE = 0x0040; |
public static final int VOLATILE = 0x0040; |
104 |
|
|
105 |
/** Transient: not serialized or deserialized. **/ |
/** |
106 |
|
* Transient: Field: not serialized or deserialized. |
107 |
|
*/ |
108 |
public static final int TRANSIENT = 0x0080; |
public static final int TRANSIENT = 0x0080; |
109 |
|
|
110 |
/** Native: use JNI to call this method. **/ |
/** |
111 |
|
* Native: Method: use JNI to call this method. |
112 |
|
*/ |
113 |
public static final int NATIVE = 0x0100; |
public static final int NATIVE = 0x0100; |
114 |
|
|
115 |
/** Interface: is an interface. **/ |
/** |
116 |
|
* Interface: Class: is an interface. |
117 |
|
*/ |
118 |
public static final int INTERFACE = 0x0200; |
public static final int INTERFACE = 0x0200; |
119 |
|
|
120 |
/** Abstract: class - may not be instantiated; |
/** |
121 |
method - may not be called. **/ |
* Abstract:<br><ul> |
122 |
|
* <li>Class: may not be instantiated.</li> |
123 |
|
* <li>Method: may not be called.</li> |
124 |
|
* </ul> |
125 |
|
*/ |
126 |
public static final int ABSTRACT = 0x0400; |
public static final int ABSTRACT = 0x0400; |
127 |
|
|
128 |
/** Class or method - expressions are FP-strict. **/ |
/** |
129 |
|
* Strictfp: Method: expressions are FP-strict.<p> |
130 |
|
* Also used as a modifier for classes, to mean that all initializers |
131 |
|
* and constructors are FP-strict, but does not show up in |
132 |
|
* Class.getModifiers. |
133 |
|
*/ |
134 |
public static final int STRICT = 0x0800; |
public static final int STRICT = 0x0800; |
135 |
|
|
136 |
|
|
137 |
/* NOTE: THIS IS HERE BECAUSE IT IS IN THE VM SPEC. |
/** |
138 |
I INCLUDE IT FOR COMPLETENESS. IT ATTACHES TO A CLASS AND MEANS |
* Super - treat invokespecial as polymorphic so that super.foo() works |
139 |
"Treat superclasses specially in invokespecial". Note that it is the |
* according to the JLS. This is a reuse of the synchronized constant |
140 |
same as synchronized. Reuse of the constant. *shudder* */ |
* to patch a hole in JDK 1.0. *shudder*. |
141 |
private static final int SUPER = 0x0020; |
*/ |
142 |
|
static final int SUPER = 0x0020; |
143 |
|
|
144 |
// This can only used by other code in this package, so it is not public. |
/** |
145 |
|
* All the flags, only used by code in this package. |
146 |
|
*/ |
147 |
static final int ALL_FLAGS = 0xfff; |
static final int ALL_FLAGS = 0xfff; |
148 |
|
|
149 |
/** Check whether the given modifier is abstract. |
/** |
150 |
|
* Check whether the given modifier is abstract. |
151 |
* @param mod the modifier. |
* @param mod the modifier. |
152 |
* @return <code>true</code> if abstract, <code>false</code> otherwise. |
* @return <code>true</code> if abstract, <code>false</code> otherwise. |
153 |
*/ |
*/ |
154 |
public static boolean isAbstract (int mod) |
public static boolean isAbstract(int mod) |
155 |
{ |
{ |
156 |
return (mod & ABSTRACT) != 0; |
return (mod & ABSTRACT) != 0; |
157 |
} |
} |
158 |
|
|
159 |
/** Check whether the given modifier is final. |
/** |
160 |
|
* Check whether the given modifier is final. |
161 |
* @param mod the modifier. |
* @param mod the modifier. |
162 |
* @return <code>true</code> if final, <code>false</code> otherwise. |
* @return <code>true</code> if final, <code>false</code> otherwise. |
163 |
*/ |
*/ |
164 |
public static boolean isFinal (int mod) |
public static boolean isFinal(int mod) |
165 |
{ |
{ |
166 |
return (mod & FINAL) != 0; |
return (mod & FINAL) != 0; |
167 |
} |
} |
168 |
|
|
169 |
/** Check whether the given modifier is an interface. |
/** |
170 |
|
* Check whether the given modifier is an interface. |
171 |
* @param mod the modifier. |
* @param mod the modifier. |
172 |
* @return <code>true</code> if an interface, <code>false</code> otherwise. |
* @return <code>true</code> if an interface, <code>false</code> otherwise. |
173 |
*/ |
*/ |
174 |
public static boolean isInterface (int mod) |
public static boolean isInterface(int mod) |
175 |
{ |
{ |
176 |
return (mod & INTERFACE) != 0; |
return (mod & INTERFACE) != 0; |
177 |
} |
} |
178 |
|
|
179 |
/** Check whether the given modifier is native. |
/** |
180 |
|
* Check whether the given modifier is native. |
181 |
* @param mod the modifier. |
* @param mod the modifier. |
182 |
* @return <code>true</code> if native, <code>false</code> otherwise. |
* @return <code>true</code> if native, <code>false</code> otherwise. |
183 |
*/ |
*/ |
184 |
public static boolean isNative (int mod) |
public static boolean isNative(int mod) |
185 |
{ |
{ |
186 |
return (mod & NATIVE) != 0; |
return (mod & NATIVE) != 0; |
187 |
} |
} |
188 |
|
|
189 |
/** Check whether the given modifier is private. |
/** |
190 |
|
* Check whether the given modifier is private. |
191 |
* @param mod the modifier. |
* @param mod the modifier. |
192 |
* @return <code>true</code> if private, <code>false</code> otherwise. |
* @return <code>true</code> if private, <code>false</code> otherwise. |
193 |
*/ |
*/ |
194 |
public static boolean isPrivate (int mod) |
public static boolean isPrivate(int mod) |
195 |
{ |
{ |
196 |
return (mod & PRIVATE) != 0; |
return (mod & PRIVATE) != 0; |
197 |
} |
} |
198 |
|
|
199 |
/** Check whether the given modifier is protected. |
/** |
200 |
|
* Check whether the given modifier is protected. |
201 |
* @param mod the modifier. |
* @param mod the modifier. |
202 |
* @return <code>true</code> if protected, <code>false</code> otherwise. |
* @return <code>true</code> if protected, <code>false</code> otherwise. |
203 |
*/ |
*/ |
204 |
public static boolean isProtected (int mod) |
public static boolean isProtected(int mod) |
205 |
{ |
{ |
206 |
return (mod & PROTECTED) != 0; |
return (mod & PROTECTED) != 0; |
207 |
} |
} |
208 |
|
|
209 |
/** Check whether the given modifier is public. |
/** |
210 |
|
* Check whether the given modifier is public. |
211 |
* @param mod the modifier. |
* @param mod the modifier. |
212 |
* @return <code>true</code> if public, <code>false</code> otherwise. |
* @return <code>true</code> if public, <code>false</code> otherwise. |
213 |
*/ |
*/ |
214 |
public static boolean isPublic (int mod) |
public static boolean isPublic(int mod) |
215 |
{ |
{ |
216 |
return (mod & PUBLIC) != 0; |
return (mod & PUBLIC) != 0; |
217 |
} |
} |
218 |
|
|
219 |
/** Check whether the given modifier is static. |
/** |
220 |
|
* Check whether the given modifier is static. |
221 |
* @param mod the modifier. |
* @param mod the modifier. |
222 |
* @return <code>true</code> if static, <code>false</code> otherwise. |
* @return <code>true</code> if static, <code>false</code> otherwise. |
223 |
*/ |
*/ |
224 |
public static boolean isStatic (int mod) |
public static boolean isStatic(int mod) |
225 |
{ |
{ |
226 |
return (mod & STATIC) != 0; |
return (mod & STATIC) != 0; |
227 |
} |
} |
228 |
|
|
229 |
/** Check whether the given modifier is strictfp. |
/** |
230 |
|
* Check whether the given modifier is strictfp. |
231 |
* @param mod the modifier. |
* @param mod the modifier. |
232 |
* @return <code>true</code> if strictfp, <code>false</code> otherwise. |
* @return <code>true</code> if strictfp, <code>false</code> otherwise. |
233 |
*/ |
*/ |
234 |
public static boolean isStrict (int mod) |
public static boolean isStrict(int mod) |
235 |
{ |
{ |
236 |
return (mod & STRICT) != 0; |
return (mod & STRICT) != 0; |
237 |
} |
} |
238 |
|
|
239 |
/** Check whether the given modifier is synchronized. |
/** |
240 |
|
* Check whether the given modifier is synchronized. |
241 |
* @param mod the modifier. |
* @param mod the modifier. |
242 |
* @return <code>true</code> if synchronized, <code>false</code> otherwise. |
* @return <code>true</code> if synchronized, <code>false</code> otherwise. |
243 |
*/ |
*/ |
244 |
public static boolean isSynchronized (int mod) |
public static boolean isSynchronized(int mod) |
245 |
{ |
{ |
246 |
return (mod & SYNCHRONIZED) != 0; |
return (mod & SYNCHRONIZED) != 0; |
247 |
} |
} |
248 |
|
|
249 |
/** Check whether the given modifier is transient. |
/** |
250 |
|
* Check whether the given modifier is transient. |
251 |
* @param mod the modifier. |
* @param mod the modifier. |
252 |
* @return <code>true</code> if transient, <code>false</code> otherwise. |
* @return <code>true</code> if transient, <code>false</code> otherwise. |
253 |
*/ |
*/ |
254 |
public static boolean isTransient (int mod) |
public static boolean isTransient(int mod) |
255 |
{ |
{ |
256 |
return (mod & TRANSIENT) != 0; |
return (mod & TRANSIENT) != 0; |
257 |
} |
} |
258 |
|
|
259 |
/** Check whether the given modifier is volatile. |
/** |
260 |
|
* Check whether the given modifier is volatile. |
261 |
* @param mod the modifier. |
* @param mod the modifier. |
262 |
* @return <code>true</code> if volatile, <code>false</code> otherwise. |
* @return <code>true</code> if volatile, <code>false</code> otherwise. |
263 |
*/ |
*/ |
264 |
public static boolean isVolatile (int mod) |
public static boolean isVolatile(int mod) |
265 |
{ |
{ |
266 |
return (mod & VOLATILE) != 0; |
return (mod & VOLATILE) != 0; |
267 |
} |
} |
268 |
|
|
269 |
/** Get a string representation of all the modifiers represented by the |
/** |
270 |
* given int. |
* Get a string representation of all the modifiers represented by the |
271 |
* The keywords are printed in this order: |
* given int. The keywords are printed in this order: |
272 |
* <code><public|private|protected> abstract static final transient |
* <code><public|private|protected> abstract static final transient |
273 |
* volatile native synchronized interface strictfp</code><P> |
* volatile native synchronized interface strictfp</code>. |
|
* |
|
|
* <STRONG>This is, near as I can tell, the "canonical order" of modifiers |
|
|
* mentioned by Sun in the reference implementation. I have inferred this |
|
|
* from the order of printing in the Field, Method and Constructor |
|
|
* classes.</STRONG> |
|
274 |
* |
* |
275 |
* @param mod the modifier. |
* @param mod the modifier. |
276 |
* @return the String representing the modifiers. |
* @return the String representing the modifiers. |
277 |
*/ |
*/ |
278 |
public static String toString (int mod) |
public static String toString(int mod) |
279 |
{ |
{ |
280 |
StringBuffer r = new StringBuffer (); |
return toString(mod, new StringBuffer()).toString(); |
|
toString(mod, r); |
|
|
return r.toString(); |
|
281 |
} |
} |
282 |
|
|
283 |
/** |
/** |
284 |
* Package private helper toString() method that can take a StringBuffer. |
* Package helper method that can take a StringBuffer. |
285 |
* @param mod the modifier |
* @param mod the modifier |
286 |
* @param r the StringBuffer to which the String representation is appended |
* @param r the StringBuffer to which the String representation is appended |
287 |
|
* @return r, with information appended |
288 |
*/ |
*/ |
289 |
static void toString (int mod, StringBuffer r) |
static StringBuffer toString(int mod, StringBuffer r) |
290 |
{ |
{ |
291 |
if (isPublic (mod)) |
if (isPublic(mod)) |
292 |
r.append("public "); |
r.append("public "); |
293 |
if (isProtected (mod)) |
if (isPrivate(mod)) |
|
r.append("protected "); |
|
|
if (isPrivate (mod)) |
|
294 |
r.append("private "); |
r.append("private "); |
295 |
if (isAbstract (mod)) |
if (isProtected(mod)) |
296 |
|
r.append("protected "); |
297 |
|
if (isAbstract(mod)) |
298 |
r.append("abstract "); |
r.append("abstract "); |
299 |
if (isStatic (mod)) |
if (isStatic(mod)) |
300 |
r.append("static "); |
r.append("static "); |
301 |
if (isFinal (mod)) |
if (isFinal(mod)) |
302 |
r.append("final "); |
r.append("final "); |
303 |
if (isTransient (mod)) |
if (isTransient(mod)) |
304 |
r.append("transient "); |
r.append("transient "); |
305 |
if (isVolatile (mod)) |
if (isVolatile(mod)) |
306 |
r.append("volatile "); |
r.append("volatile "); |
307 |
if (isNative (mod)) |
if (isNative(mod)) |
308 |
r.append("native "); |
r.append("native "); |
309 |
if (isSynchronized (mod)) |
if (isSynchronized(mod)) |
310 |
r.append("synchronized "); |
r.append("synchronized "); |
311 |
if (isInterface (mod)) |
if (isInterface(mod)) |
312 |
r.append("interface "); |
r.append("interface "); |
313 |
if (isStrict (mod)) |
if (isStrict(mod)) |
314 |
r.append("strictfp "); |
r.append("strictfp "); |
315 |
|
|
316 |
// Trim trailing space. |
// Trim trailing space. |
317 |
int l = r.length(); |
if ((mod & ALL_FLAGS) != 0) |
318 |
if (l > 0) |
r.setLength(r.length() - 1); |
319 |
r.setLength(l - 1); |
return r; |
320 |
} |
} |
321 |
} |
} |