Java Utililty Methods Reflection Constructor Get

List of utility methods to do Reflection Constructor Get

Description

The list of methods to do Reflection Constructor Get are organized into topic(s).

Method

ConstructorgetConstructor(Class clazz, Class... args)
get Constructor
for (Constructor<?> c : clazz.getDeclaredConstructors())
    if (args.length == 0 && c.getParameterTypes().length == 0
            || ClassListEqual(args, c.getParameterTypes())) {
        c.setAccessible(true);
        return c;
for (Constructor<?> c : clazz.getConstructors())
    if (args.length == 0 && c.getParameterTypes().length == 0
...
MethodHandlegetConstructor(Class clazz, Class... args)
Gets a constructor that has the specified types of arguments.
try {
    Constructor<?> ctor = clazz.getDeclaredConstructor(args);
    ctor.setAccessible(true);
    return MethodHandles.lookup().unreflectConstructor(ctor);
} catch (NoSuchMethodException e) {
    throw new IllegalStateException(
            clazz.getName() + " has no constructor with args " + Arrays.toString(args), e);
} catch (IllegalAccessException e) {
...
ConstructorgetConstructor(Class clazz, Class... parameterTypes)
get Constructor
try {
    return clazz.getDeclaredConstructor(parameterTypes);
} catch (Exception e) {
    return null;
ConstructorgetConstructor(Class clazz, Class... parameterTypes)
Fail-safe.
try {
    return clazz.getConstructor(parameterTypes);
} catch (NoSuchMethodException e) {
} catch (SecurityException e) {
return null;
ConstructorgetConstructor(Class clazz, Class... params)
Returns the constructor
try {
    return clazz.getConstructor(params);
} catch (NoSuchMethodException e) {
    return null;
ConstructorgetConstructor(Class clazz, Class... params)
Attempts to get a constructor from the specified class
if (clazz == null) {
    System.err.println("No class specified.");
    return null;
Constructor<?> constructor = null;
try {
    constructor = clazz.getDeclaredConstructor(params);
} catch (NoSuchMethodException nsme) {
...
ConstructorgetConstructor(Class clazz, Object... args)
get Constructor
Constructor<?> constructor = null;
int length = args.length;
Class<?>[] classes = new Class<?>[length];
for (int i = 0; i < length; i++) {
    Object object = args[i];
    classes[i] = object.getClass();
constructor = clazz.getConstructor(classes);
...
ConstructorgetConstructor(Class clazz, Object[] parameters)
get Constructor
Class<?>[] parameterClasses = new Class<?>[parameters.length];
for (int i = 0; i < parameters.length; i++) {
    parameterClasses[i] = parameters[i] == null ? null : parameters[i].getClass();
return clazz.getConstructor(parameterClasses);
ConstructorgetConstructor(Class cls, Class[] signature)
get Constructor
Constructor<?>[] consarr = cls.getDeclaredConstructors();
for (int i = 0; i < consarr.length; i++) {
    Class<?>[] paramtypes = consarr[i].getParameterTypes();
    if (java.util.Arrays.equals(signature, paramtypes))
        return consarr[i];
return null;
ConstructorgetConstructor(Class cls, Object... parameters)
get Constructor
Constructor<?> constructor = null;
for (Constructor<?> classMethod : cls.getConstructors()) {
    Class<?>[] parametersType = classMethod.getParameterTypes();
    boolean match = false;
    if (parametersType.length == parameters.length) {
        match = true;
        for (int i = 0, length = parametersType.length; i < length; i++) {
            if (parameters[i] != null) {
...