Here you can find the source of invokeConstructor(Constructor
Parameter | Description |
---|---|
constructor | to invoke |
parameters | to pass to constructor |
T | type of constructor |
public static <T> T invokeConstructor(Constructor<T> constructor, Object... parameters)
//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); } } }