Java Reflection Constructor Get getConstructor(Class clz, Class expectedTypes[])

Here you can find the source of getConstructor(Class clz, Class expectedTypes[])

Description

get Constructor

License

Open Source License

Declaration

public static Constructor getConstructor(Class clz, Class expectedTypes[]) 

Method Source Code


//package com.java2s;

import java.lang.reflect.Constructor;

public class Main {
    public static final Class EMPTY_CLASS_ARRAY[] = new Class[0];

    public static Constructor getConstructor(Class clz, Class expectedTypes[]) {
        Constructor constructor = null;
        try {//from   w  w  w  . j a v  a2 s . co  m
            Constructor constructors[] = clz.getConstructors();
            for (int i = 0; i < constructors.length; i++) {
                Constructor creator = constructors[i];
                if (isAssignable(expectedTypes, creator.getParameterTypes()))
                    if (constructor == null)
                        constructor = creator;
                    else if (isAssignable(creator.getParameterTypes(), constructor.getParameterTypes()))
                        constructor = creator;
            }

        } catch (Throwable thr) {
            String msg = (new StringBuilder()).append("Class: ").append(clz.getName()).append(", ")
                    .append(clz.getProtectionDomain().getCodeSource().getLocation()).append(", ")
                    .append(clz.getClassLoader()).toString();
            throw new RuntimeException(msg, thr);
        }
        return constructor;
    }

    public static boolean isAssignable(Class cls, Class toClass) {
        if (toClass == null)
            return false;
        if (cls == null)
            return !toClass.isPrimitive();
        if (cls.equals(toClass))
            return true;
        if (cls.isPrimitive()) {
            if (!toClass.isPrimitive())
                return false;
            if (Integer.TYPE.equals(cls))
                return Long.TYPE.equals(toClass) || Float.TYPE.equals(toClass) || Double.TYPE.equals(toClass);
            if (Long.TYPE.equals(cls))
                return Float.TYPE.equals(toClass) || Double.TYPE.equals(toClass);
            if (Boolean.TYPE.equals(cls))
                return false;
            if (Double.TYPE.equals(cls))
                return false;
            if (Float.TYPE.equals(cls))
                return Double.TYPE.equals(toClass);
            if (Character.TYPE.equals(cls))
                return Integer.TYPE.equals(toClass) || Long.TYPE.equals(toClass) || Float.TYPE.equals(toClass)
                        || Double.TYPE.equals(toClass);
            if (Short.TYPE.equals(cls))
                return Integer.TYPE.equals(toClass) || Long.TYPE.equals(toClass) || Float.TYPE.equals(toClass)
                        || Double.TYPE.equals(toClass);
            if (Byte.TYPE.equals(cls))
                return Short.TYPE.equals(toClass) || Integer.TYPE.equals(toClass) || Long.TYPE.equals(toClass)
                        || Float.TYPE.equals(toClass) || Double.TYPE.equals(toClass);
            else
                return false;
        } else {
            return toClass.isAssignableFrom(cls);
        }
    }

    public static boolean isAssignable(Class classArray[], Class toClassArray[]) {
        if (classArray.length != toClassArray.length)
            return false;
        if (classArray == null)
            classArray = EMPTY_CLASS_ARRAY;
        if (toClassArray == null)
            toClassArray = EMPTY_CLASS_ARRAY;
        for (int i = 0; i < classArray.length; i++)
            if (!isAssignable(classArray[i], toClassArray[i]))
                return false;

        return true;
    }

    public static boolean isAssignableFrom(Class original, Class checkedClasses[]) {
        if (checkedClasses == null)
            return false;
        for (int i = 0; i < checkedClasses.length; i++)
            if (original.isAssignableFrom(checkedClasses[i]))
                return true;

        return false;
    }
}

Related

  1. getConstructor(Class clazz, Class[] argTypes)
  2. getConstructor(Class clazz, Class[] parametersTypes)
  3. getConstructor(Class clazz, Class[] paramTypes)
  4. getConstructor(Class clazz, Class[] paramTypes)
  5. getConstructor(Class cls, Class[] args)
  6. getConstructor(Class type, Class[] argTypes)
  7. getConstructor(Class c, Class[] args)
  8. getConstructor(Class clazz, boolean declared, Class... args)
  9. getConstructor(Class clazz, Class... args)