Remove old autovect-branch by moving to "dead" directory.
[official-gcc.git] / old-autovect-branch / libjava / classpath / java / lang / Class.java
blob726c794a4133d67e9212de3fcbad627379266242
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 gnu.classpath.VMStackWalker;
43 import java.io.InputStream;
44 import java.io.ObjectStreamClass;
45 import java.io.Serializable;
46 import java.lang.reflect.Array;
47 import java.lang.reflect.Constructor;
48 import java.lang.reflect.Field;
49 import java.lang.reflect.InvocationTargetException;
50 import java.lang.reflect.Member;
51 import java.lang.reflect.Method;
52 import java.lang.reflect.Modifier;
53 import java.net.URL;
54 import java.security.AccessController;
55 import java.security.AllPermission;
56 import java.security.Permissions;
57 import java.security.PrivilegedAction;
58 import java.security.ProtectionDomain;
59 import java.util.ArrayList;
60 import java.util.Arrays;
61 import java.util.HashMap;
62 import java.util.HashSet;
65 /**
66 * A Class represents a Java type. There will never be multiple Class
67 * objects with identical names and ClassLoaders. Primitive types, array
68 * types, and void also have a Class object.
70 * <p>Arrays with identical type and number of dimensions share the same class.
71 * The array class ClassLoader is the same as the ClassLoader of the element
72 * type of the array (which can be null to indicate the bootstrap classloader).
73 * The name of an array class is <code>[&lt;signature format&gt;;</code>.
74 * <p> For example,
75 * String[]'s class is <code>[Ljava.lang.String;</code>. boolean, byte,
76 * short, char, int, long, float and double have the "type name" of
77 * Z,B,S,C,I,J,F,D for the purposes of array classes. If it's a
78 * multidimensioned array, the same principle applies:
79 * <code>int[][][]</code> == <code>[[[I</code>.
81 * <p>There is no public constructor - Class objects are obtained only through
82 * the virtual machine, as defined in ClassLoaders.
84 * @serialData Class objects serialize specially:
85 * <code>TC_CLASS ClassDescriptor</code>. For more serialization information,
86 * see {@link ObjectStreamClass}.
88 * @author John Keiser
89 * @author Eric Blake (ebb9@email.byu.edu)
90 * @author Tom Tromey (tromey@cygnus.com)
91 * @since 1.0
92 * @see ClassLoader
94 public final class Class implements Serializable
96 /**
97 * Compatible with JDK 1.0+.
99 private static final long serialVersionUID = 3206093459760846163L;
101 /** The class signers. */
102 private Object[] signers = null;
103 /** The class protection domain. */
104 private final transient ProtectionDomain pd;
106 /* We use an inner class, so that Class doesn't have a static initializer */
107 private static final class StaticData
109 static final ProtectionDomain unknownProtectionDomain;
111 static
113 Permissions permissions = new Permissions();
114 permissions.add(new AllPermission());
115 unknownProtectionDomain = new ProtectionDomain(null, permissions);
119 final transient Object vmdata;
121 /** newInstance() caches the default constructor */
122 private transient Constructor constructor;
125 * Class is non-instantiable from Java code; only the VM can create
126 * instances of this class.
128 Class(Object vmdata)
130 this(vmdata, null);
133 Class(Object vmdata, ProtectionDomain pd)
135 this.vmdata = vmdata;
136 // If the VM didn't supply a protection domain and the class is an array,
137 // we "inherit" the protection domain from the component type class. This
138 // saves the VM from having to worry about protection domains for array
139 // classes.
140 if (pd == null && isArray())
141 this.pd = getComponentType().pd;
142 else
143 this.pd = pd;
147 * Use the classloader of the current class to load, link, and initialize
148 * a class. This is equivalent to your code calling
149 * <code>Class.forName(name, true, getClass().getClassLoader())</code>.
151 * @param name the name of the class to find
152 * @return the Class object representing the class
153 * @throws ClassNotFoundException if the class was not found by the
154 * classloader
155 * @throws LinkageError if linking the class fails
156 * @throws ExceptionInInitializerError if the class loads, but an exception
157 * occurs during initialization
159 public static Class forName(String name) throws ClassNotFoundException
161 return VMClass.forName(name, true, VMStackWalker.getCallingClassLoader());
165 * Use the specified classloader to load and link a class. If the loader
166 * is null, this uses the bootstrap class loader (provide the security
167 * check succeeds). Unfortunately, this method cannot be used to obtain
168 * the Class objects for primitive types or for void, you have to use
169 * the fields in the appropriate java.lang wrapper classes.
171 * <p>Calls <code>classloader.loadclass(name, initialize)</code>.
173 * @param name the name of the class to find
174 * @param initialize whether or not to initialize the class at this time
175 * @param classloader the classloader to use to find the class; null means
176 * to use the bootstrap class loader
178 * @return the class object for the given class
180 * @throws ClassNotFoundException if the class was not found by the
181 * classloader
182 * @throws LinkageError if linking the class fails
183 * @throws ExceptionInInitializerError if the class loads, but an exception
184 * occurs during initialization
185 * @throws SecurityException if the <code>classloader</code> argument
186 * is <code>null</code> and the caller does not have the
187 * <code>RuntimePermission("getClassLoader")</code> permission
188 * @see ClassLoader
189 * @since 1.2
191 public static Class forName(String name, boolean initialize,
192 ClassLoader classloader)
193 throws ClassNotFoundException
195 if (classloader == null)
197 // Check if we may access the bootstrap classloader
198 SecurityManager sm = SecurityManager.current;
199 if (sm != null)
201 // Get the calling classloader
202 ClassLoader cl = VMStackWalker.getCallingClassLoader();
203 if (cl != null)
204 sm.checkPermission(new RuntimePermission("getClassLoader"));
207 return VMClass.forName(name, initialize, classloader);
211 * Get all the public member classes and interfaces declared in this
212 * class or inherited from superclasses. This returns an array of length
213 * 0 if there are no member classes, including for primitive types. A
214 * security check may be performed, with
215 * <code>checkMemberAccess(this, Member.PUBLIC)</code> as well as
216 * <code>checkPackageAccess</code> both having to succeed.
218 * @return all public member classes in this class
219 * @throws SecurityException if the security check fails
220 * @since 1.1
222 public Class[] getClasses()
224 memberAccessCheck(Member.PUBLIC);
225 return internalGetClasses();
229 * Like <code>getClasses()</code> but without the security checks.
231 private Class[] internalGetClasses()
233 ArrayList list = new ArrayList();
234 list.addAll(Arrays.asList(getDeclaredClasses(true)));
235 Class superClass = getSuperclass();
236 if (superClass != null)
237 list.addAll(Arrays.asList(superClass.internalGetClasses()));
238 return (Class[])list.toArray(new Class[list.size()]);
242 * Get the ClassLoader that loaded this class. If the class was loaded
243 * by the bootstrap classloader, this method will return null.
244 * If there is a security manager, and the caller's class loader is not
245 * an ancestor of the requested one, a security check of
246 * <code>RuntimePermission("getClassLoader")</code>
247 * must first succeed. Primitive types and void return null.
249 * @return the ClassLoader that loaded this class
250 * @throws SecurityException if the security check fails
251 * @see ClassLoader
252 * @see RuntimePermission
254 public ClassLoader getClassLoader()
256 if (isPrimitive())
257 return null;
259 ClassLoader loader = VMClass.getClassLoader(this);
260 // Check if we may get the classloader
261 SecurityManager sm = SecurityManager.current;
262 if (sm != null)
264 // Get the calling classloader
265 ClassLoader cl = VMStackWalker.getCallingClassLoader();
266 if (cl != null && !cl.isAncestorOf(loader))
267 sm.checkPermission(new RuntimePermission("getClassLoader"));
269 return loader;
273 * If this is an array, get the Class representing the type of array.
274 * Examples: "[[Ljava.lang.String;" would return "[Ljava.lang.String;", and
275 * calling getComponentType on that would give "java.lang.String". If
276 * this is not an array, returns null.
278 * @return the array type of this class, or null
279 * @see Array
280 * @since 1.1
282 public Class getComponentType()
284 return VMClass.getComponentType (this);
288 * Get a public constructor declared in this class. If the constructor takes
289 * no argument, an array of zero elements and null are equivalent for the
290 * types argument. A security check may be performed, with
291 * <code>checkMemberAccess(this, Member.PUBLIC)</code> as well as
292 * <code>checkPackageAccess</code> both having to succeed.
294 * @param types the type of each parameter
295 * @return the constructor
296 * @throws NoSuchMethodException if the constructor does not exist
297 * @throws SecurityException if the security check fails
298 * @see #getConstructors()
299 * @since 1.1
301 public Constructor getConstructor(Class[] types) throws NoSuchMethodException
303 memberAccessCheck(Member.PUBLIC);
304 Constructor[] constructors = getDeclaredConstructors(true);
305 for (int i = 0; i < constructors.length; i++)
307 Constructor constructor = constructors[i];
308 if (matchParameters(types, constructor.getParameterTypes()))
309 return constructor;
311 throw new NoSuchMethodException();
315 * Get all the public constructors of this class. This returns an array of
316 * length 0 if there are no constructors, including for primitive types,
317 * arrays, and interfaces. It does, however, include the default
318 * constructor if one was supplied by the compiler. A security check may
319 * be performed, with <code>checkMemberAccess(this, Member.PUBLIC)</code>
320 * as well as <code>checkPackageAccess</code> both having to succeed.
322 * @return all public constructors in this class
323 * @throws SecurityException if the security check fails
324 * @since 1.1
326 public Constructor[] getConstructors()
328 memberAccessCheck(Member.PUBLIC);
329 return getDeclaredConstructors(true);
333 * Get a constructor declared in this class. If the constructor takes no
334 * argument, an array of zero elements and null are equivalent for the
335 * types argument. A security check may be performed, with
336 * <code>checkMemberAccess(this, Member.DECLARED)</code> as well as
337 * <code>checkPackageAccess</code> both having to succeed.
339 * @param types the type of each parameter
340 * @return the constructor
341 * @throws NoSuchMethodException if the constructor does not exist
342 * @throws SecurityException if the security check fails
343 * @see #getDeclaredConstructors()
344 * @since 1.1
346 public Constructor getDeclaredConstructor(Class[] types)
347 throws NoSuchMethodException
349 memberAccessCheck(Member.DECLARED);
350 Constructor[] constructors = getDeclaredConstructors(false);
351 for (int i = 0; i < constructors.length; i++)
353 Constructor constructor = constructors[i];
354 if (matchParameters(types, constructor.getParameterTypes()))
355 return constructor;
357 throw new NoSuchMethodException();
361 * Get all the declared member classes and interfaces in this class, but
362 * not those inherited from superclasses. This returns an array of length
363 * 0 if there are no member classes, including for primitive types. A
364 * security check may be performed, with
365 * <code>checkMemberAccess(this, Member.DECLARED)</code> as well as
366 * <code>checkPackageAccess</code> both having to succeed.
368 * @return all declared member classes in this class
369 * @throws SecurityException if the security check fails
370 * @since 1.1
372 public Class[] getDeclaredClasses()
374 memberAccessCheck(Member.DECLARED);
375 return getDeclaredClasses(false);
378 Class[] getDeclaredClasses (boolean publicOnly)
380 return VMClass.getDeclaredClasses (this, publicOnly);
384 * Get all the declared constructors of this class. This returns an array of
385 * length 0 if there are no constructors, including for primitive types,
386 * arrays, and interfaces. It does, however, include the default
387 * constructor if one was supplied by the compiler. A security check may
388 * be performed, with <code>checkMemberAccess(this, Member.DECLARED)</code>
389 * as well as <code>checkPackageAccess</code> both having to succeed.
391 * @return all constructors in this class
392 * @throws SecurityException if the security check fails
393 * @since 1.1
395 public Constructor[] getDeclaredConstructors()
397 memberAccessCheck(Member.DECLARED);
398 return getDeclaredConstructors(false);
401 Constructor[] getDeclaredConstructors (boolean publicOnly)
403 return VMClass.getDeclaredConstructors (this, publicOnly);
407 * Get a field declared in this class, where name is its simple name. The
408 * implicit length field of arrays is not available. A security check may
409 * be performed, with <code>checkMemberAccess(this, Member.DECLARED)</code>
410 * as well as <code>checkPackageAccess</code> both having to succeed.
412 * @param name the name of the field
413 * @return the field
414 * @throws NoSuchFieldException if the field does not exist
415 * @throws SecurityException if the security check fails
416 * @see #getDeclaredFields()
417 * @since 1.1
419 public Field getDeclaredField(String name) throws NoSuchFieldException
421 memberAccessCheck(Member.DECLARED);
422 Field[] fields = getDeclaredFields(false);
423 for (int i = 0; i < fields.length; i++)
425 if (fields[i].getName().equals(name))
426 return fields[i];
428 throw new NoSuchFieldException();
432 * Get all the declared fields in this class, but not those inherited from
433 * superclasses. This returns an array of length 0 if there are no fields,
434 * including for primitive types. This does not return the implicit length
435 * field of arrays. A security check may be performed, with
436 * <code>checkMemberAccess(this, Member.DECLARED)</code> as well as
437 * <code>checkPackageAccess</code> both having to succeed.
439 * @return all declared fields in this class
440 * @throws SecurityException if the security check fails
441 * @since 1.1
443 public Field[] getDeclaredFields()
445 memberAccessCheck(Member.DECLARED);
446 return getDeclaredFields(false);
449 Field[] getDeclaredFields (boolean publicOnly)
451 return VMClass.getDeclaredFields (this, publicOnly);
455 * Get a method declared in this class, where name is its simple name. The
456 * implicit methods of Object are not available from arrays or interfaces.
457 * Constructors (named "&lt;init&gt;" in the class file) and class initializers
458 * (name "&lt;clinit&gt;") are not available. The Virtual Machine allows
459 * multiple methods with the same signature but differing return types; in
460 * such a case the most specific return types are favored, then the final
461 * choice is arbitrary. If the method takes no argument, an array of zero
462 * elements and null are equivalent for the types argument. A security
463 * check may be performed, with
464 * <code>checkMemberAccess(this, Member.DECLARED)</code> as well as
465 * <code>checkPackageAccess</code> both having to succeed.
467 * @param methodName the name of the method
468 * @param types the type of each parameter
469 * @return the method
470 * @throws NoSuchMethodException if the method does not exist
471 * @throws SecurityException if the security check fails
472 * @see #getDeclaredMethods()
473 * @since 1.1
475 public Method getDeclaredMethod(String methodName, Class[] types)
476 throws NoSuchMethodException
478 memberAccessCheck(Member.DECLARED);
479 Method match = matchMethod(getDeclaredMethods(false), methodName, types);
480 if (match == null)
481 throw new NoSuchMethodException(methodName);
482 return match;
486 * Get all the declared methods in this class, but not those inherited from
487 * superclasses. This returns an array of length 0 if there are no methods,
488 * including for primitive types. This does include the implicit methods of
489 * arrays and interfaces which mirror methods of Object, nor does it
490 * include constructors or the class initialization methods. The Virtual
491 * Machine allows multiple methods with the same signature but differing
492 * return types; all such methods are in the returned array. A security
493 * check may be performed, with
494 * <code>checkMemberAccess(this, Member.DECLARED)</code> as well as
495 * <code>checkPackageAccess</code> both having to succeed.
497 * @return all declared methods in this class
498 * @throws SecurityException if the security check fails
499 * @since 1.1
501 public Method[] getDeclaredMethods()
503 memberAccessCheck(Member.DECLARED);
504 return getDeclaredMethods(false);
507 Method[] getDeclaredMethods (boolean publicOnly)
509 return VMClass.getDeclaredMethods (this, publicOnly);
513 * If this is a nested or inner class, return the class that declared it.
514 * If not, return null.
516 * @return the declaring class of this class
517 * @since 1.1
519 public Class getDeclaringClass()
521 return VMClass.getDeclaringClass (this);
525 * Get a public field declared or inherited in this class, where name is
526 * its simple name. If the class contains multiple accessible fields by
527 * that name, an arbitrary one is returned. The implicit length field of
528 * arrays is not available. A security check may be performed, with
529 * <code>checkMemberAccess(this, Member.PUBLIC)</code> as well as
530 * <code>checkPackageAccess</code> both having to succeed.
532 * @param fieldName the name of the field
533 * @return the field
534 * @throws NoSuchFieldException if the field does not exist
535 * @throws SecurityException if the security check fails
536 * @see #getFields()
537 * @since 1.1
539 public Field getField(String fieldName)
540 throws NoSuchFieldException
542 memberAccessCheck(Member.PUBLIC);
543 Field field = internalGetField(fieldName);
544 if (field == null)
545 throw new NoSuchFieldException(fieldName);
546 return field;
550 * Get all the public fields declared in this class or inherited from
551 * superclasses. This returns an array of length 0 if there are no fields,
552 * including for primitive types. This does not return the implicit length
553 * field of arrays. A security check may be performed, with
554 * <code>checkMemberAccess(this, Member.PUBLIC)</code> as well as
555 * <code>checkPackageAccess</code> both having to succeed.
557 * @return all public fields in this class
558 * @throws SecurityException if the security check fails
559 * @since 1.1
561 public Field[] getFields()
563 memberAccessCheck(Member.PUBLIC);
564 return internalGetFields();
568 * Like <code>getFields()</code> but without the security checks.
570 private Field[] internalGetFields()
572 HashSet set = new HashSet();
573 set.addAll(Arrays.asList(getDeclaredFields(true)));
574 Class[] interfaces = getInterfaces();
575 for (int i = 0; i < interfaces.length; i++)
576 set.addAll(Arrays.asList(interfaces[i].internalGetFields()));
577 Class superClass = getSuperclass();
578 if (superClass != null)
579 set.addAll(Arrays.asList(superClass.internalGetFields()));
580 return (Field[])set.toArray(new Field[set.size()]);
584 * Returns the <code>Package</code> in which this class is defined
585 * Returns null when this information is not available from the
586 * classloader of this class or when the classloader of this class
587 * is null.
589 * @return the package for this class, if it is available
590 * @since 1.2
592 public Package getPackage()
594 ClassLoader cl = getClassLoader();
595 if (cl != null)
596 return cl.getPackage(getPackagePortion(getName()));
597 else
598 return VMClassLoader.getPackage(getPackagePortion(getName()));
602 * Get the interfaces this class <em>directly</em> implements, in the
603 * order that they were declared. This returns an empty array, not null,
604 * for Object, primitives, void, and classes or interfaces with no direct
605 * superinterface. Array types return Cloneable and Serializable.
607 * @return the interfaces this class directly implements
609 public Class[] getInterfaces()
611 return VMClass.getInterfaces (this);
614 private static final class MethodKey
616 private String name;
617 private Class[] params;
618 private Class returnType;
619 private int hash;
621 MethodKey(Method m)
623 name = m.getName();
624 params = m.getParameterTypes();
625 returnType = m.getReturnType();
626 hash = name.hashCode() ^ returnType.hashCode();
627 for(int i = 0; i < params.length; i++)
629 hash ^= params[i].hashCode();
633 public boolean equals(Object o)
635 if(o instanceof MethodKey)
637 MethodKey m = (MethodKey)o;
638 if(m.name.equals(name) && m.params.length == params.length && m.returnType == returnType)
640 for(int i = 0; i < params.length; i++)
642 if(m.params[i] != params[i])
644 return false;
647 return true;
650 return false;
653 public int hashCode()
655 return hash;
660 * Get a public method declared or inherited in this class, where name is
661 * its simple name. The implicit methods of Object are not available from
662 * interfaces. Constructors (named "&lt;init&gt;" in the class file) and class
663 * initializers (name "&lt;clinit&gt;") are not available. The Virtual
664 * Machine allows multiple methods with the same signature but differing
665 * return types, and the class can inherit multiple methods of the same
666 * return type; in such a case the most specific return types are favored,
667 * then the final choice is arbitrary. If the method takes no argument, an
668 * array of zero elements and null are equivalent for the types argument.
669 * A security check may be performed, with
670 * <code>checkMemberAccess(this, Member.PUBLIC)</code> as well as
671 * <code>checkPackageAccess</code> both having to succeed.
673 * @param methodName the name of the method
674 * @param types the type of each parameter
675 * @return the method
676 * @throws NoSuchMethodException if the method does not exist
677 * @throws SecurityException if the security check fails
678 * @see #getMethods()
679 * @since 1.1
681 public Method getMethod(String methodName, Class[] types)
682 throws NoSuchMethodException
684 memberAccessCheck(Member.PUBLIC);
685 Method method = internalGetMethod(methodName, types);
686 if (method == null)
687 throw new NoSuchMethodException(methodName);
688 return method;
692 * Like <code>getMethod(String,Class[])</code> but without the security
693 * checks and returns null instead of throwing NoSuchMethodException.
695 private Method internalGetMethod(String methodName, Class[] args)
697 Method match = matchMethod(getDeclaredMethods(true), methodName, args);
698 if (match != null)
699 return match;
700 Class superClass = getSuperclass();
701 if (superClass != null)
703 match = superClass.internalGetMethod(methodName, args);
704 if(match != null)
705 return match;
707 Class[] interfaces = getInterfaces();
708 for (int i = 0; i < interfaces.length; i++)
710 match = interfaces[i].internalGetMethod(methodName, args);
711 if (match != null)
712 return match;
714 return null;
717 /**
718 * Find the best matching method in <code>list</code> according to
719 * the definition of ``best matching'' used by <code>getMethod()</code>
721 * <p>
722 * Returns the method if any, otherwise <code>null</code>.
724 * @param list List of methods to search
725 * @param name Name of method
726 * @param args Method parameter types
727 * @see #getMethod(String, Class[])
729 private static Method matchMethod(Method[] list, String name, Class[] args)
731 Method match = null;
732 for (int i = 0; i < list.length; i++)
734 Method method = list[i];
735 if (!method.getName().equals(name))
736 continue;
737 if (!matchParameters(args, method.getParameterTypes()))
738 continue;
739 if (match == null
740 || match.getReturnType().isAssignableFrom(method.getReturnType()))
741 match = method;
743 return match;
747 * Check for an exact match between parameter type lists.
748 * Either list may be <code>null</code> to mean a list of
749 * length zero.
751 private static boolean matchParameters(Class[] types1, Class[] types2)
753 if (types1 == null)
754 return types2 == null || types2.length == 0;
755 if (types2 == null)
756 return types1 == null || types1.length == 0;
757 if (types1.length != types2.length)
758 return false;
759 for (int i = 0; i < types1.length; i++)
761 if (types1[i] != types2[i])
762 return false;
764 return true;
768 * Get all the public methods declared in this class or inherited from
769 * superclasses. This returns an array of length 0 if there are no methods,
770 * including for primitive types. This does not include the implicit
771 * methods of interfaces which mirror methods of Object, nor does it
772 * include constructors or the class initialization methods. The Virtual
773 * Machine allows multiple methods with the same signature but differing
774 * return types; all such methods are in the returned array. A security
775 * check may be performed, with
776 * <code>checkMemberAccess(this, Member.PUBLIC)</code> as well as
777 * <code>checkPackageAccess</code> both having to succeed.
779 * @return all public methods in this class
780 * @throws SecurityException if the security check fails
781 * @since 1.1
783 public Method[] getMethods()
785 memberAccessCheck(Member.PUBLIC);
786 // NOTE the API docs claim that no methods are returned for arrays,
787 // but Sun's implementation *does* return the public methods of Object
788 // (as would be expected), so we follow their implementation instead
789 // of their documentation.
790 return internalGetMethods();
794 * Like <code>getMethods()</code> but without the security checks.
796 private Method[] internalGetMethods()
798 HashMap map = new HashMap();
799 Method[] methods;
800 Class[] interfaces = getInterfaces();
801 for(int i = 0; i < interfaces.length; i++)
803 methods = interfaces[i].internalGetMethods();
804 for(int j = 0; j < methods.length; j++)
806 map.put(new MethodKey(methods[j]), methods[j]);
809 Class superClass = getSuperclass();
810 if(superClass != null)
812 methods = superClass.internalGetMethods();
813 for(int i = 0; i < methods.length; i++)
815 map.put(new MethodKey(methods[i]), methods[i]);
818 methods = getDeclaredMethods(true);
819 for(int i = 0; i < methods.length; i++)
821 map.put(new MethodKey(methods[i]), methods[i]);
823 return (Method[])map.values().toArray(new Method[map.size()]);
827 * Get the modifiers of this class. These can be decoded using Modifier,
828 * and is limited to one of public, protected, or private, and any of
829 * final, static, abstract, or interface. An array class has the same
830 * public, protected, or private modifier as its component type, and is
831 * marked final but not an interface. Primitive types and void are marked
832 * public and final, but not an interface.
834 * @return the modifiers of this class
835 * @see Modifier
836 * @since 1.1
838 public int getModifiers()
840 return VMClass.getModifiers (this, false);
844 * Get the name of this class, separated by dots for package separators.
845 * If the class represents a primitive type, or void, then the
846 * name of the type as it appears in the Java programming language
847 * is returned. For instance, <code>Byte.TYPE.getName()</code>
848 * returns "byte".
850 * Arrays are specially encoded as shown on this table.
851 * <pre>
852 * array type [<em>element type</em>
853 * (note that the element type is encoded per
854 * this table)
855 * boolean Z
856 * byte B
857 * char C
858 * short S
859 * int I
860 * long J
861 * float F
862 * double D
863 * void V
864 * class or interface, alone: &lt;dotted name&gt;
865 * class or interface, as element type: L&lt;dotted name&gt;;
866 * </pre>
868 * @return the name of this class
870 public String getName()
872 return VMClass.getName (this);
876 * Get a resource URL using this class's package using the
877 * getClassLoader().getResource() method. If this class was loaded using
878 * the system classloader, ClassLoader.getSystemResource() is used instead.
880 * <p>If the name you supply is absolute (it starts with a <code>/</code>),
881 * then the leading <code>/</code> is removed and it is passed on to
882 * getResource(). If it is relative, the package name is prepended, and
883 * <code>.</code>'s are replaced with <code>/</code>.
885 * <p>The URL returned is system- and classloader-dependent, and could
886 * change across implementations.
888 * @param resourceName the name of the resource, generally a path
889 * @return the URL to the resource
890 * @throws NullPointerException if name is null
891 * @since 1.1
893 public URL getResource(String resourceName)
895 String name = resourcePath(resourceName);
896 ClassLoader loader = getClassLoader();
897 if (loader == null)
898 return ClassLoader.getSystemResource(name);
899 return loader.getResource(name);
903 * Get a resource using this class's package using the
904 * getClassLoader().getResourceAsStream() method. If this class was loaded
905 * using the system classloader, ClassLoader.getSystemResource() is used
906 * instead.
908 * <p>If the name you supply is absolute (it starts with a <code>/</code>),
909 * then the leading <code>/</code> is removed and it is passed on to
910 * getResource(). If it is relative, the package name is prepended, and
911 * <code>.</code>'s are replaced with <code>/</code>.
913 * <p>The URL returned is system- and classloader-dependent, and could
914 * change across implementations.
916 * @param resourceName the name of the resource, generally a path
917 * @return an InputStream with the contents of the resource in it, or null
918 * @throws NullPointerException if name is null
919 * @since 1.1
921 public InputStream getResourceAsStream(String resourceName)
923 String name = resourcePath(resourceName);
924 ClassLoader loader = getClassLoader();
925 if (loader == null)
926 return ClassLoader.getSystemResourceAsStream(name);
927 return loader.getResourceAsStream(name);
930 private String resourcePath(String resourceName)
932 if (resourceName.length() > 0)
934 if (resourceName.charAt(0) != '/')
936 String pkg = getPackagePortion(getName());
937 if (pkg.length() > 0)
938 resourceName = pkg.replace('.','/') + '/' + resourceName;
940 else
942 resourceName = resourceName.substring(1);
945 return resourceName;
949 * Get the signers of this class. This returns null if there are no signers,
950 * such as for primitive types or void.
952 * @return the signers of this class
953 * @since 1.1
955 public Object[] getSigners()
957 return signers == null ? null : (Object[]) signers.clone ();
961 * Set the signers of this class.
963 * @param signers the signers of this class
965 void setSigners(Object[] signers)
967 this.signers = signers;
971 * Get the direct superclass of this class. If this is an interface,
972 * Object, a primitive type, or void, it will return null. If this is an
973 * array type, it will return Object.
975 * @return the direct superclass of this class
977 public Class getSuperclass()
979 return VMClass.getSuperclass (this);
983 * Return whether this class is an array type.
985 * @return whether this class is an array type
986 * @since 1.1
988 public boolean isArray()
990 return VMClass.isArray (this);
994 * Discover whether an instance of the Class parameter would be an
995 * instance of this Class as well. Think of doing
996 * <code>isInstance(c.newInstance())</code> or even
997 * <code>c.newInstance() instanceof (this class)</code>. While this
998 * checks widening conversions for objects, it must be exact for primitive
999 * types.
1001 * @param c the class to check
1002 * @return whether an instance of c would be an instance of this class
1003 * as well
1004 * @throws NullPointerException if c is null
1005 * @since 1.1
1007 public boolean isAssignableFrom(Class c)
1009 return VMClass.isAssignableFrom (this, c);
1013 * Discover whether an Object is an instance of this Class. Think of it
1014 * as almost like <code>o instanceof (this class)</code>.
1016 * @param o the Object to check
1017 * @return whether o is an instance of this class
1018 * @since 1.1
1020 public boolean isInstance(Object o)
1022 return VMClass.isInstance (this, o);
1026 * Check whether this class is an interface or not. Array types are not
1027 * interfaces.
1029 * @return whether this class is an interface or not
1031 public boolean isInterface()
1033 return VMClass.isInterface (this);
1037 * Return whether this class is a primitive type. A primitive type class
1038 * is a class representing a kind of "placeholder" for the various
1039 * primitive types, or void. You can access the various primitive type
1040 * classes through java.lang.Boolean.TYPE, java.lang.Integer.TYPE, etc.,
1041 * or through boolean.class, int.class, etc.
1043 * @return whether this class is a primitive type
1044 * @see Boolean#TYPE
1045 * @see Byte#TYPE
1046 * @see Character#TYPE
1047 * @see Short#TYPE
1048 * @see Integer#TYPE
1049 * @see Long#TYPE
1050 * @see Float#TYPE
1051 * @see Double#TYPE
1052 * @see Void#TYPE
1053 * @since 1.1
1055 public boolean isPrimitive()
1057 return VMClass.isPrimitive (this);
1061 * Get a new instance of this class by calling the no-argument constructor.
1062 * The class is initialized if it has not been already. A security check
1063 * may be performed, with <code>checkMemberAccess(this, Member.PUBLIC)</code>
1064 * as well as <code>checkPackageAccess</code> both having to succeed.
1066 * @return a new instance of this class
1067 * @throws InstantiationException if there is not a no-arg constructor
1068 * for this class, including interfaces, abstract classes, arrays,
1069 * primitive types, and void; or if an exception occurred during
1070 * the constructor
1071 * @throws IllegalAccessException if you are not allowed to access the
1072 * no-arg constructor because of scoping reasons
1073 * @throws SecurityException if the security check fails
1074 * @throws ExceptionInInitializerError if class initialization caused by
1075 * this call fails with an exception
1077 public Object newInstance()
1078 throws InstantiationException, IllegalAccessException
1080 memberAccessCheck(Member.PUBLIC);
1081 Constructor constructor;
1082 synchronized(this)
1084 constructor = this.constructor;
1086 if (constructor == null)
1088 Constructor[] constructors = getDeclaredConstructors(false);
1089 for (int i = 0; i < constructors.length; i++)
1091 if (constructors[i].getParameterTypes().length == 0)
1093 constructor = constructors[i];
1094 break;
1097 if (constructor == null)
1098 throw new InstantiationException(getName());
1099 if (!Modifier.isPublic(constructor.getModifiers())
1100 || !Modifier.isPublic(VMClass.getModifiers(this, true)))
1102 final Constructor finalConstructor = constructor;
1103 AccessController.doPrivileged(new PrivilegedAction()
1105 public Object run()
1107 finalConstructor.setAccessible(true);
1108 return null;
1112 synchronized(this)
1114 if (this.constructor == null)
1115 this.constructor = constructor;
1118 int modifiers = constructor.getModifiers();
1119 if (!Modifier.isPublic(modifiers)
1120 || !Modifier.isPublic(VMClass.getModifiers(this, true)))
1122 Class caller = VMStackWalker.getCallingClass();
1123 if (caller != null &&
1124 caller != this &&
1125 (Modifier.isPrivate(modifiers)
1126 || getClassLoader() != caller.getClassLoader()
1127 || !getPackagePortion(getName())
1128 .equals(getPackagePortion(caller.getName()))))
1129 throw new IllegalAccessException(getName()
1130 + " has an inaccessible constructor");
1134 return constructor.newInstance(null);
1136 catch (InvocationTargetException e)
1138 VMClass.throwException(e.getTargetException());
1139 throw (InternalError) new InternalError
1140 ("VMClass.throwException returned").initCause(e);
1145 * Returns the protection domain of this class. If the classloader did not
1146 * record the protection domain when creating this class the unknown
1147 * protection domain is returned which has a <code>null</code> code source
1148 * and all permissions. A security check may be performed, with
1149 * <code>RuntimePermission("getProtectionDomain")</code>.
1151 * @return the protection domain
1152 * @throws SecurityException if the security manager exists and the caller
1153 * does not have <code>RuntimePermission("getProtectionDomain")</code>.
1154 * @see RuntimePermission
1155 * @since 1.2
1157 public ProtectionDomain getProtectionDomain()
1159 SecurityManager sm = SecurityManager.current;
1160 if (sm != null)
1161 sm.checkPermission(new RuntimePermission("getProtectionDomain"));
1163 return pd == null ? StaticData.unknownProtectionDomain : pd;
1167 * Return the human-readable form of this Object. For an object, this
1168 * is either "interface " or "class " followed by <code>getName()</code>,
1169 * for primitive types and void it is just <code>getName()</code>.
1171 * @return the human-readable form of this Object
1173 public String toString()
1175 if (isPrimitive())
1176 return getName();
1177 return (isInterface() ? "interface " : "class ") + getName();
1181 * Returns the desired assertion status of this class, if it were to be
1182 * initialized at this moment. The class assertion status, if set, is
1183 * returned; the backup is the default package status; then if there is
1184 * a class loader, that default is returned; and finally the system default
1185 * is returned. This method seldom needs calling in user code, but exists
1186 * for compilers to implement the assert statement. Note that there is no
1187 * guarantee that the result of this method matches the class's actual
1188 * assertion status.
1190 * @return the desired assertion status
1191 * @see ClassLoader#setClassAssertionStatus(String, boolean)
1192 * @see ClassLoader#setPackageAssertionStatus(String, boolean)
1193 * @see ClassLoader#setDefaultAssertionStatus(boolean)
1194 * @since 1.4
1196 public boolean desiredAssertionStatus()
1198 ClassLoader c = getClassLoader();
1199 Object status;
1200 if (c == null)
1201 return VMClassLoader.defaultAssertionStatus();
1202 if (c.classAssertionStatus != null)
1203 synchronized (c)
1205 status = c.classAssertionStatus.get(getName());
1206 if (status != null)
1207 return status.equals(Boolean.TRUE);
1209 else
1211 status = ClassLoader.StaticData.
1212 systemClassAssertionStatus.get(getName());
1213 if (status != null)
1214 return status.equals(Boolean.TRUE);
1216 if (c.packageAssertionStatus != null)
1217 synchronized (c)
1219 String name = getPackagePortion(getName());
1220 if ("".equals(name))
1221 status = c.packageAssertionStatus.get(null);
1222 else
1225 status = c.packageAssertionStatus.get(name);
1226 name = getPackagePortion(name);
1228 while (! "".equals(name) && status == null);
1229 if (status != null)
1230 return status.equals(Boolean.TRUE);
1232 else
1234 String name = getPackagePortion(getName());
1235 if ("".equals(name))
1236 status = ClassLoader.StaticData.
1237 systemPackageAssertionStatus.get(null);
1238 else
1241 status = ClassLoader.StaticData.
1242 systemPackageAssertionStatus.get(name);
1243 name = getPackagePortion(name);
1245 while (! "".equals(name) && status == null);
1246 if (status != null)
1247 return status.equals(Boolean.TRUE);
1249 return c.defaultAssertionStatus;
1253 * Like <code>getField(String)</code> but without the security checks and returns null
1254 * instead of throwing NoSuchFieldException.
1256 private Field internalGetField(String name)
1258 Field[] fields = getDeclaredFields(true);
1259 for (int i = 0; i < fields.length; i++)
1261 Field field = fields[i];
1262 if (field.getName().equals(name))
1263 return field;
1265 Class[] interfaces = getInterfaces();
1266 for (int i = 0; i < interfaces.length; i++)
1268 Field field = interfaces[i].internalGetField(name);
1269 if(field != null)
1270 return field;
1272 Class superClass = getSuperclass();
1273 if (superClass != null)
1274 return superClass.internalGetField(name);
1275 return null;
1279 * Strip the last portion of the name (after the last dot).
1281 * @param name the name to get package of
1282 * @return the package name, or "" if no package
1284 private static String getPackagePortion(String name)
1286 int lastInd = name.lastIndexOf('.');
1287 if (lastInd == -1)
1288 return "";
1289 return name.substring(0, lastInd);
1293 * Perform security checks common to all of the methods that
1294 * get members of this Class.
1296 private void memberAccessCheck(int which)
1298 SecurityManager sm = SecurityManager.current;
1299 if (sm != null)
1301 sm.checkMemberAccess(this, which);
1302 Package pkg = getPackage();
1303 if (pkg != null)
1304 sm.checkPackageAccess(pkg.getName());