Java Reflection Constructor Get getConstructors(final Class cl, final int params)

Here you can find the source of getConstructors(final Class cl, final int params)

Description

Gets all the constructors in the given class or super classes, even the redefined constructors are returned.

License

Open Source License

Parameter

Parameter Description
cl the class where the constructor was declared
params the number of parameters

Return

a list that contains the found constructors, an empty list if no matching constructor was found.

Declaration

public static List getConstructors(final Class cl, final int params) 

Method Source Code


//package com.java2s;
/*/*from   ww  w.  ja v a 2s . c  om*/
 * DynamicJava - Copyright (C) 1999 Dyade
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
 * associated documentation files (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions: The above copyright notice and this
 * permission notice shall be included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL DYADE BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
 * THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 * Except as contained in this notice, the name of Dyade shall not be used in advertising or
 * otherwise to promote the sale, use or other dealings in this Software without prior written
 * authorization from Dyade.
 */

import java.lang.reflect.Constructor;

import java.util.LinkedList;
import java.util.List;

public class Main {
    /**
     * Gets all the constructors in the given class or super classes, even the redefined
     * constructors are returned.
     * 
     * @param cl the class where the constructor was declared
     * @param params the number of parameters
     * @return a list that contains the found constructors, an empty list if no matching constructor
     *         was found.
     */
    public static List getConstructors(final Class cl, final int params) {
        final List result = new LinkedList();
        final Constructor[] ms = cl.getDeclaredConstructors();

        for (int i = 0; i < ms.length; i++) {
            if (ms[i].getParameterTypes().length == params) {
                result.add(ms[i]);
            }
        }
        return result;
    }
}

Related

  1. getConstructors(Class infoClass)
  2. getConstructors(Class clazz)
  3. getConstructors(Class clazz, int args)
  4. getConstructors(Class clazz)
  5. getConstructors(Class clazz, int modifier)
  6. getConstructorSignature(final Constructor constructor)
  7. getConstructorSignatureWithLongTypeNames(Constructor init)
  8. getConstructorSimpleName(Constructor constructor)
  9. getConstructorsOfLength(final Class clazz, final int length)