List of utility methods to do Class New Instance
Object | newInstance(String className) new Instance try { return Class.forName(className).newInstance(); } catch (Exception e) { throw new RuntimeException("new instance fail : " + e.getMessage(), e); |
Object | newInstance(String className) new Instance return newInstance(className, new Object[0]); |
T | newInstance(String className) new Instance return newInstance((Class<T>) Class.forName(className));
|
Object | newInstance(String className) New instance. Class<?> clazz; try { clazz = Class.forName(className); return newInstance(clazz); } catch (ClassNotFoundException e) { throw new RuntimeException(e); |
Object | newInstance(String className) new Instance Object result = null;
Class<?> type = loadClass(className);
result = (type != null ? type.newInstance() : null);
return result;
|
T | newInstance(String className, Class extends T> instanceClazz) Returns a new instance of classname and check that it's assignable from expected class if (className == null) { return null; try { Class<?> clazz = Class.forName(className); if (instanceClazz.isAssignableFrom(clazz)) { return (T) clazz.getConstructor().newInstance(); } else { ... |
Object | newInstance(String className, Class> cls) new Instance Class<?> clazz = Class.forName(className); Object instance = clazz.newInstance(); if ((instance != null) && !cls.isAssignableFrom(instance.getClass())) throw new IllegalArgumentException(className + " is not an instanceof " + cls.getName()); return instance; |
T | newInstance(String className, Class> context) Instantiates an object using its default constructor if the className is found in the classpath and loaded. Class<T> clazz = (Class<T>) loadClass(className, context, false); if (clazz == null) throw new Exception(className + " not found in the classpath."); return clazz.newInstance(); |
T | newInstance(String className, Class new Instance return castTo.cast(newInstance(forName(className)));
|
Object | newInstance(String className, Class[] parmsCls, Object[] parms) Creates new instance of an specified class Class clazz = Class.forName(className); if (parmsCls == null) { return clazz.newInstance(); } else { Constructor constr = clazz.getConstructor(parmsCls); return constr.newInstance(parms); |