/* Class.java -- Representation of a Java class.
- Copyright (C) 1998, 1999, 2000, 2002, 2003, 2004
+ Copyright (C) 1998, 1999, 2000, 2002, 2003, 2004, 2005, 2006, 2007
Free Software Foundation
This file is part of GNU Classpath.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
-Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
-02111-1307 USA.
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
package java.lang;
+import gnu.java.lang.reflect.ClassSignatureParser;
import java.io.InputStream;
import java.io.Serializable;
+import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
+import java.lang.reflect.GenericDeclaration;
+import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
+import java.lang.reflect.Type;
+import java.lang.reflect.TypeVariable;
import java.net.URL;
import java.security.ProtectionDomain;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
+import java.util.HashMap;
+import java.util.Collection;
+import java.lang.reflect.AnnotatedElement;
+import java.lang.annotation.Annotation;
+import java.lang.annotation.Inherited;
/**
* A Class represents a Java type. There will never be multiple Class
* objects with identical names and ClassLoaders. Primitive types, array
* types, and void also have a Class object.
*
- * <p>Arrays with identical type and number of dimensions share the same
- * class (and null "system" ClassLoader, incidentally). The name of an
- * array class is <code>[<signature format>;</code> ... for example,
+ * <p>Arrays with identical type and number of dimensions share the same class.
+ * The array class ClassLoader is the same as the ClassLoader of the element
+ * type of the array (which can be null to indicate the bootstrap classloader).
+ * The name of an array class is <code>[<signature format>;</code>.
+ * <p> For example,
* String[]'s class is <code>[Ljava.lang.String;</code>. boolean, byte,
* short, char, int, long, float and double have the "type name" of
* Z,B,S,C,I,J,F,D for the purposes of array classes. If it's a
* see {@link ObjectStreamClass}.
*
* @author John Keiser
- * @author Eric Blake <ebb9@email.byu.edu>
- * @author Tom Tromey <tromey@cygnus.com>
+ * @author Eric Blake (ebb9@email.byu.edu)
+ * @author Tom Tromey (tromey@cygnus.com)
* @since 1.0
* @see ClassLoader
*/
-public final class Class implements Serializable
+public final class Class<T>
+ implements Type, AnnotatedElement, GenericDeclaration, Serializable
{
/**
* Class is non-instantiable from Java code; only the VM can create
* @throws ExceptionInInitializerError if the class loads, but an exception
* occurs during initialization
*/
- public static native Class forName (String className)
+ public static native Class<?> forName (String className)
throws ClassNotFoundException;
+ // A private internal method that is called by compiler-generated code.
+ private static Class forName (String className, Class caller)
+ throws ClassNotFoundException
+ {
+ return forName(className, true, caller.getClassLoaderInternal());
+ }
+
+
/**
* Use the specified classloader to load and link a class. If the loader
* is null, this uses the bootstrap class loader (provide the security
* @see ClassLoader
* @since 1.2
*/
- public static native Class forName (String className, boolean initialize,
- ClassLoader loader)
+ public static native Class<?> forName (String className, boolean initialize,
+ ClassLoader loader)
throws ClassNotFoundException;
/**
* @throws SecurityException if the security check fails
* @since 1.1
*/
- public native Class[] getClasses ();
-
+ public Class<?>[] getClasses()
+ {
+ memberAccessCheck(Member.PUBLIC);
+ return internalGetClasses();
+ }
+
+ /**
+ * Like <code>getClasses()</code> but without the security checks.
+ */
+ private Class<?>[] internalGetClasses()
+ {
+ ArrayList<Class> list = new ArrayList<Class>();
+ list.addAll(Arrays.asList(getDeclaredClasses(true)));
+ Class superClass = getSuperclass();
+ if (superClass != null)
+ list.addAll(Arrays.asList(superClass.internalGetClasses()));
+ return list.toArray(new Class<?>[list.size()]);
+ }
+
/**
- * Get the ClassLoader that loaded this class. If it was loaded by the
- * system classloader, this method will return null. If there is a security
- * manager, and the caller's class loader does not match the requested
- * one, a security check of <code>RuntimePermission("getClassLoader")</code>
+ * Get the ClassLoader that loaded this class. If the class was loaded
+ * by the bootstrap classloader, this method will return null.
+ * If there is a security manager, and the caller's class loader is not
+ * an ancestor of the requested one, a security check of
+ * <code>RuntimePermission("getClassLoader")</code>
* must first succeed. Primitive types and void return null.
*
* @return the ClassLoader that loaded this class
* @see RuntimePermission
*/
public native ClassLoader getClassLoader ();
-
+
+ // A private internal method that is called by compiler-generated code.
+ private final native ClassLoader getClassLoader (Class caller);
+
+ /**
+ * Internal method that circumvents the usual security checks when
+ * getting the class loader.
+ */
+ private native ClassLoader getClassLoaderInternal ();
+
/**
* If this is an array, get the Class representing the type of array.
* Examples: "[[Ljava.lang.String;" would return "[Ljava.lang.String;", and
* @see Array
* @since 1.1
*/
- public native Class getComponentType ();
+ public native Class<?> getComponentType ();
/**
* Get a public constructor declared in this class. If the constructor takes
* @see #getConstructors()
* @since 1.1
*/
- public native Constructor getConstructor(Class[] args)
- throws NoSuchMethodException, SecurityException;
-
- // This is used to implement getConstructors and
- // getDeclaredConstructors.
- private native Constructor[] _getConstructors (boolean declared)
- throws SecurityException;
+ public native Constructor<T> getConstructor(Class<?>... args)
+ throws NoSuchMethodException;
/**
* Get all the public constructors of this class. This returns an array of
* @throws SecurityException if the security check fails
* @since 1.1
*/
- public Constructor[] getConstructors()
- throws SecurityException
+ public Constructor<?>[] getConstructors()
{
- return _getConstructors(false);
+ memberAccessCheck(Member.PUBLIC);
+ return getDeclaredConstructors(true);
}
/**
* @see #getDeclaredConstructors()
* @since 1.1
*/
- public native Constructor getDeclaredConstructor(Class[] args)
- throws NoSuchMethodException, SecurityException;
+ public native Constructor<T> getDeclaredConstructor(Class<?>... args)
+ throws NoSuchMethodException;
/**
* Get all the declared member classes and interfaces in this class, but
* @throws SecurityException if the security check fails
* @since 1.1
*/
- public native Class[] getDeclaredClasses() throws SecurityException;
+ public Class<?>[] getDeclaredClasses()
+ {
+ memberAccessCheck(Member.DECLARED);
+ return getDeclaredClasses(false);
+ }
+
+ native Class<?>[] getDeclaredClasses (boolean publicOnly);
/**
* Get all the declared constructors of this class. This returns an array of
* @throws SecurityException if the security check fails
* @since 1.1
*/
- public Constructor[] getDeclaredConstructors()
- throws SecurityException
+ public Constructor<?>[] getDeclaredConstructors()
{
- return _getConstructors(true);
+ memberAccessCheck(Member.DECLARED);
+ return getDeclaredConstructors(false);
}
+ native Constructor<?>[] getDeclaredConstructors (boolean publicOnly);
+
/**
* Get a field declared in this class, where name is its simple name. The
* implicit length field of arrays is not available. A security check may
* @since 1.1
*/
public native Field getDeclaredField(String fieldName)
- throws NoSuchFieldException, SecurityException;
+ throws NoSuchFieldException;
/**
* Get all the declared fields in this class, but not those inherited from
/**
* Get a method declared in this class, where name is its simple name. The
* implicit methods of Object are not available from arrays or interfaces.
- * Constructors (named "<init>" in the class file) and class initializers
- * (name "<clinit>") are not available. The Virtual Machine allows
+ * Constructors (named "<init>" in the class file) and class initializers
+ * (name "<clinit>") are not available. The Virtual Machine allows
* multiple methods with the same signature but differing return types; in
* such a case the most specific return types are favored, then the final
* choice is arbitrary. If the method takes no argument, an array of zero
* @see #getDeclaredMethods()
* @since 1.1
*/
- public Method getDeclaredMethod(String methodName, Class[] args)
- throws NoSuchMethodException, SecurityException
+ public Method getDeclaredMethod(String methodName, Class<?>... args)
+ throws NoSuchMethodException
{
memberAccessCheck(Member.DECLARED);
* @throws SecurityException if the security check fails
* @since 1.1
*/
- public native Method[] getDeclaredMethods () throws SecurityException;
+ public native Method[] getDeclaredMethods();
/**
* If this is a nested or inner class, return the class that declared it.
* @since 1.1
*/
// This is marked as unimplemented in the JCL book.
- public native Class getDeclaringClass ();
+ public native Class<?> getDeclaringClass ();
private native Field getField (String fieldName, int hash)
- throws NoSuchFieldException, SecurityException;
+ throws NoSuchFieldException;
/**
* Get a public field declared or inherited in this class, where name is
* @since 1.1
*/
public Field getField(String fieldName)
- throws NoSuchFieldException, SecurityException
+ throws NoSuchFieldException
{
memberAccessCheck(Member.PUBLIC);
Field field = getField(fieldName, fieldName.hashCode());
/**
* Returns the <code>Package</code> in which this class is defined
* Returns null when this information is not available from the
- * classloader of this class or when the classloader of this class
- * is null.
+ * classloader of this class.
*
* @return the package for this class, if it is available
* @since 1.2
*/
public Package getPackage()
{
- ClassLoader cl = getClassLoader();
+ ClassLoader cl = getClassLoaderInternal();
if (cl != null)
- {
- String name = getName();
- String pkg = "";
- int idx = name.lastIndexOf('.');
- if (idx >= 0)
- pkg = name.substring(0, idx);
- return cl.getPackage(pkg);
- }
- return null;
+ return cl.getPackage(getPackagePortion(getName()));
+ else
+ return VMClassLoader.getPackage(getPackagePortion(getName()));
}
/**
*
* @return the interfaces this class directly implements
*/
- public native Class[] getInterfaces ();
+ public native Class<?>[] getInterfaces ();
private final native void getSignature(StringBuffer buffer);
private static final native String getSignature(Class[] args,
/**
* Get a public method declared or inherited in this class, where name is
* its simple name. The implicit methods of Object are not available from
- * interfaces. Constructors (named "<init>" in the class file) and class
- * initializers (name "<clinit>") are not available. The Virtual
+ * interfaces. Constructors (named "<init>" in the class file) and class
+ * initializers (name "<clinit>") are not available. The Virtual
* Machine allows multiple methods with the same signature but differing
* return types, and the class can inherit multiple methods of the same
* return type; in such a case the most specific return types are favored,
* @see #getMethods()
* @since 1.1
*/
- public Method getMethod(String methodName, Class[] args)
- throws NoSuchMethodException, SecurityException
+ public Method getMethod(String methodName, Class<?>... args)
+ throws NoSuchMethodException
{
memberAccessCheck(Member.PUBLIC);
* @throws SecurityException if the security check fails
* @since 1.1
*/
- public native Method[] getMethods () throws SecurityException;
+ public native Method[] getMethods();
/**
* Get the modifiers of this class. These can be decoded using Modifier,
/**
* Get the name of this class, separated by dots for package separators.
- * Primitive types and arrays are encoded as:
+ * If the class represents a primitive type, or void, then the
+ * name of the type as it appears in the Java programming language
+ * is returned. For instance, <code>Byte.TYPE.getName()</code>
+ * returns "byte".
+ *
+ * Arrays are specially encoded as shown on this table.
* <pre>
+ * array type [<em>element type</em>
+ * (note that the element type is encoded per
+ * this table)
* boolean Z
* byte B
* char C
* float F
* double D
* void V
- * array type [<em>element type</em>
* class or interface, alone: <dotted name>
* class or interface, as element type: L<dotted name>;
+ * </pre>
*
* @return the name of this class
*/
* the system classloader, ClassLoader.getSystemResource() is used instead.
*
* <p>If the name you supply is absolute (it starts with a <code>/</code>),
- * then it is passed on to getResource() as is. If it is relative, the
- * package name is prepended, and <code>.</code>'s are replaced with
- * <code>/</code>.
+ * then the leading <code>/</code> is removed and it is passed on to
+ * getResource(). If it is relative, the package name is prepended, and
+ * <code>.</code>'s are replaced with <code>/</code>.
*
* <p>The URL returned is system- and classloader-dependent, and could
* change across implementations.
public URL getResource(String resourceName)
{
String name = resourcePath(resourceName);
- ClassLoader loader = getClassLoader();
+ ClassLoader loader = getClassLoaderInternal();
if (loader == null)
return ClassLoader.getSystemResource(name);
return loader.getResource(name);
* instead.
*
* <p>If the name you supply is absolute (it starts with a <code>/</code>),
- * then it is passed on to getResource() as is. If it is relative, the
- * package name is prepended, and <code>.</code>'s are replaced with
- * <code>/</code>.
+ * then the leading <code>/</code> is removed and it is passed on to
+ * getResource(). If it is relative, the package name is prepended, and
+ * <code>.</code>'s are replaced with <code>/</code>.
*
* <p>The URL returned is system- and classloader-dependent, and could
* change across implementations.
public InputStream getResourceAsStream(String resourceName)
{
String name = resourcePath(resourceName);
- ClassLoader loader = getClassLoader();
+ ClassLoader loader = getClassLoaderInternal();
if (loader == null)
return ClassLoader.getSystemResourceAsStream(name);
return loader.getResourceAsStream(name);
private String resourcePath(String resourceName)
{
- if (resourceName.startsWith("/"))
- return resourceName.substring(1);
-
- Class c = this;
- while (c.isArray())
- c = c.getComponentType();
-
- String packageName = c.getName().replace('.', '/');
- int end = packageName.lastIndexOf('/');
- if (end != -1)
- return packageName.substring(0, end + 1) + resourceName;
+ if (resourceName.length() > 0)
+ {
+ if (resourceName.charAt(0) != '/')
+ {
+ String pkg = getPackagePortion(getName());
+ if (pkg.length() > 0)
+ resourceName = pkg.replace('.','/') + '/' + resourceName;
+ }
+ else
+ {
+ resourceName = resourceName.substring(1);
+ }
+ }
return resourceName;
}
*
* @return the direct superclass of this class
*/
- public native Class getSuperclass ();
+ public native Class<? super T> getSuperclass ();
/**
* Return whether this class is an array type.
* @throws NullPointerException if c is null
* @since 1.1
*/
- public native boolean isAssignableFrom (Class c);
+ public native boolean isAssignableFrom (Class<?> c);
/**
* Discover whether an Object is an instance of this Class. Think of it
* @throws ExceptionInInitializerError if class initialization caused by
* this call fails with an exception
*/
- public native Object newInstance ()
+ public native T newInstance ()
throws InstantiationException, IllegalAccessException;
// We need a native method to retrieve the protection domain, because we
* Returns the protection domain of this class. If the classloader did not
* record the protection domain when creating this class the unknown
* protection domain is returned which has a <code>null</code> code source
- * and all permissions.
+ * and all permissions. A security check may be performed, with
+ * <code>RuntimePermission("getProtectionDomain")</code>.
*
* @return the protection domain
* @throws SecurityException if the security manager exists and the caller
*/
public boolean desiredAssertionStatus()
{
- ClassLoader c = getClassLoader();
+ ClassLoader c = getClassLoaderInternal();
Object status;
if (c == null)
return VMClassLoader.defaultAssertionStatus();
sm.checkPackageAccess(pkg.getName());
}
}
+
+
+ /**
+ * <p>
+ * Casts this class to represent a subclass of the specified class.
+ * This method is useful for `narrowing' the type of a class so that
+ * the class object, and instances of that class, can match the contract
+ * of a more restrictive method. For example, if this class has the
+ * static type of <code>Class<Object></code>, and a dynamic type of
+ * <code>Class<Rectangle></code>, then, assuming <code>Shape</code> is
+ * a superclass of <code>Rectangle</code>, this method can be used on
+ * this class with the parameter, <code>Class<Shape></code>, to retain
+ * the same instance but with the type
+ * <code>Class<? extends Shape></code>.
+ * </p>
+ * <p>
+ * If this class can be converted to an instance which is parameterised
+ * over a subtype of the supplied type, <code>U</code>, then this method
+ * returns an appropriately cast reference to this object. Otherwise,
+ * a <code>ClassCastException</code> is thrown.
+ * </p>
+ *
+ * @param klass the class object, the parameterized type (<code>U</code>) of
+ * which should be a superclass of the parameterized type of
+ * this instance.
+ * @return a reference to this object, appropriately cast.
+ * @throws ClassCastException if this class can not be converted to one
+ * which represents a subclass of the specified
+ * type, <code>U</code>.
+ * @since 1.5
+ */
+ public <U> Class<? extends U> asSubclass(Class<U> klass)
+ {
+ if (! klass.isAssignableFrom(this))
+ throw new ClassCastException();
+ return (Class<? extends U>) this;
+ }
+
+ /**
+ * Returns the specified object, cast to this <code>Class</code>' type.
+ *
+ * @param obj the object to cast
+ * @throws ClassCastException if obj is not an instance of this class
+ * @since 1.5
+ */
+ public T cast(Object obj)
+ {
+ if (obj != null && ! isInstance(obj))
+ throw new ClassCastException();
+ return (T) obj;
+ }
+
+ /**
+ * Returns the enumeration constants of this class, or
+ * null if this class is not an <code>Enum</code>.
+ *
+ * @return an array of <code>Enum</code> constants
+ * associated with this class, or null if this
+ * class is not an <code>enum</code>.
+ * @since 1.5
+ */
+ public T[] getEnumConstants()
+ {
+ if (isEnum())
+ {
+ try
+ {
+ return (T[]) getMethod("values").invoke(null);
+ }
+ catch (NoSuchMethodException exception)
+ {
+ throw new Error("Enum lacks values() method");
+ }
+ catch (IllegalAccessException exception)
+ {
+ throw new Error("Unable to access Enum class");
+ }
+ catch (InvocationTargetException exception)
+ {
+ throw new
+ RuntimeException("The values method threw an exception",
+ exception);
+ }
+ }
+ else
+ {
+ return null;
+ }
+ }
+
+ /**
+ * Returns true if this class is an <code>Enum</code>.
+ *
+ * @return true if this is an enumeration class.
+ * @since 1.5
+ */
+ public native boolean isEnum();
+
+
+ /**
+ * Returns true if this class is a synthetic class, generated by
+ * the compiler.
+ *
+ * @return true if this is a synthetic class.
+ * @since 1.5
+ */
+ public native boolean isSynthetic();
+
+
+ /**
+ * Returns true if this class is an <code>Annotation</code>.
+ *
+ * @return true if this is an annotation class.
+ * @since 1.5
+ */
+ public native boolean isAnnotation();
+
+
+ /**
+ * Returns the simple name for this class, as used in the source
+ * code. For normal classes, this is the content returned by
+ * <code>getName()</code> which follows the last ".". Anonymous
+ * classes have no name, and so the result of calling this method is
+ * "". The simple name of an array consists of the simple name of
+ * its component type, followed by "[]". Thus, an array with the
+ * component type of an anonymous class has a simple name of simply
+ * "[]".
+ *
+ * @return the simple name for this class.
+ * @since 1.5
+ */
+ public String getSimpleName()
+ {
+ StringBuffer sb = new StringBuffer();
+ Class klass = this;
+ int arrayCount = 0;
+ while (klass.isArray())
+ {
+ klass = klass.getComponentType();
+ ++arrayCount;
+ }
+ if (! klass.isAnonymousClass())
+ {
+ String fullName = klass.getName();
+ sb.append(fullName, fullName.lastIndexOf(".") + 1, fullName.length());
+ }
+ while (arrayCount-- > 0)
+ sb.append("[]");
+ return sb.toString();
+ }
+
+ /**
+ * Returns the class which immediately encloses this class. If this class
+ * is a top-level class, this method returns <code>null</code>.
+ *
+ * @return the immediate enclosing class, or <code>null</code> if this is
+ * a top-level class.
+ * @since 1.5
+ */
+ public native Class<?> getEnclosingClass();
+
+ /**
+ * Returns the constructor which immediately encloses this class. If
+ * this class is a top-level class, or a local or anonymous class
+ * immediately enclosed by a type definition, instance initializer
+ * or static initializer, then <code>null</code> is returned.
+ *
+ * @return the immediate enclosing constructor if this class is
+ * declared within a constructor. Otherwise, <code>null</code>
+ * is returned.
+ * @since 1.5
+ */
+ public native Constructor<T> getEnclosingConstructor();
+
+ /**
+ * Returns the method which immediately encloses this class. If
+ * this class is a top-level class, or a local or anonymous class
+ * immediately enclosed by a type definition, instance initializer
+ * or static initializer, then <code>null</code> is returned.
+ *
+ * @return the immediate enclosing method if this class is
+ * declared within a method. Otherwise, <code>null</code>
+ * is returned.
+ * @since 1.5
+ */
+ public native Method getEnclosingMethod();
+
+ private native String getClassSignature();
+
+ /**
+ * <p>
+ * Returns an array of <code>Type</code> objects which represent the
+ * interfaces directly implemented by this class or extended by this
+ * interface.
+ * </p>
+ * <p>
+ * If one of the superinterfaces is a parameterized type, then the
+ * object returned for this interface reflects the actual type
+ * parameters used in the source code. Type parameters are created
+ * using the semantics specified by the <code>ParameterizedType</code>
+ * interface, and only if an instance has not already been created.
+ * </p>
+ * <p>
+ * The order of the interfaces in the array matches the order in which
+ * the interfaces are declared. For classes which represent an array,
+ * an array of two interfaces, <code>Cloneable</code> and
+ * <code>Serializable</code>, is always returned, with the objects in
+ * that order. A class representing a primitive type or void always
+ * returns an array of zero size.
+ * </p>
+ *
+ * @return an array of interfaces implemented or extended by this class.
+ * @throws GenericSignatureFormatError if the generic signature of one
+ * of the interfaces does not comply with that specified by the Java
+ * Virtual Machine specification, 3rd edition.
+ * @throws TypeNotPresentException if any of the superinterfaces refers
+ * to a non-existant type.
+ * @throws MalformedParameterizedTypeException if any of the interfaces
+ * refer to a parameterized type that can not be instantiated for
+ * some reason.
+ * @since 1.5
+ * @see java.lang.reflect.ParameterizedType
+ */
+ public Type[] getGenericInterfaces()
+ {
+ if (isPrimitive())
+ return new Type[0];
+
+ String sig = getClassSignature();
+ if (sig == null)
+ return getInterfaces();
+
+ ClassSignatureParser p = new ClassSignatureParser(this, sig);
+ return p.getInterfaceTypes();
+ }
+
+ /**
+ * <p>
+ * Returns a <code>Type</code> object representing the direct superclass,
+ * whether class, interface, primitive type or void, of this class.
+ * If this class is an array class, then a class instance representing
+ * the <code>Object</code> class is returned. If this class is primitive,
+ * an interface, or a representation of either the <code>Object</code>
+ * class or void, then <code>null</code> is returned.
+ * </p>
+ * <p>
+ * If the superclass is a parameterized type, then the
+ * object returned for this interface reflects the actual type
+ * parameters used in the source code. Type parameters are created
+ * using the semantics specified by the <code>ParameterizedType</code>
+ * interface, and only if an instance has not already been created.
+ * </p>
+ *
+ * @return the superclass of this class.
+ * @throws GenericSignatureFormatError if the generic signature of the
+ * class does not comply with that specified by the Java
+ * Virtual Machine specification, 3rd edition.
+ * @throws TypeNotPresentException if the superclass refers
+ * to a non-existant type.
+ * @throws MalformedParameterizedTypeException if the superclass
+ * refers to a parameterized type that can not be instantiated for
+ * some reason.
+ * @since 1.5
+ * @see java.lang.reflect.ParameterizedType
+ */
+ public Type getGenericSuperclass()
+ {
+ if (isArray())
+ return Object.class;
+
+ if (isPrimitive() || isInterface() || this == Object.class)
+ return null;
+
+ String sig = getClassSignature();
+ if (sig == null)
+ return getSuperclass();
+
+ ClassSignatureParser p = new ClassSignatureParser(this, sig);
+ return p.getSuperclassType();
+ }
+
+ /**
+ * Returns an array of <code>TypeVariable</code> objects that represents
+ * the type variables declared by this class, in declaration order.
+ * An array of size zero is returned if this class has no type
+ * variables.
+ *
+ * @return the type variables associated with this class.
+ * @throws GenericSignatureFormatError if the generic signature does
+ * not conform to the format specified in the Virtual Machine
+ * specification, version 3.
+ * @since 1.5
+ */
+ public TypeVariable<Class<T>>[] getTypeParameters()
+ {
+ String sig = getClassSignature();
+ if (sig == null)
+ return (TypeVariable<Class<T>>[])new TypeVariable[0];
+
+ ClassSignatureParser p = new ClassSignatureParser(this, sig);
+ return p.getTypeParameters();
+ }
+
+ /**
+ * Returns this class' annotation for the specified annotation type,
+ * or <code>null</code> if no such annotation exists.
+ *
+ * @param annotationClass the type of annotation to look for.
+ * @return this class' annotation for the specified type, or
+ * <code>null</code> if no such annotation exists.
+ * @since 1.5
+ */
+ public <A extends Annotation> A getAnnotation(Class<A> annotationClass)
+ {
+ A foundAnnotation = null;
+ Annotation[] annotations = getAnnotations();
+ for (Annotation annotation : annotations)
+ if (annotation.annotationType() == annotationClass)
+ foundAnnotation = (A) annotation;
+ return foundAnnotation;
+ }
+
+ /**
+ * Returns all annotations associated with this class. If there are
+ * no annotations associated with this class, then a zero-length array
+ * will be returned. The returned array may be modified by the client
+ * code, but this will have no effect on the annotation content of this
+ * class, and hence no effect on the return value of this method for
+ * future callers.
+ *
+ * @return this class' annotations.
+ * @since 1.5
+ */
+ public Annotation[] getAnnotations()
+ {
+ HashMap<Class, Annotation> map = new HashMap<Class, Annotation>();
+ for (Annotation a : getDeclaredAnnotations())
+ map.put((Class) a.annotationType(), a);
+ for (Class<? super T> s = getSuperclass();
+ s != null;
+ s = s.getSuperclass())
+ {
+ for (Annotation a : s.getDeclaredAnnotations())
+ {
+ Class k = (Class) a.annotationType();
+ if (! map.containsKey(k) && k.isAnnotationPresent(Inherited.class))
+ map.put(k, a);
+ }
+ }
+ Collection<Annotation> v = map.values();
+ return v.toArray(new Annotation[v.size()]);
+ }
+
+ /**
+ * <p>
+ * Returns the canonical name of this class, as defined by section
+ * 6.7 of the Java language specification. Each package, top-level class,
+ * top-level interface and primitive type has a canonical name. A member
+ * class has a canonical name, if its parent class has one. Likewise,
+ * an array type has a canonical name, if its component type does.
+ * Local or anonymous classes do not have canonical names.
+ * </p>
+ * <p>
+ * The canonical name for top-level classes, top-level interfaces and
+ * primitive types is always the same as the fully-qualified name.
+ * For array types, the canonical name is the canonical name of its
+ * component type with `[]' appended.
+ * </p>
+ * <p>
+ * The canonical name of a member class always refers to the place where
+ * the class was defined, and is composed of the canonical name of the
+ * defining class and the simple name of the member class, joined by `.'.
+ * For example, if a <code>Person</code> class has an inner class,
+ * <code>M</code>, then both its fully-qualified name and canonical name
+ * is <code>Person.M</code>. A subclass, <code>Staff</code>, of
+ * <code>Person</code> refers to the same inner class by the fully-qualified
+ * name of <code>Staff.M</code>, but its canonical name is still
+ * <code>Person.M</code>.
+ * </p>
+ * <p>
+ * Where no canonical name is present, <code>null</code> is returned.
+ * </p>
+ *
+ * @return the canonical name of the class, or <code>null</code> if the
+ * class doesn't have a canonical name.
+ * @since 1.5
+ */
+ public String getCanonicalName()
+ {
+ if (isArray())
+ {
+ String componentName = getComponentType().getCanonicalName();
+ if (componentName != null)
+ return componentName + "[]";
+ }
+ if (isMemberClass())
+ {
+ String memberName = getDeclaringClass().getCanonicalName();
+ if (memberName != null)
+ return memberName + "." + getSimpleName();
+ }
+ if (isLocalClass() || isAnonymousClass())
+ return null;
+ return getName();
+ }
+
+ /**
+ * Returns all annotations directly defined by this class. If there are
+ * no annotations associated with this class, then a zero-length array
+ * will be returned. The returned array may be modified by the client
+ * code, but this will have no effect on the annotation content of this
+ * class, and hence no effect on the return value of this method for
+ * future callers.
+ *
+ * @return the annotations directly defined by this class.
+ * @since 1.5
+ */
+ public Annotation[] getDeclaredAnnotations()
+ {
+ Annotation[] result = getDeclaredAnnotationsInternal();
+ if (result == null)
+ result = new Annotation[0];
+ return result;
+ }
+
+ private native Annotation[] getDeclaredAnnotationsInternal();
+
+ /**
+ * Returns true if an annotation for the specified type is associated
+ * with this class. This is primarily a short-hand for using marker
+ * annotations.
+ *
+ * @param annotationClass the type of annotation to look for.
+ * @return true if an annotation exists for the specified type.
+ * @since 1.5
+ */
+ public boolean isAnnotationPresent(Class<? extends Annotation>
+ annotationClass)
+ {
+ return getAnnotation(annotationClass) != null;
+ }
+
+ /**
+ * Returns true if this object represents an anonymous class.
+ *
+ * @return true if this object represents an anonymous class.
+ * @since 1.5
+ */
+ public native boolean isAnonymousClass();
+
+ /**
+ * Returns true if this object represents an local class.
+ *
+ * @return true if this object represents an local class.
+ * @since 1.5
+ */
+ public native boolean isLocalClass();
+
+ /**
+ * Returns true if this object represents an member class.
+ *
+ * @return true if this object represents an member class.
+ * @since 1.5
+ */
+ public native boolean isMemberClass();
}