Java Reflection Constructor Get getConstructors(Class infoClass)

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

Description

Gets constructors for a class.

License

Open Source License

Parameter

Parameter Description
infoClass Class for which constructors are wanted.

Return

String array of constructors.

Declaration


public static String[] getConstructors(Class infoClass) 

Method Source Code

//package com.java2s;
/*   Please see the license information at the end of this file. */

import java.lang.reflect.*;

public class Main {
    /** Gets constructors for a class.
     */*from   w  w  w .j  av  a  2  s . c o  m*/
     *   @param    infoClass    Class for which constructors are wanted.
     *
     *   @return            String array of constructors.
     */

    public static String[] getConstructors(Class infoClass) {
        String[] result = null;
        Constructor[] constructors = null;

        // Get list of constructors.
        try {
            constructors = infoClass.getDeclaredConstructors();
        } catch (SecurityException e) {
            return result;
        }
        // If no constructors, return null.

        if ((constructors == null) || (constructors.length == 0))
            return result;

        result = new String[constructors.length];

        // The "result" entries will be an array
        // of constructors with modifiers and
        // parameters.

        for (int i = 0; i < constructors.length; i++) {
            Constructor constructor = constructors[i];

            int modifiers = constructor.getModifiers();

            result[i] = "";
            // Add modifiers to constructor name.
            if (modifiers > 0) {
                result[i] = trimClassName(Modifier.toString(modifiers)) + " ";
            }
            // Trim array declarations.

            result[i] = result[i] + trimClassName(infoClass.getName()) + "(";

            // Add parameter types to constructor name.

            Class[] paramTypes = constructor.getParameterTypes();

            for (int j = 0; j < paramTypes.length; j++) {
                if (j > 0) {
                    result[i] = result[i] + ", ";
                }

                result[i] = result[i] + trimClassName(unmangleJavaName(paramTypes[j].getName()));
            }

            result[i] = result[i] + ")";

            // Add exception types to constructor name.

            Class[] exceptionTypes = constructor.getExceptionTypes();

            for (int j = 0; j < exceptionTypes.length; j++) {
                if (j == 0) {
                    result[i] = result[i] + " throws";
                } else {
                    result[i] = result[i] + ", ";
                }

                result[i] = result[i] + " " + trimClassName(unmangleJavaName(exceptionTypes[j].getName()));
            }
        }
        // Return array of constructor names and associated
        // information formatted as strings.

        return result;
    }

    /**
     *   Removes class modifiers, leaving only the class name.
     *
     *   @param   name   The class name plus modifiers, if any.
     *
     *   @return         The class name stripped of modifiers.
     *
     */

    public static String trimClassName(String name) {
        String result;
        // Find last period in class name.

        int lastPeriod = name.lastIndexOf('.');

        // If found, strip everything
        // before it.  The result is the
        // unmodified class name.
        // e.g.,
        // a.b.c.simple -> simple .
        if (lastPeriod != -1) {
            result = name.substring(lastPeriod + 1);
        } else {
            result = name;
        }
        // Return trimmed class name.
        return result;
    }

    /**
    * Convert a mangled java name to the normal Java syntax equivalent.
    *
    *    @param      name      The mangled name.
    *   @return               The unmangled name.
    *
    * <p>Arrays are indicated by one or more '[' characters (the count
    * indicates the number of "[]" pairs), followed by a single upper-case
    * letter denoting the array type.</p>
    */

    public static String unmangleJavaName(String name) {
        int arrayCount;
        // Look for '[' characters at start of 'name'.

        for (arrayCount = 0; arrayCount < name.length(); arrayCount++) {
            if (name.charAt(arrayCount) != '[') {
                break;
            }
        }
        // No '[' -- name doesn't need unmangling.
        if (arrayCount == 0) {
            return name;
        }
        // Find out what kind of array we have.
        // Append the type to the output.

        StringBuffer buf = new StringBuffer();

        switch (name.charAt(arrayCount)) {
        case 'B':
            buf.append("byte");
            break;

        case 'C':
            buf.append("char");
            break;

        case 'D':
            buf.append("double");
            break;

        case 'F':
            buf.append("float");
            break;

        case 'I':
            buf.append("int");
            break;

        case 'J':
            buf.append("long");
            break;

        case 'L':
            // Find the ';' that terminates the type declaration.
            // Place the proper number of "[]" symbols there,
            // followed by a space and the remainder of the name.

            int semicolon = name.indexOf(';');

            buf.append(name.substring(arrayCount + 1, semicolon));

            for (int i = 0; i < arrayCount; i++) {
                buf.append(" [ ]");
            }

            buf.append(' ');

            if ((semicolon + 1) <= (name.length() - 1)) {
                buf.append(name.substring(semicolon + 1, name.length() - 1));
            }

            return buf.toString();

        case 'S':
            buf.append("short");
            break;

        case 'V':
            buf.append("void");
            break;

        case 'Z':
            buf.append("boolean");
            break;

        default:
            break;
        }
        // Append appropriate number of "[]" .

        for (int i = 0; i < arrayCount; i++) {
            buf.append(" [ ]");
        }
        // Return unmangled name.
        return buf.toString();
    }
}

Related

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