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

ObjectnewInstanceHard(String name)
new Instance Hard
try {
    return Class.forName(name).newInstance();
} catch (ClassNotFoundException e) {
    throw new RuntimeException(e);
} catch (InstantiationException e) {
    throw new RuntimeException(e);
} catch (IllegalAccessException e) {
    throw new RuntimeException(e);
...
TnewInstanceOf(Class clazz)
new Instance Of
try {
    if (clazz.equals(byte[].class)) {
        return (T) new byte[] {};
    return clazz.newInstance();
} catch (Exception e) {
    throw new RuntimeException(e);
ObjectnewInstanceOf(String className)
new Instance Of
Class<?> c = Class.forName(className);
Constructor<?> constructor = c.getConstructor();
return constructor.newInstance();
TnewInstanceOrThrow(final Class clazz)
new Instance Or Throw
try {
    return clazz.newInstance();
} catch (final Exception e) {
    throw new RuntimeException("Could not instantiate class:" + clazz.getSimpleName(), e);
ListnewInstancesViaMetaAnnotation(Class declarator, Class metaAnnotationClass, Class expected)
new Instances Via Meta Annotation
List<T> result = Lists.newArrayList();
for (Annotation annotation : declarator.getAnnotations()) {
    Annotation metaAnnotation = annotation.annotationType().getAnnotation(metaAnnotationClass);
    T r = newInstanceViaAnnotation(declarator, metaAnnotation, expected, annotation);
    if (r != null)
        result.add(r);
return result;
...
TnewInstanceViaAnnotation(Class declarator, Annotation annotation, Class expected, Annotation parameter)
new Instance Via Annotation
if (annotation != null)
    for (Method f : annotation.annotationType().getDeclaredMethods())
        if (f.getReturnType() == Class.class) {
            try {
                Object objtype = f.invoke(annotation);
                if (objtype instanceof Class<?> && expected.isAssignableFrom((Class<?>) objtype)) {
                    T result = newInstance((Class<T>) objtype, declarator, parameter);
                    if (result != null)
...
TnewInstanceWithDefaults(Class annotationType)
new Instance With Defaults
Object proxy = Proxy.newProxyInstance(annotationType.getClassLoader(), new Class<?>[] { annotationType },
        new InvocationHandler() {
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                return method.getDefaultValue();
        });
return annotationType.cast(proxy);
ObjectnewInstanceWithFill(Class componentType, int length, Object filledValue)
new Instance With Fill
Object array = Array.newInstance(componentType, length);
for (int i = 0; i < Array.getLength(array); i++) {
    Array.set(array, i, filledValue);
return array;
TnewInstanceWithoutInit(Class clazz)
new Instance Without Init
try {
    ReflectionFactory rf = ReflectionFactory.getReflectionFactory();
    Constructor objDef = Object.class.getDeclaredConstructor();
    Constructor intConstr = rf.newConstructorForSerialization(clazz, objDef);
    return clazz.cast(intConstr.newInstance());
} catch (RuntimeException e) {
    throw e;
} catch (Exception e) {
...
TnewInstanceWithParameterTypes(Constructor constructor)
new Instance With Parameter Types
Constructor paramConstructor = getConstructorWithNoParams(constructor.getParameterTypes()[0]);
if (paramConstructor == null)
    return null;
setAccessible(constructor);
try {
    return constructor.newInstance(paramConstructor.newInstance());
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
    e.printStackTrace();
...