public final class Class<T> extends Object implements Serializable, GenericDeclaration, Type
B
representing the byte
base typeS
representing the short
base typeI
representing the int
base typeJ
representing the long
base typeF
representing the float
base typeD
representing the double
base typeC
representing the char
base typeZ
representing the boolean
base typeV
representing void function return valuesint
base type.Modifier and Type | Method and Description |
---|---|
<U> Class<? extends U> |
asSubclass(Class<U> cls)
Cast this Class to a subclass of the specified Class.
|
T |
cast(Object object)
Cast the specified object to this Class.
|
boolean |
desiredAssertionStatus()
Returns the assertion status for this class.
|
static Class<?> |
forName(String className)
Answers a Class object which represents the class
named by the argument.
|
static Class<?> |
forName(String className,
boolean initializeBoolean,
ClassLoader classLoader)
Answers a Class object which represents the class
named by the argument.
|
AnnotatedType[] |
getAnnotatedInterfaces()
Return the annotated types for the implemented interfaces.
|
AnnotatedType |
getAnnotatedSuperclass()
Return the annotated superclass of this class.
|
<A extends Annotation> |
getAnnotation(Class<A> annotation)
Return the specified Annotation for this Class.
|
Annotation[] |
getAnnotations()
Return the directly declared Annotations for this Class, including the Annotations
inherited from superclasses.
|
<A extends Annotation> |
getAnnotationsByType(Class<A> annotationClass)
Gets the specified type annotations of this class.
|
String |
getCanonicalName()
Return the canonical name of this Class.
|
Class<?>[] |
getClasses()
Answers an array containing all public class members
of the class which the receiver represents and its
superclasses and interfaces
|
ClassLoader |
getClassLoader()
Answers the classloader which was used to load the
class represented by the receiver.
|
Class<?> |
getComponentType()
Answers a Class object which represents the receiver's
component type if the receiver represents an array type.
|
Constructor<T> |
getConstructor(Class<?>... parameterTypes)
Answers a public Constructor object which represents the
constructor described by the arguments.
|
Constructor<?>[] |
getConstructors()
Answers an array containing Constructor objects describing
all constructors which are visible from the current execution
context.
|
<A extends Annotation> |
getDeclaredAnnotation(Class<A> annotation)
Looks through directly declared annotations for this class, not including Annotations inherited from superclasses.
|
Annotation[] |
getDeclaredAnnotations()
Returns the annotations only for this Class, not including Annotations inherited from superclasses.
|
<A extends Annotation> |
getDeclaredAnnotationsByType(Class<A> annotationClass)
Gets the specified type annotations of this class.
|
Class<?>[] |
getDeclaredClasses()
Answers an array containing all class members of the class
which the receiver represents.
|
Constructor<T> |
getDeclaredConstructor(Class<?>... parameterTypes)
Answers a Constructor object which represents the
constructor described by the arguments.
|
Constructor<?>[] |
getDeclaredConstructors()
Answers an array containing Constructor objects describing
all constructor which are defined by the receiver.
|
Field |
getDeclaredField(String name)
Answers a Field object describing the field in the receiver
named by the argument.
|
Field[] |
getDeclaredFields()
Answers an array containing Field objects describing
all fields which are defined by the receiver.
|
Method |
getDeclaredMethod(String name,
Class<?>... parameterTypes)
Answers a Method object which represents the method
described by the arguments.
|
Method[] |
getDeclaredMethods()
Answers an array containing Method objects describing
all methods which are defined by the receiver.
|
Class<?> |
getDeclaringClass()
Answers the class which declared the class represented
by the receiver.
|
Class<?> |
getEnclosingClass()
Return the enclosing Class of this Class.
|
Constructor<?> |
getEnclosingConstructor()
If this Class is defined inside a constructor, return the Constructor.
|
Method |
getEnclosingMethod()
If this Class is defined inside a method, return the Method.
|
T[] |
getEnumConstants()
Answer the array of enum constants for this Class.
|
Field |
getField(String name)
Answers a Field object describing the field in the receiver
named by the argument which must be visible from the current
execution context.
|
Field[] |
getFields()
Answers an array containing Field objects describing
all fields which are visible from the current execution
context.
|
Type[] |
getGenericInterfaces()
Answers an array of Type for the Class objects which match the
interfaces specified in the receiver classes
implements
declaration. |
Type |
getGenericSuperclass()
Answers the Type for the Class which represents the receiver's
superclass.
|
Class<?>[] |
getInterfaces()
Answers an array of Class objects which match the interfaces
specified in the receiver classes
implements
declaration |
Method |
getMethod(String name,
Class<?>... parameterTypes)
Answers a Method object which represents the method
described by the arguments.
|
Method[] |
getMethods()
Answers an array containing Method objects describing
all methods which are visible from the current execution
context.
|
int |
getModifiers()
Answers an integer which is the receiver's modifiers.
|
String |
getName()
Answers the name of the class which the receiver represents.
|
Package |
getPackage()
Returns the Package of which this class is a member.
|
ProtectionDomain |
getProtectionDomain()
Answers the ProtectionDomain of the receiver.
|
URL |
getResource(String resName)
Answers a URL referring to the
resource specified by resName.
|
InputStream |
getResourceAsStream(String resName)
Answers a read-only stream on the contents of the
resource specified by resName.
|
Object[] |
getSigners()
Answers the signers for the class represented by the
receiver, or null if there are no signers.
|
String |
getSimpleName()
Return the simple name of this Class.
|
Class<? super T> |
getSuperclass()
Answers the Class which represents the receiver's
superclass.
|
String |
getTypeName()
Answers the type name of the class which the receiver represents.
|
TypeVariable<Class<T>>[] |
getTypeParameters()
Answers an array of TypeVariable for the generic parameters declared
on this Class.
|
boolean |
isAnnotation()
Answer if this class is an Annotation.
|
boolean |
isAnnotationPresent(Class<? extends Annotation> annotation)
Answer if the specified Annotation exists for this Class.
|
boolean |
isAnonymousClass()
Answer if this Class is anonymous.
|
boolean |
isArray()
Answers true if the receiver represents an array class.
|
boolean |
isAssignableFrom(Class<?> cls)
Answers true if the type represented by the argument
can be converted via an identity conversion or a widening
reference conversion (i.e.
|
boolean |
isEnum()
Answer if this Class is an enum.
|
boolean |
isInstance(Object object)
Answers true if the argument is non-null and can be
cast to the type of the receiver.
|
boolean |
isInterface()
Answers true if the receiver represents an interface.
|
boolean |
isLocalClass()
Answer if this Class is local.
|
boolean |
isMemberClass()
Answer if this Class is a member Class.
|
boolean |
isPrimitive()
Answers true if the receiver represents a base type.
|
boolean |
isSynthetic()
Answer if this Class is synthetic.
|
T |
newInstance()
Answers a new instance of the class represented by the
receiver, created by invoking the default (i.e.
|
String |
toGenericString()
Returns a formatted string describing this Class.
|
String |
toString()
Answers a string containing a concise, human-readable
description of the receiver.
|
public static Class<?> forName(String className) throws ClassNotFoundException
className
- The name of the non-base type class to findClassNotFoundException
- If the class could not be foundClass
public static Class<?> forName(String className, boolean initializeBoolean, ClassLoader classLoader) throws ClassNotFoundException
className
- The name of the non-base type class to findinitializeBoolean
- A boolean indicating whether the class should be
initializedclassLoader
- The classloader to use to load the classClassNotFoundException
- If the class could not be foundClass
public Class<?>[] getClasses()
SecurityException
- If member access is not allowedClass
public ClassLoader getClassLoader()
ClassLoader
public Class<?> getComponentType()
Class
public Constructor<T> getConstructor(Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException
parameterTypes
- the types of the arguments.NoSuchMethodException
- if the constructor could not be found.SecurityException
- if member access is not allowedgetConstructors()
public Constructor<?>[] getConstructors() throws SecurityException
SecurityException
- if member access is not allowedgetMethods()
public Class<?>[] getDeclaredClasses() throws SecurityException
SecurityException
- if member access is not allowedClass
public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException
parameterTypes
- the types of the arguments.NoSuchMethodException
- if the constructor could not be found.SecurityException
- if member access is not allowedgetConstructors()
public Constructor<?>[] getDeclaredConstructors() throws SecurityException
SecurityException
- if member access is not allowedgetMethods()
public Field getDeclaredField(String name) throws NoSuchFieldException, SecurityException
name
- The name of the field to look for.NoSuchFieldException
- if the requested field could not be foundSecurityException
- if member access is not allowedgetDeclaredFields()
public Field[] getDeclaredFields() throws SecurityException
SecurityException
- If member access is not allowedgetFields()
public Method getDeclaredMethod(String name, Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException
name
- the name of the methodparameterTypes
- the types of the arguments.NoSuchMethodException
- if the method could not be found.SecurityException
- If member access is not allowedgetMethods()
public Method[] getDeclaredMethods() throws SecurityException
SecurityException
- if member access is not allowedgetMethods()
public Class<?> getDeclaringClass()
public Field getField(String name) throws NoSuchFieldException, SecurityException
name
- The name of the field to look for.NoSuchFieldException
- If the given field does not existSecurityException
- If access is deniedgetDeclaredFields()
public Field[] getFields() throws SecurityException
SecurityException
- If member access is not allowedgetDeclaredFields()
public Class<?>[] getInterfaces()
implements
declarationClass<?>[]
the interfaces the receiver claims to implement.public Method getMethod(String name, Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException
name
- String
the name of the methodparameterTypes
- Class<?>[]
the types of the arguments.NoSuchMethodException
- if the method could not be found.SecurityException
- if member access is not allowedgetMethods()
public Method[] getMethods() throws SecurityException
SecurityException
- if member access is not allowedgetDeclaredMethods()
public int getModifiers()
public String getName()
Class
public ProtectionDomain getProtectionDomain()
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.
Class
public URL getResource(String resName)
resName
- the name of the resource.ClassLoader
public InputStream getResourceAsStream(String resName)
resName
- the name of the resource.ClassLoader
public Object[] getSigners()
getMethods()
public Class<? super T> getSuperclass()
public boolean isArray()
true
if the receiver represents an array class
false
if it does not represent an array classpublic boolean isAssignableFrom(Class<?> cls)
cls
- Class
the class to testtrue
the argument can be assigned into the receiver
false
the argument cannot be assigned into the receiverNullPointerException
- if the parameter is nullpublic boolean isInstance(Object object)
instanceof
operator.object
- Object
the object to testtrue
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 receiverpublic boolean isInterface()
true
if the receiver represents an interface
false
if it does not represent an interfacepublic boolean isPrimitive()
true
if the receiver represents a base type
false
if it does not represent a base typepublic T newInstance() throws IllegalAccessException, InstantiationException
IllegalAccessException
- if the constructor is not visible to the sender.InstantiationException
- if the instance could not be created.public String toString()
public String toGenericString()
class
, enum
, interface
,
@interface
, or
the empty string for primitive types. The type parameter list is
omitted if there are no type parameters.public Package getPackage()
public boolean desiredAssertionStatus()
public <A extends Annotation> A getAnnotation(Class<A> annotation)
getAnnotation
in interface AnnotatedElement
A
- the type of the annotation to query for and return if presentannotation
- the Annotation typepublic Annotation[] getAnnotations()
getAnnotations
in interface AnnotatedElement
public <A extends Annotation> A getDeclaredAnnotation(Class<A> annotation)
getDeclaredAnnotation
in interface AnnotatedElement
A
- the type of the annotation to query for and return if directly presentannotation
- the Annotation to search forpublic AnnotatedType[] getAnnotatedInterfaces()
public AnnotatedType getAnnotatedSuperclass()
public String getTypeName()
getTypeName
in interface Type
public Annotation[] getDeclaredAnnotations()
getDeclaredAnnotations
in interface AnnotatedElement
public <A extends Annotation> A[] getDeclaredAnnotationsByType(Class<A> annotationClass)
An annotation which can be used more than once for the same class declaration. Repeatable annotations are annotated with Repeatable annotation which tells the container annotation for this repeatable annotation.
Example
@interface ContainerAnnotation {RepeatableAnn[] value();}
@Repeatable(ContainerAnnotation.class)
Container Annotation:
Container annotation stores the repeated annotations in its array-valued element. Using repeatable annotations more than once makes them stored in their container annotation. In this case, container annotation is visible directly on class declaration, but not the repeated annotations.
Repeated Annotation:A repeatable annotation which is used more than once for the same class.
Directly Declared Annotation :All non repeatable annotations are directly declared annotations. As for repeatable annotations, they can be directly declared annotation if and only if they are used once. Repeated annotations are not directly declared in class declaration, but their container annotation does.
-------------------------------------------------------------------------------------------------------If the specified type is not repeatable annotation, then returned array size will be 0 or 1. If specified type is repeatable annotation, then all the annotations of that type will be returned. Array size might be 0, 1 or more.
It does not search through super classes.getDeclaredAnnotationsByType
in interface AnnotatedElement
A
- the type of the annotation to query for and return
if directly or indirectly presentannotationClass
- the annotation type to search forpublic <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationClass)
getAnnotationsByType
in interface AnnotatedElement
A
- the type of the annotation to query for and return if presentannotationClass
- the annotation type to search forpublic boolean isAnnotation()
public boolean isAnnotationPresent(Class<? extends Annotation> annotation)
isAnnotationPresent
in interface AnnotatedElement
annotation
- the Annotation typepublic <U> Class<? extends U> asSubclass(Class<U> cls)
U
- the type for casting tocls
- the Class to cast toClassCastException
- if this Class is not the same or a subclass
of the specified Classpublic T cast(Object object)
object
- the object to castClassCastException
- if the specified object cannot be cast
to this Classpublic boolean isEnum()
public T[] getEnumConstants()
public boolean isSynthetic()
public TypeVariable<Class<T>>[] getTypeParameters()
getTypeParameters
in interface GenericDeclaration
public Type[] getGenericInterfaces()
implements
declaration.public Type getGenericSuperclass()
public Constructor<?> getEnclosingConstructor() throws SecurityException
SecurityException
- if declared member access or package access is not allowedisAnonymousClass()
,
isLocalClass()
public Method getEnclosingMethod() throws SecurityException
SecurityException
- if declared member access or package access is not allowedisAnonymousClass()
,
isLocalClass()
public Class<?> getEnclosingClass() throws SecurityException
SecurityException
- if package access is not allowedgetDeclaringClass()
,
isAnonymousClass()
,
isLocalClass()
,
isMemberClass()
public String getSimpleName()
isAnonymousClass()
public String getCanonicalName()
isAnonymousClass()
,
isLocalClass()
public boolean isAnonymousClass()
isLocalClass()
public boolean isLocalClass()
isAnonymousClass()
public boolean isMemberClass()
isLocalClass()
Eclipse OpenJ9 website.
To raise a bug report or suggest an improvement create an Eclipse OpenJ9 issue.
Copyright © 1993, 2025 IBM Corp. and others.