Java Utililty Methods Class New Instance

List of utility methods to do Class New Instance

Description

The list of methods to do Class New Instance are organized into topic(s).

Method

TnewInstance(Class theClass, Object... initArgs)
Create an object for the given class and initialize it from conf
T result;
try {
    Constructor<T> meth = (Constructor<T>) CONSTRUCTOR_CACHE.get(theClass);
    if (meth == null) {
        meth = theClass.getDeclaredConstructor(EMPTY_CLASS_ARRAY);
        meth.setAccessible(true);
        CONSTRUCTOR_CACHE.put(theClass, meth);
    result = meth.newInstance(initArgs);
} catch (Exception e) {
    throw new RuntimeException(e);
return result;
TnewInstance(Class theClass, String className)
Creates a new zero-arg instance of the specified class
try {
    Constructor<T> ctr = (Constructor<T>) CONSTRUCTOR_CACHE.get(className);
    if (ctr == null) {
        Class<T> clazz = (Class<T>) Class.forName(className);
        ctr = (Constructor<T>) clazz.getDeclaredConstructor(EMPTY_ARRAY);
        ctr.setAccessible(true);
        CONSTRUCTOR_CACHE.put(className, ctr);
    return ctr.newInstance();
} catch (Exception e) {
    throw new RuntimeException("Failed to instantiate task: " + className, e);
TnewInstance(Class theCls)
Create an object for the given class.
T result;
try {
    Constructor<T> meth = (Constructor<T>) constructorCache.get(theCls);
    if (null == meth) {
        meth = theCls.getDeclaredConstructor();
        meth.setAccessible(true);
        constructorCache.put(theCls, meth);
    result = meth.newInstance();
} catch (Exception e) {
    throw new RuntimeException(e);
return result;
TnewInstance(Class type)
new Instance
Constructor<T> constructor;
try {
    constructor = type.getDeclaredConstructor();
    constructor.setAccessible(true);
} catch (NoSuchMethodException e) {
    throw new IllegalArgumentException(
            "Couldn't create Page Object. Missing 0 arg constructor on type " + type, e);
T instance;
try {
    instance = constructor.newInstance();
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
    throw new IllegalArgumentException("Unable to create instance of type " + type, e);
return instance;
TnewInstance(Class type)
Creates new instance from the given type.
try {
    return type.newInstance();
} catch (InstantiationException e) {
    throw new RuntimeException(e);
} catch (IllegalAccessException e) {
    throw new RuntimeException(e);
TnewInstance(Class type)
new Instance
try {
    Constructor<T> constructor = type.getDeclaredConstructor();
    if (type.getEnclosingClass() != null && constructor == null) {
        throw new IllegalArgumentException("Inner class is not supported");
    } else {
        if (!constructor.isAccessible()) {
            constructor.setAccessible(true);
        return constructor.newInstance();
} catch (ReflectiveOperationException e) {
    throw new RuntimeException(e);
TnewInstance(Class type)
A helper method to create a new instance of a type using the default constructor arguments.
try {
    return type.newInstance();
} catch (InstantiationException e) {
    throw new RuntimeException(e);
} catch (IllegalAccessException e) {
    throw new RuntimeException(e);
TnewInstance(Class type)
new Instance
try {
    Constructor<T> customConstructor = getMungedConstructor(type);
    return customConstructor.newInstance(new Object[0]);
} catch (Exception e) {
    throw new RuntimeException("Cannot construct " + type.getName(), e);
TnewInstance(Class type)
new Instance
return newInstance(type, new Class<?>[0]);
TnewInstance(Class type)
Creates a new instance of the given type by invoking the default constructor.
try {
    Constructor<T> constructor = (Constructor<T>) CACHED_DEFAULT_CONSTRUCTORS.get(type);
    if (constructor == null) {
        CACHED_DEFAULT_CONSTRUCTORS.put(type, constructor = type.getDeclaredConstructor());
        constructor.setAccessible(true);
    return constructor.newInstance();
} catch (Exception e) {
...