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 Object newInstance(String className) 

Method Source Code


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

import java.lang.reflect.Constructor;

public class Main {

    public static Object newInstance(String className) {
        return newInstance(className, new Object[0]);
    }/* w ww  .  j  av a  2  s.co  m*/

    public static Object newInstance(String className, Object[] args) {
        try {
            Class<?> clazz = ClassForName(className);
            Constructor<?> constructor = findConstructors(clazz, args);
            if (null == constructor) {
                return null;
            }
            return constructor.newInstance(args);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    public static Class<?> ClassForName(String className) throws ClassNotFoundException {
        return Class.forName(className.trim());
    }

    private static Constructor<?> findConstructors(Class<?> clazz, Object[] args) {
        if (null == args) {
            args = new Object[0];
        }
        return findConstructors(clazz, argsToClasses(args));
    }

    private static Constructor<?> findConstructors(Class<?> clazz, Class<?>[] argsClazzes) {
        Constructor<?>[] constructors = clazz.getConstructors();
        for (Constructor<?> constructor : constructors) {
            if (methodMatch(constructor.getParameterTypes(), argsClazzes)) {
                return constructor;
            }
        }
        return null;
    }

    private static Class<?>[] argsToClasses(Object[] args) {
        Class<?>[] argsClass = new Class<?>[args.length];
        for (int i = 0; i < argsClass.length; i++) {
            if (null == args[i]) {
                argsClass[i] = null;
            } else {
                argsClass[i] = args[i].getClass();
            }
        }
        return argsClass;
    }

    private static boolean methodMatch(Class<?>[] parameterTypes, Class<?>[] argsClass) {
        if (parameterTypes.length != argsClass.length) {
            return false;
        }
        for (int i = 0; i < parameterTypes.length; i++) {
            Class<?> pc = wrapper(parameterTypes[i]);
            Class<?> ac = wrapper(argsClass[i]);
            if (null != ac) {
                if (!pc.isAssignableFrom(ac)) {
                    return false;
                }
            }
        }
        return true;
    }

    public static Class<?> wrapper(Class<?> type) {
        if (type == null) {
            return null;
        } else if (type.isPrimitive()) {
            if (boolean.class == type) {
                return Boolean.class;
            } else if (int.class == type) {
                return Integer.class;
            } else if (long.class == type) {
                return Long.class;
            } else if (short.class == type) {
                return Short.class;
            } else if (byte.class == type) {
                return Byte.class;
            } else if (double.class == type) {
                return Double.class;
            } else if (float.class == type) {
                return Float.class;
            } else if (char.class == type) {
                return Character.class;
            } else if (void.class == type) {
                return Void.class;
            }
        }
        return type;
    }
}

Related

  1. newInstance(String className)
  2. newInstance(String className)
  3. newInstance(String className)
  4. newInstance(String className)
  5. newInstance(String className)
  6. newInstance(String className)
  7. newInstance(String className)
  8. newInstance(String className)
  9. newInstance(String className, Class instanceClazz)