
refutils.ConstructorHelper Maven / Gradle / Ivy
package refutils;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
/**
* Utility class to handle constructors.
*
* @param
* @author exbjek
*/
final class ConstructorHelper {
private final Class clazz;
ConstructorHelper(final Class clazz) {
this.clazz = clazz;
}
/**
* Instantiate private empty constructor.
*
* @return the instance
*/
T instantiatePrivate() {
try {
return tryToInstantiatePrivate();
} catch (Exception ex) {
throw new IllegalArgumentException(String.format("Cannot instantiate Class: %s constructor", clazz.getName()), ex);
}
}
private T tryToInstantiatePrivate() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
Constructor constructor = clazz.getDeclaredConstructor();
constructor.setAccessible(true);
return constructor.newInstance();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy