Java Class New Instance newInstance(String className)

Here you can find the source of newInstance(String className)

Description

new Instance

License

Apache License

Declaration

public static <T> T newInstance(String className) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static <T> T newInstance(String className) {
        Class<T> clazz = forName(className);
        return newInstance(clazz);
    }//from  w  w w. j  av  a2s.c  om

    public static <T> T newInstance(Class<T> clazz) {
        try {
            return clazz.newInstance();
        } catch (InstantiationException e) {
            throw new RuntimeException(clazz.toString(), e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(clazz.toString(), e);
        }
    }

    @SuppressWarnings("unchecked")
    public static <T> Class<T> forName(String className) {
        try {
            return (Class<T>) Class.forName(className);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(className, e);
        }
    }

    @SuppressWarnings("unchecked")
    public static <T> Class<T> forName(String className, boolean initialize, ClassLoader loader) {
        try {
            return (Class<T>) Class.forName(className, initialize, loader);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(className, e);
        }
    }
}

Related

  1. newInstance(Object bean, String propertyName, Class clazz)
  2. newInstance(ProceedingJoinPoint thisJoinPoint, Class clazz, Object... newArgs)
  3. newInstance(String aclass)
  4. newInstance(String className)
  5. newInstance(String className)
  6. newInstance(String className)
  7. newInstance(String className)
  8. newInstance(String className)
  9. newInstance(String className)