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

StringgetConstructorSimpleName(Constructor constructor)
get Constructor Simple Name
final String name = constructor.getName();
return getConstructorSimpleName(name);
ListgetConstructorsOfLength(final Class clazz, final int length)
get Constructors Of Length
List<Constructor> fixedLengthConstructors = new ArrayList<Constructor>(1);
Constructor[] constructors = clazz.getDeclaredConstructors();
for (int i = 0; i < constructors.length; i++) {
    if (!Modifier.isPublic(constructors[i].getModifiers()))
        continue;
    Class[] parameterTypes = constructors[i].getParameterTypes();
    if (parameterTypes.length == length)
        fixedLengthConstructors.add(constructors[i]);
...
ConstructorgetConstructorWithArgs(Class clazz, Object... args)
Looks for a constructor matching the argument given.
if (clazz != null) {
    Constructor<?>[] constructors = clazz.getConstructors();
    for (Constructor<?> constructor : constructors) {
        if (constructor.getParameterTypes().length == args.length) {
            Class<?>[] pType = constructor.getParameterTypes();
            for (int i = 0; i < pType.length; i++) {
                if (!pType[i].equals(args[i].getClass())) {
                    continue;
...
ConstructorgetConstructorWithLeastParametersFromList( ArrayList constructorsFromType)
get Constructor With Least Parameters From List
Constructor planBConstructor = Object.class.getDeclaredConstructors()[0];
Constructor retVal;
if (!constructorsFromType.isEmpty()) {
    int index = 0;
    retVal = constructorsFromType.get(index);
    index++;
    while (retVal.getExceptionTypes().length > 0 && index < constructorsFromType.size()) {
        retVal = constructorsFromType.get(index);
...
ConstructorgetConstructorWithNoParams(Class clazz)
get Constructor With No Params
for (Constructor constructor : clazz.getConstructors()) {
    if (constructor.getParameterTypes().length == 0) {
        if (Modifier.isPrivate(constructor.getModifiers())
                || Modifier.isProtected(constructor.getModifiers())) {
            constructor.setAccessible(true);
        return constructor;
return null;
ConstructorgetConstructorWithReflection(String className, Class... parameterTypes)
get Constructor With Reflection
Class loadedClass = Class.forName(className);
return loadedClass.getConstructor(parameterTypes);