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(final Class clazz, final Class[] paramTypes)
Returns the constructor with the given parameter types, or null if not found.
for (final Constructor<?> candidate : clazz.getConstructors()) {
    if (Arrays.equals(paramTypes, candidate.getParameterTypes())) {
        return candidate;
return null;
ConstructorgetConstructor(final Class valueClass, final Class parameter)
get Constructor
final Constructor<E> constructor;
final String classID = valueClass.getName();
try {
    constructor = valueClass.getDeclaredConstructor(parameter);
} catch (final NoSuchMethodException e) {
    throw new IllegalArgumentException(classID + " does not have a constructor "
            + valueClass.getSimpleName() + '(' + getName(parameter) + ')', e);
constructor.setAccessible(true);
return constructor;
ConstructorgetConstructor(final Class clazz, final Class... parametertypes)
Get a constructor by args.
final Constructor<T> result = clazz.getDeclaredConstructor(parametertypes);
return result;
ConstructorgetConstructor(final Class clazz, final Class... parameterTypes)
Returns the public constructor of the specified class or null if no such constructor
try {
    return clazz.getConstructor(parameterTypes);
} catch (final Exception e) {
    return null;
ConstructorgetConstructor(final Class clazz, final Object... constructorArgs)
get Constructor
final Constructor<T> constructor = getStrictConstructor(clazz, constructorArgs);
return constructor == null ? findCompatibleConstructor(clazz, constructorArgs) : constructor;
ConstructorgetConstructor(final Class clazz, final Object... objs)
Returns a constructor that contains objs as arguments.
Constructor<T> ret = null;
for (final Constructor<?> classConstructor : clazz.getDeclaredConstructors()) {
    if (isMatchingConstructor(classConstructor, objs)) {
        if (ret != null) {
            throw new IllegalArgumentException(
                    "Ambiguity in the constructors for " + clazz.getName() + ".");
        ret = (Constructor<T>) classConstructor;
...
ConstructorgetConstructor(final Class instantiableClass, final Class... classes)
Get a constructor for a class and put the result in cache, to increase speed for the next call.
final int hashCode = instantiableClass.hashCode() ^ Arrays.hashCode(classes);
if (constructorCache.containsKey(hashCode)) {
    return (Constructor<T>) constructorCache.get(hashCode);
} else {
    if (!constructorsCache.containsKey(instantiableClass)) {
        constructorsCache.put(instantiableClass, instantiableClass.getDeclaredConstructors());
    boolean mismatch;
...
ConstructorgetConstructor(String className, Class... argClasses)
get Constructor
return ((Class<? extends T>) Class.forName(className)).getConstructor(argClasses);
ConstructorgetConstructor(String cls_name, Class[] param_cls)
get Constructor
try {
    Class<?> c = Class.forName(cls_name);
    return c.getConstructor(param_cls);
} catch (Exception e) {
    return null;
ConstructorgetConstructor(String string, Class... types)
get Constructor
try {
    Class clazz = Class.forName(string);
    Constructor constructor = clazz.getDeclaredConstructor(types);
    constructor.setAccessible(true);
    return constructor;
} catch (Exception ex) {
return null;
...