Category: 13. java.lang.reflect Package

https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSogp3MVc1QxQUIJt0ScGnunc6NPvFPNpdUzw&s

  • Error

    Introduction

    The java.lang.reflect Error contains the error which can occur during reflection operations.

    Errors Summary

    Sr.No.Error & Description
    1GenericSignatureFormatErrorThrown when a syntactically malformed signature attribute is encountered by a reflective method that needs to interpret the generic signature information for a type, method or constructor.
  • Exceptions

    Introduction

    The java.lang.reflect Exceptions contains the exceptions which can occur during reflection operations.

    Exceptions Summary

    Sr.No.Exception & Description
    1InvocationTargetExceptionInvocationTargetException is a checked exception that wraps an exception thrown by an invoked method or constructor.
    2MalformedParameterizedTypeExceptionThrown when a semantically malformed parameterized type is encountered by a reflective method that needs to instantiate it.
    3UndeclaredThrowableExceptionThrown by a method invocation on a proxy instance if its invocation handler’s invoke method throws a checked exception (a Throwable that is not assignable to RuntimeException or Error) that is not assignable to any of the exception types declared in the throws clause of the method that was invoked on the proxy instance and dispatched to the invocation handler.
  • Introduction

    The java.lang.reflect Interfaces contains the interfaces which are used to obtain reflective information about classes and objects.

    Interface Summary

    Sr.No.Interface & Description
    1AnnotatedElementRepresents an annotated element of the program currently running in this VM.
    2GenericArrayTypeGenericArrayType represents an array type whose component type is either a parameterized type or a type variable.
    3GenericDeclarationA common interface for all entities that declare type variables.
    4InvocationHandlerInvocationHandler is the interface implemented by the invocation handler of a proxy instance.
    5MemberMember is an interface that reflects identifying information about a single member (a field or a method) or a constructor.
    6ParameterizedTypeParameterizedType represents a parameterized type such as Collection<String>.
    7TypeType is the common superinterface for all types in the Java programming language.
    8List<E>This is an ordered collection (also known as a sequence).
    9TypeVariable<D extends GenericDeclaration>TypeVariable is the common superinterface for type variables of kinds.
    10WildcardTypeWildcardType represents a wildcard type expression, such as ?, ? extends Number, or ? super Integer.
  • Proxy 

    Introduction

    The java.lang.reflect.Proxy class provides static methods for creating dynamic proxy classes and instances, and it is also the superclass of all dynamic proxy classes created by those methods.

    Class declaration

    Following is the declaration for java.lang.reflect.Proxy class −

    public class Proxy
       extends Object
    
      implements Serializable

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Fields

    Following are the fields for java.lang.reflect.Proxy class −

    • protected InvocationHandler h − the invocation handler for this proxy instance.

    Constructors

    Sr.No.Constructor & Description
    1protected Proxy(InvocationHandler h)Constructs a new Proxy instance from a subclass (typically, a dynamic proxy class) with the specified value for its invocation handler.

    Class methods

    Sr.No.Method & Description
    1static InvocationHandler getInvocationHandler(Object proxy)Returns the invocation handler for the specified proxy instance.
    2static Class<?> getProxyClass(ClassLoader loader, Class<?>… interfaces)Returns the java.lang.Class object for a proxy class given a class loader and an array of interfaces.
    3static boolean isProxyClass(Class<?> cl)Returns true if and only if the specified class was dynamically generated to be a proxy class using the getProxyClass method or the newProxyInstance method.
    4static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h)Returns an instance of a proxy class for the specified interfaces that dispatches method invocations to the specified invocation handler.

    Methods inherited

    This class inherits methods from the following classes −

    • java.lang.Object
  • Modifier 

    Introduction

    The java.lang.reflect.Modifier class provides static methods and constants to decode class and member access modifiers. The sets of modifiers are represented as integers with distinct bit positions representing different modifiers. The values for the constants representing the modifiers are taken from the tables in sections 4.1, 4.4, 4.5, and 4.7 of The Java Virtual Machine Specification.

    Class declaration

    Following is the declaration for java.lang.reflect.Modifier class −

    public class Modifier
       extends Object
    

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Fields

    Following are the fields for java.lang.reflect.Modifier class −

    • static int ABSTRACT − The int value representing the abstract modifier.
    • static int FINAL − The int value representing the final modifier.
    • static int INTERFACE − The int value representing the interface modifier.
    • static int NATIVE − The int value representing the native modifier.
    • static int PRIVATE − The int value representing the private modifier.
    • static int PROTECTED − The int value representing the protected modifier.
    • static int PUBLIC − The int value representing the public modifier.
    • static int STATIC − The int value representing the static modifier.
    • static int STRICT − The int value representing the strictfp modifier.
    • static int SYNCHRONIZED − The int value representing the synchronized modifier.
    • static int TRANSIENT − The int value representing the transient modifier.
    • static int VOLATILE − The int value representing the volatile modifier.

    Constructors

    Sr.No.Constructor & Description
    1Modifier()Default Constructor.

    Class methods

    Sr.No.Method & Description
    1static int classModifiers()Return an int value OR-ing together the source language modifiers that can be applied to a class.
    2static int constructorModifiers()Return an int value OR-ing together the source language modifiers that can be applied to a constructor.
    3static int fieldModifiers()Return an int value OR-ing together the source language modifiers that can be applied to a field.
    4static int interfaceModifiers()Return an int value OR-ing together the source language modifiers that can be applied to an interface.
    5static boolean isAbstract(int mod)Return true if the integer argument includes the abstract modifier, false otherwise.
    6static boolean isFinal(int mod)Return true if the integer argument includes the final modifier, false otherwise.
    7static boolean isInterface(int mod)Return true if the integer argument includes the interface modifier, false otherwise.
    8static boolean isNative(int mod)Return true if the integer argument includes the native modifier, false otherwise.
    9static boolean isPrivate(int mod)Return true if the integer argument includes the private modifier, false otherwise.
    10static boolean isProtected(int mod)Return true if the integer argument includes the protected modifier, false otherwise.
    11static boolean isPublic(int mod)Return true if the integer argument includes the public modifier, false otherwise.
    12static boolean isStatic(int mod)Return true if the integer argument includes the static modifier, false otherwise.
    13static boolean isStrict(int mod)Return true if the integer argument includes the strictfp modifier, false otherwise.
    14static boolean isSynchronized(int mod)Return true if the integer argument includes the synchronized modifier, false otherwise.
    15static boolean isTransient(int mod)Return true if the integer argument includes the transient modifier, false otherwise.
    16static boolean isVolatile(int mod)Return true if the integer argument includes the volatile modifier, false otherwise.
    17static int methodModifiers()Return an int value OR-ing together the source language modifiers that can be applied to a method.
    18static String toString(int mod)Return a string describing the access modifier flags in the specified modifier.

    Methods inherited

    This class inherits methods from the following classes −

    • java.lang.Object
  • Method 

    Introduction

    The java.lang.reflect.Method class provides information about, and access to, a single method on a class or interface. The reflected method may be a class method or an instance method (including an abstract method). A Method permits widening conversions to occur when matching the actual parameters to invoke with the underlying method’s formal parameters, but it throws an IllegalArgumentException if a narrowing conversion would occur.

    Class declaration

    Following is the declaration for java.lang.reflect.Method class −

    public final class Method<T>
       extends AccessibleObject
    
      implements GenericDeclaration, Member

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Class methods

    Sr.No.Method & Description
    1boolean equals(Object obj)Compares this Method against the specified object.
    2<T extends Annotation> T getAnnotation(Class<T> annotationClass)Returns this element’s annotation for the specified type if such an annotation is present, else null.
    3Annotation[] getDeclaredAnnotations()Returns all annotations that are directly present on this element.
    4Class<T> getDeclaringClass()Returns the Class object representing the class that declares the method represented by this Method object.
    5Object getDefaultValue()Returns the default value for the annotation member represented by this Method instance.
    6Class<?>[] getExceptionTypes()Returns an array of Class objects that represent the types of exceptions declared to be thrown by the underlying constructor represented by this Constructor object.
    7Type[] getGenericExceptionTypes()Returns an array of Type objects that represent the exceptions declared to be thrown by this Constructor object.
    8Type[] getGenericParameterTypes()Returns an array of Type objects that represent the formal parameter types, in declaration order, of the method represented by this Constructor object.
    9Type getGenericReturnType()Returns a Type object that represents the formal return type of the method represented by this Method object.
    10int getModifiers()Returns the Java language modifiers for the method represented by this Method object, as an integer.
    11String getName()Returns the name of this method, as a string.
    12Annotation[][] getParameterAnnotations()Returns an array of arrays that represent the annotations on the formal parameters, in declaration order, of the method represented by this Method object.
    13Class<?>[] getParameterTypes()Returns an array of Class objects that represent the formal parameter types, in declaration order, of the constructor represented by this Method object.
    14Class<?> getReturnType()Returns a Class object that represents the formal return type of the method represented by this Method object.
    15int hashCode()Returns a hashcode for this Constructor.
    16Object invoke(Object obj, Object… args)Invokes the underlying method represented by this Method object, on the specified object with the specified parameters.
    17boolean isBridge()Returns true if this method is a bridge method; returns false otherwise.
    18boolean isSynthetic()Returns true if this method is a synthetic method; returns false otherwise.
    19boolean isVarArgs()Returns true if this method was declared to take a variable number of arguments; returns false otherwise.
    20String toGenericString()Returns a string describing this Method, including type parameters.
    21String toString()Returns a string describing this Method.

    Methods inherited

    This class inherits methods from the following classes −

    • java.lang.reflect.AccessibleObject
    • java.lang.Object
  • Field 

    Introduction

    The java.lang.reflect.Field class provides information about, and dynamic access to, a single field of a class or an interface. The reflected field may be a class (static) field or an instance field. A Field permits widening conversions to occur during a get or set access operation, but throws an IllegalArgumentException if a narrowing conversion would occur.

    Class declaration

    Following is the declaration for java.lang.reflect.Field class −

    public final class Field
       extends AccessibleObject
    
      implements Member

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Class methods

    Sr.No.Method & Description
    1boolean equals(Object obj)Compares this Field against the specified object.
    2Object get(Object obj)Returns the value of the field represented by this Field, on the specified object.
    3<T extends Annotation> T getAnnotation(Class<T> annotationClass)Returns this element’s annotation for the specified type if such an annotation is present, else null.
    4boolean getBoolean(Object obj)Gets the value of a static or instance boolean field.
    5byte getByte(Object obj)Gets the value of a static or instance byte field.
    6char getChar(Object obj)Gets the value of a static or instance field of type char or of another primitive type convertible to type char via a widening conversion.
    7Annotation[] getDeclaredAnnotations()Returns all annotations that are directly present on this element.
    8Class<?> getDeclaringClass()Returns the Class object representing the class or interface that declares the field represented by this Field object.
    9double getDouble(Object obj)Gets the value of a static or instance field of type double or of another primitive type convertible to type double via a widening conversion.
    10float getFloat(Object obj)Gets the value of a static or instance field of type float or of another primitive type convertible to type float via a widening conversion.
    11Type getGenericType()Returns a Type object that represents the declared type for the field represented by this Field object.
    12int getInt(Object obj)Gets the value of a static or instance field of type int or of another primitive type convertible to type int via a widening conversion.
    13long getLong(Object obj)Gets the value of a static or instance field of type long or of another primitive type convertible to type long via a widening conversion.
    14int getModifiers()Returns the Java language modifiers for the field represented by this Field object, as an integer.
    15String getName()RReturns the name of the field represented by this Field object.
    16short getShort(Object obj)Gets the value of a static or instance field of type short or of another primitive type convertible to type short via a widening conversion.
    17Class<?> getType()Returns a Class object that identifies the declared type for the field represented by this Field object.
    18int hashCode()Returns a hashcode for this Field.
    19boolean isEnumConstant()Returns true if this field represents an element of an enumerated type; returns false otherwise.
    20boolean isSynthetic()This method returns true if the field represented by the current object is synthetic, else it returns false.
    21void setBoolean(Object obj, boolean z)Sets the value of a field as a boolean on the specified object.
    22void setByte(Object obj, byte b)Sets the value of a field as a byte on the specified object.
    23void setChar(Object obj, char c)Sets the value of a field as a char on the specified object.
    24void setDouble(Object obj, double d)Sets the value of a field as a double on the specified object.
    25void setFloat(Object obj, float f)Sets the value of a field as a float on the specified object.
    26void setInt(Object obj, int i)Sets the value of a field as an int on the specified object.
    27void setLong(Object obj, long l)Sets the value of a field as a long on the specified object.
    28void setShort(Object obj, short s)Sets the value of a field as a short on the specified object.
    29String toGenericString()Returns a string describing this Field, including its generic type.
    30String toString()Returns a string describing this Field.

    Methods inherited

    This class inherits methods from the following classes −

    • java.lang.reflect.AccessibleObject
    • java.lang.Object
  • Constructor

    Introduction

    The java.lang.reflect.Constructor class provides information about, and access to, a single constructor for a class. Constructor permits widening conversions to occur when matching the actual parameters to newInstance() with the underlying constructor’s formal parameters, but throws an IllegalArgumentException if a narrowing conversion would occur.

    Class declaration

    Following is the declaration for java.lang.reflect.Constructor class −

    public final class Constructor<T>
       extends AccessibleObject
    
      implements GenericDeclaration, Member

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Class methods

    Sr.No.Method & Description
    1boolean equals(Object obj)Compares this Constructor against the specified object.
    2<T extends Annotation> T getAnnotation(Class<T> annotationClass)Returns this element’s annotation for the specified type if such an annotation is present, else null.
    3Annotation[] getDeclaredAnnotations()Returns all annotations that are directly present on this element.
    4Class<T> getDeclaringClass()Returns the Class object representing the class that declares the constructor represented by this Constructor object.
    5Class<?>[] getExceptionTypes()Returns an array of Class objects that represent the types of exceptions declared to be thrown by the underlying constructor represented by this Constructor object.
    6Type[] getGenericExceptionTypes()Returns an array of Type objects that represent the exceptions declared to be thrown by this Constructor object.
    7Type[] getGenericParameterTypes()Returns an array of Type objects that represent the formal parameter types, in declaration order, of the method represented by this Constructor object.
    8int getModifiers()Returns the Java language modifiers for the constructor represented by this Constructor object, as an integer.
    9String getName()Returns the name of this constructor, as a string.
    10Annotation[][] getParameterAnnotations()Returns an array of arrays that represent the annotations on the formal parameters, in declaration order, of the method represented by this Constructor object.
    11Class<?>[] getParameterTypes()Returns an array of Class objects that represent the formal parameter types, in declaration order, of the constructor represented by this Constructor object.
    12int hashCode()Returns a hashcode for this Constructor.
    13boolean isSynthetic()Returns true if this constructor is a synthetic constructor; returns false otherwise.
    14boolean isVarArgs()Returns true if this constructor was declared to take a variable number of arguments; returns false otherwise.
    15T newInstance(Object… initargs)Uses the constructor represented by this Constructor object to create and initialize a new instance of the constructor’s declaring class, with the specified initialization parameters.
    16String toGenericString()Returns a string describing this Constructor, including type parameters.
    17String toString()Returns a string describing this Constructor.

    Methods inherited

    This class inherits methods from the following classes −

    • java.lang.reflect.AccessibleObject
    • java.lang.Object
  • reflect – Array

    Introduction

    The java.lang.reflect.Array class provides static methods to dynamically create and access Java arrays. Array permits widening conversions to occur during a get or set operation, but throws an IllegalArgumentException if a narrowing conversion would occur.

    Class declaration

    Following is the declaration for java.lang.reflect.Array class −

    public final class Array
       extends Object
    

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Class methods

    Sr.No.Method & Description
    1static Object get(Object array, int index)Returns the value of the indexed component in the specified array object.
    2static boolean getBoolean(Object array, int index)Returns the value of the indexed component in the specified array object, as a boolean.
    3static byte getByte(Object array, int index)Returns the value of the indexed component in the specified array object, as a byte.
    4static char getChar(Object array, int index)Returns the value of the indexed component in the specified array object, as a char.
    5static double getDouble(Object array, int index)Returns the value of the indexed component in the specified array object, as a double.
    6static float getFloat(Object array, int index)Returns the value of the indexed component in the specified array object, as a float.
    7static int getInt(Object array, int index)Returns the value of the indexed component in the specified array object, as an int.
    8static int getLength(Object array)Returns the length of the specified array object, as an int.
    9static long getLong(Object array, int index)Returns the value of the indexed component in the specified array object, as a long.
    10static short getShort(Object array, int index)Returns the value of the indexed component in the specified array object, as a short.
    11static Object newInstance(Class<?> componentType, int… dimensions)Creates a new array with the specified component type and dimensions.
    12static Object newInstance(Class<?> componentType, int length)Creates a new array with the specified component type and length.
    13static void set(Object array, int index, Object value)Sets the value of the indexed component of the specified array object to the specified new value.
    14static void setBoolean(Object array, int index, boolean z)Sets the value of the indexed component of the specified array object to the specified boolean value.
    15static void setByte(Object array, int index, byte b)Sets the value of the indexed component of the specified array object to the specified byte value.
    16static void setChar(Object array, int index, char c)Sets the value of the indexed component of the specified array object to the specified char value.
    17static void setDouble(Object array, int index, double d)Sets the value of the indexed component of the specified array object to the specified double value.
    18static void setFloat(Object array, int index, float f)Sets the value of the indexed component of the specified array object to the specified float value.
    19static void setInt(Object array, int index, int i)Sets the value of the indexed component of the specified array object to the specified int value.
    20static void setLong(Object array, int index, long l)Sets the value of the indexed component of the specified array object to the specified long value.
    21static void setShort(Object array, int index, short s)Sets the value of the indexed component of the specified array object to the specified short value.

    Methods inherited

    This class inherits methods from the following classes −

    • java.lang.Object
  • AccessibleObject 

    Introduction

    The java.lang.reflect.AccessibleObject class is the base class for Field, Method and Constructor objects. It provides the ability to flag a reflected object as suppressing default Java language access control checks when it is used. The access checks for public, default (package) access, protected, and private members are performed when Fields, Methods or Constructors are used to set or get fields, to invoke methods, or to create and initialize new instances of classes, respectively. Setting the accessible flag in a reflected object permits sophisticated applications with sufficient privilege, such as Java Object Serialization or other persistence mechanisms, to manipulate objects in a manner that would normally be prohibited.

    Class declaration

    Following is the declaration for java.lang.reflect.AccessibleObject class −

    public class AccessibleObject
       extends Object
    
      implements AnnotatedElement

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Constructors

    Sr.No.Constructor & Description
    1protected AccessibleObject()Constructor: only used by the Java Virtual Machine.

    Class methods

    Sr.No.Method & Description
    1<T extends Annotation> T getAnnotation(Class<T> annotationClass)Returns this element’s annotation for the specified type if such an annotation is present, else null.
    2Annotation[] getAnnotations()Returns all annotations present on this element.
    3Annotation[] getDeclaredAnnotations()Returns all annotations that are directly present on this element.
    4boolean isAccessible()Get the value of the accessible flag for this object.
    5boolean isAnnotationPresent(Class<? extends Annotation> annotationClass)Returns true if an annotation for the specified type is present on this element, else false.
    6static void setAccessible(AccessibleObject[] array, boolean flag)Convenience method to set the accessible flag for an array of objects with a single security check (for efficiency).
    7void setAccessible(boolean flag)Set the accessible flag for this object to the indicated boolean value.

    Methods inherited

    This class inherits methods from the following classes −

    • java.lang.Object