java.lang
public
final
class
java.lang.Class<T>
This class must be implemented by the VM vendor. The documented natives must
be implemented to support other provided class implementations in this
package. An instance of class Class is the in-image representation of a Java
class. There are three basic types of Classes
- Classes representing object types (classes or interfaces)
- These are Classes which represent the class of a simple instance as
found in the class hierarchy. The name of one of these Classes is simply the
fully qualified class name of the class or interface that it represents. Its
signature is the letter "L", followed by its name, followed by a
semi-colon (";").
- Classes representing base types
- These Classes represent the standard Java base types. Although it is not
possible to create new instances of these Classes, they are still useful for
providing reflection information, and as the component type of array classes.
There is one of these Classes for each base type, and their signatures are:
B
representing the byte
base type
S
representing the short
base type
I
representing the int
base type
J
representing the long
base type
F
representing the float
base type
D
representing the double
base type
C
representing the char
base type
Z
representing the boolean
base type
V
representing void function return values
The name of a Class representing a base type is the keyword which is used to
represent the type in Java source code (i.e. "int" for the int
base type.
- Classes representing array classes
- These are Classes which represent the classes of Java arrays. There is
one such Class for all array instances of a given arity (number of
dimensions) and leaf component type. In this case, the name of the class is
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 base type. The signature of a Class
representing an array type is the same as its name.
Summary
Public Methods
clone,
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
toString,
wait,
wait,
wait
Details
Public Methods
public
Class<? extends U>
asSubclass(Class<U> clazz)
Casts the receiver to a subclass of the given class. If successful
returns the receiver, otherwise if the cast cannot be made throws a
ClassCastException
.
Returns
- this class cast as a subclass of the given type.
public
T
cast(Object obj)
Cast the given object to the type
T
.
If the object is
null
the result is also
null
.
Returns
- The object that has been cast.
public
boolean
desiredAssertionStatus()
Returns the assertion status for this class. Assertion is
enabled/disabled based on class loader default, package or class default
at runtime
Returns
- the assertion status for this class
public
static
Class<?>
forName(String className, boolean initializeBoolean, ClassLoader classLoader)
Returns a Class object which represents the class named by the argument.
The name should be the name of a class as described in the class
definition of
Class, however Classes representing base types can
not be found using this method. Security rules will be obeyed.
Parameters
className
| The name of the non-base type class to find |
initializeBoolean
| A boolean indicating whether the class should be
initialized |
classLoader
| The class loader to use to load the class |
public
static
Class<?>
forName(String className)
Returns a Class object which represents the class named by the argument.
The name should be the name of a class as described in the class
definition of
Class, however Classes representing base types can
not be found using this method.
Parameters
className
| The name of the non-base type class to find |
public
A
getAnnotation(Class<A> annotationClass)
Returns the annotation of the given type. If there is no annotation the
method returns
null
.
Parameters
annotationClass
| the annotation type. |
Returns
- the annotation of the given type, or
null
if none.
Returns all the annotations of the receiver. If there are no annotations
then returns an empty array.
Returns
- a copy of the array containing the receiver's annotations.
public
String
getCanonicalName()
Returns the canonical name of the receiver. If the receiver does not have
a canonical name, as defined in the Java Language Specification, then the
method returns
null
.
Returns
- the receiver canonical name, or
null
.
public
ClassLoader
getClassLoader()
Returns the class loader which was used to load the class represented by
the receiver. Returns null if the class was loaded by the bootstrap class
loader
Returns
- the receiver's class loader or nil
public
Class[]<?>
getClasses()
Returns an array containing all public class members of the class which
the receiver represents and its super classes and interfaces
Returns
- the class' public class members
public
Class<?>
getComponentType()
Returns a Class object which represents the receiver's component type if
the receiver represents an array type. Otherwise returns nil. The
component type of an array type is the type of the elements of the array.
Returns
- the component type of the receiver.
public
Constructor<T>
getConstructor(Class[] parameterTypes)
Returns a public Constructor object which represents the constructor
described by the arguments.
Parameters
parameterTypes
| the types of the arguments. |
Returns
- the constructor described by the arguments.
public
Constructor[]<T>
getConstructors()
Returns an array containing Constructor objects describing all
constructors which are visible from the current execution context.
Returns
- all visible constructors starting from the receiver.
public
Annotation[]
getDeclaredAnnotations()
Returns the annotations that are directly defined on this type.
Annotations that are inherited are not included in the result. If there
are no annotations, returns an empty array.
Returns
- a copy of the array containing the receiver's defined
annotations.
public
Class[]<?>
getDeclaredClasses()
Returns an array containing all class members of the class which the
receiver represents. Note that some of the fields which are returned may
not be visible in the current execution context.
public
Constructor<T>
getDeclaredConstructor(Class[] parameterTypes)
Returns a Constructor object which represents the constructor described
by the arguments.
Parameters
parameterTypes
| the types of the arguments. |
Returns
- the constructor described by the arguments.
public
Constructor[]<T>
getDeclaredConstructors()
Returns an array containing Constructor objects describing all
constructor which are defined by the receiver. Note that some of the
fields which are returned may not be visible in the current execution
context.
Returns
- the receiver's constructors.
public
Field
getDeclaredField(String name)
Returns a Field object describing the field in the receiver named by the
argument. Note that the Constructor may not be visible from the current
execution context.
Parameters
name
| The name of the field to look for. |
Returns
- the field in the receiver named by the argument.
public
Field[]
getDeclaredFields()
Returns an array containing Field objects describing all fields which are
defined by the receiver. Note that some of the fields which are returned
may not be visible in the current execution context.
public
Method
getDeclaredMethod(String name, Class[] parameterTypes)
Returns a Method object which represents the method described by the
arguments. Note that the associated method may not be visible from the
current execution context.
Parameters
name
| the name of the method |
parameterTypes
| the types of the arguments. |
Returns
- the method described by the arguments.
public
Method[]
getDeclaredMethods()
Returns an array containing Method objects describing all methods which
are defined by the receiver. Note that some of the methods which are
returned may not be visible in the current execution context.
public
Class<?>
getDeclaringClass()
Returns the class which declared the class represented by the receiver.
This will return null if the receiver is a member of another class.
Returns
- the declaring class of the receiver.
public
Class<?>
getEnclosingClass()
Returns the class that directly encloses the receiver. If there is no
enclosing class the method returns
null
.
Returns
- the enclosing class or
null
.
public
Constructor<?>
getEnclosingConstructor()
Gets the
Constructor, which encloses the declaration of this
class, if it is an anonymous or local/automatic class, otherwise
null
.
public
Method
getEnclosingMethod()
Gets the
Method, which encloses the declaration of this class,
if it is an anonymous or local/automatic class, otherwise
null
.
public
T[]
getEnumConstants()
Gets the
enum
constants/fields associated with this class
if it is an
enum, otherwise
null
.
Returns
- An array of the
enum
constants for this class or
null
.
public
Field
getField(String name)
Returns a Field object describing the field in the receiver named by the
argument which must be visible from the current execution context.
Parameters
name
| The name of the field to look for. |
Returns
- the field in the receiver named by the argument.
public
Field[]
getFields()
Returns an array containing Field objects describing all fields which are
visible from the current execution context.
Returns
- all visible fields starting from the receiver.
public
Type[]
getGenericInterfaces()
Gets the
types of the interface that this class directly
implements.
Returns
- An array of Type instances.
public
Type
getGenericSuperclass()
Gets the
Type that represents the super class of this class.
public
Class[]<?>
getInterfaces()
Returns an array of Class objects which match the interfaces specified in
the receiver classes
implements
declaration. The order of
entries equals the order in the original class declaration. If the
class doesn't implement any interfaces, an empty array is returned.
Returns
- Class[] the interfaces the receiver claims to implement.
public
Method
getMethod(String name, Class[] parameterTypes)
Returns a Method object which represents the method described by the
arguments.
Parameters
name
| String the name of the method |
parameterTypes
| Class[] the types of the arguments. |
Returns
- Method the method described by the arguments.
public
Method[]
getMethods()
Returns an array containing Method objects describing all methods which
are visible from the current execution context.
Returns
- Method[] all visible methods starting from the receiver.
public
int
getModifiers()
Returns an integer which which is the receiver's modifiers. Note that the
constants which describe the bits which are returned are implemented in
class
Modifier which may not be available on the target.
public
String
getName()
Returns the name of the class which the receiver represents. For a
description of the format which is used, see the class definition of
Class.
public
Package
getPackage()
Returns the Package of which this class is a member. A class has a
Package if it was loaded from a SecureClassLoader
Returns
- Package the Package of which this class is a member or null
Returns the ProtectionDomain of the receiver.
Note: In order to conserve space in embedded targets, we allow this
method to answer null for classes in the system protection domain (i.e.
for system classes). System classes are always given full permissions
(i.e. AllPermission). This is not changeable via the
java.security.Policy.
Returns
- ProtectionDomain the receiver's ProtectionDomain.
public
URL
getResource(String resName)
Returns a read-only stream on the contents of the resource specified by
resName. The mapping between the resource name and the stream is managed
by the class' class loader.
Parameters
resName
| the name of the resource. |
Returns
- a stream on the resource.
public
InputStream
getResourceAsStream(String resName)
Returns a read-only stream on the contents of the resource specified by
resName. The mapping between the resource name and the stream is managed
by the class' class loader.
Parameters
resName
| the name of the resource. |
Returns
- a stream on the resource.
public
Object[]
getSigners()
Returns the signers for the class represented by the receiver, or null if
there are no signers.
Returns
- the signers of the receiver.
public
String
getSimpleName()
Returns the simple name of the receiver as defined in the source code. If
there is no name (the class is anonymous) returns an empty string, and if
the receiver is an array returns the name of the underlying type with
square braces appended (e.g.
"Integer[]"
).
Returns
- the simple name of the receiver.
public
Class<? super T>
getSuperclass()
Returns the Class which represents the receiver's superclass. For Classes
which represent base types, interfaces, and for
Object the method
returns null.
Returns
- the receiver's superclass.
public
synchronized
TypeVariable[]<Class<T>>
getTypeParameters()
Gets the type variables associated with this class. Returns an empty
array if the class is not generic or does not make use of type
variables otherwise.
public
boolean
isAnnotation()
Indicates whether or not this class is an annotation.
Returns
- A value of
true
if this class is an annotation,
otherwise false
.
public
boolean
isAnnotationPresent(Class<? extends Annotation> annotationClass)
Indicates whether or not the given annotation is present for this class.
Parameters
annotationClass
| The annotation to look for in this class. |
Returns
- A value of
true
if the annotation is present,
otherwise false
.
public
boolean
isAnonymousClass()
Indicates whether or not this class was anonymously declared.
Returns
- A value of
true
if this class is anonymous,
otherwise false
.
public
boolean
isArray()
Returns true if the receiver represents an array class.
Returns
true
if the receiver represents an array class
false
if it does not represent an array class
public
boolean
isAssignableFrom(Class<?> cls)
Returns true if the type represented by the argument can be converted via
an identity conversion or a widening reference conversion (i.e. if either
the receiver or the argument represent primitive types, only the identity
conversion applies).
Parameters
cls
| Class the class to test |
Returns
true
the argument can be assigned into the
receiver false
the argument cannot be assigned
into the receiver
public
boolean
isEnum()
Indicates whether or not this class is an
enum
.
Returns
- A value of
true
if this class is an Enum,
otherwise false
.
public
boolean
isInstance(Object object)
Returns true if the argument is non-null and can be cast to the type of
the receiver. This is the runtime version of the
instanceof
operator.
Parameters
object
| Object the object to test
|
Returns
true
the argument can be cast to the type of the
receiver false
the argument is null or cannot be
cast to the type of the receiver
public
boolean
isInterface()
Returns true if the receiver represents an interface.
Returns
true
if the receiver represents an interface
false
if it does not represent an interface
public
boolean
isLocalClass()
Returns whether the receiver is defined locally.
Returns
true
if the class is local, otherwise
false
.
public
boolean
isMemberClass()
Returns whether the receiver is a member class.
Returns
true
if the class is a member class, otherwise
false
.
public
boolean
isPrimitive()
Returns true if the receiver represents a base type.
Returns
true
if the receiver represents a base type
false
if it does not represent a base type
public
boolean
isSynthetic()
Returns whether the receiver is a synthetic type.
Returns
true
if the receiver is a synthetic type and
false
otherwise.
public
T
newInstance()
Returns a new instance of the class represented by the receiver, created
by invoking the default (i.e. 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 message is sent, an
IllegalAccessException is thrown.
Returns
- a new instance of the class represented by the receiver.
public
String
toString()
Returns a string containing a concise, human-readable description of the
receiver.
Returns
- a printable representation for the receiver.