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(final ClassLoader classLoader, final String className, final Object... constructorArgs)
new Instance
try {
    return (T) newInstance(Class.forName(className, true, classLoader), constructorArgs);
} catch (final ClassNotFoundException e) {
    e.printStackTrace();
return null;
RnewInstance(final Object obj, final String clazz)
new Instance
final Class<?> c = obj.getClass().getClassLoader().loadClass(clazz);
return (R) c.newInstance();
TnewInstance(final String className, ClassLoader cl)
new Instance
Object o = __primitiveInstances.get(className);
if (null != o)
    return (T) o;
try {
    Class<T> c = (Class<T>) Class.forName(className, true, cl);
    return c.newInstance();
} catch (Exception e) {
    throw new Exception(e);
...
ObjectnewInstance(final String className, final Object[] args)
Creates a new instance of the given class by passing the given arguments to the constructor.
try {
    return newInstance(Class.forName(className), args);
} catch (final ClassNotFoundException e) {
    throw new IllegalArgumentException(className + " not found");
TnewInstance(final String fullyQualifiedClass)
Leads to OSGi classpath problems.
try {
    return (T) Thread.currentThread().getContextClassLoader().loadClass(fullyQualifiedClass).newInstance();
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
    throw new RuntimeException(e);
EnewInstance(Map> providerMap, String provider, Class[] paramsClass, Object[] paramValues)
Creates a new instance of the specified provider looking for the associated class in the configuration map.
Class<? extends E> clazz = providerMap.get(provider);
if (clazz == null)
    throw new IllegalArgumentException("No configuration for provider: " + provider);
E instance = null;
try {
    Constructor<? extends E> cloudcastConstructor;
    cloudcastConstructor = clazz.getConstructor(paramsClass);
    instance = cloudcastConstructor.newInstance(paramValues);
...
TnewInstance(Object bean, String propertyName, Class clazz)
new Instance
try {
    return clazz.newInstance();
} catch (InstantiationException e) {
    throw new IllegalArgumentException("The " + bean.getClass().getSimpleName() + "'s " + propertyName
            + " (" + clazz.getName() + ") does not have a public no-arg constructor", e);
} catch (IllegalAccessException e) {
    throw new IllegalArgumentException("The " + bean.getClass().getSimpleName() + "'s " + propertyName
            + " (" + clazz.getName() + ") does not have a public no-arg constructor", e);
...
TnewInstance(ProceedingJoinPoint thisJoinPoint, Class clazz, Object... newArgs)
new Instance
ConstructorSignature signature = (ConstructorSignature) thisJoinPoint.getSignature();
Class<?>[] parameterTypes = new Class[signature.getParameterTypes().length
        + (newArgs != null ? newArgs.length : 0)];
Object[] newConstructorArgs = new Object[parameterTypes.length];
for (int i = 0; i < signature.getParameterTypes().length; i++) {
    parameterTypes[i] = signature.getParameterTypes()[i];
    newConstructorArgs[i] = thisJoinPoint.getArgs()[i];
for (int i = 0, j = newConstructorArgs.length - newArgs.length; i < newArgs.length; i++, j++) {
    parameterTypes[j] = newArgs[i].getClass();
    newConstructorArgs[j] = newArgs[i];
Constructor<T> constructor = clazz.getConstructor(parameterTypes);
constructor.setAccessible(true);
return constructor.newInstance(newConstructorArgs);
ObjectnewInstance(String aclass)
new Instance
try {
    Class<?> lClass = Class.forName(aclass);
    Object instance = lClass.newInstance();
    return instance;
} catch (ClassNotFoundException cnfe) {
    throw new RuntimeException();
} catch (InstantiationException insExp) {
    throw new RuntimeException();
...
ObjectnewInstance(String className)
This method will create a new instance of the class specified by className.
try {
    return Class.forName(className).newInstance();
} catch (ClassNotFoundException e) {
    throw new IllegalArgumentException("Not found " + className);
} catch (IllegalAccessException e) {
    throw new IllegalArgumentException("No access to " + className);
} catch (InstantiationException e) {
    throw new IllegalArgumentException("Cannot instantiate " + className);
...