Here you can find the source of newInstance(final String className, ClassLoader cl)
public static <T> T newInstance(final String className, ClassLoader cl) throws Exception
//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; } }