Java Utililty Methods Reflection Constructor Invoke

List of utility methods to do Reflection Constructor Invoke

  1. HOME
  2. Java
  3. R
  4. Reflection Constructor Invoke

Description

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

Method

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

invoke.

try {
 return constructor.newInstance(args);
} catch (InstantiationException e) {
 throw e.getCause();
T invoke(Constructor constructor, Object... args)
invoke
try {
 return constructor.newInstance(args);
} catch (InstantiationException e) {
 throw e.getCause();
Object invokeConstructor(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);
...
Object invokeConstructor(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;
Object invokeConstructor(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;
T invokeConstructor(Class clazz, Class[] argTypes, Object[] args)
invoke Constructor
Constructor<T> constructor = clazz.getConstructor(argTypes);
return constructor.newInstance(args);
T invokeConstructor(Class cls)
invoke Constructor
try {
 Constructor<T> constructor = cls.getDeclaredConstructor();
 constructor.setAccessible(true);
 return constructor.newInstance();
} catch (Exception ignored) {
 return null;
T invokeConstructor(Class type, Object value)
invoke Constructor
return invokeConstructor(type, value, value.getClass());
Object invokeConstructor(Constructor constructor, Object... arguments)
invoke Constructor
try {
 return constructor.newInstance(arguments);
} catch (Exception e) {
 throw new IllegalStateException("constructor invocation error", e);


AltStyle によって変換されたページ (->オリジナル) /