集册 Java实例教程 生成用于在远程调用期间唯一标识方法的方法签名。

生成用于在远程调用期间唯一标识方法的方法签名。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

463
生成用于在远程调用期间唯一标识方法的方法签名。

/**

 * Licensed under Apache License v2. See LICENSE for more information.

 */

//package com.nowjava;
/**来自 
 N o  w  J a v a . c o m - 时  代  Java**/

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(")");/* from 时代Java公众号 - nowjava.com*/

        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.i
展开阅读全文