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 clazz)
new Instance
Constructor<T> publicConstructor;
try {
    publicConstructor = clazz.getConstructor();
} catch (NoSuchMethodException e) {
    try {
        Constructor<T> privateConstructor = clazz.getDeclaredConstructor();
        privateConstructor.setAccessible(true);
        return privateConstructor.newInstance();
...
TnewInstance(Class clazz)
new Instance
try {
    return clazz.newInstance();
} catch (Exception e) {
    throw new RuntimeException("could not instantiate class " + clazz.getName(), e);
TnewInstance(Class clazz)
Creates a new instance from the same type as the given Class.
return clazz.newInstance();
TnewInstance(Class clazz)
new Instance
T result;
try {
    @SuppressWarnings("unchecked")
    Constructor<T> meth = (Constructor<T>) CONSTRUCTOR_CACHE.get(clazz);
    if (meth == null) {
        meth = clazz.getDeclaredConstructor(EMPTY_CLASS_ARRAY);
        meth.setAccessible(true);
        CONSTRUCTOR_CACHE.put(clazz, meth);
...
TnewInstance(Class clazz)
new Instance
try {
    return (T) clazz.newInstance();
} catch (Exception e) {
    throw new RuntimeException("Instance class " + clazz + " error!");
TnewInstance(Class clazz)
Constructs a new instance of the class using the no-arg constructor.
Constructor<T> cons = getConstructor(clazz);
return cons.newInstance(EMPTY_OBJECT_ARRAY);
TnewInstance(Class clazz)
Create an object for the given class using its default constructor.
return newInstance(clazz, EMPTY_CLASSES);
TnewInstance(Class clazz)
new Instance
Constructor<T> ctor = null;
try {
    ctor = clazz.getDeclaredConstructor(new Class<?>[0]);
    ctor.setAccessible(true);
    T o = ctor.newInstance(new Object[0]);
    return o;
} catch (Throwable t) {
    return null;
...
TnewInstance(Class clazz, Class[] argumentTypes, Object[] arguments)
new Instance
if (argumentTypes == null || argumentTypes.length < 1) {
    return newDefaultInstance(clazz);
try {
    Constructor<T> ctr = clazz.getDeclaredConstructor(argumentTypes);
    ctr.setAccessible(true);
    return ctr.newInstance(arguments);
} catch (Exception e) {
...
TnewInstance(Class clazz, Class[] parameterTypes, Object[] initargs)
new Instance
try {
    Constructor<T> c = clazz.getDeclaredConstructor(parameterTypes);
    c.setAccessible(true);
    return c.newInstance(initargs);
} catch (SecurityException e) {
    throw new RuntimeException(e);
} catch (NoSuchMethodException e) {
    throw new RuntimeException(e);
...