Java Reflection Constructor Get getConstructor(Class cls, Object... parameters)

Here you can find the source of getConstructor(Class cls, Object... parameters)

Description

get Constructor

License

LGPL

Declaration

public static Constructor<?> getConstructor(Class<?> cls, Object... parameters) 

Method Source Code

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

import java.lang.reflect.*;

public class Main {
    public static Constructor<?> getConstructor(Class<?> cls, Object... parameters) {
        Constructor<?> constructor = null;

        for (Constructor<?> classMethod : cls.getConstructors()) {
            Class<?>[] parametersType = classMethod.getParameterTypes();
            boolean match = false;

            if (parametersType.length == parameters.length) {
                match = true;/*www. j  av  a2  s.co  m*/
                for (int i = 0, length = parametersType.length; i < length; i++) {
                    if (parameters[i] != null) {
                        if (!parametersType[i].isAssignableFrom(parameters[i].getClass())) {
                            match = false;
                            break;
                        }
                    }
                }
            }

            if (match) {
                constructor = classMethod;
                break;
            }
        }
        return constructor;
    }
}

Related

  1. getConstructor(Class clazz, Class... params)
  2. getConstructor(Class clazz, Class... params)
  3. getConstructor(Class clazz, Object... args)
  4. getConstructor(Class clazz, Object[] parameters)
  5. getConstructor(Class cls, Class[] signature)
  6. getConstructor(Class klass, Class... parameterTypes)
  7. getConstructor(Class sourceClass, boolean isFindDeclaredConstructor, boolean isUpwardFind, Class... constructorParameterTypes)
  8. getConstructor(Class theClass, Class[] paramTypes)
  9. getConstructor(Class type, Constructor c)