Java Reflection Constructor Get getConstructorSignature(final Constructor constructor)

Here you can find the source of getConstructorSignature(final Constructor constructor)

Description

Returns JVM type signature for a constructor.

License

Open Source License

Parameter

Parameter Description
constructor a parameter

Declaration

public static String getConstructorSignature(final Constructor constructor) 

Method Source Code

//package com.java2s;
/**************************************************************************************
 * Copyright (c) Jonas Bon?r, Alexandre Vasseur. All rights reserved.                 *
 * http://aspectwerkz.codehaus.org                                                    *
 * ---------------------------------------------------------------------------------- *
 * The software in this package is published under the terms of the LGPL license      *
 * a copy of which has been included with this distribution in the license.txt file.  *
 **************************************************************************************/

import java.lang.reflect.Constructor;

import java.lang.reflect.Method;

public class Main {
    /**/*  ww w. j  a va  2  s  .  com*/
     * Returns JVM type signature for a constructor.
     *
     * @param constructor
     * @return
     */
    public static String getConstructorSignature(final Constructor constructor) {
        return getMethodSignature(constructor.getParameterTypes(), Void.TYPE);
    }

    /**
     * Returns JVM type signature for a method.
     *
     * @param method
     * @return
     */
    public static String getMethodSignature(final Method method) {
        return getMethodSignature(method.getParameterTypes(), method.getReturnType());
    }

    /**
     * Returns JVM type signature for given list of parameters and return type.
     *
     * @param paramTypes
     * @param retType
     * @return
     */
    private static String getMethodSignature(Class[] paramTypes, Class retType) {
        StringBuffer sbuf = new StringBuffer();
        sbuf.append('(');
        for (int i = 0; i < paramTypes.length; i++) {
            sbuf.append(getClassSignature(paramTypes[i]));
        }
        sbuf.append(')');
        sbuf.append(getClassSignature(retType));
        return sbuf.toString();
    }

    /**
     * Returns JVM type signature for given class.
     *
     * @param cl
     * @return
     */
    public static String getClassSignature(Class cl) {
        StringBuffer sbuf = new StringBuffer();
        while (cl.isArray()) {
            sbuf.append('[');
            cl = cl.getComponentType();
        }
        if (cl.isPrimitive()) {
            if (cl == Integer.TYPE) {
                sbuf.append('I');
            } else if (cl == Byte.TYPE) {
                sbuf.append('B');
            } else if (cl == Long.TYPE) {
                sbuf.append('J');
            } else if (cl == Float.TYPE) {
                sbuf.append('F');
            } else if (cl == Double.TYPE) {
                sbuf.append('D');
            } else if (cl == Short.TYPE) {
                sbuf.append('S');
            } else if (cl == Character.TYPE) {
                sbuf.append('C');
            } else if (cl == Boolean.TYPE) {
                sbuf.append('Z');
            } else if (cl == Void.TYPE) {
                sbuf.append('V');
            } else {
                throw new InternalError();
            }
        } else {
            sbuf.append('L' + cl.getName().replace('.', '/') + ';');
        }
        return sbuf.toString();
    }
}

Related

  1. getConstructors(Class clazz)
  2. getConstructors(Class clazz, int args)
  3. getConstructors(Class clazz)
  4. getConstructors(Class clazz, int modifier)
  5. getConstructors(final Class cl, final int params)
  6. getConstructorSignatureWithLongTypeNames(Constructor init)
  7. getConstructorSimpleName(Constructor constructor)
  8. getConstructorsOfLength(final Class clazz, final int length)
  9. getConstructorWithArgs(Class clazz, Object... args)