Source Home >> Java Source 1.6.0 >> java.lang.Class V 0.09
  • 0001/*
  • 0002 * @(#)Class.java 1.201 06/08/07
  • 0003 *
  • 0004 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
  • 0005 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  • 0006 */
  • 0007
  • 0008package java.lang;
  • 0009
  • 0010import java.lang.reflect.Array;
  • 0011import java.lang.reflect.GenericArrayType;
  • 0012import java.lang.reflect.Member;
  • 0013import java.lang.reflect.Field;
  • 0014import java.lang.reflect.Method;
  • 0015import java.lang.reflect.Constructor;
  • 0016import java.lang.reflect.GenericDeclaration;
  • 0017import java.lang.reflect.Modifier;
  • 0018import java.lang.reflect.Type;
  • 0019import java.lang.reflect.TypeVariable;
  • 0020import java.lang.reflect.InvocationTargetException;
  • 0021import java.lang.ref.SoftReference;
  • 0022import java.io.InputStream;
  • 0023import java.io.ObjectStreamField;
  • 0024import java.security.AccessController;
  • 0025import java.security.PrivilegedAction;
  • 0026import java.util.ArrayList;
  • 0027import java.util.Arrays;
  • 0028import java.util.Collection;
  • 0029import java.util.HashSet;
  • 0030import java.util.Iterator;
  • 0031import java.util.List;
  • 0032import java.util.LinkedList;
  • 0033import java.util.LinkedHashSet;
  • 0034import java.util.Set;
  • 0035import java.util.Map;
  • 0036import java.util.HashMap;
  • 0037import sun.misc.Unsafe;
  • 0038import sun.reflect.ConstantPool;
  • 0039import sun.reflect.Reflection;
  • 0040import sun.reflect.ReflectionFactory;
  • 0041import sun.reflect.SignatureIterator;
  • 0042import sun.reflect.generics.factory.CoreReflectionFactory;
  • 0043import sun.reflect.generics.factory.GenericsFactory;
  • 0044import sun.reflect.generics.repository.ClassRepository;
  • 0045import sun.reflect.generics.repository.MethodRepository;
  • 0046import sun.reflect.generics.repository.ConstructorRepository;
  • 0047import sun.reflect.generics.scope.ClassScope;
  • 0048import sun.security.util.SecurityConstants;
  • 0049import java.lang.annotation.Annotation;
  • 0050import sun.reflect.annotation.*;
  • 0051
  • 0052/**
  • 0053 * Instances of the class <code>Class</code> represent classes and
  • 0054 * interfaces in a running Java application. An enum is a kind of
  • 0055 * class and an annotation is a kind of interface. Every array also
  • 0056 * belongs to a class that is reflected as a <code>Class</code> object
  • 0057 * that is shared by all arrays with the same element type and number
  • 0058 * of dimensions. The primitive Java types (<code>boolean</code>,
  • 0059 * <code>byte</code>, <code>char</code>, <code>short</code>,
  • 0060 * <code>int</code>, <code>long</code>, <code>float</code>, and
  • 0061 * <code>double</code>), and the keyword <code>void</code> are also
  • 0062 * represented as <code>Class</code> objects.
  • 0063 *
  • 0064 * <p> <code>Class</code> has no public constructor. Instead <code>Class</code>
  • 0065 * objects are constructed automatically by the Java Virtual Machine as classes
  • 0066 * are loaded and by calls to the <code>defineClass</code> method in the class
  • 0067 * loader.
  • 0068 *
  • 0069 * <p> The following example uses a <code>Class</code> object to print the
  • 0070 * class name of an object:
  • 0071 *
  • 0072 * <p> <blockquote><pre>
  • 0073 * void printClassName(Object obj) {
  • 0074 * System.out.println("The class of " + obj +
  • 0075 * " is " + obj.getClass().getName());
  • 0076 * }
  • 0077 * </pre></blockquote>
  • 0078 *
  • 0079 * <p> It is also possible to get the <code>Class</code> object for a named
  • 0080 * type (or for void) using a class literal
  • 0081 * (JLS Section <A HREF="http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#251530">15.8.2</A>).
  • 0082 * For example:
  • 0083 *
  • 0084 * <p> <blockquote><pre>
  • 0085 * System.out.println("The name of class Foo is: "+Foo.class.getName());
  • 0086 * </pre></blockquote>
  • 0087 *
  • 0088 * @param <T> the type of the class modeled by this {@code Class}
  • 0089 * object. For example, the type of {@code String.class} is {@code
  • 0090 * Class<String>}. Use {@code Class<?>} if the class being modeled is
  • 0091 * unknown.
  • 0092 *
  • 0093 * @author unascribed
  • 0094 * @version 1.201, 08/07/06
  • 0095 * @see java.lang.ClassLoader#defineClass(byte[], int, int)
  • 0096 * @since JDK1.0
  • 0097 */
  • 0098public final
  • 0099 class Class<T> implements java.io.Serializable,
  • 0100 java.lang.reflect.GenericDeclaration,
  • 0101 java.lang.reflect.Type,
  • 0102 java.lang.reflect.AnnotatedElement {
  • 0103 private static final int ANNOTATION= 0x00002000;
  • 0104 private static final int ENUM = 0x00004000;
  • 0105 private static final int SYNTHETIC = 0x00001000;
  • 0106
  • 0107 private static native void registerNatives();
  • 0108 static {
  • 0109 registerNatives();
  • 0110 }
  • 0111
  • 0112 /*
  • 0113 * Constructor. Only the Java Virtual Machine creates Class
  • 0114 * objects.
  • 0115 */
  • 0116 private Class() {}
  • 0117
  • 0118
  • 0119 /**
  • 0120 * Converts the object to a string. The string representation is the
  • 0121 * string "class" or "interface", followed by a space, and then by the
  • 0122 * fully qualified name of the class in the format returned by
  • 0123 * <code>getName</code>. If this <code>Class</code> object represents a
  • 0124 * primitive type, this method returns the name of the primitive type. If
  • 0125 * this <code>Class</code> object represents void this method returns
  • 0126 * "void".
  • 0127 *
  • 0128 * @return a string representation of this class object.
  • 0129 */
  • 0130 public String toString() {
  • 0131 return (isInterface() ? "interface " : (isPrimitive() ? "" : "class "))
  • 0132 + getName();
  • 0133 }
  • 0134
  • 0135
  • 0136 /**
  • 0137 * Returns the <code>Class</code> object associated with the class or
  • 0138 * interface with the given string name. Invoking this method is
  • 0139 * equivalent to:
  • 0140 *
  • 0141 * <blockquote><pre>
  • 0142 * Class.forName(className, true, currentLoader)
  • 0143 * </pre></blockquote>
  • 0144 *
  • 0145 * where <code>currentLoader</code> denotes the defining class loader of
  • 0146 * the current class.
  • 0147 *
  • 0148 * <p> For example, the following code fragment returns the
  • 0149 * runtime <code>Class</code> descriptor for the class named
  • 0150 * <code>java.lang.Thread</code>:
  • 0151 *
  • 0152 * <blockquote><pre>
  • 0153 * Class t = Class.forName("java.lang.Thread")
  • 0154 * </pre></blockquote>
  • 0155 * <p>
  • 0156 * A call to <tt>forName("X")</tt> causes the class named
  • 0157 * <tt>X</tt> to be initialized.
  • 0158 *
  • 0159 * @param className the fully qualified name of the desired class.
  • 0160 * @return the <code>Class</code> object for the class with the
  • 0161 * specified name.
  • 0162 * @exception LinkageError if the linkage fails
  • 0163 * @exception ExceptionInInitializerError if the initialization provoked
  • 0164 * by this method fails
  • 0165 * @exception ClassNotFoundException if the class cannot be located
  • 0166 */
  • 0167 public static Class<?> forName(String className)
  • 0168 throws ClassNotFoundException {
  • 0169 return forName0(className, true, ClassLoader.getCallerClassLoader());
  • 0170 }
  • 0171
  • 0172
  • 0173 /**
  • 0174 * Returns the <code>Class</code> object associated with the class or
  • 0175 * interface with the given string name, using the given class loader.
  • 0176 * Given the fully qualified name for a class or interface (in the same
  • 0177 * format returned by <code>getName</code>) this method attempts to
  • 0178 * locate, load, and link the class or interface. The specified class
  • 0179 * loader is used to load the class or interface. If the parameter
  • 0180 * <code>loader</code> is null, the class is loaded through the bootstrap
  • 0181 * class loader. The class is initialized only if the
  • 0182 * <code>initialize</code> parameter is <code>true</code> and if it has
  • 0183 * not been initialized earlier.
  • 0184 *
  • 0185 * <p> If <code>name</code> denotes a primitive type or void, an attempt
  • 0186 * will be made to locate a user-defined class in the unnamed package whose
  • 0187 * name is <code>name</code>. Therefore, this method cannot be used to
  • 0188 * obtain any of the <code>Class</code> objects representing primitive
  • 0189 * types or void.
  • 0190 *
  • 0191 * <p> If <code>name</code> denotes an array class, the component type of
  • 0192 * the array class is loaded but not initialized.
  • 0193 *
  • 0194 * <p> For example, in an instance method the expression:
  • 0195 *
  • 0196 * <blockquote><pre>
  • 0197 * Class.forName("Foo")
  • 0198 * </pre></blockquote>
  • 0199 *
  • 0200 * is equivalent to:
  • 0201 *
  • 0202 * <blockquote><pre>
  • 0203 * Class.forName("Foo", true, this.getClass().getClassLoader())
  • 0204 * </pre></blockquote>
  • 0205 *
  • 0206 * Note that this method throws errors related to loading, linking or
  • 0207 * initializing as specified in Sections 12.2, 12.3 and 12.4 of <em>The
  • 0208 * Java Language Specification</em>.
  • 0209 * Note that this method does not check whether the requested class
  • 0210 * is accessible to its caller.
  • 0211 *
  • 0212 * <p> If the <code>loader</code> is <code>null</code>, and a security
  • 0213 * manager is present, and the caller's class loader is not null, then this
  • 0214 * method calls the security manager's <code>checkPermission</code> method
  • 0215 * with a <code>RuntimePermission("getClassLoader")</code> permission to
  • 0216 * ensure it's ok to access the bootstrap class loader.
  • 0217 *
  • 0218 * @param name fully qualified name of the desired class
  • 0219 * @param initialize whether the class must be initialized
  • 0220 * @param loader class loader from which the class must be loaded
  • 0221 * @return class object representing the desired class
  • 0222 *
  • 0223 * @exception LinkageError if the linkage fails
  • 0224 * @exception ExceptionInInitializerError if the initialization provoked
  • 0225 * by this method fails
  • 0226 * @exception ClassNotFoundException if the class cannot be located by
  • 0227 * the specified class loader
  • 0228 *
  • 0229 * @see java.lang.Class#forName(String)
  • 0230 * @see java.lang.ClassLoader
  • 0231 * @since 1.2
  • 0232 */
  • 0233 public static Class<?> forName(String name, boolean initialize,
  • 0234 ClassLoader loader)
  • 0235 throws ClassNotFoundException
  • 0236 {
  • 0237 if (loader == null) {
  • 0238 SecurityManager sm = System.getSecurityManager();
  • 0239 if (sm != null) {
  • 0240 ClassLoader ccl = ClassLoader.getCallerClassLoader();
  • 0241 if (ccl != null) {
  • 0242 sm.checkPermission(
  • 0243 SecurityConstants.GET_CLASSLOADER_PERMISSION);
  • 0244 }
  • 0245 }
  • 0246 }
  • 0247 return forName0(name, initialize, loader);
  • 0248 }
  • 0249
  • 0250 /** Called after security checks have been made. */
  • 0251 private static native Class forName0(String name, boolean initialize,
  • 0252 ClassLoader loader)
  • 0253 throws ClassNotFoundException;
  • 0254
  • 0255 /**
  • 0256 * Creates a new instance of the class represented by this <tt>Class</tt>
  • 0257 * object. The class is instantiated as if by a <code>new</code>
  • 0258 * expression with an empty argument list. The class is initialized if it
  • 0259 * has not already been initialized.
  • 0260 *
  • 0261 * <p>Note that this method propagates any exception thrown by the
  • 0262 * nullary constructor, including a checked exception. Use of
  • 0263 * this method effectively bypasses the compile-time exception
  • 0264 * checking that would otherwise be performed by the compiler.
  • 0265 * The {@link
  • 0266 * java.lang.reflect.Constructor#newInstance(java.lang.Object...)
  • 0267 * Constructor.newInstance} method avoids this problem by wrapping
  • 0268 * any exception thrown by the constructor in a (checked) {@link
  • 0269 * java.lang.reflect.InvocationTargetException}.
  • 0270 *
  • 0271 * @return a newly allocated instance of the class represented by this
  • 0272 * object.
  • 0273 * @exception IllegalAccessException if the class or its nullary
  • 0274 * constructor is not accessible.
  • 0275 * @exception InstantiationException
  • 0276 * if this <code>Class</code> represents an abstract class,
  • 0277 * an interface, an array class, a primitive type, or void;
  • 0278 * or if the class has no nullary constructor;
  • 0279 * or if the instantiation fails for some other reason.
  • 0280 * @exception ExceptionInInitializerError if the initialization
  • 0281 * provoked by this method fails.
  • 0282 * @exception SecurityException
  • 0283 * If a security manager, <i>s</i>, is present and any of the
  • 0284 * following conditions is met:
  • 0285 *
  • 0286 * <ul>
  • 0287 *
  • 0288 * <li> invocation of
  • 0289 * <tt>{@link SecurityManager#checkMemberAccess
  • 0290 * s.checkMemberAccess(this, Member.PUBLIC)}</tt> denies
  • 0291 * creation of new instances of this class
  • 0292 *
  • 0293 * <li> the caller's class loader is not the same as or an
  • 0294 * ancestor of the class loader for the current class and
  • 0295 * invocation of <tt>{@link SecurityManager#checkPackageAccess
  • 0296 * s.checkPackageAccess()}</tt> denies access to the package
  • 0297 * of this class
  • 0298 *
  • 0299 * </ul>
  • 0300 *
  • 0301 */
  • 0302 public T newInstance()
  • 0303 throws InstantiationException, IllegalAccessException
  • 0304 {
  • 0305 if (System.getSecurityManager() != null) {
  • 0306 checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
  • 0307 }
  • 0308 return newInstance0();
  • 0309 }
  • 0310
  • 0311 private T newInstance0()
  • 0312 throws InstantiationException, IllegalAccessException
  • 0313 {
  • 0314 // NOTE: the following code may not be strictly correct under
  • 0315 // the current Java memory model.
  • 0316
  • 0317 // Constructor lookup
  • 0318 if (cachedConstructor == null) {
  • 0319 if (this == Class.class) {
  • 0320 throw new IllegalAccessException(
  • 0321 "Can not call newInstance() on the Class for java.lang.Class"
  • 0322 );
  • 0323 }
  • 0324 try {
  • 0325 Class[] empty = {};
  • 0326 final Constructor<T> c = getConstructor0(empty, Member.DECLARED);
  • 0327 // Disable accessibility checks on the constructor
  • 0328 // since we have to do the security check here anyway
  • 0329 // (the stack depth is wrong for the Constructor's
  • 0330 // security check to work)
  • 0331 java.security.AccessController.doPrivileged
  • 0332 (new java.security.PrivilegedAction() {
  • 0333 public Object run() {
  • 0334 c.setAccessible(true);
  • 0335 return null;
  • 0336 }
  • 0337 });
  • 0338 cachedConstructor = c;
  • 0339 } catch (NoSuchMethodException e) {
  • 0340 throw new InstantiationException(getName());
  • 0341 }
  • 0342 }
  • 0343 Constructor<T> tmpConstructor = cachedConstructor;
  • 0344 // Security check (same as in java.lang.reflect.Constructor)
  • 0345 int modifiers = tmpConstructor.getModifiers();
  • 0346 if (!Reflection.quickCheckMemberAccess(this, modifiers)) {
  • 0347 Class caller = Reflection.getCallerClass(3);
  • 0348 if (newInstanceCallerCache != caller) {
  • 0349 Reflection.ensureMemberAccess(caller, this, null, modifiers);
  • 0350 newInstanceCallerCache = caller;
  • 0351 }
  • 0352 }
  • 0353 // Run constructor
  • 0354 try {
  • 0355 return tmpConstructor.newInstance((Object[])null);
  • 0356 } catch (InvocationTargetException e) {
  • 0357 Unsafe.getUnsafe().throwException(e.getTargetException());
  • 0358 // Not reached
  • 0359 return null;
  • 0360 }
  • 0361 }
  • 0362 private volatile transient Constructor<T> cachedConstructor;
  • 0363 private volatile transient Class newInstanceCallerCache;
  • 0364
  • 0365
  • 0366 /**
  • 0367 * Determines if the specified <code>Object</code> is assignment-compatible
  • 0368 * with the object represented by this <code>Class</code>. This method is
  • 0369 * the dynamic equivalent of the Java language <code>instanceof</code>
  • 0370 * operator. The method returns <code>true</code> if the specified
  • 0371 * <code>Object</code> argument is non-null and can be cast to the
  • 0372 * reference type represented by this <code>Class</code> object without
  • 0373 * raising a <code>ClassCastException.</code> It returns <code>false</code>
  • 0374 * otherwise.
  • 0375 *
  • 0376 * <p> Specifically, if this <code>Class</code> object represents a
  • 0377 * declared class, this method returns <code>true</code> if the specified
  • 0378 * <code>Object</code> argument is an instance of the represented class (or
  • 0379 * of any of its subclasses); it returns <code>false</code> otherwise. If
  • 0380 * this <code>Class</code> object represents an array class, this method
  • 0381 * returns <code>true</code> if the specified <code>Object</code> argument
  • 0382 * can be converted to an object of the array class by an identity
  • 0383 * conversion or by a widening reference conversion; it returns
  • 0384 * <code>false</code> otherwise. If this <code>Class</code> object
  • 0385 * represents an interface, this method returns <code>true</code> if the
  • 0386 * class or any superclass of the specified <code>Object</code> argument
  • 0387 * implements this interface; it returns <code>false</code> otherwise. If
  • 0388 * this <code>Class</code> object represents a primitive type, this method
  • 0389 * returns <code>false</code>.
  • 0390 *
  • 0391 * @param obj the object to check
  • 0392 * @return true if <code>obj</code> is an instance of this class
  • 0393 *
  • 0394 * @since JDK1.1
  • 0395 */
  • 0396 public native boolean isInstance(Object obj);
  • 0397
  • 0398
  • 0399 /**
  • 0400 * Determines if the class or interface represented by this
  • 0401 * <code>Class</code> object is either the same as, or is a superclass or
  • 0402 * superinterface of, the class or interface represented by the specified
  • 0403 * <code>Class</code> parameter. It returns <code>true</code> if so;
  • 0404 * otherwise it returns <code>false</code>. If this <code>Class</code>
  • 0405 * object represents a primitive type, this method returns
  • 0406 * <code>true</code> if the specified <code>Class</code> parameter is
  • 0407 * exactly this <code>Class</code> object; otherwise it returns
  • 0408 * <code>false</code>.
  • 0409 *
  • 0410 * <p> Specifically, this method tests whether the type represented by the
  • 0411 * specified <code>Class</code> parameter can be converted to the type
  • 0412 * represented by this <code>Class</code> object via an identity conversion
  • 0413 * or via a widening reference conversion. See <em>The Java Language
  • 0414 * Specification</em>, sections 5.1.1 and 5.1.4 , for details.
  • 0415 *
  • 0416 * @param cls the <code>Class</code> object to be checked
  • 0417 * @return the <code>boolean</code> value indicating whether objects of the
  • 0418 * type <code>cls</code> can be assigned to objects of this class
  • 0419 * @exception NullPointerException if the specified Class parameter is
  • 0420 * null.
  • 0421 * @since JDK1.1
  • 0422 */
  • 0423 public native boolean isAssignableFrom(Class<?> cls);
  • 0424
  • 0425
  • 0426 /**
  • 0427 * Determines if the specified <code>Class</code> object represents an
  • 0428 * interface type.
  • 0429 *
  • 0430 * @return <code>true</code> if this object represents an interface;
  • 0431 * <code>false</code> otherwise.
  • 0432 */
  • 0433 public native boolean isInterface();
  • 0434
  • 0435
  • 0436 /**
  • 0437 * Determines if this <code>Class</code> object represents an array class.
  • 0438 *
  • 0439 * @return <code>true</code> if this object represents an array class;
  • 0440 * <code>false</code> otherwise.
  • 0441 * @since JDK1.1
  • 0442 */
  • 0443 public native boolean isArray();
  • 0444
  • 0445
  • 0446 /**
  • 0447 * Determines if the specified <code>Class</code> object represents a
  • 0448 * primitive type.
  • 0449 *
  • 0450 * <p> There are nine predefined <code>Class</code> objects to represent
  • 0451 * the eight primitive types and void. These are created by the Java
  • 0452 * Virtual Machine, and have the same names as the primitive types that
  • 0453 * they represent, namely <code>boolean</code>, <code>byte</code>,
  • 0454 * <code>char</code>, <code>short</code>, <code>int</code>,
  • 0455 * <code>long</code>, <code>float</code>, and <code>double</code>.
  • 0456 *
  • 0457 * <p> These objects may only be accessed via the following public static
  • 0458 * final variables, and are the only <code>Class</code> objects for which
  • 0459 * this method returns <code>true</code>.
  • 0460 *
  • 0461 * @return true if and only if this class represents a primitive type
  • 0462 *
  • 0463 * @see java.lang.Boolean#TYPE
  • 0464 * @see java.lang.Character#TYPE
  • 0465 * @see java.lang.Byte#TYPE
  • 0466 * @see java.lang.Short#TYPE
  • 0467 * @see java.lang.Integer#TYPE
  • 0468 * @see java.lang.Long#TYPE
  • 0469 * @see java.lang.Float#TYPE
  • 0470 * @see java.lang.Double#TYPE
  • 0471 * @see java.lang.Void#TYPE
  • 0472 * @since JDK1.1
  • 0473 */
  • 0474 public native boolean isPrimitive();
  • 0475
  • 0476 /**
  • 0477 * Returns true if this <tt>Class</tt> object represents an annotation
  • 0478 * type. Note that if this method returns true, {@link #isInterface()}
  • 0479 * would also return true, as all annotation types are also interfaces.
  • 0480 *
  • 0481 * @return <tt>true</tt> if this class object represents an annotation
  • 0482 * type; <tt>false</tt> otherwise
  • 0483 * @since 1.5
  • 0484 */
  • 0485 public boolean isAnnotation() {
  • 0486 return (getModifiers() & ANNOTATION) != 0;
  • 0487 }
  • 0488
  • 0489 /**
  • 0490 * Returns <tt>true</tt> if this class is a synthetic class;
  • 0491 * returns <tt>false</tt> otherwise.
  • 0492 * @return <tt>true</tt> if and only if this class is a synthetic class as
  • 0493 * defined by the Java Language Specification.
  • 0494 * @since 1.5
  • 0495 */
  • 0496 public boolean isSynthetic() {
  • 0497 return (getModifiers() & SYNTHETIC) != 0;
  • 0498 }
  • 0499
  • 0500 /**
  • 0501 * Returns the name of the entity (class, interface, array class,
  • 0502 * primitive type, or void) represented by this <tt>Class</tt> object,
  • 0503 * as a <tt>String</tt>.
  • 0504 *
  • 0505 * <p> If this class object represents a reference type that is not an
  • 0506 * array type then the binary name of the class is returned, as specified
  • 0507 * by the Java Language Specification, Second Edition.
  • 0508 *
  • 0509 * <p> If this class object represents a primitive type or void, then the
  • 0510 * name returned is a <tt>String</tt> equal to the Java language
  • 0511 * keyword corresponding to the primitive type or void.
  • 0512 *
  • 0513 * <p> If this class object represents a class of arrays, then the internal
  • 0514 * form of the name consists of the name of the element type preceded by
  • 0515 * one or more '<tt>[</tt>' characters representing the depth of the array
  • 0516 * nesting. The encoding of element type names is as follows:
  • 0517 *
  • 0518 * <blockquote><table summary="Element types and encodings">
  • 0519 * <tr><th> Element Type <th>     <th> Encoding
  • 0520 * <tr><td> boolean <td>     <td align=center> Z
  • 0521 * <tr><td> byte <td>     <td align=center> B
  • 0522 * <tr><td> char <td>     <td align=center> C
  • 0523 * <tr><td> class or interface
  • 0524 * <td>     <td align=center> L<i>classname</i>;
  • 0525 * <tr><td> double <td>     <td align=center> D
  • 0526 * <tr><td> float <td>     <td align=center> F
  • 0527 * <tr><td> int <td>     <td align=center> I
  • 0528 * <tr><td> long <td>     <td align=center> J
  • 0529 * <tr><td> short <td>     <td align=center> S
  • 0530 * </table></blockquote>
  • 0531 *
  • 0532 * <p> The class or interface name <i>classname</i> is the binary name of
  • 0533 * the class specified above.
  • 0534 *
  • 0535 * <p> Examples:
  • 0536 * <blockquote><pre>
  • 0537 * String.class.getName()
  • 0538 * returns "java.lang.String"
  • 0539 * byte.class.getName()
  • 0540 * returns "byte"
  • 0541 * (new Object[3]).getClass().getName()
  • 0542 * returns "[Ljava.lang.Object;"
  • 0543 * (new int[3][4][5][6][7][8][9]).getClass().getName()
  • 0544 * returns "[[[[[[[I"
  • 0545 * </pre></blockquote>
  • 0546 *
  • 0547 * @return the name of the class or interface
  • 0548 * represented by this object.
  • 0549 */
  • 0550 public String getName() {
  • 0551 if (name == null)
  • 0552 name = getName0();
  • 0553 return name;
  • 0554 }
  • 0555
  • 0556 // cache the name to reduce the number of calls into the VM
  • 0557 private transient String name;
  • 0558 private native String getName0();
  • 0559
  • 0560 /**
  • 0561 * Returns the class loader for the class. Some implementations may use
  • 0562 * null to represent the bootstrap class loader. This method will return
  • 0563 * null in such implementations if this class was loaded by the bootstrap
  • 0564 * class loader.
  • 0565 *
  • 0566 * <p> If a security manager is present, and the caller's class loader is
  • 0567 * not null and the caller's class loader is not the same as or an ancestor of
  • 0568 * the class loader for the class whose class loader is requested, then
  • 0569 * this method calls the security manager's <code>checkPermission</code>
  • 0570 * method with a <code>RuntimePermission("getClassLoader")</code>
  • 0571 * permission to ensure it's ok to access the class loader for the class.
  • 0572 *
  • 0573 * <p>If this object
  • 0574 * represents a primitive type or void, null is returned.
  • 0575 *
  • 0576 * @return the class loader that loaded the class or interface
  • 0577 * represented by this object.
  • 0578 * @throws SecurityException
  • 0579 * if a security manager exists and its
  • 0580 * <code>checkPermission</code> method denies
  • 0581 * access to the class loader for the class.
  • 0582 * @see java.lang.ClassLoader
  • 0583 * @see SecurityManager#checkPermission
  • 0584 * @see java.lang.RuntimePermission
  • 0585 */
  • 0586 public ClassLoader getClassLoader() {
  • 0587 ClassLoader cl = getClassLoader0();
  • 0588 if (cl == null)
  • 0589 return null;
  • 0590 SecurityManager sm = System.getSecurityManager();
  • 0591 if (sm != null) {
  • 0592 ClassLoader ccl = ClassLoader.getCallerClassLoader();
  • 0593 if (ccl != null && ccl != cl && !cl.isAncestor(ccl)) {
  • 0594 sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
  • 0595 }
  • 0596 }
  • 0597 return cl;
  • 0598 }
  • 0599
  • 0600 // Package-private to allow ClassLoader access
  • 0601 native ClassLoader getClassLoader0();
  • 0602
  • 0603
  • 0604 /**
  • 0605 * Returns an array of <tt>TypeVariable</tt> objects that represent the
  • 0606 * type variables declared by the generic declaration represented by this
  • 0607 * <tt>GenericDeclaration</tt> object, in declaration order. Returns an
  • 0608 * array of length 0 if the underlying generic declaration declares no type
  • 0609 * variables.
  • 0610 *
  • 0611 * @return an array of <tt>TypeVariable</tt> objects that represent
  • 0612 * the type variables declared by this generic declaration
  • 0613 * @throws GenericSignatureFormatError if the generic
  • 0614 * signature of this generic declaration does not conform to
  • 0615 * the format specified in the Java Virtual Machine Specification,
  • 0616 * 3rd edition
  • 0617 * @since 1.5
  • 0618 */
  • 0619 public TypeVariable<Class<T>>[] getTypeParameters() {
  • 0620 if (getGenericSignature() != null)
  • 0621 return (TypeVariable<Class<T>>[])getGenericInfo().getTypeParameters();
  • 0622 else
  • 0623 return (TypeVariable<Class<T>>[])new TypeVariable[0];
  • 0624 }
  • 0625
  • 0626
  • 0627 /**
  • 0628 * Returns the <code>Class</code> representing the superclass of the entity
  • 0629 * (class, interface, primitive type or void) represented by this
  • 0630 * <code>Class</code>. If this <code>Class</code> represents either the
  • 0631 * <code>Object</code> class, an interface, a primitive type, or void, then
  • 0632 * null is returned. If this object represents an array class then the
  • 0633 * <code>Class</code> object representing the <code>Object</code> class is
  • 0634 * returned.
  • 0635 *
  • 0636 * @return the superclass of the class represented by this object.
  • 0637 */
  • 0638 public native Class<? super T> getSuperclass();
  • 0639
  • 0640
  • 0641 /**
  • 0642 * Returns the <tt>Type</tt> representing the direct superclass of
  • 0643 * the entity (class, interface, primitive type or void) represented by
  • 0644 * this <tt>Class</tt>.
  • 0645 *
  • 0646 * <p>If the superclass is a parameterized type, the <tt>Type</tt>
  • 0647 * object returned must accurately reflect the actual type
  • 0648 * parameters used in the source code. The parameterized type
  • 0649 * representing the superclass is created if it had not been
  • 0650 * created before. See the declaration of {@link
  • 0651 * java.lang.reflect.ParameterizedType ParameterizedType} for the
  • 0652 * semantics of the creation process for parameterized types. If
  • 0653 * this <tt>Class</tt> represents either the <tt>Object</tt>
  • 0654 * class, an interface, a primitive type, or void, then null is
  • 0655 * returned. If this object represents an array class then the
  • 0656 * <tt>Class</tt> object representing the <tt>Object</tt> class is
  • 0657 * returned.
  • 0658 *
  • 0659 * @throws GenericSignatureFormatError if the generic
  • 0660 * class signature does not conform to the format specified in the
  • 0661 * Java Virtual Machine Specification, 3rd edition
  • 0662 * @throws TypeNotPresentException if the generic superclass
  • 0663 * refers to a non-existent type declaration
  • 0664 * @throws MalformedParameterizedTypeException if the
  • 0665 * generic superclass refers to a parameterized type that cannot be
  • 0666 * instantiated for any reason
  • 0667 * @return the superclass of the class represented by this object
  • 0668 * @since 1.5
  • 0669 */
  • 0670 public Type getGenericSuperclass() {
  • 0671 if (getGenericSignature() != null) {
  • 0672 // Historical irregularity:
  • 0673 // Generic signature marks interfaces with superclass = Object
  • 0674 // but this API returns null for interfaces
  • 0675 if (isInterface())
  • 0676 return null;
  • 0677 return getGenericInfo().getSuperclass();
  • 0678 } else
  • 0679 return getSuperclass();
  • 0680 }
  • 0681
  • 0682 /**
  • 0683 * Gets the package for this class. The class loader of this class is used
  • 0684 * to find the package. If the class was loaded by the bootstrap class
  • 0685 * loader the set of packages loaded from CLASSPATH is searched to find the
  • 0686 * package of the class. Null is returned if no package object was created
  • 0687 * by the class loader of this class.
  • 0688 *
  • 0689 * <p> Packages have attributes for versions and specifications only if the
  • 0690 * information was defined in the manifests that accompany the classes, and
  • 0691 * if the class loader created the package instance with the attributes
  • 0692 * from the manifest.
  • 0693 *
  • 0694 * @return the package of the class, or null if no package
  • 0695 * information is available from the archive or codebase.
  • 0696 */
  • 0697 public Package getPackage() {
  • 0698 return Package.getPackage(this);
  • 0699 }
  • 0700
  • 0701
  • 0702 /**
  • 0703 * Determines the interfaces implemented by the class or interface
  • 0704 * represented by this object.
  • 0705 *
  • 0706 * <p> If this object represents a class, the return value is an array
  • 0707 * containing objects representing all interfaces implemented by the
  • 0708 * class. The order of the interface objects in the array corresponds to
  • 0709 * the order of the interface names in the <code>implements</code> clause
  • 0710 * of the declaration of the class represented by this object. For
  • 0711 * example, given the declaration:
  • 0712 * <blockquote><pre>
  • 0713 * class Shimmer implements FloorWax, DessertTopping { ... }
  • 0714 * </pre></blockquote>
  • 0715 * suppose the value of <code>s</code> is an instance of
  • 0716 * <code>Shimmer</code>; the value of the expression:
  • 0717 * <blockquote><pre>
  • 0718 * s.getClass().getInterfaces()[0]
  • 0719 * </pre></blockquote>
  • 0720 * is the <code>Class</code> object that represents interface
  • 0721 * <code>FloorWax</code>; and the value of:
  • 0722 * <blockquote><pre>
  • 0723 * s.getClass().getInterfaces()[1]
  • 0724 * </pre></blockquote>
  • 0725 * is the <code>Class</code> object that represents interface
  • 0726 * <code>DessertTopping</code>.
  • 0727 *
  • 0728 * <p> If this object represents an interface, the array contains objects
  • 0729 * representing all interfaces extended by the interface. The order of the
  • 0730 * interface objects in the array corresponds to the order of the interface
  • 0731 * names in the <code>extends</code> clause of the declaration of the
  • 0732 * interface represented by this object.
  • 0733 *
  • 0734 * <p> If this object represents a class or interface that implements no
  • 0735 * interfaces, the method returns an array of length 0.
  • 0736 *
  • 0737 * <p> If this object represents a primitive type or void, the method
  • 0738 * returns an array of length 0.
  • 0739 *
  • 0740 * @return an array of interfaces implemented by this class.
  • 0741 */
  • 0742 public native Class<?>[] getInterfaces();
  • 0743
  • 0744 /**
  • 0745 * Returns the <tt>Type</tt>s representing the interfaces
  • 0746 * directly implemented by the class or interface represented by
  • 0747 * this object.
  • 0748 *
  • 0749 * <p>If a superinterface is a parameterized type, the
  • 0750 * <tt>Type</tt> object returned for it must accurately reflect
  • 0751 * the actual type parameters used in the source code. The
  • 0752 * parameterized type representing each superinterface is created
  • 0753 * if it had not been created before. See the declaration of
  • 0754 * {@link java.lang.reflect.ParameterizedType ParameterizedType}
  • 0755 * for the semantics of the creation process for parameterized
  • 0756 * types.
  • 0757 *
  • 0758 * <p> If this object represents a class, the return value is an
  • 0759 * array containing objects representing all interfaces
  • 0760 * implemented by the class. The order of the interface objects in
  • 0761 * the array corresponds to the order of the interface names in
  • 0762 * the <tt>implements</tt> clause of the declaration of the class
  • 0763 * represented by this object. In the case of an array class, the
  • 0764 * interfaces <tt>Cloneable</tt> and <tt>Serializable</tt> are
  • 0765 * returned in that order.
  • 0766 *
  • 0767 * <p>If this object represents an interface, the array contains
  • 0768 * objects representing all interfaces directly extended by the
  • 0769 * interface. The order of the interface objects in the array
  • 0770 * corresponds to the order of the interface names in the
  • 0771 * <tt>extends</tt> clause of the declaration of the interface
  • 0772 * represented by this object.
  • 0773 *
  • 0774 * <p>If this object represents a class or interface that
  • 0775 * implements no interfaces, the method returns an array of length
  • 0776 * 0.
  • 0777 *
  • 0778 * <p>If this object represents a primitive type or void, the
  • 0779 * method returns an array of length 0.
  • 0780 *
  • 0781 * @throws GenericSignatureFormatError
  • 0782 * if the generic class signature does not conform to the format
  • 0783 * specified in the Java Virtual Machine Specification, 3rd edition
  • 0784 * @throws TypeNotPresentException if any of the generic
  • 0785 * superinterfaces refers to a non-existent type declaration
  • 0786 * @throws MalformedParameterizedTypeException if any of the
  • 0787 * generic superinterfaces refer to a parameterized type that cannot
  • 0788 * be instantiated for any reason
  • 0789 * @return an array of interfaces implemented by this class
  • 0790 * @since 1.5
  • 0791 */
  • 0792 public Type[] getGenericInterfaces() {
  • 0793 if (getGenericSignature() != null)
  • 0794 return getGenericInfo().getSuperInterfaces();
  • 0795 else
  • 0796 return getInterfaces();
  • 0797 }
  • 0798
  • 0799
  • 0800 /**
  • 0801 * Returns the <code>Class</code> representing the component type of an
  • 0802 * array. If this class does not represent an array class this method