List of utility methods to do Class New Instance
Object | newInstance(String clazzStr) new Instance try { Class<?> clazz = loadClass(clazzStr); return instantiate(clazz); } catch (Exception ex) { return null; |
T | newInstance(String cls, Class new Instance Class<?> clazz; try { clazz = Class.forName(cls); } catch (ClassNotFoundException e) { throw new IllegalArgumentException("class not found: " + cls, e); Object obj; try { ... |
Object | newInstance(String klass) new Instance try { Class c = Class.forName(klass); return c.newInstance(); } catch (Exception e) { throw new RuntimeException(e); |
Object | newInstance(String name) new Instance try { return forName(name).newInstance(); } catch (InstantiationException e) { throw new IllegalStateException(e.getMessage(), e); } catch (IllegalAccessException e) { throw new IllegalStateException(e.getMessage(), e); |
Object | newInstance(String name, ClassLoader loader) new Instance return newInstance(forName(name, loader));
|
Object | newInstance(String s) Creates a new instance of the given class. final ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); try { Class clazz = classLoader.loadClass(s); Constructor constructor = clazz.getConstructor(); return constructor.newInstance(); } catch (ClassNotFoundException e) { return null; } catch (IllegalAccessException e) { ... |
Object | newInstance(String type) new Instance Object object = Class.forName(type).newInstance();
return object;
|
T | newInstance(String type, Class new Instance return forName(type, cast).newInstance();
|
T | newInstance(T obj) Generic newInstance that tries to clone an object. try { Object n = obj.getClass().getConstructor().newInstance(); return (T) n; } catch (NullPointerException e) { throw new IllegalArgumentException("Null pointer exception in newInstance()", e); |
T | newInstance(T obj, Class> argType, Object arg) new Instance Object newObj = obj.getClass().getConstructor(argType).newInstance(arg);
return (T) newObj;
|