Java Reflection Constructor Get getConstructors(Class clazz, int args)

Here you can find the source of getConstructors(Class clazz, int args)

Description

get Constructors

License

LGPL

Declaration

public static Constructor<?>[] getConstructors(Class<?> clazz, int args) 

Method Source Code


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

import java.lang.reflect.Constructor;

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static Constructor<?>[] getConstructors(Class<?> clazz, int args) {
        try {//w  w  w .j a  v  a 2s  . co m
            List<Constructor<?>> list = new ArrayList<Constructor<?>>();

            for (Constructor<?> constructor : clazz.getDeclaredConstructors()) {
                if (constructor.getParameterTypes().length == args) {
                    constructor.setAccessible(true);
                    list.add(constructor);
                }
            }

            for (Constructor<?> constructor : clazz.getDeclaredConstructors())
                if (constructor.getParameterTypes().length == args)
                    list.add(constructor);

            return list.toArray(new Constructor<?>[list.size()]);
        } catch (Throwable error) {
            return null;
        }
    }
}

Related

  1. getConstructorOptional(Class cls, Class... argsTypes)
  2. getConstructorOrFail(Class clazz, Class... argTypes)
  3. getConstructors(Class cl)
  4. getConstructors(Class infoClass)
  5. getConstructors(Class clazz)
  6. getConstructors(Class clazz)
  7. getConstructors(Class clazz, int modifier)
  8. getConstructors(final Class cl, final int params)
  9. getConstructorSignature(final Constructor constructor)