Java Reflection Constructor Get getConstructor(final Class clazz, final Object... constructorArgs)

Here you can find the source of getConstructor(final Class clazz, final Object... constructorArgs)

Description

get Constructor

License

Apache License

Declaration

public static <T> Constructor<T> getConstructor(final Class<T> clazz, final Object... constructorArgs) 

Method Source Code

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

import java.lang.reflect.Constructor;

import java.util.Arrays;

public class Main {
    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;
    }// w ww . ja va  2  s  .  com

    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));
    }

    public static Class<?> getClass(final String className) {
        try {
            return Class.forName(className);
        } catch (final ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
}

Related

  1. getConstructor(final Class clazz, final Class... parameterTypes)
  2. getConstructor(final Class clazz, final Class[] paramTypes)
  3. getConstructor(final Class valueClass, final Class parameter)
  4. getConstructor(final Class clazz, final Class... parametertypes)
  5. getConstructor(final Class clazz, final Class... parameterTypes)
  6. getConstructor(final Class clazz, final Object... objs)
  7. getConstructor(final Class instantiableClass, final Class... classes)
  8. getConstructor(String className, Class... argClasses)
  9. getConstructor(String cls_name, Class[] param_cls)