集册 Java实例教程 获取方法通用返回类型

获取方法通用返回类型

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

558
获取方法通用返回类型


//package com.nowjava;//from NowJava.com - 时代Java


import java.lang.reflect.Method;

import java.lang.reflect.ParameterizedType;

import java.lang.reflect.Type;


public class Main {

    public static Class<?> getMethodGenericReturnType(Method method) {

        return chooseOneType(method.getReturnType(),

                method.getGenericReturnType());

    }


    static Class<?> chooseOneType(Class<?> literalType, Type genericType) {

        if (genericType instanceof ParameterizedType) {

            return digFromGenericType(genericType);

        }

        return literalType;

    }
    /** from 
    N  o w  J a v a . c o m**/


    static Class<?> digFromGenericType(Type genericType) {

        Type[] typeArguments = ((ParameterizedType) genericType)

                .getActualTypeArguments();

        Type type = typeArguments[0];

        if (type 
展开阅读全文