Java Reflection Constructor Get getConstructors(Class cl)

Here you can find the source of getConstructors(Class cl)

Description

Prints all constructors of a class

License

Open Source License

Parameter

Parameter Description
cl a class

Declaration

public static String getConstructors(Class cl) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.lang.reflect.*;

public class Main {
    /**/*  w  w w . java  2 s  .  c o  m*/
     * Prints all constructors of a class
     *
     * @param cl a class
     */
    public static String getConstructors(Class cl) {
        String clName = cl.getName();
        Class superClass = cl.getSuperclass();
        String superClassName = "";
        if (superClass != null)
            superClassName = superClass.getName();
        StringBuilder constr = new StringBuilder(
                "\n\nConstructors for class: " + clName + "  inherits  " + superClassName + " \n");
        Constructor[] constructors = cl.getDeclaredConstructors();

        for (Constructor c : constructors) {
            String name = c.getName();
            constr.append("     " + Modifier.toString(c.getModifiers()));
            constr.append(" " + name + "(");

            // print parameter types
            Class[] paramTypes = c.getParameterTypes();
            for (int j = 0; j < paramTypes.length; j++) {
                if (j > 0)
                    constr.append(", ");
                constr.append(paramTypes[j].getName());
            }
            constr.append("); \n");
        }
        return constr.toString();
    }
}

Related

  1. getConstructorFromSignature(Class clazz, String cstrSig)
  2. getConstructorIfAvailable(Class clazz, Class... paramTypes)
  3. getConstructorLabel(java.lang.reflect.Constructor con)
  4. getConstructorOptional(Class cls, Class... argsTypes)
  5. getConstructorOrFail(Class clazz, Class... argTypes)
  6. getConstructors(Class infoClass)
  7. getConstructors(Class clazz)
  8. getConstructors(Class clazz, int args)
  9. getConstructors(Class clazz)