Java Reflection Method Signature getMethodSignature(Class[] paramTypes, Class retType)

Here you can find the source of getMethodSignature(Class[] paramTypes, Class retType)

Description

Returns JVM type signature for given list of parameters and return type.

License

Open Source License

Parameter

Parameter Description
paramTypes a parameter
retType a parameter

Declaration

private static String getMethodSignature(Class[] paramTypes, Class retType) 

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.Method;

public class Main {
    /**/*from  w w  w  .j  ava 2 s  . co m*/
     * 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. getMethod(Class cls, String name, Class[] signature)
  2. getMethod(Signature signature)
  3. getMethodByFunctionSignature(Class clazz, String signature)
  4. getMethodFromSignature(Class clazz, String methodSig)
  5. getMethodSignature(Method m)
  6. getMethodSignature(Method method)
  7. getMethodSignature(Method method)
  8. getMethodSignature(Method method)