public final class Class<T> extends Object implements Serializable, AnnotatedElement, GenericDeclaration, Type
Class
instances: those representing real classes and interfaces, those representing
primitive types, and those representing array classes.
These represent an ordinary class or interface as found in the class
hierarchy. The name associated with these Class
instances is simply
the fully qualified class name of the class or interface that it represents.
In addition to this human-readable name, each class is also associated by a
so-called signature, which is the letter "L", followed by the
class name and a semicolon (";"). The signature is what the runtime system
uses internally for identifying the class (for example in a DEX file).
These represent the standard Java primitive types and hence share their
names (for example "int" for the int
primitive type). Although it is
not possible to create new instances based on these Class
instances,
they are still useful for providing reflection information, and as the
component type of array classes. There is one Class
instance for each
primitive type, and their signatures are:
B
representing the byte
primitive typeS
representing the short
primitive typeI
representing the int
primitive typeJ
representing the long
primitive typeF
representing the float
primitive typeD
representing the double
primitive typeC
representing the char
primitive typeZ
representing the boolean
primitive typeV
representing void function return values
These represent the classes of Java arrays. There is one such Class
instance per combination of array leaf component type and arity (number of
dimensions). In this case, the name associated with the Class
consists of one or more left square brackets (one per dimension in the array)
followed by the signature of the class representing the leaf component type,
which can be either an object type or a primitive type. The signature of a
Class
representing an array type is the same as its name. Examples
of array class signatures are:
[I
representing the int[]
type[Ljava/lang/String;
representing the String[]
type[[[C
representing the char[][][]
type (three dimensions!)Modifier and Type | Method and Description |
---|---|
<U> Class<? extends U> |
asSubclass(Class<U> clazz)
Casts this
Class to represent a subclass of the specified class. |
T |
cast(Object obj)
Casts the specified object to the type represented by this
Class . |
boolean |
desiredAssertionStatus()
Returns the assertion status for the class represented by this
Class . |
static Class<?> |
forName(String className)
Returns a
Class object which represents the class with the
specified name. |
static Class<?> |
forName(String className,
boolean initializeBoolean,
ClassLoader classLoader)
Returns a
Class object which represents the class with the
specified name. |
<A extends Annotation> |
getAnnotation(Class<A> annotationType)
Returns, for this element, the annotation with the specified type, or
null if no annotation with the specified type is present
(including inherited annotations). |
Annotation[] |
getAnnotations()
Returns all the annotations of this class.
|
String |
getCanonicalName()
Returns the canonical name of this class.
|
Class<?>[] |
getClasses()
Returns an array containing
Class objects for all public classes
and interfaces that are members of this class. |
ClassLoader |
getClassLoader()
Returns the class loader which was used to load the class represented by
this
Class . |
Class<?> |
getComponentType()
Returns a
Class object which represents the component type if
this class represents an array type. |
Constructor<T> |
getConstructor(Class<?>... parameterTypes)
Returns a
Constructor object which represents the public
constructor matching the specified parameter types. |
Constructor<?>[] |
getConstructors()
Returns an array containing
Constructor objects for all public
constructors for the class represented by this Class . |
Annotation[] |
getDeclaredAnnotations()
Returns the annotations that are directly defined on the class
represented by this
Class . |
Class<?>[] |
getDeclaredClasses()
Returns an array containing
Class objects for all classes and
interfaces that are declared as members of the class which this Class represents. |
Constructor<T> |
getDeclaredConstructor(Class<?>... parameterTypes)
Returns a
Constructor object which represents the constructor
matching the specified parameter types that is declared by the class
represented by this Class . |
Constructor<?>[] |
getDeclaredConstructors()
Returns an array containing
Constructor objects for all
constructors declared in the class represented by this Class . |
Field |
getDeclaredField(String name)
Returns a
Field object for the field with the specified name
which is declared in the class represented by this Class . |
Field[] |
getDeclaredFields()
Returns an array containing
Field objects for all fields declared
in the class represented by this Class . |
Method |
getDeclaredMethod(String name,
Class<?>... parameterTypes)
Returns a
Method object which represents the method matching the
specified name and parameter types that is declared by the class
represented by this Class . |
Method[] |
getDeclaredMethods()
Returns an array containing
Method objects for all methods
declared in the class represented by this Class . |
Class<?> |
getDeclaringClass()
Returns the declaring
Class of this Class . |
Class<?> |
getEnclosingClass()
Returns the enclosing
Class of this Class . |
Constructor<?> |
getEnclosingConstructor()
Gets the enclosing
Constructor of this Class , if it is an
anonymous or local/automatic class; otherwise null . |
Method |
getEnclosingMethod()
Gets the enclosing
Method of this Class , if it is an
anonymous or local/automatic class; otherwise null . |
T[] |
getEnumConstants()
Gets the
enum constants associated with this Class . |
Field |
getField(String name)
Returns a
Field object which represents the public field with the
specified name. |
Field[] |
getFields()
Returns an array containing
Field objects for all public fields
for the class C represented by this Class . |
Type[] |
getGenericInterfaces()
Gets the
Type s of the interfaces that this Class directly
implements. |
Type |
getGenericSuperclass()
Gets the
Type that represents the superclass of this class . |
Class<?>[] |
getInterfaces()
Returns an array of
Class objects that match the interfaces
specified in the implements declaration of the class represented
by this Class . |
Method |
getMethod(String name,
Class<?>... parameterTypes)
Returns a
Method object which represents the public method with
the specified name and parameter types. |
Method[] |
getMethods()
Returns an array containing
Method objects for all public methods
for the class C represented by this Class . |
int |
getModifiers()
Returns an integer that represents the modifiers of the class represented
by this
Class . |
String |
getName()
Returns the name of the class represented by this
Class . |
Package |
getPackage()
Returns the
Package of which the class represented by this
Class is a member. |
ProtectionDomain |
getProtectionDomain()
Returns null.
|
URL |
getResource(String resName)
Returns the URL of the resource specified by
resName . |
InputStream |
getResourceAsStream(String resName)
Returns a read-only stream for the contents of the resource specified by
resName . |
Object[] |
getSigners()
Returns null.
|
String |
getSimpleName()
Returns the simple name of the class represented by this
Class as
defined in the source code. |
Class<? super T> |
getSuperclass()
Returns the
Class object which represents the superclass of the
class represented by this Class . |
TypeVariable<Class<T>>[] |
getTypeParameters()
Returns an array containing
TypeVariable objects for type
variables declared by the generic class represented by this Class . |
boolean |
isAnnotation()
Indicates whether this
Class represents an annotation class. |
boolean |
isAnnotationPresent(Class<? extends Annotation> annotationType)
Indicates whether or not this element has an annotation with the
specified annotation type (including inherited annotations).
|
boolean |
isAnonymousClass()
Indicates whether the class represented by this
Class is
anonymously declared. |
boolean |
isArray()
Indicates whether the class represented by this
Class is an array
class. |
boolean |
isAssignableFrom(Class<?> cls)
Indicates whether the specified class type can be converted to the class
represented by this
Class . |
boolean |
isEnum()
Indicates whether the class represented by this
Class is an
enum . |
boolean |
isInstance(Object object)
Indicates whether the specified object can be cast to the class
represented by this
Class . |
boolean |
isInterface()
Indicates whether this
Class represents an interface. |
boolean |
isLocalClass()
Indicates whether the class represented by this
Class is defined
locally. |
boolean |
isMemberClass()
Indicates whether the class represented by this
Class is a member
class. |
boolean |
isPrimitive()
Indicates whether this
Class represents a primitive type. |
boolean |
isSynthetic()
Indicates whether this
Class represents a synthetic type. |
T |
newInstance()
Returns a new instance of the class represented by this
Class ,
created by invoking the default (that is, zero-argument) constructor. |
String |
toString()
Returns a string containing a concise, human-readable description of this
object.
|
public static Class<?> forName(String className) throws ClassNotFoundException
Class
object which represents the class with the
specified name. The name should be the name of a class as described in
the class definition
; however, Class
es representing
primitive types can not be found using this method.
If the class has not been loaded so far, it is being loaded and linked first. This is done through either the class loader of the calling class or one of its parent class loaders. The class is also being initialized, which means that a possible static initializer block is executed.
className
- the name of the non-primitive-type class to find.Class
instance.ClassNotFoundException
- if the requested class can not be found.LinkageError
- if an error occurs during linkageExceptionInInitializerError
- if an exception occurs during static initialization of a
class.public static Class<?> forName(String className, boolean initializeBoolean, ClassLoader classLoader) throws ClassNotFoundException
Class
object which represents the class with the
specified name. The name should be the name of a class as described in
the class definition
, however Class
es representing
primitive types can not be found using this method. Security rules will
be obeyed.
If the class has not been loaded so far, it is being loaded and linked first. This is done through either the specified class loader or one of its parent class loaders. The caller can also request the class to be initialized, which means that a possible static initializer block is executed.
className
- the name of the non-primitive-type class to find.initializeBoolean
- indicates whether the class should be initialized.classLoader
- the class loader to use to load the class.Class
instance.ClassNotFoundException
- if the requested class can not be found.LinkageError
- if an error occurs during linkageExceptionInInitializerError
- if an exception occurs during static initialization of a
class.public Class<?>[] getClasses()
Class
objects for all public classes
and interfaces that are members of this class. This includes public
members inherited from super classes and interfaces. If there are no such
class members or if this object represents a primitive type then an array
of length 0 is returned.public <A extends Annotation> A getAnnotation(Class<A> annotationType)
AnnotatedElement
null
if no annotation with the specified type is present
(including inherited annotations).getAnnotation
in interface AnnotatedElement
annotationType
- the type of the annotation to search fornull
public Annotation[] getAnnotations()
getAnnotations
in interface AnnotatedElement
getDeclaredAnnotations()
public String getCanonicalName()
null
.null
if it does not have a
canonical name.public ClassLoader getClassLoader()
Class
. Implementations are free to return null
for
classes that were loaded by the bootstrap class loader. The Android
reference implementation, though, returns a reference to an actual
representation of the bootstrap class loader.ClassLoader
public Class<?> getComponentType()
Class
object which represents the component type if
this class represents an array type. Returns null
if this class
does not represent an array type. The component type of an array type is
the type of the elements of the array.public Constructor<T> getConstructor(Class<?>... parameterTypes) throws NoSuchMethodException
Constructor
object which represents the public
constructor matching the specified parameter types.parameterTypes
- the parameter types of the requested constructor.
(Class[]) null
is equivalent to the empty array.parameterTypes
.NoSuchMethodException
- if the constructor can not be found.getDeclaredConstructor(Class[])
public Constructor<?>[] getConstructors()
Constructor
objects for all public
constructors for the class represented by this Class
. If there
are no public constructors or if this Class
represents an array
class, a primitive type or void then an empty array is returned.Class
.getDeclaredConstructors()
public Annotation[] getDeclaredAnnotations()
Class
. Annotations that are inherited are not
included in the result. If there are no annotations at all, an empty
array is returned.getDeclaredAnnotations
in interface AnnotatedElement
Class
represents.getAnnotations()
public Class<?>[] getDeclaredClasses()
Class
objects for all classes and
interfaces that are declared as members of the class which this Class
represents. If there are no classes or interfaces declared or if
this class represents an array class, a primitive type or void, then an
empty array is returned.Class
objects for all the classes and
interfaces that are used in member declarations.public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes) throws NoSuchMethodException
Constructor
object which represents the constructor
matching the specified parameter types that is declared by the class
represented by this Class
.parameterTypes
- the parameter types of the requested constructor.
(Class[]) null
is equivalent to the empty array.parameterTypes
.NoSuchMethodException
- if the requested constructor can not be found.getConstructor(Class[])
public Constructor<?>[] getDeclaredConstructors()
Constructor
objects for all
constructors declared in the class represented by this Class
. If
there are no constructors or if this Class
represents an array
class, a primitive type or void then an empty array is returned.Class
.getConstructors()
public Field getDeclaredField(String name) throws NoSuchFieldException
Field
object for the field with the specified name
which is declared in the class represented by this Class
.name
- the name of the requested field.NoSuchFieldException
- if the requested field can not be found.getField(String)
public Field[] getDeclaredFields()
Field
objects for all fields declared
in the class represented by this Class
. If there are no fields or
if this Class
represents an array class, a primitive type or void
then an empty array is returned.getFields()
public Method getDeclaredMethod(String name, Class<?>... parameterTypes) throws NoSuchMethodException
Method
object which represents the method matching the
specified name and parameter types that is declared by the class
represented by this Class
.name
- the requested method's name.parameterTypes
- the parameter types of the requested method.
(Class[]) null
is equivalent to the empty array.name
and parameterTypes
.NoSuchMethodException
- if the requested constructor can not be found.NullPointerException
- if name
is null
.getMethod(String, Class[])
public Method[] getDeclaredMethods()
Method
objects for all methods
declared in the class represented by this Class
. If there are no
methods or if this Class
represents an array class, a primitive
type or void then an empty array is returned.Class
.getMethods()
public Class<?> getDeclaringClass()
Class
of this Class
. Returns
null
if the class is not a member of another class or if this
Class
represents an array class, a primitive type or void.Class
or null
.public Class<?> getEnclosingClass()
Class
of this Class
. If there is no
enclosing class the method returns null
.Class
or null
.public Constructor<?> getEnclosingConstructor()
Constructor
of this Class
, if it is an
anonymous or local/automatic class; otherwise null
.Constructor
instance or null
.public Method getEnclosingMethod()
Method
of this Class
, if it is an
anonymous or local/automatic class; otherwise null
.Method
instance or null
.public T[] getEnumConstants()
enum
constants associated with this Class
.
Returns null
if this Class
does not represent an enum
type.enum
constants or null
.public Field getField(String name) throws NoSuchFieldException
Field
object which represents the public field with the
specified name. This method first searches the class C represented by
this Class
, then the interfaces implemented by C and finally the
superclasses of C.name
- the name of the requested field.name
.NoSuchFieldException
- if the field can not be found.getDeclaredField(String)
public Field[] getFields()
Field
objects for all public fields
for the class C represented by this Class
. Fields may be declared
in C, the interfaces it implements or in the superclasses of C. The
elements in the returned array are in no particular order.
If there are no public fields or if this class represents an array class,
a primitive type or void
then an empty array is returned.
Class
.getDeclaredFields()
public Type[] getGenericInterfaces()
Type
s of the interfaces that this Class
directly
implements. If the Class
represents a primitive type or void
then an empty array is returned.Type
instances directly implemented by the
class represented by this class
.public Type getGenericSuperclass()
Type
that represents the superclass of this class
.Type
representing the superclass.public Class<?>[] getInterfaces()
Class
objects that match the interfaces
specified in the implements
declaration of the class represented
by this Class
. The order of the elements in the array is
identical to the order in the original class declaration. If the class
does not implement any interfaces, an empty array is returned.public Method getMethod(String name, Class<?>... parameterTypes) throws NoSuchMethodException
Method
object which represents the public method with
the specified name and parameter types. This method first searches the
class C represented by this Class
, then the superclasses of C and
finally the interfaces implemented by C and finally the superclasses of C
for a method with matching name.name
- the requested method's name.parameterTypes
- the parameter types of the requested method.
(Class[]) null
is equivalent to the empty array.name
.NoSuchMethodException
- if the method can not be found.getDeclaredMethod(String, Class[])
public Method[] getMethods()
Method
objects for all public methods
for the class C represented by this Class
. Methods may be
declared in C, the interfaces it implements or in the superclasses of C.
The elements in the returned array are in no particular order.
If there are no public methods or if this Class
represents a
primitive type or void
then an empty array is returned.
Class
.getDeclaredMethods()
public int getModifiers()
Class
. The returned value is a combination of bits
defined by constants in the Modifier
class.Class
.public String getName()
Class
. For a
description of the format which is used, see the class definition of
Class
.Class
.public String getSimpleName()
Class
as
defined in the source code. If there is no name (that is, the class is
anonymous) then an empty string is returned. If the receiver is an array
then the name of the underlying type with square braces appended (for
example "Integer[]"
) is returned.Class
.public ProtectionDomain getProtectionDomain()
public URL getResource(String resName)
resName
. The mapping
between the resource name and the URL is managed by the class' class
loader.resName
- the name of the resource.URL
object or null
if
the resource can not be found.ClassLoader
public InputStream getResourceAsStream(String resName)
resName
. The mapping between the resource name and the stream is
managed by the class' class loader.resName
- the name of the resource.null
if no
resource with the specified name can be found.ClassLoader
public Object[] getSigners()
ClassLoader
can load classes from multiple dex files.
All classes from any given dex file will have the same signers, but different dex
files may have different signers. This does not fit well with the original
ClassLoader
-based model of getSigners
.)public Class<? super T> getSuperclass()
Class
object which represents the superclass of the
class represented by this Class
. If this Class
represents
the Object
class, a primitive type, an interface or void then the
method returns null
. If this Class
represents an array
class then the Object
class is returned.Class
.public TypeVariable<Class<T>>[] getTypeParameters()
TypeVariable
objects for type
variables declared by the generic class represented by this Class
. Returns an empty array if the class is not generic.getTypeParameters
in interface GenericDeclaration
public boolean isAnnotation()
Class
represents an annotation class.true
if this Class
represents an annotation
class; false
otherwise.public boolean isAnnotationPresent(Class<? extends Annotation> annotationType)
AnnotatedElement
isAnnotationPresent
in interface AnnotatedElement
annotationType
- the type of the annotation to search fortrue
if the annotation exists, false
otherwisepublic boolean isAnonymousClass()
Class
is
anonymously declared.true
if the class represented by this Class
is
anonymous; false
otherwise.public boolean isArray()
Class
is an array
class.true
if the class represented by this Class
is an
array class; false
otherwise.public boolean isAssignableFrom(Class<?> cls)
Class
. Conversion may be done via an identity
conversion or a widening reference conversion (if either the receiver or
the argument represent primitive types, only the identity conversion
applies).cls
- the class to check.true
if cls
can be converted to the class
represented by this Class
; false
otherwise.NullPointerException
- if cls
is null
.public boolean isEnum()
Class
is an
enum
.true
if the class represented by this Class
is an
enum
; false
otherwise.public boolean isInstance(Object object)
Class
. This is the runtime version of the
instanceof
operator.object
- the object to check.true
if object
can be cast to the type
represented by this Class
; false
if object
is null
or cannot be cast.public boolean isInterface()
Class
represents an interface.true
if this Class
represents an interface;
false
otherwise.public boolean isLocalClass()
Class
is defined
locally.true
if the class represented by this Class
is
defined locally; false
otherwise.public boolean isMemberClass()
Class
is a member
class.true
if the class represented by this Class
is a
member class; false
otherwise.public boolean isPrimitive()
Class
represents a primitive type.true
if this Class
represents a primitive type;
false
otherwise.public boolean isSynthetic()
Class
represents a synthetic type.true
if this Class
represents a synthetic type;
false
otherwise.public T newInstance() throws InstantiationException, IllegalAccessException
Class
,
created by invoking the default (that is, zero-argument) constructor. If
there is no such constructor, or if the creation fails (either because of
a lack of available memory or because an exception is thrown by the
constructor), an InstantiationException
is thrown. If the default
constructor exists but is not accessible from the context where this
method is invoked, an IllegalAccessException
is thrown.Class
.IllegalAccessException
- if the default constructor is not visible.InstantiationException
- if the instance can not be created.public String toString()
Object
getClass().getName() + '@' + Integer.toHexString(hashCode())
See Writing a useful
toString
method
if you intend implementing your own toString
method.
public Package getPackage()
Package
of which the class represented by this
Class
is a member. Returns null
if no Package
object was created by the class loader of the class.Package
of which this Class
is a
member or null
.public boolean desiredAssertionStatus()
Class
. Assertion is enabled / disabled based on the class loader,
package or class default at runtime.Class
.public <U> Class<? extends U> asSubclass(Class<U> clazz)
Class
to represent a subclass of the specified class.
If successful, this Class
is returned; otherwise a ClassCastException
is thrown.clazz
- the required type.Class
cast as a subclass of the given type.ClassCastException
- if this Class
cannot be cast to the specified type.public T cast(Object obj)
Class
.
If the object is null
then the result is also null
.obj
- the object to cast.ClassCastException
- if the object cannot be cast to the specified type.