Java Reflection Method Signature getMethodSignature(Method method)

Here you can find the source of getMethodSignature(Method method)

Description

get Method Signature

License

Apache License

Declaration

public static String getMethodSignature(Method method) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.lang.reflect.Method;

import java.util.Arrays;

import java.util.List;
import java.util.Map;

import java.util.concurrent.ConcurrentHashMap;

public class Main {
    private static final Map<Method, List<Class<?>>> parameterTypeCache = new ConcurrentHashMap<>();

    public static String getMethodSignature(Method method) {
        StringBuilder sb = new StringBuilder();
        if (method.getReturnType() != null) {
            sb.append(method.getReturnType().getName()).append("#");
        }//from ww  w.j a  v  a 2s  .c o m

        sb.append(method.getName());

        Class<?>[] parameterTypes = method.getParameterTypes();
        if (parameterTypes != null) {
            for (int i = 0, length = parameterTypes.length; i < length; i++) {
                if (i == 0) {
                    sb.append(":");
                } else {
                    sb.append(",");
                }
                sb.append(parameterTypes[i].getName());
            }
        }
        return sb.toString();
    }

    /**
     * @param method
     * @return {@code List<Class<?>>}
     */
    public static List<Class<?>> getParameterTypes(Method method) {
        if (parameterTypeCache.containsKey(method)) {
            return parameterTypeCache.get(method);
        }

        List<Class<?>> types = Arrays.asList(method.getParameterTypes());
        parameterTypeCache.put(method, types);

        return types;
    }
}

Related

  1. getMethodSignature(Method method)
  2. getMethodSignature(Method method)
  3. getMethodSignature(Method method)
  4. getMethodSignature(Method method)
  5. getMethodSignature(Method method)
  6. getMethodSignatureWithLongTypeNames(Method method)
  7. getMethodWithSignature(Class clazz, String methodName, Class[] params)