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

TinvokeConstructor(Constructor constructor, Object... parameters)
invoke Constructor
try {
    return constructor.newInstance(parameters);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException ex) {
    throw new IllegalStateException(ex);
TinvokeConstructor(final Class clazz, final Class[] paramTypes, final Object... args)
Invokes a constructor.
assert paramTypes.length == args.length;
@Nullable
T obj = null;
try {
    if (paramTypes.length > 0) {
        final Constructor<T> constructor = clazz.getConstructor(paramTypes);
        obj = constructor.newInstance(args);
    } else {
...
ObjectinvokeConstructor(final Constructor constructor, final Object... args)
Invoke the specified Constructor with the supplied arguments.
if (constructor == null) {
    throw new IllegalArgumentException("Constructor must not be null.");
constructor.setAccessible(true);
return constructor.newInstance(args);
ObjectinvokeConstructor(final Constructor constructor, final Object[] args)
Construct an object with the given Constructor and the given array of arguments.
return constructor.newInstance(args);
TinvokeConstructor(final Constructor constructor, final Object... args)
invoke Constructor
try {
    final T object = constructor.newInstance(args);
    return object;
} catch (final RuntimeException e) {
    throw e;
} catch (final Error e) {
    throw e;
} catch (final InvocationTargetException e) {
...
ObjectinvokeConstructor(String className, Class[] paramTypes, Object[] paramValues)
Invokes constructor in class by given className , paramTypes and paramValues .
Class<?> clazz = Class.forName(className);
Constructor<?> constructor = clazz.getConstructor(paramTypes);
return constructor.newInstance(paramValues);
ObjectinvokeConstructor(String className, Object... arguments)
Returns an instantiation of the class specified by the input String using the arguments provided.
try {
    Class<?> cls = Class.forName(className);
    Class<?>[] parameterTypes = new Class<?>[arguments.length];
    int i = 0;
    for (Object argument : arguments)
        parameterTypes[i++] = argument.getClass();
    Constructor<?> constructor = cls.getConstructor(parameterTypes);
    if (!constructor.isAccessible())
...
TinvokeConstructorOrFail(Constructor constructor, Object... args)
Invokes the given constructor with optional arguments args , returning the new instance created by constructor .
try {
    return constructor.newInstance(args);
} catch (InvocationTargetException e) {
    throw new RuntimeException("Failed to call constructor '"
            + constructor.getDeclaringClass().getSimpleName() + "': " + e.getCause().getMessage(),
            e.getCause());
} catch (Exception e) {
    throw new RuntimeException("Failed to call constructor '"
...
ObjectinvokeCtor(Constructor ctor, String str)
invoke Ctor
try {
    return ctor.newInstance(new Object[] { str });
} catch (Throwable t) {
    return null;
ObjectinvokeExactConstructor(Class klass, Object arg)

Convenience method returning new instance of klazz using a single argument constructor.

Object[] args = { arg };
return invokeExactConstructor(klass, args);