/** * Instantiates an object of the given class with the given arguments. If you want to instantiate a member class, you must provide * the object it is a member of as first argument. * * @param fromClass the class to instantiate an object from * @param args the arguments to pass to the constructor * @return an object of the given type * @throws IllegalArgumentException if the number of actual and formal parameters differ; if an unwrapping conversion for primitive * arguments fails; or if, after possible unwrapping, a parameter value cannot be converted to the corresponding formal * parameter type by a method invocation conversion. * @throws IllegalAccessException if this Constructor object enforces Java language access control and the underlying constructor is * inaccessible. * @throws InvocationTargetException if the underlying constructor throws an exception. * @throws NoSuchMethodException if the constructor could not be found * @throws InstantiationException if the class that declares the underlying constructor represents an abstract class. * * @see PrivilegedAccessor#instantiate(Class,Class[],Object[]) */ public static <T> T instantiate(final Class<? extends T> fromClass, final Object[] args) throws IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException { return instantiate(fromClass, getParameterTypes(args), args); }
/** * Instantiates an object of the given class with the given arguments. If you want to instantiate a member class, you must provide * the object it is a member of as first argument. * * @param fromClass the class to instantiate an object from * @param arguments the arguments to pass to the constructor * @return an object of the given type * @throws IllegalArgumentException if the class can't be instantiated. This could be the case if the number of actual and formal * parameters differ; if an unwrapping conversion for primitive arguments fails; or if, after possible unwrapping, a * parameter value cannot be converted to the corresponding formal parameter type by a method invocation conversion; if * this Constructor object enforces Java language access control and the underlying constructor is inaccessible; if the * underlying constructor throws an exception; if the constructor could not be found; or if the class that declares the * underlying constructor represents an abstract class. * * @see PrivilegedAccessor#instantiate(Class,Object[]) */ public static <T> T instantiate(final Class<? extends T> fromClass, final Object... arguments) { try { return PrivilegedAccessor.instantiate(fromClass, correctVarargs(arguments)); } catch (Exception e) { throw new IllegalArgumentException("Can't instantiate class " + fromClass + " with arguments " + arguments, e); } }
/** * Instantiates an object of the given class with the given arguments and the given argument types. If you want to instantiate a * member class, you must provide the object it is a member of as first argument. * * @param fromClass the class to instantiate an object from * @param arguments the arguments to pass to the constructor * @param argumentTypes the fully qualified types of the arguments of the constructor * @return an object of the given type * @throws IllegalArgumentException if the class can't be instantiated. This could be the case if the number of actual and formal * parameters differ; if an unwrapping conversion for primitive arguments fails; if, after possible unwrapping, a * parameter value cannot be converted to the corresponding formal parameter type by a method invocation conversion; if * this Constructor object enforces Java language access control and the underlying constructor is inaccessible; if the * underlying constructor throws an exception; if the constructor could not be found; or if the class that declares the * underlying constructor represents an abstract class. * * @see PrivilegedAccessor#instantiate(Class,Class[],Object[]) */ public static <T> T instantiate(final Class<? extends T> fromClass, final Class<?>[] argumentTypes, final Object... arguments) { try { return PrivilegedAccessor.instantiate(fromClass, argumentTypes, correctVarargs(arguments)); } catch (Exception e) { throw new IllegalArgumentException("Can't instantiate class " + fromClass + " with arguments " + arguments, e); } }