Here you can find the source of newInstance(final ClassLoader classLoader, final String className, final Object... constructorArgs)
@SuppressWarnings("unchecked") public static <T> T newInstance(final ClassLoader classLoader, final String className, final Object... constructorArgs)
//package com.java2s; //License from project: Apache License import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.util.Arrays; public class Main { @SuppressWarnings("unchecked") public static <T> T newInstance(final ClassLoader classLoader, final String className, final Object... constructorArgs) { try {//from ww w . java2 s . co m return (T) newInstance(Class.forName(className, true, classLoader), constructorArgs); } catch (final ClassNotFoundException e) { e.printStackTrace(); } return null; } @SuppressWarnings("unchecked") public static <T> T newInstance(final String className, final Object... constructorArgs) { try { return (T) newInstance(Class.forName(className), constructorArgs); } catch (final ClassNotFoundException e) { // e.printStackTrace(); } return null; } public static <T> T newInstance(final Class<T> clazz, final Object... constructorArgs) { final Constructor<T> constructor = getConstructor(clazz, constructorArgs); if (constructor != null) { try { return constructor.newInstance(constructorArgs); } catch (final InstantiationException e) { e.printStackTrace(); } catch (final IllegalAccessException e) { e.printStackTrace(); } catch (final IllegalArgumentException e) { e.printStackTrace(); } catch (final InvocationTargetException e) { e.printStackTrace(); } } return null; } public static <T> Constructor<T> getConstructor(final Class<T> clazz, final Object... constructorArgs) { final Constructor<T> constructor = getStrictConstructor(clazz, constructorArgs); return constructor == null ? findCompatibleConstructor(clazz, constructorArgs) : constructor; } public static <T> Constructor<T> getStrictConstructor(final Class<T> clazz, final Object... constructorArgs) { final Class<?>[] argsTypes = objsToClasses(constructorArgs); try { return clazz.getConstructor(argsTypes); } catch (final NoSuchMethodException e) { e.printStackTrace(); } catch (final SecurityException e) { e.printStackTrace(); } return null; } public static <T> Constructor<T> findCompatibleConstructor(final Class<T> clazz, final Object... constructorArgs) { @SuppressWarnings("unchecked") final Constructor<T>[] constructors = (Constructor<T>[]) clazz.getConstructors(); final Class<?>[] argsTypes = objsToClasses(constructorArgs); Constructor<T> c = null; for (final Constructor<T> constructor : constructors) { final Class<?>[] parametersTypes = constructor.getParameterTypes(); if (parametersTypes.length == argsTypes.length) { if (Arrays.equals(parametersTypes, argsTypes)) { return constructor; } if (checkAllAssignable(parametersTypes, constructorArgs)) { c = constructor; } } } return c; } public static Class<?>[] objsToClasses(final Object... objs) { final Class<?>[] types = new Class<?>[objs.length]; int index = 0; for (final Object o : objs) { types[index] = o.getClass(); ++index; } return types; } public static boolean checkAllAssignable(final Class<?>[] superClasses, final Class<?>... subClasses) { if (superClasses.length != subClasses.length) { return false; } int index = 0; for (final Class<?> c : superClasses) { if (!c.isAssignableFrom(subClasses[index])) { return false; } ++index; } return true; } public static boolean checkAllAssignable(final Class<?>[] superClasses, final Object... objects) { return checkAllAssignable(superClasses, objsToClasses(objects)); } }