Java Reflection Constructor Get getConstructor(Class clazz, Class[] paramTypes)

Here you can find the source of getConstructor(Class clazz, Class[] paramTypes)

Description

get Constructor

License

Open Source License

Declaration

public static Constructor getConstructor(Class clazz, Class[] paramTypes) 

Method Source Code

//package com.java2s;
/*// ww w  .j a va2 s.c om
 * $Id$
 * --------------------------------------------------------------------------------------
 * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 *
 * The software in this package is published under the terms of the CPAL v1.0
 * license, a copy of which has been included with this distribution in the
 * LICENSE.txt file.
 */

import java.lang.reflect.Constructor;

import java.lang.reflect.Method;

public class Main {
    public static Constructor getConstructor(Class clazz, Class[] paramTypes) {
        return getConstructor(clazz, paramTypes, false);
    }

    /**
     *  Returns available constructor in the target class that as the parameters specified.
     *
     * @param clazz the class to search
     * @param paramTypes the param types to match against
     * @param exactMatch should exact types be used (i.e. equals rather than isAssignableFrom.)
     * @return The matching constructor or null if no matching constructor is found
     */
    public static Constructor getConstructor(Class clazz, Class[] paramTypes, boolean exactMatch) {
        Constructor[] ctors = clazz.getConstructors();
        for (int i = 0; i < ctors.length; i++) {
            Class[] types = ctors[i].getParameterTypes();
            if (types.length == paramTypes.length) {
                int matchCount = 0;
                for (int x = 0; x < types.length; x++) {
                    if (paramTypes[x] == null) {
                        matchCount++;
                    } else {
                        if (exactMatch) {
                            if (paramTypes[x].equals(types[x]) || types[x].equals(paramTypes[x])) {
                                matchCount++;
                            }
                        } else {
                            if (paramTypes[x].isAssignableFrom(types[x])
                                    || types[x].isAssignableFrom(paramTypes[x])) {
                                matchCount++;
                            }
                        }
                    }
                }
                if (matchCount == types.length) {
                    return ctors[i];
                }
            }
        }
        return null;
    }

    public static Class<?>[] getParameterTypes(Object bean, String methodName) {
        if (!methodName.startsWith("set")) {
            methodName = "set" + methodName.substring(0, 1).toUpperCase() + methodName.substring(1);
        }

        Method methods[] = bean.getClass().getMethods();

        for (int i = 0; i < methods.length; i++) {
            if (methods[i].getName().equals(methodName)) {
                return methods[i].getParameterTypes();
            }
        }

        return new Class[] {};
    }
}

Related

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