Java Reflection Method Return getMethodDesc(Class returnType, Class... params)

Here you can find the source of getMethodDesc(Class returnType, Class... params)

Description

get Method Desc

License

Apache License

Declaration

public static String getMethodDesc(Class<?> returnType, Class<?>... params) 

Method Source Code

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

import java.lang.reflect.Method;

public class Main {

    public static String getMethodDesc(Class<?> returnType, Class<?>... params) {
        StringBuilder sb = new StringBuilder("(");
        for (Class<?> c : params) {
            sb.append(getDesc(c));//from ww w  .  j a  va2 s.  c o  m
        }
        sb.append(')');
        sb.append(getDesc(returnType));
        return sb.toString();
    }

    public static String getDesc(Method method) {
        StringBuffer buf = new StringBuffer();
        buf.append("(");
        java.lang.Class<?>[] types = method.getParameterTypes();
        for (int i = 0; i < types.length; ++i) {
            buf.append(getDesc(types[i]));
        }
        buf.append(")");
        buf.append(getDesc(method.getReturnType()));
        return buf.toString();
    }

    public static String getDesc(Class<?> returnType) {
        if (returnType.isPrimitive()) {
            return getPrimitiveLetter(returnType);
        } else if (returnType.isArray()) {
            return "[" + getDesc(returnType.getComponentType());
        } else {
            return "L" + getType(returnType) + ";";
        }
    }

    public static String getPrimitiveLetter(Class<?> type) {
        if (Integer.TYPE.equals(type)) {
            return "I";
        } else if (Void.TYPE.equals(type)) {
            return "V";
        } else if (Boolean.TYPE.equals(type)) {
            return "Z";
        } else if (Character.TYPE.equals(type)) {
            return "C";
        } else if (Byte.TYPE.equals(type)) {
            return "B";
        } else if (Short.TYPE.equals(type)) {
            return "S";
        } else if (Float.TYPE.equals(type)) {
            return "F";
        } else if (Long.TYPE.equals(type)) {
            return "J";
        } else if (Double.TYPE.equals(type)) {
            return "D";
        }

        throw new IllegalStateException("Type: " + type.getCanonicalName() + " is not a primitive type");
    }

    public static String getType(Class<?> parameterType) {
        if (parameterType.isArray()) {
            return "[" + getDesc(parameterType.getComponentType());
        } else {
            if (!parameterType.isPrimitive()) {
                String clsName = parameterType.getName();
                return clsName.replace('.', '/');
            } else {
                return getPrimitiveLetter(parameterType);
            }
        }
    }
}

Related

  1. getMethod(Class clazz, String name, String returnType, String[] args)
  2. getMethod(Class parentClass, Class returnType, String methodName, Class... types)
  3. getMethod(Class type, String name, Class returnType, Class paramType, boolean caseSensitive)
  4. getMethodByReturnType(final Class source, final Class type)
  5. getMethodByTypes(Class clazz, String name, Class returnType, Class... parameterTypes)
  6. getMethode(Class clasS, Class Returntype, int modifier, Class... classes)
  7. getMethodGenericReturnType(Method method, Class rawType, int index)
  8. getMethodGenericReturnType(Method method, int index)
  9. getMethodGenericReturnType(Method method, int index)