List of utility methods to do Class New Instance
T | newInstance(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; |
R | newInstance(final Object obj, final String clazz) new Instance final Class<?> c = obj.getClass().getClassLoader().loadClass(clazz); return (R) c.newInstance(); |
T | newInstance(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); ... |
Object | newInstance(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"); |
T | newInstance(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); |
E | newInstance(Map 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); ... |
T | newInstance(Object bean, String propertyName, Class 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); ... |
T | newInstance(ProceedingJoinPoint thisJoinPoint, Class 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); |
Object | newInstance(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(); ... |
Object | newInstance(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); ... |