Constructor Reflection

Constructor provides information about a single constructor for a class.

Get the annotation declared for this constructor

<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.
Annotation[] getDeclaredAnnotations()
Returns all annotations that are directly present on this element.

Get the declaring class

Class<T> getDeclaringClass()
Returns the Class object representing the class that declares the constructor represented by this Constructor object.

Get the generic exception types

Class<?>[] 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.
Type[] getGenericExceptionTypes()
Returns an array of Type objects that represent the exceptions declared to be thrown by this Constructor object.

Get the generic parameter types

Type[] getGenericParameterTypes()
Returns an array of Type objects that represent the formal parameter types, in declaration order, of the method represented by this Constructor object.

Get the modifiers

int getModifiers()
Returns the Java language modifiers for the constructor represented by this Constructor object, as an integer.

Get the name of the contructor

String getName()
Returns the name of this constructor, as a string.

Get parameter's annotation, type

Annotation[][] 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.
Class<?>[] getParameterTypes()
Returns an array of Class objects that represent the formal parameter types, in declaration order, of the constructor represented by this Constructor object.

Get typed parameters

TypeVariable<Constructor<T>>[] getTypeParameters()
Returns an array of TypeVariable objects that represent the type variables declared by the generic declaration represented by this GenericDeclaration object, in declaration order.

Is this contructor a Synthetic, and does it have the variable number of arguments

boolean isSynthetic()
Returns true if this constructor is a synthetic constructor; returns false otherwise.
boolean isVarArgs()
Returns true if this constructor was declared to take a variable number of arguments; returns false otherwise.

Create new instance using the Contructor

T 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.

String representation of the contructor

String toGenericString()
Returns a string describing this Constructor, including type parameters.
String toString()
Returns a string describing this Constructor.

Revised from Open JDK source code


  import java.lang.reflect.Constructor;

public class Main {
  public static void main(String[] argv) throws Exception {
      Constructor[] constructors = String.class.getDeclaredConstructors();

      for (int i = 0; i < constructors.length; i++) {
        Constructor c = constructors[i];
        System.out.println(c.toGenericString());
      }
    }
  
}
  
Home 
  Java Book 
    Reflection  

Constructor:
  1. Constructor Reflection
  2. Print out the contructors declared by String class with Reflection
  3. Create new instance by using its contructor