Module java.base
Package java.lang

Class Class<T>

  • All Implemented Interfaces:
    Serializable, AnnotatedElement, GenericDeclaration, Type

    public final class Class<T>
    extends Object
    implements Serializable, GenericDeclaration, Type
    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 ofP 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.
    See Also:
    Serialized Form
    • Method Detail

      • forName

        public static Class<?> forName​(String className)
                                throws ClassNotFoundException
        Answers 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 java.lang.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
        Returns:
        the named Class
        Throws:
        ClassNotFoundException - If the class could not be found
        See Also:
        Class
      • forName

        public static Class<?> forName​(String className,
                                       boolean initializeBoolean,
                                       ClassLoader classLoader)
                                throws ClassNotFoundException
        Answers 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 java.lang.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 classloader to use to load the class
        Returns:
        the named class.
        Throws:
        ClassNotFoundException - If the class could not be found
        See Also:
        Class
      • forName

        public static Class<?> forName​(Module module,
                                       String name)
        Answers a Class object which represents the class with the given name in the given module. The name should be the name of a class as described in the class definition of java.lang.Class, however Classes representing base types can not be found using this method. It does not invoke the class initializer. Note that this method does not check whether the requested class is accessible to its caller. Security rules will be obeyed.
        Parameters:
        module - The name of the module
        name - The name of the non-base type class to find
        Returns:
        The Class object representing the named class
        See Also:
        Class
      • getClasses

        public Class<?>[] getClasses()
        Answers an array containing all public class members of the class which the receiver represents and its superclasses and interfaces
        Returns:
        the class' public class members
        Throws:
        SecurityException - If member access is not allowed
        See Also:
        Class
      • getClassLoader

        public ClassLoader getClassLoader()
        Answers the classloader which was used to load the class represented by the receiver. Answer null if the class was loaded by the system class loader.
        Returns:
        the receiver's class loader or nil
        See Also:
        ClassLoader
      • getComponentType

        public Class<?> getComponentType()
        Answers a Class object which represents the receiver's component type if the receiver represents an array type. Otherwise answers nil. The component type of an array type is the type of the elements of the array.
        Returns:
        the component type of the receiver.
        See Also:
        Class
      • getConstructors

        public Constructor<?>[] getConstructors()
                                         throws SecurityException
        Answers an array containing Constructor objects describing all constructors which are visible from the current execution context.
        Returns:
        all visible constructors starting from the receiver.
        Throws:
        SecurityException - if member access is not allowed
        See Also:
        getMethods()
      • getDeclaredClasses

        public Class<?>[] getDeclaredClasses()
                                      throws SecurityException
        Answers 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.
        Returns:
        the class' class members
        Throws:
        SecurityException - if member access is not allowed
        See Also:
        Class
      • getDeclaredConstructors

        public Constructor<?>[] getDeclaredConstructors()
                                                 throws SecurityException
        Answers 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.
        Throws:
        SecurityException - if member access is not allowed
        See Also:
        getMethods()
      • getDeclaredField

        public Field getDeclaredField​(String name)
                               throws NoSuchFieldException,
                                      SecurityException
        Answers 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.
        Throws:
        NoSuchFieldException - if the requested field could not be found
        SecurityException - if member access is not allowed
        See Also:
        getDeclaredFields()
      • getDeclaredFields

        public Field[] getDeclaredFields()
                                  throws SecurityException
        Answers 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.
        Returns:
        the receiver's fields.
        Throws:
        SecurityException - If member access is not allowed
        See Also:
        getFields()
      • getDeclaredMethod

        public Method getDeclaredMethod​(String name,
                                        Class<?>... parameterTypes)
                                 throws NoSuchMethodException,
                                        SecurityException
        Answers 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.
        Throws:
        NoSuchMethodException - if the method could not be found.
        SecurityException - If member access is not allowed
        See Also:
        getMethods()
      • getDeclaredMethods

        public Method[] getDeclaredMethods()
                                    throws SecurityException
        Answers 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.
        Returns:
        the receiver's methods.
        Throws:
        SecurityException - if member access is not allowed
        See Also:
        getMethods()
      • getDeclaringClass

        public Class<?> getDeclaringClass()
        Answers the class which declared the class represented by the receiver. This will return null if the receiver is not a member of another class.
        Returns:
        the declaring class of the receiver.
      • getFields

        public Field[] getFields()
                          throws SecurityException
        Answers an array containing Field objects describing all fields which are visible from the current execution context.
        Returns:
        all visible fields starting from the receiver.
        Throws:
        SecurityException - If member access is not allowed
        See Also:
        getDeclaredFields()
      • getInterfaces

        public Class<?>[] getInterfaces()
        Answers an array of Class objects which match the interfaces specified in the receiver classes implements declaration
        Returns:
        Class<?>[] the interfaces the receiver claims to implement.
      • getMethods

        public Method[] getMethods()
                            throws SecurityException
        Answers 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.
        Throws:
        SecurityException - if member access is not allowed
        See Also:
        getDeclaredMethods()
      • getModifiers

        public int getModifiers()
        Answers an integer which is the receiver's modifiers. Note that the constants which describe the bits which are returned are implemented in class java.lang.reflect.Modifier which may not be available on the target.
        Returns:
        the receiver's modifiers
      • getModule

        public Module getModule()
        Answers the module to which the receiver belongs. If this class doesn't belong to a named module, the unnamedModule of the classloader loaded this class is returned; If this class represents an array type, the module for the element type is returned; If this class represents a primitive type or void, module java.base is returned.
        Returns:
        the module to which the receiver belongs
      • getName

        public String getName()
        Answers the name of the class which the receiver represents. For a description of the format which is used, see the class definition of java.lang.Class.
        Returns:
        the receiver's name.
        See Also:
        Class
      • getProtectionDomain

        public ProtectionDomain getProtectionDomain()
        Answers 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.
        See Also:
        Class
      • getPackageName

        public String getPackageName()
        Answers the name of the package to which the receiver belongs. For example, Object.class.getPackageName() returns "java.lang". Returns "java.lang" if this class represents a primitive type or void, and the element type's package name in the case of an array type.
        Returns:
        String the receiver's package name
        See Also:
        getPackage()
      • getResource

        public URL getResource​(String resName)
        Answers a URL referring to the resource specified by resName. The mapping between the resource name and the URL is managed by the class's class loader.
        Parameters:
        resName - the name of the resource.
        Returns:
        a stream on the resource.
        See Also:
        ClassLoader
      • getResourceAsStream

        public InputStream getResourceAsStream​(String resName)
        Answers 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's class loader.
        Parameters:
        resName - the name of the resource.
        Returns:
        a stream on the resource.
        See Also:
        ClassLoader
      • getSigners

        public Object[] getSigners()
        Answers the signers for the class represented by the receiver, or null if there are no signers.
        Returns:
        the signers of the receiver.
        See Also:
        getMethods()
      • getSuperclass

        public Class<? super T> getSuperclass()
        Answers the Class which represents the receiver's superclass. For Classes which represent base types, interfaces, and for java.lang.Object the method answers null.
        Returns:
        the receiver's superclass.
      • isArray

        public boolean isArray()
        Answers 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
      • isAssignableFrom

        public 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. 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
        Throws:
        NullPointerException - if the parameter is null
      • isInstance

        public boolean isInstance​(Object object)
        Answers 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
      • isInterface

        public boolean isInterface()
        Answers true if the receiver represents an interface.
        Returns:
        true if the receiver represents an interface false if it does not represent an interface
      • isPrimitive

        public boolean isPrimitive()
        Answers 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
      • newInstance

        @Deprecated(forRemoval=false,
                    since="9")
        public T newInstance()
                      throws IllegalAccessException,
                             InstantiationException
        Deprecated.
        Answers 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.
        Throws:
        IllegalAccessException - if the constructor is not visible to the sender.
        InstantiationException - if the instance could not be created.
      • toString

        public String toString()
        Answers a string containing a concise, human-readable description of the receiver.
        Overrides:
        toString in class Object
        Returns:
        a printable representation for the receiver.
      • toGenericString

        public String toGenericString()
        Returns a formatted string describing this Class. The string has the following format: modifier1 modifier2 ... kind name<typeparam1, typeparam2, ...>. kind is one of class, enum, interface, @interface, or the empty string for primitive types. The type parameter list is omitted if there are no type parameters. For array classes, the string has the following format instead: name<typeparam1, typeparam2, ...> followed by a number of [] pairs, one pair for each dimension of the array.
        Returns:
        a formatted string describing this class
        Since:
        1.8
      • getPackage

        public Package getPackage()
        Returns the Package of which this class is a member. A class has a Package iff it was loaded from a SecureClassLoader.
        Returns:
        Package the Package of which this class is a member or null in the case of primitive or array types
      • desiredAssertionStatus

        public boolean desiredAssertionStatus()
        Returns the assertion status for this class. Assertion is enabled/disabled based on classloader default, package or class default at runtime
        Returns:
        the assertion status for this class
        Since:
        1.4
      • getAnnotation

        public <A extends Annotation> A getAnnotation​(Class<A> annotation)
        Return the specified Annotation for this Class. Inherited Annotations are searched.
        Specified by:
        getAnnotation in interface AnnotatedElement
        Type Parameters:
        A - the type of the annotation to query for and return if present
        Parameters:
        annotation - the Annotation type
        Returns:
        the specified Annotation or null
        Since:
        1.5
      • getAnnotations

        public Annotation[] getAnnotations()
        Return the directly declared Annotations for this Class, including the Annotations inherited from superclasses. If an annotation type has been included before, then next occurrences will not be included. Repeated annotations are not included since they will be stored in their container annotation. But container annotations are included. (If a container annotation is repeatable and it is repeated, then these container annotations' container annotation is included. )
        Specified by:
        getAnnotations in interface AnnotatedElement
        Returns:
        an array of Annotation
        Since:
        1.5
      • getDeclaredAnnotation

        public <A extends Annotation> A getDeclaredAnnotation​(Class<A> annotation)
        Looks through directly declared annotations for this class, not including Annotations inherited from superclasses.
        Specified by:
        getDeclaredAnnotation in interface AnnotatedElement
        Type Parameters:
        A - the type of the annotation to query for and return if directly present
        Parameters:
        annotation - the Annotation to search for
        Returns:
        directly declared annotation of specified annotation type.
        Since:
        1.8
      • getAnnotatedInterfaces

        public AnnotatedType[] getAnnotatedInterfaces()
        Return the annotated types for the implemented interfaces.
        Returns:
        array, possibly empty, of AnnotatedTypes
      • getAnnotatedSuperclass

        public AnnotatedType getAnnotatedSuperclass()
        Return the annotated superclass of this class.
        Returns:
        null if this class is Object, an interface, a primitive type, or an array type. Otherwise return (possibly empty) AnnotatedType.
      • getTypeName

        public String getTypeName()
        Answers the type name of the class which the receiver represents.
        Specified by:
        getTypeName in interface Type
        Returns:
        the fully qualified type name, with brackets if an array class
        Since:
        1.8
      • getDeclaredAnnotations

        public Annotation[] getDeclaredAnnotations()
        Returns the annotations only for this Class, not including Annotations inherited from superclasses. It includes all the directly declared annotations. Repeated annotations are not included but their container annotation does.
        Specified by:
        getDeclaredAnnotations in interface AnnotatedElement
        Returns:
        an array of declared annotations
        Since:
        1.5
      • getDeclaredAnnotationsByType

        public <A extends Annotation> A[] getDeclaredAnnotationsByType​(Class<A> annotationClass)
        Gets the specified type annotations of this class.
        Terms used for annotations :

        Repeatable Annotation :

        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.
        Specified by:
        getDeclaredAnnotationsByType in interface AnnotatedElement
        Type Parameters:
        A - the type of the annotation to query for and return if directly or indirectly present
        Parameters:
        annotationClass - the annotation type to search for
        Returns:
        array of declared annotations in the specified annotation type
        Since:
        1.8
      • getAnnotationsByType

        public <A extends Annotation> A[] getAnnotationsByType​(Class<A> annotationClass)
        Gets the specified type annotations of this class. 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 searches through superclasses until it finds the inherited specified annotationClass.
        Specified by:
        getAnnotationsByType in interface AnnotatedElement
        Type Parameters:
        A - the type of the annotation to query for and return if present
        Parameters:
        annotationClass - the annotation type to search for
        Returns:
        array of declared annotations in the specified annotation type
        Since:
        1.8
      • isAnnotation

        public boolean isAnnotation()
        Answer if this class is an Annotation.
        Returns:
        true if this class is an Annotation
        Since:
        1.5
      • isAnnotationPresent

        public boolean isAnnotationPresent​(Class<? extends Annotation> annotation)
        Answer if the specified Annotation exists for this Class. Inherited Annotations are searched.
        Specified by:
        isAnnotationPresent in interface AnnotatedElement
        Parameters:
        annotation - the Annotation type
        Returns:
        true if the specified Annotation exists
        Since:
        1.5
      • asSubclass

        public <U> Class<? extends U> asSubclass​(Class<U> cls)
        Cast this Class to a subclass of the specified Class.
        Type Parameters:
        U - the type for casting to
        Parameters:
        cls - the Class to cast to
        Returns:
        this Class, cast to a subclass of the specified Class
        Throws:
        ClassCastException - if this Class is not the same or a subclass of the specified Class
        Since:
        1.5
      • cast

        public T cast​(Object object)
        Cast the specified object to this Class.
        Parameters:
        object - the object to cast
        Returns:
        the specified object, cast to this Class
        Throws:
        ClassCastException - if the specified object cannot be cast to this Class
        Since:
        1.5
      • isEnum

        public boolean isEnum()
        Answer if this Class is an enum.
        Returns:
        true if this Class is an enum
        Since:
        1.5
      • getEnumConstants

        public T[] getEnumConstants()
        Answer the array of enum constants for this Class. Returns null if this class is not an enum.
        Returns:
        the array of enum constants, or null
        Since:
        1.5
      • isSynthetic

        public boolean isSynthetic()
        Answer if this Class is synthetic. A synthetic Class is created by the compiler.
        Returns:
        true if this Class is synthetic.
        Since:
        1.5
      • getTypeParameters

        public TypeVariable<Class<T>>[] getTypeParameters()
        Answers an array of TypeVariable for the generic parameters declared on this Class.
        Specified by:
        getTypeParameters in interface GenericDeclaration
        Returns:
        the TypeVariable[] for the generic parameters
        Since:
        1.5
      • getGenericInterfaces

        public Type[] getGenericInterfaces()
        Answers an array of Type for the Class objects which match the interfaces specified in the receiver classes implements declaration.
        Returns:
        Type[] the interfaces the receiver claims to implement.
        Since:
        1.5
      • getGenericSuperclass

        public Type getGenericSuperclass()
        Answers the Type for the Class which represents the receiver's superclass. For classes which represent base types, interfaces, and for java.lang.Object the method answers null.
        Returns:
        the Type for the receiver's superclass.
        Since:
        1.5
      • getSimpleName

        public String getSimpleName()
        Return the simple name of this Class. The simple name does not include the package or the name of the enclosing class. The simple name of an anonymous class is "".
        Returns:
        the simple name
        Since:
        1.5
        See Also:
        isAnonymousClass()
      • getCanonicalName

        public String getCanonicalName()
        Return the canonical name of this Class. The canonical name is null for a local or anonymous class. The canonical name includes the package and the name of the enclosing class.
        Returns:
        the canonical name or null
        Since:
        1.5
        See Also:
        isAnonymousClass(), isLocalClass()
      • isAnonymousClass

        public boolean isAnonymousClass()
        Answer if this Class is anonymous. An unnamed Class defined inside a method.
        Returns:
        true if this Class is anonymous.
        Since:
        1.5
        See Also:
        isLocalClass()
      • isLocalClass

        public boolean isLocalClass()
        Answer if this Class is local. A named Class defined inside a method.
        Returns:
        true if this Class is local.
        Since:
        1.5
        See Also:
        isAnonymousClass()
      • isMemberClass

        public boolean isMemberClass()
        Answer if this Class is a member Class. A Class defined inside another Class.
        Returns:
        true if this Class is local.
        Since:
        1.5
        See Also:
        isLocalClass()
      • getNestHost

        public Class<?> getNestHost()
                             throws SecurityException
        Answers the host class of the receiver's nest.
        Returns:
        the host class of the receiver.
        Throws:
        SecurityException - if nestHost is not same as the current class, a security manager is present, the classloader of the caller is not the same or an ancestor of nestHost class, and checkPackageAccess() denies access
      • isNestmateOf

        public boolean isNestmateOf​(Class<?> that)
        Returns true if the class passed has the same nest top as this class.
        Parameters:
        that - The class to compare
        Returns:
        true if class is a nestmate of this class; false otherwise.
      • getNestMembers

        public Class<?>[] getNestMembers()
                                  throws LinkageError,
                                         SecurityException
        Answers the nest member classes of the receiver's nest host.
        Returns:
        the host class of the receiver.
        Throws:
        SecurityException - if a SecurityManager is present and package access is not allowed
        LinkageError - if there is any problem loading or validating a nest member or the nest host
        SecurityException - if a returned class is not the current class, a security manager is enabled, the caller's class loader is not the same or an ancestor of that returned class, and the checkPackageAccess() denies access