svn merge -r108665:108708 svn+ssh://gcc.gnu.org/svn/gcc/trunk
[official-gcc.git] / libjava / java / lang / Class.java
blob60ca4577e992612c1fe255fb7cbd41b9d1fb9145
1 /* Class.java -- Representation of a Java class.
2 Copyright (C) 1998, 1999, 2000, 2002, 2003, 2004, 2005
3 Free Software Foundation
5 This file is part of GNU Classpath.
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.
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.
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.
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.
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. */
39 package java.lang;
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;
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.
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>.
69 * <p>There is no public constructor - Class objects are obtained only through
70 * the virtual machine, as defined in ClassLoaders.
72 * @serialData Class objects serialize specially:
73 * <code>TC_CLASS ClassDescriptor</code>. For more serialization information,
74 * see {@link ObjectStreamClass}.
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
82 public final class Class implements Serializable
84 /**
85 * Class is non-instantiable from Java code; only the VM can create
86 * instances of this class.
88 private Class ()
92 // Initialize the class.
93 private native void initializeClass ();
95 // finalization
96 protected native void finalize () throws Throwable;
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>.
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
111 public static native Class forName (String className)
112 throws ClassNotFoundException;
115 * Use the specified classloader to load and link a class. If the loader
116 * is null, this uses the bootstrap class loader (provide the security
117 * check succeeds). Unfortunately, this method cannot be used to obtain
118 * the Class objects for primitive types or for void, you have to use
119 * the fields in the appropriate java.lang wrapper classes.
121 * <p>Calls <code>classloader.loadclass(name, initialize)</code>.
123 * @param name the name of the class to find
124 * @param initialize whether or not to initialize the class at this time
125 * @param classloader the classloader to use to find the class; null means
126 * to use the bootstrap class loader
127 * @throws ClassNotFoundException if the class was not found by the
128 * classloader
129 * @throws LinkageError if linking the class fails
130 * @throws ExceptionInInitializerError if the class loads, but an exception
131 * occurs during initialization
132 * @throws SecurityException if the <code>classloader</code> argument
133 * is <code>null</code> and the caller does not have the
134 * <code>RuntimePermission("getClassLoader")</code> permission
135 * @see ClassLoader
136 * @since 1.2
138 public static native Class forName (String className, boolean initialize,
139 ClassLoader loader)
140 throws ClassNotFoundException;
143 * Get all the public member classes and interfaces declared in this
144 * class or inherited from superclasses. This returns an array of length
145 * 0 if there are no member classes, including for primitive types. A
146 * security check may be performed, with
147 * <code>checkMemberAccess(this, Member.PUBLIC)</code> as well as
148 * <code>checkPackageAccess</code> both having to succeed.
150 * @return all public member classes in this class
151 * @throws SecurityException if the security check fails
152 * @since 1.1
154 public Class[] getClasses()
156 memberAccessCheck(Member.PUBLIC);
157 return internalGetClasses();
161 * Like <code>getClasses()</code> but without the security checks.
163 private Class[] internalGetClasses()
165 ArrayList list = new ArrayList();
166 list.addAll(Arrays.asList(getDeclaredClasses(true)));
167 Class superClass = getSuperclass();
168 if (superClass != null)
169 list.addAll(Arrays.asList(superClass.internalGetClasses()));
170 return (Class[])list.toArray(new Class[list.size()]);
174 * Get the ClassLoader that loaded this class. If the class was loaded
175 * by the bootstrap classloader, this method will return null.
176 * If there is a security manager, and the caller's class loader is not
177 * an ancestor of the requested one, a security check of
178 * <code>RuntimePermission("getClassLoader")</code>
179 * must first succeed. Primitive types and void return null.
181 * @return the ClassLoader that loaded this class
182 * @throws SecurityException if the security check fails
183 * @see ClassLoader
184 * @see RuntimePermission
186 public native ClassLoader getClassLoader ();
189 * If this is an array, get the Class representing the type of array.
190 * Examples: "[[Ljava.lang.String;" would return "[Ljava.lang.String;", and
191 * calling getComponentType on that would give "java.lang.String". If
192 * this is not an array, returns null.
194 * @return the array type of this class, or null
195 * @see Array
196 * @since 1.1
198 public native Class getComponentType ();
201 * Get a public constructor declared in this class. If the constructor takes
202 * no argument, an array of zero elements and null are equivalent for the
203 * types argument. A security check may be performed, with
204 * <code>checkMemberAccess(this, Member.PUBLIC)</code> as well as
205 * <code>checkPackageAccess</code> both having to succeed.
207 * @param types the type of each parameter
208 * @return the constructor
209 * @throws NoSuchMethodException if the constructor does not exist
210 * @throws SecurityException if the security check fails
211 * @see #getConstructors()
212 * @since 1.1
214 public native Constructor getConstructor(Class[] args)
215 throws NoSuchMethodException;
218 * Get all the public constructors of this class. This returns an array of
219 * length 0 if there are no constructors, including for primitive types,
220 * arrays, and interfaces. It does, however, include the default
221 * constructor if one was supplied by the compiler. A security check may
222 * be performed, with <code>checkMemberAccess(this, Member.PUBLIC)</code>
223 * as well as <code>checkPackageAccess</code> both having to succeed.
225 * @return all public constructors in this class
226 * @throws SecurityException if the security check fails
227 * @since 1.1
229 public Constructor[] getConstructors()
231 memberAccessCheck(Member.PUBLIC);
232 return getDeclaredConstructors(true);
236 * Get a constructor declared in this class. If the constructor takes no
237 * argument, an array of zero elements and null are equivalent for the
238 * types argument. A security check may be performed, with
239 * <code>checkMemberAccess(this, Member.DECLARED)</code> as well as
240 * <code>checkPackageAccess</code> both having to succeed.
242 * @param types the type of each parameter
243 * @return the constructor
244 * @throws NoSuchMethodException if the constructor does not exist
245 * @throws SecurityException if the security check fails
246 * @see #getDeclaredConstructors()
247 * @since 1.1
249 public native Constructor getDeclaredConstructor(Class[] args)
250 throws NoSuchMethodException;
253 * Get all the declared member classes and interfaces in this class, but
254 * not those inherited from superclasses. This returns an array of length
255 * 0 if there are no member classes, including for primitive types. A
256 * security check may be performed, with
257 * <code>checkMemberAccess(this, Member.DECLARED)</code> as well as
258 * <code>checkPackageAccess</code> both having to succeed.
260 * @return all declared member classes in this class
261 * @throws SecurityException if the security check fails
262 * @since 1.1
264 public Class[] getDeclaredClasses()
266 memberAccessCheck(Member.DECLARED);
267 return getDeclaredClasses(false);
270 native Class[] getDeclaredClasses (boolean publicOnly);
273 * Get all the declared constructors of this class. This returns an array of
274 * length 0 if there are no constructors, including for primitive types,
275 * arrays, and interfaces. It does, however, include the default
276 * constructor if one was supplied by the compiler. A security check may
277 * be performed, with <code>checkMemberAccess(this, Member.DECLARED)</code>
278 * as well as <code>checkPackageAccess</code> both having to succeed.
280 * @return all constructors in this class
281 * @throws SecurityException if the security check fails
282 * @since 1.1
284 public Constructor[] getDeclaredConstructors()
286 memberAccessCheck(Member.DECLARED);
287 return getDeclaredConstructors(false);
290 native Constructor[] getDeclaredConstructors (boolean publicOnly);
293 * Get a field declared in this class, where name is its simple name. The
294 * implicit length field of arrays is not available. A security check may
295 * be performed, with <code>checkMemberAccess(this, Member.DECLARED)</code>
296 * as well as <code>checkPackageAccess</code> both having to succeed.
298 * @param name the name of the field
299 * @return the field
300 * @throws NoSuchFieldException if the field does not exist
301 * @throws SecurityException if the security check fails
302 * @see #getDeclaredFields()
303 * @since 1.1
305 public native Field getDeclaredField(String fieldName)
306 throws NoSuchFieldException;
309 * Get all the declared fields in this class, but not those inherited from
310 * superclasses. This returns an array of length 0 if there are no fields,
311 * including for primitive types. This does not return the implicit length
312 * field of arrays. A security check may be performed, with
313 * <code>checkMemberAccess(this, Member.DECLARED)</code> as well as
314 * <code>checkPackageAccess</code> both having to succeed.
316 * @return all declared fields in this class
317 * @throws SecurityException if the security check fails
318 * @since 1.1
320 public Field[] getDeclaredFields()
322 memberAccessCheck(Member.DECLARED);
323 return getDeclaredFields(false);
326 native Field[] getDeclaredFields (boolean publicOnly);
328 private native Method _getDeclaredMethod(String methodName, Class[] args);
331 * Get a method declared in this class, where name is its simple name. The
332 * implicit methods of Object are not available from arrays or interfaces.
333 * Constructors (named "&lt;init&gt;" in the class file) and class initializers
334 * (name "&lt;clinit&gt;") are not available. The Virtual Machine allows
335 * multiple methods with the same signature but differing return types; in
336 * such a case the most specific return types are favored, then the final
337 * choice is arbitrary. If the method takes no argument, an array of zero
338 * elements and null are equivalent for the types argument. A security
339 * check may be performed, with
340 * <code>checkMemberAccess(this, Member.DECLARED)</code> as well as
341 * <code>checkPackageAccess</code> both having to succeed.
343 * @param methodName the name of the method
344 * @param types the type of each parameter
345 * @return the method
346 * @throws NoSuchMethodException if the method does not exist
347 * @throws SecurityException if the security check fails
348 * @see #getDeclaredMethods()
349 * @since 1.1
351 public Method getDeclaredMethod(String methodName, Class[] args)
352 throws NoSuchMethodException
354 memberAccessCheck(Member.DECLARED);
356 if ("<init>".equals(methodName) || "<clinit>".equals(methodName))
357 throw new NoSuchMethodException(methodName);
359 Method match = _getDeclaredMethod(methodName, args);
360 if (match == null)
361 throw new NoSuchMethodException(methodName);
362 return match;
366 * Get all the declared methods in this class, but not those inherited from
367 * superclasses. This returns an array of length 0 if there are no methods,
368 * including for primitive types. This does include the implicit methods of
369 * arrays and interfaces which mirror methods of Object, nor does it
370 * include constructors or the class initialization methods. The Virtual
371 * Machine allows multiple methods with the same signature but differing
372 * return types; all such methods are in the returned array. A security
373 * check may be performed, with
374 * <code>checkMemberAccess(this, Member.DECLARED)</code> as well as
375 * <code>checkPackageAccess</code> both having to succeed.
377 * @return all declared methods in this class
378 * @throws SecurityException if the security check fails
379 * @since 1.1
381 public native Method[] getDeclaredMethods();
384 * If this is a nested or inner class, return the class that declared it.
385 * If not, return null.
387 * @return the declaring class of this class
388 * @since 1.1
390 // This is marked as unimplemented in the JCL book.
391 public native Class getDeclaringClass ();
393 private native Field getField (String fieldName, int hash)
394 throws NoSuchFieldException;
397 * Get a public field declared or inherited in this class, where name is
398 * its simple name. If the class contains multiple accessible fields by
399 * that name, an arbitrary one is returned. The implicit length field of
400 * arrays is not available. A security check may be performed, with
401 * <code>checkMemberAccess(this, Member.PUBLIC)</code> as well as
402 * <code>checkPackageAccess</code> both having to succeed.
404 * @param fieldName the name of the field
405 * @return the field
406 * @throws NoSuchFieldException if the field does not exist
407 * @throws SecurityException if the security check fails
408 * @see #getFields()
409 * @since 1.1
411 public Field getField(String fieldName)
412 throws NoSuchFieldException
414 memberAccessCheck(Member.PUBLIC);
415 Field field = getField(fieldName, fieldName.hashCode());
416 if (field == null)
417 throw new NoSuchFieldException(fieldName);
418 return field;
422 * Get all the public fields declared in this class or inherited from
423 * superclasses. This returns an array of length 0 if there are no fields,
424 * including for primitive types. This does not return the implicit length
425 * field of arrays. A security check may be performed, with
426 * <code>checkMemberAccess(this, Member.PUBLIC)</code> as well as
427 * <code>checkPackageAccess</code> both having to succeed.
429 * @return all public fields in this class
430 * @throws SecurityException if the security check fails
431 * @since 1.1
433 public Field[] getFields()
435 memberAccessCheck(Member.PUBLIC);
436 return internalGetFields();
440 * Like <code>getFields()</code> but without the security checks.
442 private Field[] internalGetFields()
444 HashSet set = new HashSet();
445 set.addAll(Arrays.asList(getDeclaredFields(true)));
446 Class[] interfaces = getInterfaces();
447 for (int i = 0; i < interfaces.length; i++)
448 set.addAll(Arrays.asList(interfaces[i].internalGetFields()));
449 Class superClass = getSuperclass();
450 if (superClass != null)
451 set.addAll(Arrays.asList(superClass.internalGetFields()));
452 return (Field[])set.toArray(new Field[set.size()]);
456 * Returns the <code>Package</code> in which this class is defined
457 * Returns null when this information is not available from the
458 * classloader of this class.
460 * @return the package for this class, if it is available
461 * @since 1.2
463 public Package getPackage()
465 ClassLoader cl = getClassLoader();
466 if (cl != null)
467 return cl.getPackage(getPackagePortion(getName()));
468 else
469 return VMClassLoader.getPackage(getPackagePortion(getName()));
473 * Get the interfaces this class <em>directly</em> implements, in the
474 * order that they were declared. This returns an empty array, not null,
475 * for Object, primitives, void, and classes or interfaces with no direct
476 * superinterface. Array types return Cloneable and Serializable.
478 * @return the interfaces this class directly implements
480 public native Class[] getInterfaces ();
482 private final native void getSignature(StringBuffer buffer);
483 private static final native String getSignature(Class[] args,
484 boolean is_construtor);
486 public native Method _getMethod(String methodName, Class[] args);
489 * Get a public method declared or inherited in this class, where name is
490 * its simple name. The implicit methods of Object are not available from
491 * interfaces. Constructors (named "&lt;init&gt;" in the class file) and class
492 * initializers (name "&lt;clinit&gt;") are not available. The Virtual
493 * Machine allows multiple methods with the same signature but differing
494 * return types, and the class can inherit multiple methods of the same
495 * return type; in such a case the most specific return types are favored,
496 * then the final choice is arbitrary. If the method takes no argument, an
497 * array of zero elements and null are equivalent for the types argument.
498 * A security check may be performed, with
499 * <code>checkMemberAccess(this, Member.PUBLIC)</code> as well as
500 * <code>checkPackageAccess</code> both having to succeed.
502 * @param methodName the name of the method
503 * @param types the type of each parameter
504 * @return the method
505 * @throws NoSuchMethodException if the method does not exist
506 * @throws SecurityException if the security check fails
507 * @see #getMethods()
508 * @since 1.1
510 public Method getMethod(String methodName, Class[] args)
511 throws NoSuchMethodException
513 memberAccessCheck(Member.PUBLIC);
515 if ("<init>".equals(methodName) || "<clinit>".equals(methodName))
516 throw new NoSuchMethodException(methodName);
518 Method method = _getMethod(methodName, args);
519 if (method == null)
520 throw new NoSuchMethodException(methodName);
521 return method;
524 private native int _getMethods (Method[] result, int offset);
527 * Get all the public methods declared in this class or inherited from
528 * superclasses. This returns an array of length 0 if there are no methods,
529 * including for primitive types. This does not include the implicit
530 * methods of interfaces which mirror methods of Object, nor does it
531 * include constructors or the class initialization methods. The Virtual
532 * Machine allows multiple methods with the same signature but differing
533 * return types; all such methods are in the returned array. A security
534 * check may be performed, with
535 * <code>checkMemberAccess(this, Member.PUBLIC)</code> as well as
536 * <code>checkPackageAccess</code> both having to succeed.
538 * @return all public methods in this class
539 * @throws SecurityException if the security check fails
540 * @since 1.1
542 public native Method[] getMethods();
545 * Get the modifiers of this class. These can be decoded using Modifier,
546 * and is limited to one of public, protected, or private, and any of
547 * final, static, abstract, or interface. An array class has the same
548 * public, protected, or private modifier as its component type, and is
549 * marked final but not an interface. Primitive types and void are marked
550 * public and final, but not an interface.
552 * @return the modifiers of this class
553 * @see Modifer
554 * @since 1.1
556 public native int getModifiers ();
559 * Get the name of this class, separated by dots for package separators.
560 * If the class represents a primitive type, or void, then the
561 * name of the type as it appears in the Java programming language
562 * is returned. For instance, <code>Byte.TYPE.getName()</code>
563 * returns "byte".
565 * Arrays are specially encoded as shown on this table.
566 * <pre>
567 * array type [<em>element type</em>
568 * (note that the element type is encoded per
569 * this table)
570 * boolean Z
571 * byte B
572 * char C
573 * short S
574 * int I
575 * long J
576 * float F
577 * double D
578 * void V
579 * class or interface, alone: &lt;dotted name&gt;
580 * class or interface, as element type: L&lt;dotted name&gt;;
581 * </pre>
583 * @return the name of this class
585 public native String getName ();
588 * Get a resource URL using this class's package using the
589 * getClassLoader().getResource() method. If this class was loaded using
590 * the system classloader, ClassLoader.getSystemResource() is used instead.
592 * <p>If the name you supply is absolute (it starts with a <code>/</code>),
593 * then the leading <code>/</code> is removed and it is passed on to
594 * getResource(). If it is relative, the package name is prepended, and
595 * <code>.</code>'s are replaced with <code>/</code>.
597 * <p>The URL returned is system- and classloader-dependent, and could
598 * change across implementations.
600 * @param resourceName the name of the resource, generally a path
601 * @return the URL to the resource
602 * @throws NullPointerException if name is null
603 * @since 1.1
605 public URL getResource(String resourceName)
607 String name = resourcePath(resourceName);
608 ClassLoader loader = getClassLoader();
609 if (loader == null)
610 return ClassLoader.getSystemResource(name);
611 return loader.getResource(name);
615 * Get a resource using this class's package using the
616 * getClassLoader().getResourceAsStream() method. If this class was loaded
617 * using the system classloader, ClassLoader.getSystemResource() is used
618 * instead.
620 * <p>If the name you supply is absolute (it starts with a <code>/</code>),
621 * then the leading <code>/</code> is removed and it is passed on to
622 * getResource(). If it is relative, the package name is prepended, and
623 * <code>.</code>'s are replaced with <code>/</code>.
625 * <p>The URL returned is system- and classloader-dependent, and could
626 * change across implementations.
628 * @param resourceName the name of the resource, generally a path
629 * @return an InputStream with the contents of the resource in it, or null
630 * @throws NullPointerException if name is null
631 * @since 1.1
633 public InputStream getResourceAsStream(String resourceName)
635 String name = resourcePath(resourceName);
636 ClassLoader loader = getClassLoader();
637 if (loader == null)
638 return ClassLoader.getSystemResourceAsStream(name);
639 return loader.getResourceAsStream(name);
642 private String resourcePath(String resourceName)
644 if (resourceName.length() > 0)
646 if (resourceName.charAt(0) != '/')
648 String pkg = getPackagePortion(getName());
649 if (pkg.length() > 0)
650 resourceName = pkg.replace('.','/') + '/' + resourceName;
652 else
654 resourceName = resourceName.substring(1);
657 return resourceName;
661 * Get the signers of this class. This returns null if there are no signers,
662 * such as for primitive types or void.
664 * @return the signers of this class
665 * @since 1.1
667 public native Object[] getSigners ();
670 * Set the signers of this class.
672 * @param signers the signers of this class
674 native void setSigners(Object[] signers);
677 * Get the direct superclass of this class. If this is an interface,
678 * Object, a primitive type, or void, it will return null. If this is an
679 * array type, it will return Object.
681 * @return the direct superclass of this class
683 public native Class getSuperclass ();
686 * Return whether this class is an array type.
688 * @return whether this class is an array type
689 * @since 1.1
691 public native boolean isArray ();
694 * Discover whether an instance of the Class parameter would be an
695 * instance of this Class as well. Think of doing
696 * <code>isInstance(c.newInstance())</code> or even
697 * <code>c.newInstance() instanceof (this class)</code>. While this
698 * checks widening conversions for objects, it must be exact for primitive
699 * types.
701 * @param c the class to check
702 * @return whether an instance of c would be an instance of this class
703 * as well
704 * @throws NullPointerException if c is null
705 * @since 1.1
707 public native boolean isAssignableFrom (Class c);
710 * Discover whether an Object is an instance of this Class. Think of it
711 * as almost like <code>o instanceof (this class)</code>.
713 * @param o the Object to check
714 * @return whether o is an instance of this class
715 * @since 1.1
717 public native boolean isInstance (Object o);
720 * Check whether this class is an interface or not. Array types are not
721 * interfaces.
723 * @return whether this class is an interface or not
725 public native boolean isInterface ();
728 * Return whether this class is a primitive type. A primitive type class
729 * is a class representing a kind of "placeholder" for the various
730 * primitive types, or void. You can access the various primitive type
731 * classes through java.lang.Boolean.TYPE, java.lang.Integer.TYPE, etc.,
732 * or through boolean.class, int.class, etc.
734 * @return whether this class is a primitive type
735 * @see Boolean#TYPE
736 * @see Byte#TYPE
737 * @see Character#TYPE
738 * @see Short#TYPE
739 * @see Integer#TYPE
740 * @see Long#TYPE
741 * @see Float#TYPE
742 * @see Double#TYPE
743 * @see Void#TYPE
744 * @since 1.1
746 public native boolean isPrimitive ();
749 * Get a new instance of this class by calling the no-argument constructor.
750 * The class is initialized if it has not been already. A security check
751 * may be performed, with <code>checkMemberAccess(this, Member.PUBLIC)</code>
752 * as well as <code>checkPackageAccess</code> both having to succeed.
754 * @return a new instance of this class
755 * @throws InstantiationException if there is not a no-arg constructor
756 * for this class, including interfaces, abstract classes, arrays,
757 * primitive types, and void; or if an exception occurred during
758 * the constructor
759 * @throws IllegalAccessException if you are not allowed to access the
760 * no-arg constructor because of scoping reasons
761 * @throws SecurityException if the security check fails
762 * @throws ExceptionInInitializerError if class initialization caused by
763 * this call fails with an exception
765 public native Object newInstance ()
766 throws InstantiationException, IllegalAccessException;
768 // We need a native method to retrieve the protection domain, because we
769 // can't add fields to java.lang.Class that are accessible from Java.
770 private native ProtectionDomain getProtectionDomain0();
773 * Returns the protection domain of this class. If the classloader did not
774 * record the protection domain when creating this class the unknown
775 * protection domain is returned which has a <code>null</code> code source
776 * and all permissions. A security check may be performed, with
777 * <code>RuntimePermission("getProtectionDomain")</code>.
779 * @return the protection domain
780 * @throws SecurityException if the security manager exists and the caller
781 * does not have <code>RuntimePermission("getProtectionDomain")</code>.
782 * @see RuntimePermission
783 * @since 1.2
785 public ProtectionDomain getProtectionDomain()
787 SecurityManager sm = System.getSecurityManager();
788 if (sm != null)
789 sm.checkPermission(VMClassLoader.protectionDomainPermission);
791 ProtectionDomain protectionDomain = getProtectionDomain0();
793 if (protectionDomain == null)
794 return VMClassLoader.unknownProtectionDomain;
795 else
796 return protectionDomain;
800 * Return the human-readable form of this Object. For an object, this
801 * is either "interface " or "class " followed by <code>getName()</code>,
802 * for primitive types and void it is just <code>getName()</code>.
804 * @return the human-readable form of this Object
806 public String toString()
808 if (isPrimitive())
809 return getName();
810 return (isInterface() ? "interface " : "class ") + getName();
814 * Returns the desired assertion status of this class, if it were to be
815 * initialized at this moment. The class assertion status, if set, is
816 * returned; the backup is the default package status; then if there is
817 * a class loader, that default is returned; and finally the system default
818 * is returned. This method seldom needs calling in user code, but exists
819 * for compilers to implement the assert statement. Note that there is no
820 * guarantee that the result of this method matches the class's actual
821 * assertion status.
823 * @return the desired assertion status
824 * @see ClassLoader#setClassAssertionStatus(String, boolean)
825 * @see ClassLoader#setPackageAssertionStatus(String, boolean)
826 * @see ClassLoader#setDefaultAssertionStatus(boolean)
827 * @since 1.4
829 public boolean desiredAssertionStatus()
831 ClassLoader c = getClassLoader();
832 Object status;
833 if (c == null)
834 return VMClassLoader.defaultAssertionStatus();
835 if (c.classAssertionStatus != null)
836 synchronized (c)
838 status = c.classAssertionStatus.get(getName());
839 if (status != null)
840 return status.equals(Boolean.TRUE);
842 else
844 status = ClassLoader.systemClassAssertionStatus.get(getName());
845 if (status != null)
846 return status.equals(Boolean.TRUE);
848 if (c.packageAssertionStatus != null)
849 synchronized (c)
851 String name = getPackagePortion(getName());
852 if ("".equals(name))
853 status = c.packageAssertionStatus.get(null);
854 else
857 status = c.packageAssertionStatus.get(name);
858 name = getPackagePortion(name);
860 while (! "".equals(name) && status == null);
861 if (status != null)
862 return status.equals(Boolean.TRUE);
864 else
866 String name = getPackagePortion(getName());
867 if ("".equals(name))
868 status = ClassLoader.systemPackageAssertionStatus.get(null);
869 else
872 status = ClassLoader.systemPackageAssertionStatus.get(name);
873 name = getPackagePortion(name);
875 while (! "".equals(name) && status == null);
876 if (status != null)
877 return status.equals(Boolean.TRUE);
879 return c.defaultAssertionStatus;
883 * Strip the last portion of the name (after the last dot).
885 * @param name the name to get package of
886 * @return the package name, or "" if no package
888 private static String getPackagePortion(String name)
890 int lastInd = name.lastIndexOf('.');
891 if (lastInd == -1)
892 return "";
893 return name.substring(0, lastInd);
897 * Perform security checks common to all of the methods that
898 * get members of this Class.
900 private void memberAccessCheck(int which)
902 SecurityManager sm = System.getSecurityManager();
903 if (sm != null)
905 sm.checkMemberAccess(this, which);
906 Package pkg = getPackage();
907 if (pkg != null)
908 sm.checkPackageAccess(pkg.getName());