List of utility methods to do Class New Instance
Object | newInstance(Field field) Get type of field and create an instance to that type try { return field.getType().newInstance(); } catch (InstantiationException | IllegalAccessException e) { throw new RuntimeException("Can not new an instance of class " + field.getType() + " for field " + field.getName() + " on class " + field.getDeclaringClass(), e); |
Object | newInstance(final Class> clazz) new Instance try { return clazz.newInstance(); } catch (final Throwable t) { ; return null; |
Object | newInstance(final Class> clazz, final Object[] parameters) new Instance assert clazz != null && parameters != null; return newInstance(clazz, createParameterTypes(parameters), parameters); |
T | newInstance(final Class new Instance if (!arrayClass.isArray()) { throw new IllegalArgumentException("not an array"); return (T) Array.newInstance(arrayClass.getComponentType(), length); |
T | newInstance(final Class new Instance if (clazz.isAnonymousClass()) { throw new RuntimeException("Can not create new instance of an anonymous class"); } else if (clazz.isInterface()) throw new RuntimeException("Can not create new instance of an interface"); else if (clazz.isAnnotation()) throw new RuntimeException("Can not create new instance of an annotation"); for (Constructor<?> constructor : clazz.getConstructors()) { T object = (T) instantiateWithConstructor(constructor); ... |
T | newInstance(final Class Create new instance of the given class or throw RuntimeException if object can't be instantiated. try { return clazz.newInstance(); } catch (InstantiationException ex) { throw new RuntimeException("Failed to create object of class " + clazz, ex); } catch (IllegalAccessException ex) { throw new RuntimeException("Failed to create object of class " + clazz, ex); |
T | newInstance(final Class new Instance if (clazz.isAnonymousClass() || clazz.isInterface() || clazz.isAnnotation()) { throw new RuntimeException("Can not build new instance of an " + clazz.getName()); T instance = null; for (Constructor<?> constructor : clazz.getConstructors()) { instance = (T) instantiateWithConstructor(constructor); if (isValidInstance(instance)) return instance; ... |
T | newInstance(final Class new Instance if (clazz.isInterface()) { if (Map.class.equals(clazz)) { return (T) new LinkedHashMap(); } else if (List.class.equals(clazz)) { return (T) new ArrayList(); } else if (Set.class.equals(clazz)) { return (T) new LinkedHashSet(); } else if (Collection.class.equals(clazz)) { ... |
T | newInstance(final Class new Instance try { final Constructor<T> constructor = clazz.getDeclaredConstructor(); constructor.setAccessible(true); return constructor.newInstance(); } catch (ReflectiveOperationException e) { throw new IllegalStateException(e); |
T[] | newInstance(final Class Create new array instance. Throws: - NullPointerException if the specified array type parameter is null ;- NegativeArraySizeException if the specified array length is negative. return (T[]) Array.newInstance(type, length);
|