Java Reflection Constructor Get getConstructor(Class clazz, Class... args)

Here you can find the source of getConstructor(Class clazz, Class... args)

Description

get Constructor

License

Open Source License

Declaration

public static Constructor<?> getConstructor(Class<?> clazz, Class<?>... args) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.lang.reflect.Constructor;

public class Main {
    public static Constructor<?> getConstructor(Class<?> clazz, Class<?>... args) {
        for (Constructor<?> c : clazz.getDeclaredConstructors())
            if (args.length == 0 && c.getParameterTypes().length == 0
                    || ClassListEqual(args, c.getParameterTypes())) {
                c.setAccessible(true);// w w  w  .  j a  v a 2  s  . c o m
                return c;
            }
        for (Constructor<?> c : clazz.getConstructors())
            if (args.length == 0 && c.getParameterTypes().length == 0
                    || ClassListEqual(args, c.getParameterTypes())) {
                c.setAccessible(true);
                return c;
            }
        return null;
    }

    public static boolean ClassListEqual(Class<?>[] l1, Class<?>[] l2) {
        if (l1.length != l2.length)
            return false;
        for (int i = 0; i < l1.length; i++)
            if (l1[i] != l2[i])
                return false;
        return true;
    }
}

Related

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