Java Utililty Methods Reflection Constructor Invoke

List of utility methods to do Reflection Constructor Invoke

Description

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

Method

voidinvoke(Constructor constructor)
invoke
constructor.setAccessible(true);
try {
    constructor.newInstance();
} catch (Exception e) {
    throw new IllegalStateException("invoking constructor failed.", e);
Tinvoke(Constructor constructor, Object... args)

invoke.

try {
    return constructor.newInstance(args);
} catch (InstantiationException e) {
    throw e.getCause();
Tinvoke(Constructor constructor, Object... args)
invoke
try {
    return constructor.newInstance(args);
} catch (InstantiationException e) {
    throw e.getCause();
ObjectinvokeConstructor(Class clazz, Class[] paramTypes, Object[] params)
Creates a new instance of the given clazz, paramTypes, params
if (clazz == null)
    throw new IllegalArgumentException("Class cannot be null!");
if (paramTypes == null)
    throw new IllegalArgumentException("ParamTypes cannot be null");
if (params == null)
    throw new IllegalArgumentException("Params cannot be null!");
final Constructor constructor = clazz.getDeclaredConstructor(paramTypes);
constructor.setAccessible(true);
...
ObjectinvokeConstructor(Class clazz, Object... parameters)
invoke Constructor
try {
    Constructor constructor = clazz.getConstructor(convertToMethodParameters(parameters));
    return constructor.newInstance(parameters);
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException
        | InvocationTargetException e) {
    e.printStackTrace();
    return null;
ObjectinvokeConstructor(Class objectClass, Class[] classes, Object... params)
invoke Constructor
Object result = null;
Constructor<?> constructor;
try {
    constructor = objectClass.getDeclaredConstructor(classes);
} catch (Exception e) {
    throw new RuntimeException(
            "Exception obtaining the constructor for class '" + objectClass + "' with params=" + params, e);
boolean accessible = constructor.isAccessible();
constructor.setAccessible(true);
try {
    result = constructor.newInstance(params);
} catch (Exception e) {
    throw new RuntimeException(
            "Exception invoking the constructor for class '" + objectClass + "' with params=" + params, e);
} finally {
    constructor.setAccessible(accessible);
return result;
TinvokeConstructor(Class clazz, Class[] argTypes, Object[] args)
invoke Constructor
Constructor<T> constructor = clazz.getConstructor(argTypes);
return constructor.newInstance(args);
TinvokeConstructor(Class cls)
invoke Constructor
try {
    Constructor<T> constructor = cls.getDeclaredConstructor();
    constructor.setAccessible(true);
    return constructor.newInstance();
} catch (Exception ignored) {
    return null;
TinvokeConstructor(Class type, Object value)
invoke Constructor
return invokeConstructor(type, value, value.getClass());
ObjectinvokeConstructor(Constructor constructor, Object... arguments)
invoke Constructor
try {
    return constructor.newInstance(arguments);
} catch (Exception e) {
    throw new IllegalStateException("constructor invocation error", e);