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(String type, Class[] paramTypes)
get Constructor
return getConstructor(forName(type), paramTypes);
ConstructorgetConstructor(String typeString)
get Constructor
Class<Object> clazz;
try {
    @SuppressWarnings("unchecked")
    Class<Object> clazzCast = (Class<Object>) Class.forName(typeString);
    clazz = clazzCast;
} catch (ClassNotFoundException e) {
    throw new IllegalArgumentException("Unknown class for type " + typeString);
try {
    @SuppressWarnings("unchecked")
    Constructor<C> constructor = (Constructor<C>) clazz.getConstructor(new Class[] { String.class });
    return constructor;
} catch (Exception e) {
    throw new IllegalArgumentException(
            "Could not find constructor with single String argument for " + clazz);
ObjectgetConstructorAccessor(Class enumClass, Class[] additionalParameterTypes)
get Constructor Accessor
Class<?>[] parameterTypes = new Class[additionalParameterTypes.length + 2];
parameterTypes[0] = String.class;
parameterTypes[1] = int.class;
System.arraycopy(additionalParameterTypes, 0, parameterTypes, 2, additionalParameterTypes.length);
return newConstructorAccessor.invoke(reflectionFactory, enumClass.getDeclaredConstructor(parameterTypes));
String[]getConstructorCalls(Class aClass)
Gets an array of all Constructor calls for the given class
Constructor[] constructors = aClass.getConstructors();
String[] cons = new String[constructors.length];
int i = 0;
for (Constructor c : constructors) {
    cons[i++] = getJavaCallString(c);
return cons;
StringgetConstructorDescriptor(final Constructor c)
Returns the descriptor corresponding to the given constructor.
Class<?>[] parameters = c.getParameterTypes();
StringBuffer buf = new StringBuffer();
buf.append('(');
for (int i = 0; i < parameters.length; ++i) {
    getDescriptor(buf, parameters[i]);
return buf.append(")V").toString();
java.lang.reflect.ConstructorgetConstructorForArguments( java.lang.reflect.Constructor[] constructors, Object... arguments)
get Constructor For Arguments
java.lang.reflect.Constructor<T> rv = null;
for (java.lang.reflect.Constructor<T> constructor : constructors) {
    Class<?>[] parameterClses = constructor.getParameterTypes();
    if (parameterClses.length == arguments.length) {
        if (rv != null) {
            throw new RuntimeException("more than one constructor matches arguments");
        } else {
            rv = constructor;
...
ConstructorgetConstructorFromSignature(Class clazz, String cstrSig)
Get constructor from signature
Constructor<?> retVal = null;
final String methodName = getMethodNameFromSignature(cstrSig);
if (!methodName.equals("<init>")) {
    throw new IllegalArgumentException("Not a constructor signature");
final Class<?>[] paramTypes = getParametersFromSignature(cstrSig);
retVal = clazz.getConstructor(paramTypes);
return retVal;
...
ConstructorgetConstructorIfAvailable(Class clazz, Class... paramTypes)
Determine whether the given class has a public constructor with the given signature, and return it if available (else return null).
if (clazz == null) {
    throw new IllegalArgumentException("Class must not be null");
try {
    return clazz.getConstructor(paramTypes);
} catch (NoSuchMethodException ex) {
    return null;
StringgetConstructorLabel(java.lang.reflect.Constructor con)
get Constructor Label
StringBuilder buffer = new StringBuilder(60);
buffer.append(con.getDeclaringClass().getSimpleName());
buffer.append('(');
Class<?>[] paraTypes = con.getParameterTypes();
for (int i = 0; i < paraTypes.length; i++) {
    buffer.append(paraTypes[i].getSimpleName());
    if (i < paraTypes.length - 1) {
        buffer.append(',');
...
ConstructorgetConstructorOptional(Class cls, Class... argsTypes)
Retrieve the constructor of a class for given optional argument types.
return getConstructorOptional(0, cls, argsTypes);