Java Utililty Methods Class New Instance

List of utility methods to do Class New Instance

Description

The list of methods to do Class New Instance are organized into topic(s).

Method

ObjectnewInstance(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);
ObjectnewInstance(final Class clazz)
new Instance
try {
    return clazz.newInstance();
} catch (final Throwable t) {
    ; 
return null;
ObjectnewInstance(final Class clazz, final Object[] parameters)
new Instance
assert clazz != null && parameters != null;
return newInstance(clazz, createParameterTypes(parameters), parameters);
TnewInstance(final Class arrayClass, final int length)
new Instance
if (!arrayClass.isArray()) {
    throw new IllegalArgumentException("not an array");
return (T) Array.newInstance(arrayClass.getComponentType(), length);
TnewInstance(final Class clazz)
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);
...
TnewInstance(final Class clazz)
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);
TnewInstance(final Class clazz)
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;
...
TnewInstance(final Class clazz)
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)) {
...
TnewInstance(final Class clazz)
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 type, final int length)
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);