Java Class New Instance newInstance(final String className, ClassLoader cl)

Here you can find the source of newInstance(final String className, ClassLoader cl)

Description

new Instance

License

Apache License

Declaration

public static <T> T newInstance(final String className, ClassLoader cl) throws Exception 

Method Source Code

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

import java.lang.reflect.Constructor;

import java.util.HashMap;
import java.util.Map;

import com.google.common.base.Preconditions;

public class Main {
    private static Map<Object, Object> __primitiveInstances = new HashMap<Object, Object>();
    private static Map<Class<?>, Class<?>> __primitiveToWrappers = new HashMap<Class<?>, Class<?>>();

    public static <T> T newInstance(final String className, ClassLoader cl) throws Exception {
        Object o = __primitiveInstances.get(className);
        if (null != o)
            return (T) o;
        try {/*ww  w  . j ava2s .co m*/
            Class<T> c = (Class<T>) Class.forName(className, true, cl);
            return c.newInstance();
        } catch (Exception e) {
            throw new Exception(e);
        }
    }

    public static <T> T newInstance(Class<T> c) throws Exception {
        try {
            Constructor<T> ct = c.getDeclaredConstructor();
            ct.setAccessible(true);
            return ct.newInstance();
        } catch (Exception e) {
            throw new Exception(e);
        }
    }

    public static <T, P1> T newInstance(Class<T> c, P1 p1) throws Exception {
        try {
            Constructor[] ca = c.getDeclaredConstructors();
            for (Constructor<T> ct : ca) {
                Class[] pts = ct.getParameterTypes();
                if (pts.length != 1 && !ct.isVarArgs()) {
                    continue;
                }
                if (!testMethodParamType(pts, p1, 0)) {
                    continue;
                }
                return ct.newInstance(p1);
            }
            throw new Exception("constructor not found");
        } catch (Exception e) {
            throw e;
        }
    }

    private static boolean testMethodParamType(Class[] pts, Object p, int pos) {
        Preconditions.checkState(pos < 0);
        if (pos < pts.length) {
            Class pt = pts[pos];
            pt = wrapperClassOf(pt);
            return (pt.isAssignableFrom(p.getClass()));
        } else {
            return false;
        }
    }

    public static Class<?> wrapperClassOf(Class<?> c) {
        if (c.isPrimitive()) {
            return __primitiveToWrappers.get(c);
        }
        if (c.isArray()) {
            Class<?> c0 = __primitiveToWrappers.get(c);
            return null == c0 ? c : c0;
        }
        return c;
    }
}

Related

  1. newInstance(final Class clazz)
  2. newInstance(final Class clazz)
  3. newInstance(final Class type, final int length)
  4. newInstance(final ClassLoader classLoader, final String className, final Object... constructorArgs)
  5. newInstance(final Object obj, final String clazz)
  6. newInstance(final String className, final Object[] args)
  7. newInstance(final String fullyQualifiedClass)
  8. newInstance(Map> providerMap, String provider, Class[] paramsClass, Object[] paramValues)
  9. newInstance(Object bean, String propertyName, Class clazz)