1 |
780 |
jeremybenn |
/* VMClass.java -- VM Specific Class methods
|
2 |
|
|
Copyright (C) 2003, 2004, 2005, 2006 Free Software Foundation
|
3 |
|
|
|
4 |
|
|
This file is part of GNU Classpath.
|
5 |
|
|
|
6 |
|
|
GNU Classpath is free software; you can redistribute it and/or modify
|
7 |
|
|
it under the terms of the GNU General Public License as published by
|
8 |
|
|
the Free Software Foundation; either version 2, or (at your option)
|
9 |
|
|
any later version.
|
10 |
|
|
|
11 |
|
|
GNU Classpath is distributed in the hope that it will be useful, but
|
12 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
|
|
General Public License for more details.
|
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License
|
17 |
|
|
along with GNU Classpath; see the file COPYING. If not, write to the
|
18 |
|
|
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
|
|
02110-1301 USA.
|
20 |
|
|
|
21 |
|
|
Linking this library statically or dynamically with other modules is
|
22 |
|
|
making a combined work based on this library. Thus, the terms and
|
23 |
|
|
conditions of the GNU General Public License cover the whole
|
24 |
|
|
combination.
|
25 |
|
|
|
26 |
|
|
As a special exception, the copyright holders of this library give you
|
27 |
|
|
permission to link this library with independent modules to produce an
|
28 |
|
|
executable, regardless of the license terms of these independent
|
29 |
|
|
modules, and to copy and distribute the resulting executable under
|
30 |
|
|
terms of your choice, provided that you also meet, for each linked
|
31 |
|
|
independent module, the terms and conditions of the license of that
|
32 |
|
|
module. An independent module is a module which is not derived from
|
33 |
|
|
or based on this library. If you modify this library, you may extend
|
34 |
|
|
this exception to your version of the library, but you are not
|
35 |
|
|
obligated to do so. If you do not wish to do so, delete this
|
36 |
|
|
exception statement from your version. */
|
37 |
|
|
|
38 |
|
|
package java.lang;
|
39 |
|
|
|
40 |
|
|
import java.lang.annotation.Annotation;
|
41 |
|
|
import java.lang.reflect.Array;
|
42 |
|
|
import java.lang.reflect.Constructor;
|
43 |
|
|
import java.lang.reflect.Field;
|
44 |
|
|
import java.lang.reflect.Method;
|
45 |
|
|
import java.lang.reflect.Modifier;
|
46 |
|
|
|
47 |
|
|
/*
|
48 |
|
|
* This class is a reference version, mainly for compiling a class library
|
49 |
|
|
* jar. It is likely that VM implementers replace this with their own
|
50 |
|
|
* version that can communicate effectively with the VM.
|
51 |
|
|
*/
|
52 |
|
|
|
53 |
|
|
/**
|
54 |
|
|
*
|
55 |
|
|
* @author Etienne Gagnon (etienne.gagnon@uqam.ca)
|
56 |
|
|
* @author Archie Cobbs (archie@dellroad.org)
|
57 |
|
|
* @author C. Brian Jones (cbj@gnu.org)
|
58 |
|
|
* @author Tom Tromey (tromey@cygnus.com)
|
59 |
|
|
* @author Andrew John Hughes (gnu_andrew@member.fsf.org)
|
60 |
|
|
*/
|
61 |
|
|
final class VMClass
|
62 |
|
|
{
|
63 |
|
|
|
64 |
|
|
// Only static methods. Cannot be instantiated.
|
65 |
|
|
private VMClass()
|
66 |
|
|
{
|
67 |
|
|
}
|
68 |
|
|
|
69 |
|
|
/**
|
70 |
|
|
* Discover whether an Object is an instance of this Class. Think of it
|
71 |
|
|
* as almost like <code>o instanceof (this class)</code>.
|
72 |
|
|
*
|
73 |
|
|
* @param klass the Class object that's calling us
|
74 |
|
|
* @param o the Object to check
|
75 |
|
|
* @return whether o is an instance of this class
|
76 |
|
|
* @since 1.1
|
77 |
|
|
*/
|
78 |
|
|
static native boolean isInstance(Class klass, Object o);
|
79 |
|
|
|
80 |
|
|
/**
|
81 |
|
|
* Discover whether an instance of the Class parameter would be an
|
82 |
|
|
* instance of this Class as well. Think of doing
|
83 |
|
|
* <code>isInstance(c.newInstance())</code> or even
|
84 |
|
|
* <code>c.newInstance() instanceof (this class)</code>. While this
|
85 |
|
|
* checks widening conversions for objects, it must be exact for primitive
|
86 |
|
|
* types.
|
87 |
|
|
*
|
88 |
|
|
* @param klass the Class object that's calling us
|
89 |
|
|
* @param c the class to check
|
90 |
|
|
* @return whether an instance of c would be an instance of this class
|
91 |
|
|
* as well
|
92 |
|
|
* @throws NullPointerException if c is null
|
93 |
|
|
* @since 1.1
|
94 |
|
|
*/
|
95 |
|
|
static native boolean isAssignableFrom(Class klass, Class c);
|
96 |
|
|
|
97 |
|
|
/**
|
98 |
|
|
* Check whether this class is an interface or not. Array types are not
|
99 |
|
|
* interfaces.
|
100 |
|
|
*
|
101 |
|
|
* @param klass the Class object that's calling us
|
102 |
|
|
* @return whether this class is an interface or not
|
103 |
|
|
*/
|
104 |
|
|
static native boolean isInterface(Class klass);
|
105 |
|
|
|
106 |
|
|
/**
|
107 |
|
|
* Return whether this class is a primitive type. A primitive type class
|
108 |
|
|
* is a class representing a kind of "placeholder" for the various
|
109 |
|
|
* primitive types, or void. You can access the various primitive type
|
110 |
|
|
* classes through java.lang.Boolean.TYPE, java.lang.Integer.TYPE, etc.,
|
111 |
|
|
* or through boolean.class, int.class, etc.
|
112 |
|
|
*
|
113 |
|
|
* @param klass the Class object that's calling us
|
114 |
|
|
* @return whether this class is a primitive type
|
115 |
|
|
* @see Boolean#TYPE
|
116 |
|
|
* @see Byte#TYPE
|
117 |
|
|
* @see Character#TYPE
|
118 |
|
|
* @see Short#TYPE
|
119 |
|
|
* @see Integer#TYPE
|
120 |
|
|
* @see Long#TYPE
|
121 |
|
|
* @see Float#TYPE
|
122 |
|
|
* @see Double#TYPE
|
123 |
|
|
* @see Void#TYPE
|
124 |
|
|
* @since 1.1
|
125 |
|
|
*/
|
126 |
|
|
static native boolean isPrimitive(Class klass);
|
127 |
|
|
|
128 |
|
|
/**
|
129 |
|
|
* Get the name of this class, separated by dots for package separators.
|
130 |
|
|
* Primitive types and arrays are encoded as:
|
131 |
|
|
* <pre>
|
132 |
|
|
* boolean Z
|
133 |
|
|
* byte B
|
134 |
|
|
* char C
|
135 |
|
|
* short S
|
136 |
|
|
* int I
|
137 |
|
|
* long J
|
138 |
|
|
* float F
|
139 |
|
|
* double D
|
140 |
|
|
* void V
|
141 |
|
|
* array type [<em>element type</em>
|
142 |
|
|
* class or interface, alone: <dotted name>
|
143 |
|
|
* class or interface, as element type: L<dotted name>;
|
144 |
|
|
*
|
145 |
|
|
* @param klass the Class object that's calling us
|
146 |
|
|
* @return the name of this class
|
147 |
|
|
*/
|
148 |
|
|
static native String getName(Class klass);
|
149 |
|
|
|
150 |
|
|
/**
|
151 |
|
|
* Get the direct superclass of this class. If this is an interface,
|
152 |
|
|
* Object, a primitive type, or void, it will return null. If this is an
|
153 |
|
|
* array type, it will return Object.
|
154 |
|
|
*
|
155 |
|
|
* @param klass the Class object that's calling us
|
156 |
|
|
* @return the direct superclass of this class
|
157 |
|
|
*/
|
158 |
|
|
static native Class getSuperclass(Class klass);
|
159 |
|
|
|
160 |
|
|
/**
|
161 |
|
|
* Get the interfaces this class <EM>directly</EM> implements, in the
|
162 |
|
|
* order that they were declared. This returns an empty array, not null,
|
163 |
|
|
* for Object, primitives, void, and classes or interfaces with no direct
|
164 |
|
|
* superinterface. Array types return Cloneable and Serializable.
|
165 |
|
|
*
|
166 |
|
|
* @param klass the Class object that's calling us
|
167 |
|
|
* @return the interfaces this class directly implements
|
168 |
|
|
*/
|
169 |
|
|
static native Class[] getInterfaces(Class klass);
|
170 |
|
|
|
171 |
|
|
/**
|
172 |
|
|
* If this is an array, get the Class representing the type of array.
|
173 |
|
|
* Examples: "[[Ljava.lang.String;" would return "[Ljava.lang.String;", and
|
174 |
|
|
* calling getComponentType on that would give "java.lang.String". If
|
175 |
|
|
* this is not an array, returns null.
|
176 |
|
|
*
|
177 |
|
|
* @param klass the Class object that's calling us
|
178 |
|
|
* @return the array type of this class, or null
|
179 |
|
|
* @see Array
|
180 |
|
|
* @since 1.1
|
181 |
|
|
*/
|
182 |
|
|
static native Class getComponentType(Class klass);
|
183 |
|
|
|
184 |
|
|
/**
|
185 |
|
|
* Get the modifiers of this class. These can be decoded using Modifier,
|
186 |
|
|
* and is limited to one of public, protected, or private, and any of
|
187 |
|
|
* final, static, abstract, or interface. An array class has the same
|
188 |
|
|
* public, protected, or private modifier as its component type, and is
|
189 |
|
|
* marked final but not an interface. Primitive types and void are marked
|
190 |
|
|
* public and final, but not an interface.
|
191 |
|
|
*
|
192 |
|
|
* @param klass the Class object that's calling us
|
193 |
|
|
* @param ignoreInnerClassesAttrib if set, return the real modifiers, not
|
194 |
|
|
* the ones specified in the InnerClasses attribute.
|
195 |
|
|
* @return the modifiers of this class
|
196 |
|
|
* @see Modifier
|
197 |
|
|
* @since 1.1
|
198 |
|
|
*/
|
199 |
|
|
static native int getModifiers(Class klass, boolean ignoreInnerClassesAttrib);
|
200 |
|
|
|
201 |
|
|
/**
|
202 |
|
|
* If this is a nested or inner class, return the class that declared it.
|
203 |
|
|
* If not, return null.
|
204 |
|
|
*
|
205 |
|
|
* @param klass the Class object that's calling us
|
206 |
|
|
* @return the declaring class of this class
|
207 |
|
|
* @since 1.1
|
208 |
|
|
*/
|
209 |
|
|
static native Class getDeclaringClass(Class klass);
|
210 |
|
|
|
211 |
|
|
/**
|
212 |
|
|
* Like <code>getDeclaredClasses()</code> but without the security checks.
|
213 |
|
|
*
|
214 |
|
|
* @param klass the Class object that's calling us
|
215 |
|
|
* @param publicOnly Only public classes should be returned
|
216 |
|
|
*/
|
217 |
|
|
static native Class[] getDeclaredClasses(Class klass, boolean publicOnly);
|
218 |
|
|
|
219 |
|
|
/**
|
220 |
|
|
* Like <code>getDeclaredFields()</code> but without the security checks.
|
221 |
|
|
*
|
222 |
|
|
* @param klass the Class object that's calling us
|
223 |
|
|
* @param publicOnly Only public fields should be returned
|
224 |
|
|
*/
|
225 |
|
|
static native Field[] getDeclaredFields(Class klass, boolean publicOnly);
|
226 |
|
|
|
227 |
|
|
/**
|
228 |
|
|
* Like <code>getDeclaredMethods()</code> but without the security checks.
|
229 |
|
|
*
|
230 |
|
|
* @param klass the Class object that's calling us
|
231 |
|
|
* @param publicOnly Only public methods should be returned
|
232 |
|
|
*/
|
233 |
|
|
static native Method[] getDeclaredMethods(Class klass, boolean publicOnly);
|
234 |
|
|
|
235 |
|
|
/**
|
236 |
|
|
* Like <code>getDeclaredConstructors()</code> but without
|
237 |
|
|
* the security checks.
|
238 |
|
|
*
|
239 |
|
|
* @param klass the Class object that's calling us
|
240 |
|
|
* @param publicOnly Only public constructors should be returned
|
241 |
|
|
*/
|
242 |
|
|
static native Constructor[] getDeclaredConstructors(Class klass, boolean publicOnly);
|
243 |
|
|
|
244 |
|
|
/**
|
245 |
|
|
* Return the class loader of this class.
|
246 |
|
|
*
|
247 |
|
|
* @param klass the Class object that's calling us
|
248 |
|
|
* @return the class loader
|
249 |
|
|
*/
|
250 |
|
|
static native ClassLoader getClassLoader(Class klass);
|
251 |
|
|
|
252 |
|
|
/**
|
253 |
|
|
* Load the requested class and record the specified loader as the
|
254 |
|
|
* initiating class loader.
|
255 |
|
|
*
|
256 |
|
|
* @param name the name of the class to find
|
257 |
|
|
* @param initialize should the class initializer be run?
|
258 |
|
|
* @param loader the class loader to use (or null for the bootstrap loader)
|
259 |
|
|
* @return the Class object representing the class or null for noop
|
260 |
|
|
* @throws ClassNotFoundException if the class was not found by the
|
261 |
|
|
* class loader
|
262 |
|
|
* @throws LinkageError if linking the class fails
|
263 |
|
|
* @throws ExceptionInInitializerError if the class loads, but an exception
|
264 |
|
|
* occurs during initialization
|
265 |
|
|
*/
|
266 |
|
|
static native Class forName(String name, boolean initialize,
|
267 |
|
|
ClassLoader loader)
|
268 |
|
|
throws ClassNotFoundException;
|
269 |
|
|
|
270 |
|
|
/**
|
271 |
|
|
* Return whether this class is an array type.
|
272 |
|
|
*
|
273 |
|
|
* @param klass the Class object that's calling us
|
274 |
|
|
* @return true if this class is an array type
|
275 |
|
|
* operation
|
276 |
|
|
*/
|
277 |
|
|
static native boolean isArray(Class klass);
|
278 |
|
|
|
279 |
|
|
/**
|
280 |
|
|
* Throw a checked exception without declaring it.
|
281 |
|
|
*/
|
282 |
|
|
static native void throwException(Throwable t);
|
283 |
|
|
|
284 |
|
|
/**
|
285 |
|
|
* Returns the simple name for the specified class, as used in the source
|
286 |
|
|
* code. For normal classes, this is the content returned by
|
287 |
|
|
* <code>getName()</code> which follows the last ".". Anonymous
|
288 |
|
|
* classes have no name, and so the result of calling this method is
|
289 |
|
|
* "". The simple name of an array consists of the simple name of
|
290 |
|
|
* its component type, followed by "[]". Thus, an array with the
|
291 |
|
|
* component type of an anonymous class has a simple name of simply
|
292 |
|
|
* "[]".
|
293 |
|
|
*
|
294 |
|
|
* @param klass the class whose simple name should be returned.
|
295 |
|
|
* @return the simple name for this class.
|
296 |
|
|
*/
|
297 |
|
|
static String getSimpleName(Class klass)
|
298 |
|
|
{
|
299 |
|
|
if (isAnonymousClass(klass))
|
300 |
|
|
return "";
|
301 |
|
|
if (isArray(klass))
|
302 |
|
|
{
|
303 |
|
|
return getComponentType(klass).getSimpleName() + "[]";
|
304 |
|
|
}
|
305 |
|
|
String fullName = getName(klass);
|
306 |
|
|
int pos = fullName.lastIndexOf("$");
|
307 |
|
|
if (pos == -1)
|
308 |
|
|
pos = 0;
|
309 |
|
|
else
|
310 |
|
|
{
|
311 |
|
|
++pos;
|
312 |
|
|
while (Character.isDigit(fullName.charAt(pos)))
|
313 |
|
|
++pos;
|
314 |
|
|
}
|
315 |
|
|
int packagePos = fullName.lastIndexOf(".", pos);
|
316 |
|
|
if (packagePos == -1)
|
317 |
|
|
return fullName.substring(pos);
|
318 |
|
|
else
|
319 |
|
|
return fullName.substring(packagePos + 1);
|
320 |
|
|
}
|
321 |
|
|
|
322 |
|
|
/**
|
323 |
|
|
* Returns all annotations directly defined by the specified class. If
|
324 |
|
|
* there are no annotations associated with this class, then a zero-length
|
325 |
|
|
* array will be returned. The returned array may be modified by the client
|
326 |
|
|
* code, but this will have no effect on the annotation content of this
|
327 |
|
|
* class, and hence no effect on the return value of this method for
|
328 |
|
|
* future callers.
|
329 |
|
|
*
|
330 |
|
|
* @param klass the class whose annotations should be returned.
|
331 |
|
|
* @return the annotations directly defined by the specified class.
|
332 |
|
|
* @since 1.5
|
333 |
|
|
*/
|
334 |
|
|
static native Annotation[] getDeclaredAnnotations(Class klass);
|
335 |
|
|
|
336 |
|
|
/**
|
337 |
|
|
* <p>
|
338 |
|
|
* Returns the canonical name of the specified class, as defined by section
|
339 |
|
|
* 6.7 of the Java language specification. Each package, top-level class,
|
340 |
|
|
* top-level interface and primitive type has a canonical name. A member
|
341 |
|
|
* class has a canonical name, if its parent class has one. Likewise,
|
342 |
|
|
* an array type has a canonical name, if its component type does.
|
343 |
|
|
* Local or anonymous classes do not have canonical names.
|
344 |
|
|
* </p>
|
345 |
|
|
* <p>
|
346 |
|
|
* The canonical name for top-level classes, top-level interfaces and
|
347 |
|
|
* primitive types is always the same as the fully-qualified name.
|
348 |
|
|
* For array types, the canonical name is the canonical name of its
|
349 |
|
|
* component type with `[]' appended.
|
350 |
|
|
* </p>
|
351 |
|
|
* <p>
|
352 |
|
|
* The canonical name of a member class always refers to the place where
|
353 |
|
|
* the class was defined, and is composed of the canonical name of the
|
354 |
|
|
* defining class and the simple name of the member class, joined by `.'.
|
355 |
|
|
* For example, if a <code>Person</code> class has an inner class,
|
356 |
|
|
* <code>M</code>, then both its fully-qualified name and canonical name
|
357 |
|
|
* is <code>Person.M</code>. A subclass, <code>Staff</code>, of
|
358 |
|
|
* <code>Person</code> refers to the same inner class by the fully-qualified
|
359 |
|
|
* name of <code>Staff.M</code>, but its canonical name is still
|
360 |
|
|
* <code>Person.M</code>.
|
361 |
|
|
* </p>
|
362 |
|
|
* <p>
|
363 |
|
|
* Where no canonical name is present, <code>null</code> is returned.
|
364 |
|
|
* </p>
|
365 |
|
|
*
|
366 |
|
|
* @param klass the class whose canonical name should be retrieved.
|
367 |
|
|
* @return the canonical name of the class, or <code>null</code> if the
|
368 |
|
|
* class doesn't have a canonical name.
|
369 |
|
|
* @since 1.5
|
370 |
|
|
*/
|
371 |
|
|
static String getCanonicalName(Class klass)
|
372 |
|
|
{
|
373 |
|
|
if (isLocalClass(klass) || isAnonymousClass(klass))
|
374 |
|
|
return null;
|
375 |
|
|
if (isArray(klass))
|
376 |
|
|
{
|
377 |
|
|
String componentName = getComponentType(klass).getCanonicalName();
|
378 |
|
|
if (componentName != null)
|
379 |
|
|
return componentName + "[]";
|
380 |
|
|
}
|
381 |
|
|
if (isMemberClass(klass))
|
382 |
|
|
{
|
383 |
|
|
String memberName = getDeclaringClass(klass).getCanonicalName();
|
384 |
|
|
if (memberName != null)
|
385 |
|
|
return memberName + "." + getSimpleName(klass);
|
386 |
|
|
else
|
387 |
|
|
return memberName;
|
388 |
|
|
}
|
389 |
|
|
return getName(klass);
|
390 |
|
|
}
|
391 |
|
|
|
392 |
|
|
/**
|
393 |
|
|
* Returns the class which immediately encloses the specified class. If
|
394 |
|
|
* the class is a top-level class, this method returns <code>null</code>.
|
395 |
|
|
*
|
396 |
|
|
* @param klass the class whose enclosing class should be returned.
|
397 |
|
|
* @return the immediate enclosing class, or <code>null</code> if this is
|
398 |
|
|
* a top-level class.
|
399 |
|
|
* @since 1.5
|
400 |
|
|
*/
|
401 |
|
|
static native Class getEnclosingClass(Class klass);
|
402 |
|
|
|
403 |
|
|
/**
|
404 |
|
|
* Returns the constructor which immediately encloses the specified class.
|
405 |
|
|
* If the class is a top-level class, or a local or anonymous class
|
406 |
|
|
* immediately enclosed by a type definition, instance initializer
|
407 |
|
|
* or static initializer, then <code>null</code> is returned.
|
408 |
|
|
*
|
409 |
|
|
* @param klass the class whose enclosing constructor should be returned.
|
410 |
|
|
* @return the immediate enclosing constructor if the specified class is
|
411 |
|
|
* declared within a constructor. Otherwise, <code>null</code>
|
412 |
|
|
* is returned.
|
413 |
|
|
* @since 1.5
|
414 |
|
|
*/
|
415 |
|
|
static native Constructor getEnclosingConstructor(Class klass);
|
416 |
|
|
|
417 |
|
|
/**
|
418 |
|
|
* Returns the method which immediately encloses the specified class. If
|
419 |
|
|
* the class is a top-level class, or a local or anonymous class
|
420 |
|
|
* immediately enclosed by a type definition, instance initializer
|
421 |
|
|
* or static initializer, then <code>null</code> is returned.
|
422 |
|
|
*
|
423 |
|
|
* @param klass the class whose enclosing method should be returned.
|
424 |
|
|
* @return the immediate enclosing method if the specified class is
|
425 |
|
|
* declared within a method. Otherwise, <code>null</code>
|
426 |
|
|
* is returned.
|
427 |
|
|
* @since 1.5
|
428 |
|
|
*/
|
429 |
|
|
static native Method getEnclosingMethod(Class klass);
|
430 |
|
|
|
431 |
|
|
/**
|
432 |
|
|
* Returns the class signature as specified in Class File Format
|
433 |
|
|
* chapter in the VM specification, or null if the class is not
|
434 |
|
|
* generic.
|
435 |
|
|
*
|
436 |
|
|
* @param klass the klass to test.
|
437 |
|
|
* @return a ClassSignature string.
|
438 |
|
|
* @since 1.5
|
439 |
|
|
*/
|
440 |
|
|
static native String getClassSignature(Class klass);
|
441 |
|
|
|
442 |
|
|
/**
|
443 |
|
|
* Returns true if the specified class represents an anonymous class.
|
444 |
|
|
*
|
445 |
|
|
* @param klass the klass to test.
|
446 |
|
|
* @return true if the specified class represents an anonymous class.
|
447 |
|
|
* @since 1.5
|
448 |
|
|
*/
|
449 |
|
|
static native boolean isAnonymousClass(Class klass);
|
450 |
|
|
|
451 |
|
|
/**
|
452 |
|
|
* Returns true if the specified class represents an local class.
|
453 |
|
|
*
|
454 |
|
|
* @param klass the klass to test.
|
455 |
|
|
* @return true if the specified class represents an local class.
|
456 |
|
|
* @since 1.5
|
457 |
|
|
*/
|
458 |
|
|
static native boolean isLocalClass(Class klass);
|
459 |
|
|
|
460 |
|
|
/**
|
461 |
|
|
* Returns true if the specified class represents an member class.
|
462 |
|
|
*
|
463 |
|
|
* @param klass the klass to test.
|
464 |
|
|
* @return true if the specified class represents an member class.
|
465 |
|
|
* @since 1.5
|
466 |
|
|
*/
|
467 |
|
|
static native boolean isMemberClass(Class klass);
|
468 |
|
|
|
469 |
|
|
} // class VMClass
|