List of utility methods to do Class New Instance
Object | newInstance(Class> clazz) Attempts to create and return a new instance of given class by invoking default constructor. try { return clazz.newInstance(); } catch (InstantiationException | IllegalAccessException e) { throw new RuntimeException(String .format("Failed to create an instance of %s by invoking " + "default constructor.", clazz), e); |
T | newInstance(Class> clazz) new Instance try { return (T) clazz.newInstance(); } catch (Exception e) { throw new RuntimeException("instance Class: " + clazz.getName() + " with ex: " + e.getMessage(), e); |
Object | newInstance(Class> clazz) new Instance try { return clazz.getConstructor(new Class[0]).newInstance(new Object[0]); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); |
Object | newInstance(Class> clazz) Create a new instance if (clazz == null) throw new IllegalArgumentException("Null clazz"); try { return clazz.newInstance(); } catch (Throwable t) { throw handleErrors("new", clazz.getName(), null, null, t); |
Object | newInstance(Class> clazz) new Instance return clazz.newInstance();
|
Object | newInstance(Class> clazz) new Instance return newInstance(clazz, EMPTY_PARAMS);
|
Object | newInstance(Class> clazz, Class>[] args, Object[] objects) new Instance try { return clazz.getConstructor(args).newInstance(objects); } catch (Exception ex) { return null; |
T | newInstance(Class> clazz, Object... args) new Instance try { if (args == null || args.length == 0) { return (T) clazz.newInstance(); Class<?>[] params = new Class<?>[args.length]; for (int i = 0; i < args.length; i++) { Object arg = args[i]; params[i] = arg == null ? null : arg.getClass(); ... |
Object | newInstance(Class> clazz, Object... args) Creates a new instance of the specified class return newInstance(clazz, getArgTypes(args), args);
|
Object | newInstance(Class> cls) new Instance try { return cls.newInstance(); } catch (Throwable t) { try { Constructor<?>[] constructors = cls.getConstructors(); if (constructors != null && constructors.length == 0) { throw new RuntimeException("Illegal constructor: " + cls.getName()); Constructor<?> constructor = constructors[0]; if (constructor.getParameterTypes().length > 0) { for (Constructor<?> c : constructors) { if (c.getParameterTypes().length < constructor.getParameterTypes().length) { constructor = c; if (constructor.getParameterTypes().length == 0) { break; return constructor.newInstance(new Object[constructor.getParameterTypes().length]); } catch (InstantiationException e) { throw new RuntimeException(e.getMessage(), e); } catch (IllegalAccessException e) { throw new RuntimeException(e.getMessage(), e); } catch (InvocationTargetException e) { throw new RuntimeException(e.getMessage(), e); |