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 cls)
new Instance
Object instance;
try {
    instance = cls.newInstance();
} catch (Exception e) {
    throw new RuntimeException(e);
return (T) instance;
ObjectnewInstance(Class cls)
new Instance
try {
    return cls.newInstance();
} catch (InstantiationException e) {
    e.printStackTrace();
} catch (IllegalAccessException e) {
    e.printStackTrace();
return null;
...
TnewInstance(Class cls, Map, Constructor> cache)
new Instance
final Constructor ctor = getDefaultConstructor(cls, cache);
try {
    return (T) ctor.newInstance();
} catch (Throwable t) {
    throw new RuntimeException("Failed to instantiate " + cls.getName(), t);
ObjectnewInstance(Class componentType, int... dimensions)
new Instance
return Array.newInstance(componentType, dimensions);
ObjectnewInstance(Class entity)

newInstance.

if (entity == null) {
    return null;
try {
    return entity.newInstance();
} catch (Exception e) {
    throw new RuntimeException(e);
TnewInstance(Class theClass, Class expected)
Create an object for the given class and initialize it from conf
T result;
try {
    if (!expected.isAssignableFrom(theClass)) {
        throw new Exception("Specified class " + theClass.getName() + "" + "does not extend/implement "
                + expected.getName());
    Class<? extends T> clazz = (Class<? extends T>) theClass;
    Constructor<? extends T> meth = clazz.getDeclaredConstructor(EMPTY_ARRAY);
...
InewInstance(Class interfaceDefinition, String className, ClassLoader classLoader)
new Instance
try {
    Class<I> spiClass;
    if (classLoader == null) {
        spiClass = (Class<I>) Class.forName(className);
    } else {
        spiClass = (Class<I>) classLoader.loadClass(className);
    return spiClass.newInstance();
...
TnewInstance(Class aClass, Object... args)
new Instance
try {
    return (T) aClass.getConstructor(typeArgs(args)).newInstance(args);
} catch (Exception e) {
    throw new RuntimeException(e);
T[]newInstance(Class arrayComponentClass, T value)
Create single element array of the specified class which contains the specified value
T[] array = (T[]) Array.newInstance(arrayComponentClass, 1);
array[0] = value;
return array;
TnewInstance(Class beanClass)
new Instance
T instance = null;
try {
    instance = beanClass.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
    throw new IllegalArgumentException("create instance of " + beanClass.getSimpleName() + "failed. WTF?",
            e);
return instance;
...