java.lang.reflect - Quick Guide

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.
Following is the declaration for java.lang.reflect.AccessibleObject class −
public class AccessibleObject
   extends Object
      implements AnnotatedElement

Constructor: only used by the Java Virtual Machine.
Returns this element's annotation for the specified type if such an annotation is present, else null.
Returns all annotations present on this element.
Returns all annotations that are directly present on this element.
Get the value of the accessible flag for this object.
Returns true if an annotation for the specified type is present on this element, else false.
Convenience method to set the accessible flag for an array of objects with a single security check (for efficiency).
Set the accessible flag for this object to the indicated boolean value.
This class inherits methods from the following classes −
java.lang.Object
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.
Following is the declaration for java.lang.reflect.Array class −
public final class Array
   extends Object

Returns the value of the indexed component in the specified array object.
Returns the value of the indexed component in the specified array object, as a boolean.
Returns the value of the indexed component in the specified array object, as a byte.
Returns the value of the indexed component in the specified array object, as a char.
Returns the value of the indexed component in the specified array object, as a double.
Returns the value of the indexed component in the specified array object, as a float.
Returns the value of the indexed component in the specified array object, as an int.
Returns the length of the specified array object, as an int.
Returns the value of the indexed component in the specified array object, as a long.
Returns the value of the indexed component in the specified array object, as a short.
Creates a new array with the specified component type and dimensions.
Creates a new array with the specified component type and length.
Sets the value of the indexed component of the specified array object to the specified new value.
Sets the value of the indexed component of the specified array object to the specified boolean value.
Sets the value of the indexed component of the specified array object to the specified byte value.
Sets the value of the indexed component of the specified array object to the specified char value.
Sets the value of the indexed component of the specified array object to the specified double value.
Sets the value of the indexed component of the specified array object to the specified float value.
Sets the value of the indexed component of the specified array object to the specified int value.
Sets the value of the indexed component of the specified array object to the specified long value.
Sets the value of the indexed component of the specified array object to the specified short value.
This class inherits methods from the following classes −
java.lang.Object
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.
Following is the declaration for java.lang.reflect.Constructor class −
public final class Constructor<T>
   extends AccessibleObject
      implements GenericDeclaration, Member

Compares this Constructor against the specified object.
Returns this element's annotation for the specified type if such an annotation is present, else null.
Returns all annotations that are directly present on this element.
Returns the Class object representing the class that declares the constructor represented by this Constructor object.
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.
Returns an array of Type objects that represent the exceptions declared to be thrown by this Constructor object.
Returns an array of Type objects that represent the formal parameter types, in declaration order, of the method represented by this Constructor object.
Returns the Java language modifiers for the constructor represented by this Constructor object, as an integer.
Returns the name of this constructor, as a string.
Returns an array of arrays that represent the annotations on the formal parameters, in declaration order, of the method represented by this Constructor object.
Returns an array of Class objects that represent the formal parameter types, in declaration order, of the constructor represented by this Constructor object.
Returns a hashcode for this Constructor.
Returns true if this constructor is a synthetic constructor; returns false otherwise.
Returns true if this constructor was declared to take a variable number of arguments; returns false otherwise.
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.
Returns a string describing this Constructor, including type parameters.
Returns a string describing this Constructor.
This class inherits methods from the following classes −
java.lang.reflect.AccessibleObject
java.lang.Object
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.
Following is the declaration for java.lang.reflect.Field class −
public final class Field
   extends AccessibleObject
      implements Member

Compares this Field against the specified object.
Returns the value of the field represented by this Field, on the specified object.
Returns this element's annotation for the specified type if such an annotation is present, else null.
Gets the value of a static or instance boolean field.
Gets the value of a static or instance byte field.
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.
Returns all annotations that are directly present on this element.
Returns the Class object representing the class or interface that declares the field represented by this Field object.
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.
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.
Returns a Type object that represents the declared type for the field represented by this Field object.
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.
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.
Returns the Java language modifiers for the field represented by this Field object, as an integer.
RReturns the name of the field represented by this Field object.
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.
Returns a Class object that identifies the declared type for the field represented by this Field object.
Returns a hashcode for this Field.
Returns true if this field represents an element of an enumerated type; returns false otherwise.
This method returns true if the field represented by the current object is synthetic, else it returns false.
Sets the value of a field as a boolean on the specified object.
Sets the value of a field as a byte on the specified object.
Sets the value of a field as a char on the specified object.
Sets the value of a field as a double on the specified object.
Sets the value of a field as a float on the specified object.
Sets the value of a field as an int on the specified object.
Sets the value of a field as a long on the specified object.
Sets the value of a field as a short on the specified object.
Returns a string describing this Field, including its generic type.
Returns a string describing this Field.
This class inherits methods from the following classes −
java.lang.reflect.AccessibleObject
java.lang.Object
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.
Following is the declaration for java.lang.reflect.Method class −
public final class Method<T>
   extends AccessibleObject
      implements GenericDeclaration, Member

Compares this Method against the specified object.
Returns this element's annotation for the specified type if such an annotation is present, else null.
Returns all annotations that are directly present on this element.
Returns the Class object representing the class that declares the method represented by this Method object.
Returns the default value for the annotation member represented by this Method instance.
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.
Returns an array of Type objects that represent the exceptions declared to be thrown by this Constructor object.
Returns an array of Type objects that represent the formal parameter types, in declaration order, of the method represented by this Constructor object.
Returns a Type object that represents the formal return type of the method represented by this Method object.
Returns the Java language modifiers for the method represented by this Method object, as an integer.
Returns the name of this method, as a string.
Returns an array of arrays that represent the annotations on the formal parameters, in declaration order, of the method represented by this Method object.
Returns an array of Class objects that represent the formal parameter types, in declaration order, of the constructor represented by this Method object.
Returns a Class object that represents the formal return type of the method represented by this Method object.
Returns a hashcode for this Constructor.
Invokes the underlying method represented by this Method object, on the specified object with the specified parameters.
Returns true if this method is a bridge method; returns false otherwise.
Returns true if this method is a synthetic method; returns false otherwise.
Returns true if this method was declared to take a variable number of arguments; returns false otherwise.
Returns a string describing this Method, including type parameters.
Returns a string describing this Method.
This class inherits methods from the following classes −
java.lang.reflect.AccessibleObject
java.lang.Object
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.
Following is the declaration for java.lang.reflect.Modifier class −
public class Modifier
   extends Object

Following are the fields for java.lang.reflect.Modifier class −
static int ABSTRACT − The int value representing the abstract modifier.
static int ABSTRACT − The int value representing the abstract modifier.
static int FINAL − The int value representing the final modifier.
static int FINAL − The int value representing the final modifier.
static int INTERFACE − The int value representing the interface modifier.
static int INTERFACE − The int value representing the interface modifier.
static int NATIVE − The int value representing the native modifier.
static int NATIVE − The int value representing the native modifier.
static int PRIVATE − The int value representing the private modifier.
static int PRIVATE − The int value representing the private modifier.
static int PROTECTED − The int value representing the protected modifier.
static int PROTECTED − The int value representing the protected modifier.
static int PUBLIC − The int value representing the public modifier.
static int PUBLIC − The int value representing the public modifier.
static int STATIC − The int value representing the static modifier.
static int STATIC − The int value representing the static modifier.
static int STRICT − The int value representing the strictfp modifier.
static int STRICT − The int value representing the strictfp modifier.
static int SYNCHRONIZED − The int value representing the synchronized modifier.
static int SYNCHRONIZED − The int value representing the synchronized modifier.
static int TRANSIENT − The int value representing the transient modifier.
static int TRANSIENT − The int value representing the transient modifier.
static int VOLATILE − The int value representing the volatile modifier.
static int VOLATILE − The int value representing the volatile modifier.
Default Constructor.
Return an int value OR-ing together the source language modifiers that can be applied to a class.
Return an int value OR-ing together the source language modifiers that can be applied to a constructor.
Return an int value OR-ing together the source language modifiers that can be applied to a field.
Return an int value OR-ing together the source language modifiers that can be applied to an interface.
Return true if the integer argument includes the abstract modifier, false otherwise.
Return true if the integer argument includes the final modifier, false otherwise.
Return true if the integer argument includes the interface modifier, false otherwise.
Return true if the integer argument includes the native modifier, false otherwise.
Return true if the integer argument includes the private modifier, false otherwise.
Return true if the integer argument includes the protected modifier, false otherwise.
Return true if the integer argument includes the public modifier, false otherwise.
Return true if the integer argument includes the static modifier, false otherwise.
Return true if the integer argument includes the strictfp modifier, false otherwise.
Return true if the integer argument includes the synchronized modifier, false otherwise.
Return true if the integer argument includes the transient modifier, false otherwise.
Return true if the integer argument includes the volatile modifier, false otherwise.
Return an int value OR-ing together the source language modifiers that can be applied to a method.
Return a string describing the access modifier flags in the specified modifier.
This class inherits methods from the following classes −
java.lang.Object
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.
Following is the declaration for java.lang.reflect.Proxy class −
public class Proxy
   extends Object
      implements Serializable

Following are the fields for java.lang.reflect.Proxy class −
protected InvocationHandler h − the invocation handler for this proxy instance.
protected InvocationHandler h − the invocation handler for this proxy instance.
Constructs a new Proxy instance from a subclass (typically, a dynamic proxy class) with the specified value for its invocation handler.
Returns the invocation handler for the specified proxy instance.
Returns the java.lang.Class object for a proxy class given a class loader and an array of interfaces.
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.
Returns an instance of a proxy class for the specified interfaces that dispatches method invocations to the specified invocation handler.
This class inherits methods from the following classes −
java.lang.Object
The java.lang.reflect Interfaces contains the interfaces which are used to obtain reflective information about classes and objects.
AnnotatedElement
Represents an annotated element of the program currently running in this VM.
GenericArrayType
GenericArrayType represents an array type whose component type is either a parameterized type or a type variable.
GenericDeclaration
A common interface for all entities that declare type variables.
InvocationHandler
InvocationHandler is the interface implemented by the invocation handler of a proxy instance.
Member
Member is an interface that reflects identifying information about a single member (a field or a method) or a constructor.
ParameterizedType
ParameterizedType represents a parameterized type such as Collection<String>.
Type
Type is the common superinterface for all types in the Java programming language.
List<E>
This is an ordered collection (also known as a sequence).
TypeVariable<D extends GenericDeclaration>
TypeVariable is the common superinterface for type variables of kinds.
WildcardType
WildcardType represents a wildcard type expression, such as ?, ? extends Number, or ? super Integer.
The java.lang.reflect Exceptions contains the exceptions which can occur during reflection operations.
InvocationTargetException
InvocationTargetException is a checked exception that wraps an exception thrown by an invoked method or constructor.
MalformedParameterizedTypeException
Thrown when a semantically malformed parameterized type is encountered by a reflective method that needs to instantiate it.
UndeclaredThrowableException
Thrown 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.

The java.lang.reflect Error contains the error which can occur during reflection operations.
GenericSignatureFormatError
Thrown 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.
 Print
 Add Notes
 Bookmark this page