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 klass, Class... parameterTypes)
get Constructor
Constructor<?>[] constructors = klass.getConstructors();
SEARCH: for (Constructor<?> c : constructors) {
    Class<?>[] constructorParameterTypes = c.getParameterTypes();
    if (constructorParameterTypes.length != parameterTypes.length)
        continue SEARCH;
    for (int i = 0; i < parameterTypes.length; i++) {
        if (!constructorParameterTypes[i].isAssignableFrom(parameterTypes[i]))
            continue SEARCH;
...
ConstructorgetConstructor(Class sourceClass, boolean isFindDeclaredConstructor, boolean isUpwardFind, Class... constructorParameterTypes)
get Constructor
Constructor<?> method = null;
try {
    method = isFindDeclaredConstructor ? sourceClass.getDeclaredConstructor(constructorParameterTypes)
            : sourceClass.getConstructor(constructorParameterTypes);
} catch (NoSuchMethodException e1) {
    if (isUpwardFind) {
        Class<?> classs = sourceClass.getSuperclass();
        while (method == null && classs != null) {
...
ConstructorgetConstructor(Class theClass, Class[] paramTypes)
get Constructor
Constructor<?> constructor = null;
try {
    constructor = theClass.getDeclaredConstructor(paramTypes);
    constructor.setAccessible(true);
} catch (NoSuchMethodException | SecurityException e) {
if (constructor == null) {
    Class<?> superClasss = theClass.getSuperclass();
...
ConstructorgetConstructor(Class type, Constructor c)
get Constructor
if (c == null || Modifier.isPublic(type.getModifiers())) {
    return c;
Constructor<?> cp = null;
Class<?> sup = type.getSuperclass();
if (sup != null) {
    try {
        cp = sup.getConstructor(c.getParameterTypes());
...
ConstructorgetConstructor(Class type, Object[] args)
get Constructor
for (Constructor<?> c : type.getConstructors()) {
    if (instanceOfs(c.getParameterTypes(), args))
        return c;
return null;
ConstructorgetConstructor(Class type, Class[] givenTypes)
Returns the constructor where the formal parameter list matches the givenTypes argument.
return type.getConstructor(givenTypes);
ConstructorgetConstructor(Class aClass)
get Constructor
Constructor constr = aClass.getDeclaredConstructor();
try {
    constr.setAccessible(true);
    return constr;
} catch (Exception ex) {
    return aClass.getConstructor();
ConstructorgetConstructor(Class cl, Class... params)
Returns the specified declared constructor, making it accessible.
try {
    Constructor<T> result = cl.getDeclaredConstructor(params);
    result.setAccessible(true);
    return result;
} catch (Exception e) {
    throw new RuntimeException("Unexpected exception: " + e, e);
ConstructorgetConstructor(Class clazz)
Returns the empty argument constructor of the class.
if (clazz == null) {
    throw new IllegalArgumentException("class cannot be null");
Constructor<T> cons = clazz.getConstructor(EMPTY_CLASS_ARRAY);
cons.setAccessible(true);
return cons;
ConstructorgetConstructor(Class clazz, Class... parameterTypes)
Returns a public constructor of the given class with the given parameter types.
try {
    if (parameterTypes != null && parameterTypes.length > 0) {
        return clazz.getConstructor(parameterTypes);
    } else {
        return clazz.getConstructor();
} catch (Exception exp) {
    return null;
...