[multiple changes]
[gcc.git] / libjava / java / lang / Class.java
1 /* Class.java -- Representation of a Java class.
2 Copyright (C) 1998, 1999, 2000, 2002, 2003, 2004, 2005
3 Free Software Foundation
4
5 This file is part of GNU Classpath.
6
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING. If not, write to the
19 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301 USA.
21
22 Linking this library statically or dynamically with other modules is
23 making a combined work based on this library. Thus, the terms and
24 conditions of the GNU General Public License cover the whole
25 combination.
26
27 As a special exception, the copyright holders of this library give you
28 permission to link this library with independent modules to produce an
29 executable, regardless of the license terms of these independent
30 modules, and to copy and distribute the resulting executable under
31 terms of your choice, provided that you also meet, for each linked
32 independent module, the terms and conditions of the license of that
33 module. An independent module is a module which is not derived from
34 or based on this library. If you modify this library, you may extend
35 this exception to your version of the library, but you are not
36 obligated to do so. If you do not wish to do so, delete this
37 exception statement from your version. */
38
39 package java.lang;
40
41 import java.io.InputStream;
42 import java.io.Serializable;
43 import java.lang.reflect.Constructor;
44 import java.lang.reflect.Field;
45 import java.lang.reflect.Member;
46 import java.lang.reflect.Method;
47 import java.net.URL;
48 import java.security.ProtectionDomain;
49 import java.util.ArrayList;
50 import java.util.Arrays;
51 import java.util.HashSet;
52
53 /**
54 * A Class represents a Java type. There will never be multiple Class
55 * objects with identical names and ClassLoaders. Primitive types, array
56 * types, and void also have a Class object.
57 *
58 * <p>Arrays with identical type and number of dimensions share the same class.
59 * The array class ClassLoader is the same as the ClassLoader of the element
60 * type of the array (which can be null to indicate the bootstrap classloader).
61 * The name of an array class is <code>[&lt;signature format&gt;;</code>.
62 * <p> For example,
63 * String[]'s class is <code>[Ljava.lang.String;</code>. boolean, byte,
64 * short, char, int, long, float and double have the "type name" of
65 * Z,B,S,C,I,J,F,D for the purposes of array classes. If it's a
66 * multidimensioned array, the same principle applies:
67 * <code>int[][][]</code> == <code>[[[I</code>.
68 *
69 * <p>There is no public constructor - Class objects are obtained only through
70 * the virtual machine, as defined in ClassLoaders.
71 *
72 * @serialData Class objects serialize specially:
73 * <code>TC_CLASS ClassDescriptor</code>. For more serialization information,
74 * see {@link ObjectStreamClass}.
75 *
76 * @author John Keiser
77 * @author Eric Blake (ebb9@email.byu.edu)
78 * @author Tom Tromey (tromey@cygnus.com)
79 * @since 1.0
80 * @see ClassLoader
81 */
82 public final class Class implements Serializable
83 {
84 /**
85 * Class is non-instantiable from Java code; only the VM can create
86 * instances of this class.
87 */
88 private Class ()
89 {
90 }
91
92 // Initialize the class.
93 private native void initializeClass ();
94
95 // finalization
96 protected native void finalize () throws Throwable;
97
98 /**
99 * Use the classloader of the current class to load, link, and initialize
100 * a class. This is equivalent to your code calling
101 * <code>Class.forName(name, true, getClass().getClassLoader())</code>.
102 *
103 * @param name the name of the class to find
104 * @return the Class object representing the class
105 * @throws ClassNotFoundException if the class was not found by the
106 * classloader
107 * @throws LinkageError if linking the class fails
108 * @throws ExceptionInInitializerError if the class loads, but an exception
109 * occurs during initialization
110 */
111 public static native Class forName (String className)
112 throws ClassNotFoundException;
113
114 // A private internal method that is called by compiler-generated code.
115 private static Class forName (String className, Class caller)
116 throws ClassNotFoundException
117 {
118 return forName(className, true, caller.getClassLoader());
119 }
120
121
122 /**
123 * Use the specified classloader to load and link a class. If the loader
124 * is null, this uses the bootstrap class loader (provide the security
125 * check succeeds). Unfortunately, this method cannot be used to obtain
126 * the Class objects for primitive types or for void, you have to use
127 * the fields in the appropriate java.lang wrapper classes.
128 *
129 * <p>Calls <code>classloader.loadclass(name, initialize)</code>.
130 *
131 * @param name the name of the class to find
132 * @param initialize whether or not to initialize the class at this time
133 * @param classloader the classloader to use to find the class; null means
134 * to use the bootstrap class loader
135 * @throws ClassNotFoundException if the class was not found by the
136 * classloader
137 * @throws LinkageError if linking the class fails
138 * @throws ExceptionInInitializerError if the class loads, but an exception
139 * occurs during initialization
140 * @throws SecurityException if the <code>classloader</code> argument
141 * is <code>null</code> and the caller does not have the
142 * <code>RuntimePermission("getClassLoader")</code> permission
143 * @see ClassLoader
144 * @since 1.2
145 */
146 public static native Class forName (String className, boolean initialize,
147 ClassLoader loader)
148 throws ClassNotFoundException;
149
150 /**
151 * Get all the public member classes and interfaces declared in this
152 * class or inherited from superclasses. This returns an array of length
153 * 0 if there are no member classes, including for primitive types. A
154 * security check may be performed, with
155 * <code>checkMemberAccess(this, Member.PUBLIC)</code> as well as
156 * <code>checkPackageAccess</code> both having to succeed.
157 *
158 * @return all public member classes in this class
159 * @throws SecurityException if the security check fails
160 * @since 1.1
161 */
162 public Class[] getClasses()
163 {
164 memberAccessCheck(Member.PUBLIC);
165 return internalGetClasses();
166 }
167
168 /**
169 * Like <code>getClasses()</code> but without the security checks.
170 */
171 private Class[] internalGetClasses()
172 {
173 ArrayList list = new ArrayList();
174 list.addAll(Arrays.asList(getDeclaredClasses(true)));
175 Class superClass = getSuperclass();
176 if (superClass != null)
177 list.addAll(Arrays.asList(superClass.internalGetClasses()));
178 return (Class[])list.toArray(new Class[list.size()]);
179 }
180
181 /**
182 * Get the ClassLoader that loaded this class. If the class was loaded
183 * by the bootstrap classloader, this method will return null.
184 * If there is a security manager, and the caller's class loader is not
185 * an ancestor of the requested one, a security check of
186 * <code>RuntimePermission("getClassLoader")</code>
187 * must first succeed. Primitive types and void return null.
188 *
189 * @return the ClassLoader that loaded this class
190 * @throws SecurityException if the security check fails
191 * @see ClassLoader
192 * @see RuntimePermission
193 */
194 public native ClassLoader getClassLoader ();
195
196 // A private internal method that is called by compiler-generated code.
197 private final native ClassLoader getClassLoader (Class caller);
198
199 /**
200 * If this is an array, get the Class representing the type of array.
201 * Examples: "[[Ljava.lang.String;" would return "[Ljava.lang.String;", and
202 * calling getComponentType on that would give "java.lang.String". If
203 * this is not an array, returns null.
204 *
205 * @return the array type of this class, or null
206 * @see Array
207 * @since 1.1
208 */
209 public native Class getComponentType ();
210
211 /**
212 * Get a public constructor declared in this class. If the constructor takes
213 * no argument, an array of zero elements and null are equivalent for the
214 * types argument. A security check may be performed, with
215 * <code>checkMemberAccess(this, Member.PUBLIC)</code> as well as
216 * <code>checkPackageAccess</code> both having to succeed.
217 *
218 * @param types the type of each parameter
219 * @return the constructor
220 * @throws NoSuchMethodException if the constructor does not exist
221 * @throws SecurityException if the security check fails
222 * @see #getConstructors()
223 * @since 1.1
224 */
225 public native Constructor getConstructor(Class[] args)
226 throws NoSuchMethodException;
227
228 /**
229 * Get all the public constructors of this class. This returns an array of
230 * length 0 if there are no constructors, including for primitive types,
231 * arrays, and interfaces. It does, however, include the default
232 * constructor if one was supplied by the compiler. A security check may
233 * be performed, with <code>checkMemberAccess(this, Member.PUBLIC)</code>
234 * as well as <code>checkPackageAccess</code> both having to succeed.
235 *
236 * @return all public constructors in this class
237 * @throws SecurityException if the security check fails
238 * @since 1.1
239 */
240 public Constructor[] getConstructors()
241 {
242 memberAccessCheck(Member.PUBLIC);
243 return getDeclaredConstructors(true);
244 }
245
246 /**
247 * Get a constructor declared in this class. If the constructor takes no
248 * argument, an array of zero elements and null are equivalent for the
249 * types argument. A security check may be performed, with
250 * <code>checkMemberAccess(this, Member.DECLARED)</code> as well as
251 * <code>checkPackageAccess</code> both having to succeed.
252 *
253 * @param types the type of each parameter
254 * @return the constructor
255 * @throws NoSuchMethodException if the constructor does not exist
256 * @throws SecurityException if the security check fails
257 * @see #getDeclaredConstructors()
258 * @since 1.1
259 */
260 public native Constructor getDeclaredConstructor(Class[] args)
261 throws NoSuchMethodException;
262
263 /**
264 * Get all the declared member classes and interfaces in this class, but
265 * not those inherited from superclasses. This returns an array of length
266 * 0 if there are no member classes, including for primitive types. A
267 * security check may be performed, with
268 * <code>checkMemberAccess(this, Member.DECLARED)</code> as well as
269 * <code>checkPackageAccess</code> both having to succeed.
270 *
271 * @return all declared member classes in this class
272 * @throws SecurityException if the security check fails
273 * @since 1.1
274 */
275 public Class[] getDeclaredClasses()
276 {
277 memberAccessCheck(Member.DECLARED);
278 return getDeclaredClasses(false);
279 }
280
281 native Class[] getDeclaredClasses (boolean publicOnly);
282
283 /**
284 * Get all the declared constructors of this class. This returns an array of
285 * length 0 if there are no constructors, including for primitive types,
286 * arrays, and interfaces. It does, however, include the default
287 * constructor if one was supplied by the compiler. A security check may
288 * be performed, with <code>checkMemberAccess(this, Member.DECLARED)</code>
289 * as well as <code>checkPackageAccess</code> both having to succeed.
290 *
291 * @return all constructors in this class
292 * @throws SecurityException if the security check fails
293 * @since 1.1
294 */
295 public Constructor[] getDeclaredConstructors()
296 {
297 memberAccessCheck(Member.DECLARED);
298 return getDeclaredConstructors(false);
299 }
300
301 native Constructor[] getDeclaredConstructors (boolean publicOnly);
302
303 /**
304 * Get a field declared in this class, where name is its simple name. The
305 * implicit length field of arrays is not available. A security check may
306 * be performed, with <code>checkMemberAccess(this, Member.DECLARED)</code>
307 * as well as <code>checkPackageAccess</code> both having to succeed.
308 *
309 * @param name the name of the field
310 * @return the field
311 * @throws NoSuchFieldException if the field does not exist
312 * @throws SecurityException if the security check fails
313 * @see #getDeclaredFields()
314 * @since 1.1
315 */
316 public native Field getDeclaredField(String fieldName)
317 throws NoSuchFieldException;
318
319 /**
320 * Get all the declared fields in this class, but not those inherited from
321 * superclasses. This returns an array of length 0 if there are no fields,
322 * including for primitive types. This does not return the implicit length
323 * field of arrays. A security check may be performed, with
324 * <code>checkMemberAccess(this, Member.DECLARED)</code> as well as
325 * <code>checkPackageAccess</code> both having to succeed.
326 *
327 * @return all declared fields in this class
328 * @throws SecurityException if the security check fails
329 * @since 1.1
330 */
331 public Field[] getDeclaredFields()
332 {
333 memberAccessCheck(Member.DECLARED);
334 return getDeclaredFields(false);
335 }
336
337 native Field[] getDeclaredFields (boolean publicOnly);
338
339 private native Method _getDeclaredMethod(String methodName, Class[] args);
340
341 /**
342 * Get a method declared in this class, where name is its simple name. The
343 * implicit methods of Object are not available from arrays or interfaces.
344 * Constructors (named "&lt;init&gt;" in the class file) and class initializers
345 * (name "&lt;clinit&gt;") are not available. The Virtual Machine allows
346 * multiple methods with the same signature but differing return types; in
347 * such a case the most specific return types are favored, then the final
348 * choice is arbitrary. If the method takes no argument, an array of zero
349 * elements and null are equivalent for the types argument. A security
350 * check may be performed, with
351 * <code>checkMemberAccess(this, Member.DECLARED)</code> as well as
352 * <code>checkPackageAccess</code> both having to succeed.
353 *
354 * @param methodName the name of the method
355 * @param types the type of each parameter
356 * @return the method
357 * @throws NoSuchMethodException if the method does not exist
358 * @throws SecurityException if the security check fails
359 * @see #getDeclaredMethods()
360 * @since 1.1
361 */
362 public Method getDeclaredMethod(String methodName, Class[] args)
363 throws NoSuchMethodException
364 {
365 memberAccessCheck(Member.DECLARED);
366
367 if ("<init>".equals(methodName) || "<clinit>".equals(methodName))
368 throw new NoSuchMethodException(methodName);
369
370 Method match = _getDeclaredMethod(methodName, args);
371 if (match == null)
372 throw new NoSuchMethodException(methodName);
373 return match;
374 }
375
376 /**
377 * Get all the declared methods in this class, but not those inherited from
378 * superclasses. This returns an array of length 0 if there are no methods,
379 * including for primitive types. This does include the implicit methods of
380 * arrays and interfaces which mirror methods of Object, nor does it
381 * include constructors or the class initialization methods. The Virtual
382 * Machine allows multiple methods with the same signature but differing
383 * return types; all such methods are in the returned array. A security
384 * check may be performed, with
385 * <code>checkMemberAccess(this, Member.DECLARED)</code> as well as
386 * <code>checkPackageAccess</code> both having to succeed.
387 *
388 * @return all declared methods in this class
389 * @throws SecurityException if the security check fails
390 * @since 1.1
391 */
392 public native Method[] getDeclaredMethods();
393
394 /**
395 * If this is a nested or inner class, return the class that declared it.
396 * If not, return null.
397 *
398 * @return the declaring class of this class
399 * @since 1.1
400 */
401 // This is marked as unimplemented in the JCL book.
402 public native Class getDeclaringClass ();
403
404 private native Field getField (String fieldName, int hash)
405 throws NoSuchFieldException;
406
407 /**
408 * Get a public field declared or inherited in this class, where name is
409 * its simple name. If the class contains multiple accessible fields by
410 * that name, an arbitrary one is returned. The implicit length field of
411 * arrays is not available. A security check may be performed, with
412 * <code>checkMemberAccess(this, Member.PUBLIC)</code> as well as
413 * <code>checkPackageAccess</code> both having to succeed.
414 *
415 * @param fieldName the name of the field
416 * @return the field
417 * @throws NoSuchFieldException if the field does not exist
418 * @throws SecurityException if the security check fails
419 * @see #getFields()
420 * @since 1.1
421 */
422 public Field getField(String fieldName)
423 throws NoSuchFieldException
424 {
425 memberAccessCheck(Member.PUBLIC);
426 Field field = getField(fieldName, fieldName.hashCode());
427 if (field == null)
428 throw new NoSuchFieldException(fieldName);
429 return field;
430 }
431
432 /**
433 * Get all the public fields declared in this class or inherited from
434 * superclasses. This returns an array of length 0 if there are no fields,
435 * including for primitive types. This does not return the implicit length
436 * field of arrays. A security check may be performed, with
437 * <code>checkMemberAccess(this, Member.PUBLIC)</code> as well as
438 * <code>checkPackageAccess</code> both having to succeed.
439 *
440 * @return all public fields in this class
441 * @throws SecurityException if the security check fails
442 * @since 1.1
443 */
444 public Field[] getFields()
445 {
446 memberAccessCheck(Member.PUBLIC);
447 return internalGetFields();
448 }
449
450 /**
451 * Like <code>getFields()</code> but without the security checks.
452 */
453 private Field[] internalGetFields()
454 {
455 HashSet set = new HashSet();
456 set.addAll(Arrays.asList(getDeclaredFields(true)));
457 Class[] interfaces = getInterfaces();
458 for (int i = 0; i < interfaces.length; i++)
459 set.addAll(Arrays.asList(interfaces[i].internalGetFields()));
460 Class superClass = getSuperclass();
461 if (superClass != null)
462 set.addAll(Arrays.asList(superClass.internalGetFields()));
463 return (Field[])set.toArray(new Field[set.size()]);
464 }
465
466 /**
467 * Returns the <code>Package</code> in which this class is defined
468 * Returns null when this information is not available from the
469 * classloader of this class.
470 *
471 * @return the package for this class, if it is available
472 * @since 1.2
473 */
474 public Package getPackage()
475 {
476 ClassLoader cl = getClassLoader();
477 if (cl != null)
478 return cl.getPackage(getPackagePortion(getName()));
479 else
480 return VMClassLoader.getPackage(getPackagePortion(getName()));
481 }
482
483 /**
484 * Get the interfaces this class <em>directly</em> implements, in the
485 * order that they were declared. This returns an empty array, not null,
486 * for Object, primitives, void, and classes or interfaces with no direct
487 * superinterface. Array types return Cloneable and Serializable.
488 *
489 * @return the interfaces this class directly implements
490 */
491 public native Class[] getInterfaces ();
492
493 private final native void getSignature(StringBuffer buffer);
494 private static final native String getSignature(Class[] args,
495 boolean is_construtor);
496
497 public native Method _getMethod(String methodName, Class[] args);
498
499 /**
500 * Get a public method declared or inherited in this class, where name is
501 * its simple name. The implicit methods of Object are not available from
502 * interfaces. Constructors (named "&lt;init&gt;" in the class file) and class
503 * initializers (name "&lt;clinit&gt;") are not available. The Virtual
504 * Machine allows multiple methods with the same signature but differing
505 * return types, and the class can inherit multiple methods of the same
506 * return type; in such a case the most specific return types are favored,
507 * then the final choice is arbitrary. If the method takes no argument, an
508 * array of zero elements and null are equivalent for the types argument.
509 * A security check may be performed, with
510 * <code>checkMemberAccess(this, Member.PUBLIC)</code> as well as
511 * <code>checkPackageAccess</code> both having to succeed.
512 *
513 * @param methodName the name of the method
514 * @param types the type of each parameter
515 * @return the method
516 * @throws NoSuchMethodException if the method does not exist
517 * @throws SecurityException if the security check fails
518 * @see #getMethods()
519 * @since 1.1
520 */
521 public Method getMethod(String methodName, Class[] args)
522 throws NoSuchMethodException
523 {
524 memberAccessCheck(Member.PUBLIC);
525
526 if ("<init>".equals(methodName) || "<clinit>".equals(methodName))
527 throw new NoSuchMethodException(methodName);
528
529 Method method = _getMethod(methodName, args);
530 if (method == null)
531 throw new NoSuchMethodException(methodName);
532 return method;
533 }
534
535 private native int _getMethods (Method[] result, int offset);
536
537 /**
538 * Get all the public methods declared in this class or inherited from
539 * superclasses. This returns an array of length 0 if there are no methods,
540 * including for primitive types. This does not include the implicit
541 * methods of interfaces which mirror methods of Object, nor does it
542 * include constructors or the class initialization methods. The Virtual
543 * Machine allows multiple methods with the same signature but differing
544 * return types; all such methods are in the returned array. A security
545 * check may be performed, with
546 * <code>checkMemberAccess(this, Member.PUBLIC)</code> as well as
547 * <code>checkPackageAccess</code> both having to succeed.
548 *
549 * @return all public methods in this class
550 * @throws SecurityException if the security check fails
551 * @since 1.1
552 */
553 public native Method[] getMethods();
554
555 /**
556 * Get the modifiers of this class. These can be decoded using Modifier,
557 * and is limited to one of public, protected, or private, and any of
558 * final, static, abstract, or interface. An array class has the same
559 * public, protected, or private modifier as its component type, and is
560 * marked final but not an interface. Primitive types and void are marked
561 * public and final, but not an interface.
562 *
563 * @return the modifiers of this class
564 * @see Modifer
565 * @since 1.1
566 */
567 public native int getModifiers ();
568
569 /**
570 * Get the name of this class, separated by dots for package separators.
571 * If the class represents a primitive type, or void, then the
572 * name of the type as it appears in the Java programming language
573 * is returned. For instance, <code>Byte.TYPE.getName()</code>
574 * returns "byte".
575 *
576 * Arrays are specially encoded as shown on this table.
577 * <pre>
578 * array type [<em>element type</em>
579 * (note that the element type is encoded per
580 * this table)
581 * boolean Z
582 * byte B
583 * char C
584 * short S
585 * int I
586 * long J
587 * float F
588 * double D
589 * void V
590 * class or interface, alone: &lt;dotted name&gt;
591 * class or interface, as element type: L&lt;dotted name&gt;;
592 * </pre>
593 *
594 * @return the name of this class
595 */
596 public native String getName ();
597
598 /**
599 * Get a resource URL using this class's package using the
600 * getClassLoader().getResource() method. If this class was loaded using
601 * the system classloader, ClassLoader.getSystemResource() is used instead.
602 *
603 * <p>If the name you supply is absolute (it starts with a <code>/</code>),
604 * then the leading <code>/</code> is removed and it is passed on to
605 * getResource(). If it is relative, the package name is prepended, and
606 * <code>.</code>'s are replaced with <code>/</code>.
607 *
608 * <p>The URL returned is system- and classloader-dependent, and could
609 * change across implementations.
610 *
611 * @param resourceName the name of the resource, generally a path
612 * @return the URL to the resource
613 * @throws NullPointerException if name is null
614 * @since 1.1
615 */
616 public URL getResource(String resourceName)
617 {
618 String name = resourcePath(resourceName);
619 ClassLoader loader = getClassLoader();
620 if (loader == null)
621 return ClassLoader.getSystemResource(name);
622 return loader.getResource(name);
623 }
624
625 /**
626 * Get a resource using this class's package using the
627 * getClassLoader().getResourceAsStream() method. If this class was loaded
628 * using the system classloader, ClassLoader.getSystemResource() is used
629 * instead.
630 *
631 * <p>If the name you supply is absolute (it starts with a <code>/</code>),
632 * then the leading <code>/</code> is removed and it is passed on to
633 * getResource(). If it is relative, the package name is prepended, and
634 * <code>.</code>'s are replaced with <code>/</code>.
635 *
636 * <p>The URL returned is system- and classloader-dependent, and could
637 * change across implementations.
638 *
639 * @param resourceName the name of the resource, generally a path
640 * @return an InputStream with the contents of the resource in it, or null
641 * @throws NullPointerException if name is null
642 * @since 1.1
643 */
644 public InputStream getResourceAsStream(String resourceName)
645 {
646 String name = resourcePath(resourceName);
647 ClassLoader loader = getClassLoader();
648 if (loader == null)
649 return ClassLoader.getSystemResourceAsStream(name);
650 return loader.getResourceAsStream(name);
651 }
652
653 private String resourcePath(String resourceName)
654 {
655 if (resourceName.length() > 0)
656 {
657 if (resourceName.charAt(0) != '/')
658 {
659 String pkg = getPackagePortion(getName());
660 if (pkg.length() > 0)
661 resourceName = pkg.replace('.','/') + '/' + resourceName;
662 }
663 else
664 {
665 resourceName = resourceName.substring(1);
666 }
667 }
668 return resourceName;
669 }
670
671 /**
672 * Get the signers of this class. This returns null if there are no signers,
673 * such as for primitive types or void.
674 *
675 * @return the signers of this class
676 * @since 1.1
677 */
678 public native Object[] getSigners ();
679
680 /**
681 * Set the signers of this class.
682 *
683 * @param signers the signers of this class
684 */
685 native void setSigners(Object[] signers);
686
687 /**
688 * Get the direct superclass of this class. If this is an interface,
689 * Object, a primitive type, or void, it will return null. If this is an
690 * array type, it will return Object.
691 *
692 * @return the direct superclass of this class
693 */
694 public native Class getSuperclass ();
695
696 /**
697 * Return whether this class is an array type.
698 *
699 * @return whether this class is an array type
700 * @since 1.1
701 */
702 public native boolean isArray ();
703
704 /**
705 * Discover whether an instance of the Class parameter would be an
706 * instance of this Class as well. Think of doing
707 * <code>isInstance(c.newInstance())</code> or even
708 * <code>c.newInstance() instanceof (this class)</code>. While this
709 * checks widening conversions for objects, it must be exact for primitive
710 * types.
711 *
712 * @param c the class to check
713 * @return whether an instance of c would be an instance of this class
714 * as well
715 * @throws NullPointerException if c is null
716 * @since 1.1
717 */
718 public native boolean isAssignableFrom (Class c);
719
720 /**
721 * Discover whether an Object is an instance of this Class. Think of it
722 * as almost like <code>o instanceof (this class)</code>.
723 *
724 * @param o the Object to check
725 * @return whether o is an instance of this class
726 * @since 1.1
727 */
728 public native boolean isInstance (Object o);
729
730 /**
731 * Check whether this class is an interface or not. Array types are not
732 * interfaces.
733 *
734 * @return whether this class is an interface or not
735 */
736 public native boolean isInterface ();
737
738 /**
739 * Return whether this class is a primitive type. A primitive type class
740 * is a class representing a kind of "placeholder" for the various
741 * primitive types, or void. You can access the various primitive type
742 * classes through java.lang.Boolean.TYPE, java.lang.Integer.TYPE, etc.,
743 * or through boolean.class, int.class, etc.
744 *
745 * @return whether this class is a primitive type
746 * @see Boolean#TYPE
747 * @see Byte#TYPE
748 * @see Character#TYPE
749 * @see Short#TYPE
750 * @see Integer#TYPE
751 * @see Long#TYPE
752 * @see Float#TYPE
753 * @see Double#TYPE
754 * @see Void#TYPE
755 * @since 1.1
756 */
757 public native boolean isPrimitive ();
758
759 /**
760 * Get a new instance of this class by calling the no-argument constructor.
761 * The class is initialized if it has not been already. A security check
762 * may be performed, with <code>checkMemberAccess(this, Member.PUBLIC)</code>
763 * as well as <code>checkPackageAccess</code> both having to succeed.
764 *
765 * @return a new instance of this class
766 * @throws InstantiationException if there is not a no-arg constructor
767 * for this class, including interfaces, abstract classes, arrays,
768 * primitive types, and void; or if an exception occurred during
769 * the constructor
770 * @throws IllegalAccessException if you are not allowed to access the
771 * no-arg constructor because of scoping reasons
772 * @throws SecurityException if the security check fails
773 * @throws ExceptionInInitializerError if class initialization caused by
774 * this call fails with an exception
775 */
776 public native Object newInstance ()
777 throws InstantiationException, IllegalAccessException;
778
779 // We need a native method to retrieve the protection domain, because we
780 // can't add fields to java.lang.Class that are accessible from Java.
781 private native ProtectionDomain getProtectionDomain0();
782
783 /**
784 * Returns the protection domain of this class. If the classloader did not
785 * record the protection domain when creating this class the unknown
786 * protection domain is returned which has a <code>null</code> code source
787 * and all permissions. A security check may be performed, with
788 * <code>RuntimePermission("getProtectionDomain")</code>.
789 *
790 * @return the protection domain
791 * @throws SecurityException if the security manager exists and the caller
792 * does not have <code>RuntimePermission("getProtectionDomain")</code>.
793 * @see RuntimePermission
794 * @since 1.2
795 */
796 public ProtectionDomain getProtectionDomain()
797 {
798 SecurityManager sm = System.getSecurityManager();
799 if (sm != null)
800 sm.checkPermission(VMClassLoader.protectionDomainPermission);
801
802 ProtectionDomain protectionDomain = getProtectionDomain0();
803
804 if (protectionDomain == null)
805 return VMClassLoader.unknownProtectionDomain;
806 else
807 return protectionDomain;
808 }
809
810 /**
811 * Return the human-readable form of this Object. For an object, this
812 * is either "interface " or "class " followed by <code>getName()</code>,
813 * for primitive types and void it is just <code>getName()</code>.
814 *
815 * @return the human-readable form of this Object
816 */
817 public String toString()
818 {
819 if (isPrimitive())
820 return getName();
821 return (isInterface() ? "interface " : "class ") + getName();
822 }
823
824 /**
825 * Returns the desired assertion status of this class, if it were to be
826 * initialized at this moment. The class assertion status, if set, is
827 * returned; the backup is the default package status; then if there is
828 * a class loader, that default is returned; and finally the system default
829 * is returned. This method seldom needs calling in user code, but exists
830 * for compilers to implement the assert statement. Note that there is no
831 * guarantee that the result of this method matches the class's actual
832 * assertion status.
833 *
834 * @return the desired assertion status
835 * @see ClassLoader#setClassAssertionStatus(String, boolean)
836 * @see ClassLoader#setPackageAssertionStatus(String, boolean)
837 * @see ClassLoader#setDefaultAssertionStatus(boolean)
838 * @since 1.4
839 */
840 public boolean desiredAssertionStatus()
841 {
842 ClassLoader c = getClassLoader();
843 Object status;
844 if (c == null)
845 return VMClassLoader.defaultAssertionStatus();
846 if (c.classAssertionStatus != null)
847 synchronized (c)
848 {
849 status = c.classAssertionStatus.get(getName());
850 if (status != null)
851 return status.equals(Boolean.TRUE);
852 }
853 else
854 {
855 status = ClassLoader.systemClassAssertionStatus.get(getName());
856 if (status != null)
857 return status.equals(Boolean.TRUE);
858 }
859 if (c.packageAssertionStatus != null)
860 synchronized (c)
861 {
862 String name = getPackagePortion(getName());
863 if ("".equals(name))
864 status = c.packageAssertionStatus.get(null);
865 else
866 do
867 {
868 status = c.packageAssertionStatus.get(name);
869 name = getPackagePortion(name);
870 }
871 while (! "".equals(name) && status == null);
872 if (status != null)
873 return status.equals(Boolean.TRUE);
874 }
875 else
876 {
877 String name = getPackagePortion(getName());
878 if ("".equals(name))
879 status = ClassLoader.systemPackageAssertionStatus.get(null);
880 else
881 do
882 {
883 status = ClassLoader.systemPackageAssertionStatus.get(name);
884 name = getPackagePortion(name);
885 }
886 while (! "".equals(name) && status == null);
887 if (status != null)
888 return status.equals(Boolean.TRUE);
889 }
890 return c.defaultAssertionStatus;
891 }
892
893 /**
894 * Strip the last portion of the name (after the last dot).
895 *
896 * @param name the name to get package of
897 * @return the package name, or "" if no package
898 */
899 private static String getPackagePortion(String name)
900 {
901 int lastInd = name.lastIndexOf('.');
902 if (lastInd == -1)
903 return "";
904 return name.substring(0, lastInd);
905 }
906
907 /**
908 * Perform security checks common to all of the methods that
909 * get members of this Class.
910 */
911 private void memberAccessCheck(int which)
912 {
913 SecurityManager sm = System.getSecurityManager();
914 if (sm != null)
915 {
916 sm.checkMemberAccess(this, which);
917 Package pkg = getPackage();
918 if (pkg != null)
919 sm.checkPackageAccess(pkg.getName());
920 }
921 }
922 }