Merge from the pain train
[official-gcc.git] / libjava / java / lang / Class.java
blobbf11861985d7da0460c91017a904812ab694a84e
1 /* Class.java -- Representation of a Java class.
2 Copyright (C) 1998, 1999, 2000, 2002, 2003, 2004
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., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307 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.Arrays;
50 import java.util.HashSet;
52 /**
53 * A Class represents a Java type. There will never be multiple Class
54 * objects with identical names and ClassLoaders. Primitive types, array
55 * types, and void also have a Class object.
57 * <p>Arrays with identical type and number of dimensions share the same
58 * class (and null "system" ClassLoader, incidentally). The name of an
59 * array class is <code>[&lt;signature format&gt;;</code> ... for example,
60 * String[]'s class is <code>[Ljava.lang.String;</code>. boolean, byte,
61 * short, char, int, long, float and double have the "type name" of
62 * Z,B,S,C,I,J,F,D for the purposes of array classes. If it's a
63 * multidimensioned array, the same principle applies:
64 * <code>int[][][]</code> == <code>[[[I</code>.
66 * <p>There is no public constructor - Class objects are obtained only through
67 * the virtual machine, as defined in ClassLoaders.
69 * @serialData Class objects serialize specially:
70 * <code>TC_CLASS ClassDescriptor</code>. For more serialization information,
71 * see {@link ObjectStreamClass}.
73 * @author John Keiser
74 * @author Eric Blake (ebb9@email.byu.edu)
75 * @author Tom Tromey (tromey@cygnus.com)
76 * @since 1.0
77 * @see ClassLoader
79 public final class Class implements Serializable
81 /**
82 * Class is non-instantiable from Java code; only the VM can create
83 * instances of this class.
85 private Class ()
89 // Initialize the class.
90 private native void initializeClass ();
92 // finalization
93 protected native void finalize () throws Throwable;
95 /**
96 * Use the classloader of the current class to load, link, and initialize
97 * a class. This is equivalent to your code calling
98 * <code>Class.forName(name, true, getClass().getClassLoader())</code>.
100 * @param name the name of the class to find
101 * @return the Class object representing the class
102 * @throws ClassNotFoundException if the class was not found by the
103 * classloader
104 * @throws LinkageError if linking the class fails
105 * @throws ExceptionInInitializerError if the class loads, but an exception
106 * occurs during initialization
108 public static native Class forName (String className)
109 throws ClassNotFoundException;
112 * Use the specified classloader to load and link a class. If the loader
113 * is null, this uses the bootstrap class loader (provide the security
114 * check succeeds). Unfortunately, this method cannot be used to obtain
115 * the Class objects for primitive types or for void, you have to use
116 * the fields in the appropriate java.lang wrapper classes.
118 * <p>Calls <code>classloader.loadclass(name, initialize)</code>.
120 * @param name the name of the class to find
121 * @param initialize whether or not to initialize the class at this time
122 * @param classloader the classloader to use to find the class; null means
123 * to use the bootstrap class loader
124 * @throws ClassNotFoundException if the class was not found by the
125 * classloader
126 * @throws LinkageError if linking the class fails
127 * @throws ExceptionInInitializerError if the class loads, but an exception
128 * occurs during initialization
129 * @throws SecurityException if the <code>classloader</code> argument
130 * is <code>null</code> and the caller does not have the
131 * <code>RuntimePermission("getClassLoader")</code> permission
132 * @see ClassLoader
133 * @since 1.2
135 public static native Class forName (String className, boolean initialize,
136 ClassLoader loader)
137 throws ClassNotFoundException;
140 * Get all the public member classes and interfaces declared in this
141 * class or inherited from superclasses. This returns an array of length
142 * 0 if there are no member classes, including for primitive types. A
143 * security check may be performed, with
144 * <code>checkMemberAccess(this, Member.PUBLIC)</code> as well as
145 * <code>checkPackageAccess</code> both having to succeed.
147 * @return all public member classes in this class
148 * @throws SecurityException if the security check fails
149 * @since 1.1
151 public native Class[] getClasses ();
154 * Get the ClassLoader that loaded this class. If it was loaded by the
155 * system classloader, this method will return null. If there is a security
156 * manager, and the caller's class loader does not match the requested
157 * one, a security check of <code>RuntimePermission("getClassLoader")</code>
158 * must first succeed. Primitive types and void return null.
160 * @return the ClassLoader that loaded this class
161 * @throws SecurityException if the security check fails
162 * @see ClassLoader
163 * @see RuntimePermission
165 public native ClassLoader getClassLoader ();
168 * If this is an array, get the Class representing the type of array.
169 * Examples: "[[Ljava.lang.String;" would return "[Ljava.lang.String;", and
170 * calling getComponentType on that would give "java.lang.String". If
171 * this is not an array, returns null.
173 * @return the array type of this class, or null
174 * @see Array
175 * @since 1.1
177 public native Class getComponentType ();
180 * Get a public constructor declared in this class. If the constructor takes
181 * no argument, an array of zero elements and null are equivalent for the
182 * types argument. A security check may be performed, with
183 * <code>checkMemberAccess(this, Member.PUBLIC)</code> as well as
184 * <code>checkPackageAccess</code> both having to succeed.
186 * @param types the type of each parameter
187 * @return the constructor
188 * @throws NoSuchMethodException if the constructor does not exist
189 * @throws SecurityException if the security check fails
190 * @see #getConstructors()
191 * @since 1.1
193 public native Constructor getConstructor(Class[] args)
194 throws NoSuchMethodException;
196 // This is used to implement getConstructors and
197 // getDeclaredConstructors.
198 private native Constructor[] _getConstructors (boolean declared);
201 * Get all the public constructors of this class. This returns an array of
202 * length 0 if there are no constructors, including for primitive types,
203 * arrays, and interfaces. It does, however, include the default
204 * constructor if one was supplied by the compiler. A security check may
205 * be performed, with <code>checkMemberAccess(this, Member.PUBLIC)</code>
206 * as well as <code>checkPackageAccess</code> both having to succeed.
208 * @return all public constructors in this class
209 * @throws SecurityException if the security check fails
210 * @since 1.1
212 public Constructor[] getConstructors()
214 return _getConstructors(false);
218 * Get a constructor declared in this class. If the constructor takes no
219 * argument, an array of zero elements and null are equivalent for the
220 * types argument. A security check may be performed, with
221 * <code>checkMemberAccess(this, Member.DECLARED)</code> as well as
222 * <code>checkPackageAccess</code> both having to succeed.
224 * @param types the type of each parameter
225 * @return the constructor
226 * @throws NoSuchMethodException if the constructor does not exist
227 * @throws SecurityException if the security check fails
228 * @see #getDeclaredConstructors()
229 * @since 1.1
231 public native Constructor getDeclaredConstructor(Class[] args)
232 throws NoSuchMethodException;
235 * Get all the declared member classes and interfaces in this class, but
236 * not those inherited from superclasses. This returns an array of length
237 * 0 if there are no member classes, including for primitive types. A
238 * 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 * @return all declared member classes in this class
243 * @throws SecurityException if the security check fails
244 * @since 1.1
246 public native Class[] getDeclaredClasses();
249 * Get all the declared constructors of this class. This returns an array of
250 * length 0 if there are no constructors, including for primitive types,
251 * arrays, and interfaces. It does, however, include the default
252 * constructor if one was supplied by the compiler. A security check may
253 * be performed, with <code>checkMemberAccess(this, Member.DECLARED)</code>
254 * as well as <code>checkPackageAccess</code> both having to succeed.
256 * @return all constructors in this class
257 * @throws SecurityException if the security check fails
258 * @since 1.1
260 public Constructor[] getDeclaredConstructors()
262 return _getConstructors(true);
266 * Get a field declared in this class, where name is its simple name. The
267 * implicit length field of arrays is not available. A security check may
268 * be performed, with <code>checkMemberAccess(this, Member.DECLARED)</code>
269 * as well as <code>checkPackageAccess</code> both having to succeed.
271 * @param name the name of the field
272 * @return the field
273 * @throws NoSuchFieldException if the field does not exist
274 * @throws SecurityException if the security check fails
275 * @see #getDeclaredFields()
276 * @since 1.1
278 public native Field getDeclaredField(String fieldName)
279 throws NoSuchFieldException;
282 * Get all the declared fields in this class, but not those inherited from
283 * superclasses. This returns an array of length 0 if there are no fields,
284 * including for primitive types. This does not return the implicit length
285 * field of arrays. A security check may be performed, with
286 * <code>checkMemberAccess(this, Member.DECLARED)</code> as well as
287 * <code>checkPackageAccess</code> both having to succeed.
289 * @return all declared fields in this class
290 * @throws SecurityException if the security check fails
291 * @since 1.1
293 public Field[] getDeclaredFields()
295 memberAccessCheck(Member.DECLARED);
296 return getDeclaredFields(false);
299 native Field[] getDeclaredFields (boolean publicOnly);
301 private native Method _getDeclaredMethod(String methodName, Class[] args);
304 * Get a method declared in this class, where name is its simple name. The
305 * implicit methods of Object are not available from arrays or interfaces.
306 * Constructors (named "<init>" in the class file) and class initializers
307 * (name "<clinit>") are not available. The Virtual Machine allows
308 * multiple methods with the same signature but differing return types; in
309 * such a case the most specific return types are favored, then the final
310 * choice is arbitrary. If the method takes no argument, an array of zero
311 * elements and null are equivalent for the types argument. A security
312 * check may be performed, with
313 * <code>checkMemberAccess(this, Member.DECLARED)</code> as well as
314 * <code>checkPackageAccess</code> both having to succeed.
316 * @param methodName the name of the method
317 * @param types the type of each parameter
318 * @return the method
319 * @throws NoSuchMethodException if the method does not exist
320 * @throws SecurityException if the security check fails
321 * @see #getDeclaredMethods()
322 * @since 1.1
324 public Method getDeclaredMethod(String methodName, Class[] args)
325 throws NoSuchMethodException
327 memberAccessCheck(Member.DECLARED);
329 if ("<init>".equals(methodName) || "<clinit>".equals(methodName))
330 throw new NoSuchMethodException(methodName);
332 Method match = _getDeclaredMethod(methodName, args);
333 if (match == null)
334 throw new NoSuchMethodException(methodName);
335 return match;
339 * Get all the declared methods in this class, but not those inherited from
340 * superclasses. This returns an array of length 0 if there are no methods,
341 * including for primitive types. This does include the implicit methods of
342 * arrays and interfaces which mirror methods of Object, nor does it
343 * include constructors or the class initialization methods. The Virtual
344 * Machine allows multiple methods with the same signature but differing
345 * return types; all such methods are in the returned array. A security
346 * check may be performed, with
347 * <code>checkMemberAccess(this, Member.DECLARED)</code> as well as
348 * <code>checkPackageAccess</code> both having to succeed.
350 * @return all declared methods in this class
351 * @throws SecurityException if the security check fails
352 * @since 1.1
354 public native Method[] getDeclaredMethods();
357 * If this is a nested or inner class, return the class that declared it.
358 * If not, return null.
360 * @return the declaring class of this class
361 * @since 1.1
363 // This is marked as unimplemented in the JCL book.
364 public native Class getDeclaringClass ();
366 private native Field getField (String fieldName, int hash)
367 throws NoSuchFieldException;
370 * Get a public field declared or inherited in this class, where name is
371 * its simple name. If the class contains multiple accessible fields by
372 * that name, an arbitrary one is returned. The implicit length field of
373 * arrays is not available. A security check may be performed, with
374 * <code>checkMemberAccess(this, Member.PUBLIC)</code> as well as
375 * <code>checkPackageAccess</code> both having to succeed.
377 * @param fieldName the name of the field
378 * @return the field
379 * @throws NoSuchFieldException if the field does not exist
380 * @throws SecurityException if the security check fails
381 * @see #getFields()
382 * @since 1.1
384 public Field getField(String fieldName)
385 throws NoSuchFieldException
387 memberAccessCheck(Member.PUBLIC);
388 Field field = getField(fieldName, fieldName.hashCode());
389 if (field == null)
390 throw new NoSuchFieldException(fieldName);
391 return field;
395 * Get all the public fields declared in this class or inherited from
396 * superclasses. This returns an array of length 0 if there are no fields,
397 * including for primitive types. This does not return the implicit length
398 * field of arrays. A security check may be performed, with
399 * <code>checkMemberAccess(this, Member.PUBLIC)</code> as well as
400 * <code>checkPackageAccess</code> both having to succeed.
402 * @return all public fields in this class
403 * @throws SecurityException if the security check fails
404 * @since 1.1
406 public Field[] getFields()
408 memberAccessCheck(Member.PUBLIC);
409 return internalGetFields();
413 * Like <code>getFields()</code> but without the security checks.
415 private Field[] internalGetFields()
417 HashSet set = new HashSet();
418 set.addAll(Arrays.asList(getDeclaredFields(true)));
419 Class[] interfaces = getInterfaces();
420 for (int i = 0; i < interfaces.length; i++)
421 set.addAll(Arrays.asList(interfaces[i].internalGetFields()));
422 Class superClass = getSuperclass();
423 if (superClass != null)
424 set.addAll(Arrays.asList(superClass.internalGetFields()));
425 return (Field[])set.toArray(new Field[set.size()]);
429 * Returns the <code>Package</code> in which this class is defined
430 * Returns null when this information is not available from the
431 * classloader of this class or when the classloader of this class
432 * is null.
434 * @return the package for this class, if it is available
435 * @since 1.2
437 public Package getPackage()
439 ClassLoader cl = getClassLoader();
440 if (cl != null)
442 String name = getName();
443 String pkg = "";
444 int idx = name.lastIndexOf('.');
445 if (idx >= 0)
446 pkg = name.substring(0, idx);
447 return cl.getPackage(pkg);
449 return null;
453 * Get the interfaces this class <em>directly</em> implements, in the
454 * order that they were declared. This returns an empty array, not null,
455 * for Object, primitives, void, and classes or interfaces with no direct
456 * superinterface. Array types return Cloneable and Serializable.
458 * @return the interfaces this class directly implements
460 public native Class[] getInterfaces ();
462 private final native void getSignature(StringBuffer buffer);
463 private static final native String getSignature(Class[] args,
464 boolean is_construtor);
466 public native Method _getMethod(String methodName, Class[] args);
469 * Get a public method declared or inherited in this class, where name is
470 * its simple name. The implicit methods of Object are not available from
471 * interfaces. Constructors (named "<init>" in the class file) and class
472 * initializers (name "<clinit>") are not available. The Virtual
473 * Machine allows multiple methods with the same signature but differing
474 * return types, and the class can inherit multiple methods of the same
475 * return type; in such a case the most specific return types are favored,
476 * then the final choice is arbitrary. If the method takes no argument, an
477 * array of zero elements and null are equivalent for the types argument.
478 * A security check may be performed, with
479 * <code>checkMemberAccess(this, Member.PUBLIC)</code> as well as
480 * <code>checkPackageAccess</code> both having to succeed.
482 * @param methodName the name of the method
483 * @param types the type of each parameter
484 * @return the method
485 * @throws NoSuchMethodException if the method does not exist
486 * @throws SecurityException if the security check fails
487 * @see #getMethods()
488 * @since 1.1
490 public Method getMethod(String methodName, Class[] args)
491 throws NoSuchMethodException
493 memberAccessCheck(Member.PUBLIC);
495 if ("<init>".equals(methodName) || "<clinit>".equals(methodName))
496 throw new NoSuchMethodException(methodName);
498 Method method = _getMethod(methodName, args);
499 if (method == null)
500 throw new NoSuchMethodException(methodName);
501 return method;
504 private native int _getMethods (Method[] result, int offset);
507 * Get all the public methods declared in this class or inherited from
508 * superclasses. This returns an array of length 0 if there are no methods,
509 * including for primitive types. This does not include the implicit
510 * methods of interfaces which mirror methods of Object, nor does it
511 * include constructors or the class initialization methods. The Virtual
512 * Machine allows multiple methods with the same signature but differing
513 * return types; all such methods are in the returned array. A security
514 * check may be performed, with
515 * <code>checkMemberAccess(this, Member.PUBLIC)</code> as well as
516 * <code>checkPackageAccess</code> both having to succeed.
518 * @return all public methods in this class
519 * @throws SecurityException if the security check fails
520 * @since 1.1
522 public native Method[] getMethods();
525 * Get the modifiers of this class. These can be decoded using Modifier,
526 * and is limited to one of public, protected, or private, and any of
527 * final, static, abstract, or interface. An array class has the same
528 * public, protected, or private modifier as its component type, and is
529 * marked final but not an interface. Primitive types and void are marked
530 * public and final, but not an interface.
532 * @return the modifiers of this class
533 * @see Modifer
534 * @since 1.1
536 public native int getModifiers ();
539 * Get the name of this class, separated by dots for package separators.
540 * Primitive types and arrays are encoded as:
541 * <pre>
542 * boolean Z
543 * byte B
544 * char C
545 * short S
546 * int I
547 * long J
548 * float F
549 * double D
550 * void V
551 * array type [<em>element type</em>
552 * class or interface, alone: &lt;dotted name&gt;
553 * class or interface, as element type: L&lt;dotted name&gt;;
555 * @return the name of this class
557 public native String getName ();
560 * Get a resource URL using this class's package using the
561 * getClassLoader().getResource() method. If this class was loaded using
562 * the system classloader, ClassLoader.getSystemResource() is used instead.
564 * <p>If the name you supply is absolute (it starts with a <code>/</code>),
565 * then it is passed on to getResource() as is. If it is relative, the
566 * package name is prepended, and <code>.</code>'s are replaced with
567 * <code>/</code>.
569 * <p>The URL returned is system- and classloader-dependent, and could
570 * change across implementations.
572 * @param resourceName the name of the resource, generally a path
573 * @return the URL to the resource
574 * @throws NullPointerException if name is null
575 * @since 1.1
577 public URL getResource(String resourceName)
579 String name = resourcePath(resourceName);
580 ClassLoader loader = getClassLoader();
581 if (loader == null)
582 return ClassLoader.getSystemResource(name);
583 return loader.getResource(name);
587 * Get a resource using this class's package using the
588 * getClassLoader().getResourceAsStream() method. If this class was loaded
589 * using the system classloader, ClassLoader.getSystemResource() is used
590 * instead.
592 * <p>If the name you supply is absolute (it starts with a <code>/</code>),
593 * then it is passed on to getResource() as is. If it is relative, the
594 * package name is prepended, and <code>.</code>'s are replaced with
595 * <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 an InputStream with the contents of the resource in it, or null
602 * @throws NullPointerException if name is null
603 * @since 1.1
605 public InputStream getResourceAsStream(String resourceName)
607 String name = resourcePath(resourceName);
608 ClassLoader loader = getClassLoader();
609 if (loader == null)
610 return ClassLoader.getSystemResourceAsStream(name);
611 return loader.getResourceAsStream(name);
614 private String resourcePath(String resourceName)
616 if (resourceName.startsWith("/"))
617 return resourceName.substring(1);
619 Class c = this;
620 while (c.isArray())
621 c = c.getComponentType();
623 String packageName = c.getName().replace('.', '/');
624 int end = packageName.lastIndexOf('/');
625 if (end != -1)
626 return packageName.substring(0, end + 1) + resourceName;
627 return resourceName;
631 * Get the signers of this class. This returns null if there are no signers,
632 * such as for primitive types or void.
634 * @return the signers of this class
635 * @since 1.1
637 public native Object[] getSigners ();
640 * Set the signers of this class.
642 * @param signers the signers of this class
644 native void setSigners(Object[] signers);
647 * Get the direct superclass of this class. If this is an interface,
648 * Object, a primitive type, or void, it will return null. If this is an
649 * array type, it will return Object.
651 * @return the direct superclass of this class
653 public native Class getSuperclass ();
656 * Return whether this class is an array type.
658 * @return whether this class is an array type
659 * @since 1.1
661 public native boolean isArray ();
664 * Discover whether an instance of the Class parameter would be an
665 * instance of this Class as well. Think of doing
666 * <code>isInstance(c.newInstance())</code> or even
667 * <code>c.newInstance() instanceof (this class)</code>. While this
668 * checks widening conversions for objects, it must be exact for primitive
669 * types.
671 * @param c the class to check
672 * @return whether an instance of c would be an instance of this class
673 * as well
674 * @throws NullPointerException if c is null
675 * @since 1.1
677 public native boolean isAssignableFrom (Class c);
680 * Discover whether an Object is an instance of this Class. Think of it
681 * as almost like <code>o instanceof (this class)</code>.
683 * @param o the Object to check
684 * @return whether o is an instance of this class
685 * @since 1.1
687 public native boolean isInstance (Object o);
690 * Check whether this class is an interface or not. Array types are not
691 * interfaces.
693 * @return whether this class is an interface or not
695 public native boolean isInterface ();
698 * Return whether this class is a primitive type. A primitive type class
699 * is a class representing a kind of "placeholder" for the various
700 * primitive types, or void. You can access the various primitive type
701 * classes through java.lang.Boolean.TYPE, java.lang.Integer.TYPE, etc.,
702 * or through boolean.class, int.class, etc.
704 * @return whether this class is a primitive type
705 * @see Boolean#TYPE
706 * @see Byte#TYPE
707 * @see Character#TYPE
708 * @see Short#TYPE
709 * @see Integer#TYPE
710 * @see Long#TYPE
711 * @see Float#TYPE
712 * @see Double#TYPE
713 * @see Void#TYPE
714 * @since 1.1
716 public native boolean isPrimitive ();
719 * Get a new instance of this class by calling the no-argument constructor.
720 * The class is initialized if it has not been already. A security check
721 * may be performed, with <code>checkMemberAccess(this, Member.PUBLIC)</code>
722 * as well as <code>checkPackageAccess</code> both having to succeed.
724 * @return a new instance of this class
725 * @throws InstantiationException if there is not a no-arg constructor
726 * for this class, including interfaces, abstract classes, arrays,
727 * primitive types, and void; or if an exception occurred during
728 * the constructor
729 * @throws IllegalAccessException if you are not allowed to access the
730 * no-arg constructor because of scoping reasons
731 * @throws SecurityException if the security check fails
732 * @throws ExceptionInInitializerError if class initialization caused by
733 * this call fails with an exception
735 public native Object newInstance ()
736 throws InstantiationException, IllegalAccessException;
738 // We need a native method to retrieve the protection domain, because we
739 // can't add fields to java.lang.Class that are accessible from Java.
740 private native ProtectionDomain getProtectionDomain0();
743 * Returns the protection domain of this class. If the classloader did not
744 * record the protection domain when creating this class the unknown
745 * protection domain is returned which has a <code>null</code> code source
746 * and all permissions.
748 * @return the protection domain
749 * @throws SecurityException if the security manager exists and the caller
750 * does not have <code>RuntimePermission("getProtectionDomain")</code>.
751 * @see RuntimePermission
752 * @since 1.2
754 public ProtectionDomain getProtectionDomain()
756 SecurityManager sm = System.getSecurityManager();
757 if (sm != null)
758 sm.checkPermission(VMClassLoader.protectionDomainPermission);
760 ProtectionDomain protectionDomain = getProtectionDomain0();
762 if (protectionDomain == null)
763 return VMClassLoader.unknownProtectionDomain;
764 else
765 return protectionDomain;
769 * Return the human-readable form of this Object. For an object, this
770 * is either "interface " or "class " followed by <code>getName()</code>,
771 * for primitive types and void it is just <code>getName()</code>.
773 * @return the human-readable form of this Object
775 public String toString()
777 if (isPrimitive())
778 return getName();
779 return (isInterface() ? "interface " : "class ") + getName();
783 * Returns the desired assertion status of this class, if it were to be
784 * initialized at this moment. The class assertion status, if set, is
785 * returned; the backup is the default package status; then if there is
786 * a class loader, that default is returned; and finally the system default
787 * is returned. This method seldom needs calling in user code, but exists
788 * for compilers to implement the assert statement. Note that there is no
789 * guarantee that the result of this method matches the class's actual
790 * assertion status.
792 * @return the desired assertion status
793 * @see ClassLoader#setClassAssertionStatus(String, boolean)
794 * @see ClassLoader#setPackageAssertionStatus(String, boolean)
795 * @see ClassLoader#setDefaultAssertionStatus(boolean)
796 * @since 1.4
798 public boolean desiredAssertionStatus()
800 ClassLoader c = getClassLoader();
801 Object status;
802 if (c == null)
803 return VMClassLoader.defaultAssertionStatus();
804 if (c.classAssertionStatus != null)
805 synchronized (c)
807 status = c.classAssertionStatus.get(getName());
808 if (status != null)
809 return status.equals(Boolean.TRUE);
811 else
813 status = ClassLoader.systemClassAssertionStatus.get(getName());
814 if (status != null)
815 return status.equals(Boolean.TRUE);
817 if (c.packageAssertionStatus != null)
818 synchronized (c)
820 String name = getPackagePortion(getName());
821 if ("".equals(name))
822 status = c.packageAssertionStatus.get(null);
823 else
826 status = c.packageAssertionStatus.get(name);
827 name = getPackagePortion(name);
829 while (! "".equals(name) && status == null);
830 if (status != null)
831 return status.equals(Boolean.TRUE);
833 else
835 String name = getPackagePortion(getName());
836 if ("".equals(name))
837 status = ClassLoader.systemPackageAssertionStatus.get(null);
838 else
841 status = ClassLoader.systemPackageAssertionStatus.get(name);
842 name = getPackagePortion(name);
844 while (! "".equals(name) && status == null);
845 if (status != null)
846 return status.equals(Boolean.TRUE);
848 return c.defaultAssertionStatus;
852 * Strip the last portion of the name (after the last dot).
854 * @param name the name to get package of
855 * @return the package name, or "" if no package
857 private static String getPackagePortion(String name)
859 int lastInd = name.lastIndexOf('.');
860 if (lastInd == -1)
861 return "";
862 return name.substring(0, lastInd);
866 * Perform security checks common to all of the methods that
867 * get members of this Class.
869 private void memberAccessCheck(int which)
871 SecurityManager sm = System.getSecurityManager();
872 if (sm != null)
874 sm.checkMemberAccess(this, which);
875 Package pkg = getPackage();
876 if (pkg != null)
877 sm.checkPackageAccess(pkg.getName());