Generate the method signature used to uniquely identity a method during remote invocation. - Java Reflection

Java examples for Reflection:Method

Description

Generate the method signature used to uniquely identity a method during remote invocation.

Demo Code

/**/*from w w w.  ja v a  2 s  .co m*/
 * Licensed under Apache License v2. See LICENSE for more information.
 */
//package com.java2s;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class Main {
    private static final Map<Class<?>, String> TYPESCODES = new HashMap<Class<?>, String>();

    /**
     * Generate the method signature used to uniquely identity a method during remote
     * invocation.
     * 
     * @param method the method
     * @return the signature
     */
    public static String getMethodSignature(Method method) {
        StringBuilder sb = new StringBuilder(method.getName()).append("(");
        for (Class<?> parameterType : method.getParameterTypes()) {
            appendTypeSignature(sb, parameterType);
        }
        sb.append(")");
        appendTypeSignature(sb, method.getReturnType());
        return sb.toString();
    }

    private static void appendTypeSignature(StringBuilder buffer,
            Class<?> clazz) {
        if (clazz.isArray()) {
            buffer.append("[");
            appendTypeSignature(buffer, clazz.getComponentType());
        } else if (clazz.isPrimitive()) {
            buffer.append(TYPESCODES.get(clazz));
        } else {
            buffer.append("L")
                    .append(clazz.getName().replaceAll("\\.", "/"))
                    .append(";");
        }
    }
}

Related Tutorials