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[] expectedTypes)
get Constructor
Constructor<T> constructor = null;
if (expectedTypes == null) {
    expectedTypes = EMPTY_CLASS_ARRAY;
try {
    constructor = clazz.getConstructor(expectedTypes);
} catch (NoSuchMethodException e) {
    Constructor[] constructors = clazz.getConstructors();
...
java.lang.reflect.ConstructorgetConstructor(Class cls, Class... parameterClses)
get Constructor
try {
    return cls.getConstructor(parameterClses);
} catch (NoSuchMethodException nsme) {
    StringBuffer sb = new StringBuffer();
    sb.append(cls.getName());
    for (Class<?> parameterCls : parameterClses) {
        sb.append(" ");
        sb.append(parameterCls.getName());
...
ConstructorgetConstructor(Class cls, Class[] args)
Gets the constructor of a class that takes the specified set of arguments if any matches.
try {
    return cls.getConstructor(args);
} catch (NoSuchMethodException e) {
    throw new IllegalStateException(e);
ConstructorgetConstructor(Class cls, Object... args)
Retrieve the constructor of a class for given argument values.
Class<?>[] argsTypes = toClass(args);
return getConstructor(cls, argsTypes);
ConstructorgetConstructor(Class instanceType)
get Constructor
Constructor<?>[] constructors = instanceType.getConstructors();
for (Constructor<?> constructor : constructors) {
    if (constructor.getModifiers() == Modifier.PUBLIC) {
        return (Constructor<T>) constructor;
return null;
ConstructorgetConstructor(Class targetClass, Class... parameterTypes)
Gets constructor of targetClass.
try {
    return targetClass.getConstructor(parameterTypes);
} catch (NoSuchMethodException ex) {
    throw new IllegalStateException(ex);
ConstructorgetConstructor(Class type, Class... parameterTypes)
Gets declared constructor for given Class and given parameters
Constructor<T> constructor;
try {
    constructor = type.getDeclaredConstructor(parameterTypes);
} catch (NoSuchMethodException ex) {
    throw new IOException(ex);
} catch (SecurityException ex) {
    throw new IOException(ex);
return constructor;
ConstructorgetConstructor(Class type, Class[] parameterTypes)
get Constructor
try {
    return type.getConstructor(parameterTypes);
} catch (NoSuchMethodException e1) {
    try {
        return type.getDeclaredConstructor(parameterTypes);
    } catch (NoSuchMethodException e2) {
        throw e2;
ConstructorgetConstructor(final Class theClass, final Class... parameterTypes)
Returns class constructor for the specified argument types.
if (parameterTypes.length == 0) {
    return theClass.getConstructor();
} else {
    for (final Constructor constructor : theClass.getDeclaredConstructors()) {
        final Class[] types = constructor.getParameterTypes();
        if (types.length != parameterTypes.length) {
            continue;
        if (types.length == parameterTypes.length && types.length == 0) {
            return constructor;
        boolean fits = true;
        for (int i = 0; i < types.length; i++) {
            if (!isAssignable(types[i], parameterTypes[i])) {
                fits = false;
                break;
        if (fits) {
            return constructor;
    throw new NoSuchMethodException(theClass.getCanonicalName() + argumentTypesToString(parameterTypes));
Optional>getConstructor(final Class clazz, final Class... parameterTypes)
TODO: Documentation
try {
    final Constructor<? extends E> c = clazz.getDeclaredConstructor(parameterTypes);
    return Optional.<Constructor<? extends E>>of(c);
} catch (final Exception e) {
    return Optional.<Constructor<? extends E>>empty();