Java Reflection Constructor Invoke invokeConstructor(Constructor constructor, Object... parameters)

Here you can find the source of invokeConstructor(Constructor constructor, Object... parameters)

Description

invoke Constructor

License

Open Source License

Parameter

Parameter Description
constructor to invoke
parameters to pass to constructor
T type of constructor

Return

new instance of class or if any exception occurs

Declaration

public static <T> T invokeConstructor(Constructor<T> constructor, Object... parameters) 

Method Source Code

//package com.java2s;
// License as published by the Free Software Foundation; either

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class Main {
    /**//from  w  ww .j av a  2  s  .  c o m
     * @param constructor
     *            to invoke
     * @param parameters
     *            to pass to constructor
     * @param <T>
     *            type of constructor
     * @return new instance of class or {@link IllegalStateException} if any exception occurs
     * @see Constructor#newInstance(Object...)
     */
    public static <T> T invokeConstructor(Constructor<T> constructor, Object... parameters) {
        try {
            return constructor.newInstance(parameters);
        } catch (InstantiationException | IllegalAccessException | InvocationTargetException ex) {
            throw new IllegalStateException(ex);
        }
    }
}

Related

  1. invokeConstructor(Class objectClass, Class[] classes, Object... params)
  2. invokeConstructor(Class clazz, Class[] argTypes, Object[] args)
  3. invokeConstructor(Class cls)
  4. invokeConstructor(Class type, Object value)
  5. invokeConstructor(Constructor constructor, Object... arguments)
  6. invokeConstructor(final Class clazz, final Class[] paramTypes, final Object... args)
  7. invokeConstructor(final Constructor constructor, final Object... args)
  8. invokeConstructor(final Constructor constructor, final Object[] args)
  9. invokeConstructor(final Constructor constructor, final Object... args)