Java Reflection Constructor Get getConstructorIfAvailable(Class clazz, Class... paramTypes)

Here you can find the source of getConstructorIfAvailable(Class clazz, Class... paramTypes)

Description

Determine whether the given class has a public constructor with the given signature, and return it if available (else return null).

License

Open Source License

Parameter

Parameter Description
clazz the clazz to analyze
paramTypes the parameter types of the method

Return

the constructor, or null if not found

Declaration

public static <T> Constructor<T> getConstructorIfAvailable(Class<T> clazz, Class<?>... paramTypes) 

Method Source Code

//package com.java2s;

import java.lang.reflect.Constructor;

public class Main {
    /**/* w  w  w .java2  s.co m*/
     * Determine whether the given class has a public constructor with the given signature,
     * and return it if available (else return <code>null</code>).
     * <p>Essentially translates <code>NoSuchMethodException</code> to <code>null</code>.
     * @param clazz the clazz to analyze
     * @param paramTypes the parameter types of the method
     * @return the constructor, or <code>null</code> if not found
     * @see java.lang.Class#getConstructor
     */
    public static <T> Constructor<T> getConstructorIfAvailable(Class<T> clazz, Class<?>... paramTypes) {
        if (clazz == null) {
            throw new IllegalArgumentException("Class must not be null");
        }
        try {
            return clazz.getConstructor(paramTypes);
        } catch (NoSuchMethodException ex) {
            return null;
        }
    }
}

Related

  1. getConstructorAccessor(Class enumClass, Class[] additionalParameterTypes)
  2. getConstructorCalls(Class aClass)
  3. getConstructorDescriptor(final Constructor c)
  4. getConstructorForArguments( java.lang.reflect.Constructor[] constructors, Object... arguments)
  5. getConstructorFromSignature(Class clazz, String cstrSig)
  6. getConstructorLabel(java.lang.reflect.Constructor con)
  7. getConstructorOptional(Class cls, Class... argsTypes)
  8. getConstructorOrFail(Class clazz, Class... argTypes)
  9. getConstructors(Class cl)