Java Utililty Methods Reflection Constructor Get

List of utility methods to do Reflection Constructor Get

Description

The list of methods to do Reflection Constructor Get are organized into topic(s).

Method

ConstructorgetConstructor(Class clazz, Class... argTypes)
get Constructor
try {
    return clazz.getConstructor(argTypes);
} catch (NoSuchMethodException e) {
    throw new IllegalStateException(e);
ConstructorgetConstructor(Class clazz, Class[] argTypes)
get Constructor
if (clazz == null || argTypes == null) {
    throw new IllegalArgumentException();
try {
    return clazz.getConstructor(argTypes);
} catch (NoSuchMethodException e) {
    return null;
ConstructorgetConstructor(Class clazz, Class[] parametersTypes)
Gets the constructor with specified parameter types.
return clazz.getDeclaredConstructor(parametersTypes);
ConstructorgetConstructor(Class clazz, Class[] paramTypes)
get Constructor
for (Constructor c : clazz.getConstructors()) {
    if (isAssignable(c.getParameterTypes(), paramTypes))
        return c;
return clazz.getConstructor(paramTypes);
ConstructorgetConstructor(Class clazz, Class[] paramTypes)
get Constructor
return getConstructor(clazz, paramTypes, false);
ConstructorgetConstructor(Class cls, Class[] args)
get Constructor
return getConstructor(cls, args, null);
ConstructorgetConstructor(Class clz, Class expectedTypes[])
get Constructor
Constructor constructor = null;
try {
    Constructor constructors[] = clz.getConstructors();
    for (int i = 0; i < constructors.length; i++) {
        Constructor creator = constructors[i];
        if (isAssignable(expectedTypes, creator.getParameterTypes()))
            if (constructor == null)
                constructor = creator;
...
ConstructorgetConstructor(Class type, Class[] argTypes)
Returns a Constructor for the given method signature, or null if no such Constructor can be found.
if (null == type || null == argTypes) {
    throw new NullPointerException();
Constructor ctor = null;
try {
    ctor = type.getConstructor(argTypes);
} catch (Exception e) {
    ctor = null;
...
ConstructorgetConstructor(Class c, Class[] args)
Answer the constructor of the class c which takes arguments of the type(s) in args, or null if there isn't one.
try {
    return c.getConstructor(args);
} catch (NoSuchMethodException e) {
    return null;
ConstructorgetConstructor(Class clazz, boolean declared, Class... args)
get Constructor
try {
    Constructor<?> constructor = null;
    if (declared) {
        constructor = clazz.getDeclaredConstructor(args);
        constructor.setAccessible(true);
    } else
        constructor = clazz.getConstructor(args);
    return constructor;
...