Java Reflection Generic Type getGenericTypeName(Method method)

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

Description

Get generic type of specific method's return type in string.

License

Open Source License

Parameter

Parameter Description
method a parameter

Declaration

public static String getGenericTypeName(Method method) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.lang.reflect.Method;

public class Main {
    /**//w  ww . j a  v a 2s .c  o  m
     * Get generic type of specific method's return type in string.
     * <p>
     * If the return type of method is not generic, null will be returned.
     * </p>
     * @author Fangwei_Cai
     * @create 2016-7-23 15:56:32
     * @param method
     * @return
     */
    public static String getGenericTypeName(Method method) {

        String genericTypeName = null;

        String returnTypeName = method.getGenericReturnType().toString();
        if (returnTypeName.matches(".*<.*>")) {
            genericTypeName = returnTypeName.substring(returnTypeName.indexOf("<") + 1,
                    returnTypeName.indexOf(">"));
        }

        return genericTypeName;
    }
}

Related

  1. getGenericType(Object target)
  2. getGenericType(Type genType, int index)
  3. getGenericType(Type t, int index)
  4. getGenericType(Type type)
  5. getGenericTypeArguments(Method m, int i)
  6. getGenericTypes(Constructor targetConstructor, Integer constructorArgOrderingNumber)
  7. getGenericTypes(final Type type)