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

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

Description

Attempts to get a constructor from the specified class

License

LGPL

Parameter

Parameter Description
params - The parameters for the constructor

Return

The constructor if found, otherwise null

Declaration

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

Method Source Code


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

import java.lang.reflect.Constructor;

public class Main {
    /**//w ww.  j a v a 2s.co  m
     * Attempts to get a constructor from the specified class
     * 
     * @param class - The class to retrieve constructor from
     * @param params - The parameters for the constructor
     * @return The constructor if found, otherwise null
     */
    public static Constructor<?> getConstructor(Class<?> clazz, Class<?>... params) {
        if (clazz == null) {
            System.err.println("No class specified.");
            return null;
        }
        Constructor<?> constructor = null;
        try {
            constructor = clazz.getDeclaredConstructor(params);
        } catch (NoSuchMethodException nsme) {
        }
        if (constructor == null) {
            System.err.println(clazz.getName() + "does not have specified constructor");
            return null;
        } else {
            try {
                constructor.setAccessible(true);
            } catch (SecurityException se) {
            }
            return constructor;
        }
    }
}

Related

  1. getConstructor(Class clazz, Class... args)
  2. getConstructor(Class clazz, Class... args)
  3. getConstructor(Class clazz, Class... parameterTypes)
  4. getConstructor(Class clazz, Class... parameterTypes)
  5. getConstructor(Class clazz, Class... params)
  6. getConstructor(Class clazz, Object... args)
  7. getConstructor(Class clazz, Object[] parameters)
  8. getConstructor(Class cls, Class[] signature)
  9. getConstructor(Class cls, Object... parameters)