反射详解二

  • 获取方法的返回值类型
        方法                                 描述
Type getGenericReturnType()     返回表示返回值类型的Type对象
Class<?> getReturnType()         返回表示返回值类型的Class对象

这两个方法的区别主要是在返回值类型上:
-Type是一个接口
-Class是一个Type接口的子类.
-Type接口只有一个方法
-Class类方法比较多

  • Type接口方法
        方法                                                         描述
default String getTypeName()     Returns a string describing this type, including information about any type parameters.

 

 这个方法会返回返回值类型的全限定名,如:
-java.lang.String
-void

Class类获取类名方法

    方法                                                                 描述
String getName()/getSimpleName()     Returns the name of the entity (class, interface, array class, primitive type, or void) represented by this Class object, as a String.

这个方法也会返回类的全限定名,和上面的一样.

获取方法的返回值类型名称

所以如果想获取一个方法的返回值类型的名称,使用如下两种方式都可以:

  • method.getReturnType().getName();
  • method.getGenericReturnType().getTypeName();

封装成方法

 

public static String getMethodReturnTypeName1(Method method) {
    return method.getGenericReturnType().getTypeName();
}

public static String getMethodReturnTypeName2(Method method) {
    return method.getReturnType().getName();
}

 

程序示例

复制代码
package returntype;

public class Return {

    public String returnStringMethod() {
        System.out.println("调用返回值为String的方法");
        return "返回值";
    }

    public void returnVoidMethod() {
        System.out.println("调用void方法");
    }
}
复制代码

测试类:

复制代码
package returntype;

import java.lang.reflect.Method;
import java.lang.reflect.Type;

public class TestMethodReturnType {
    public static void main(String[] args) throws Exception {
        // 加载类
        Class<?> class1 = Class.forName("returntype.Return");
        //testReturnType1(class1);
        testReturnType2(class1);
    }

    private static void testReturnType2(Class<?> class1) {
        Method[] methods = class1.getDeclaredMethods();
        for (Method method : methods) {
            System.out.print("方法名:" + method.getName());
            System.out.print(" 返回值类型1:" + getMethodReturnTypeName1(method));
            System.out.println(" 返回值类型2:" + getMethodReturnTypeName2(method));
        }
    }

    private static void testReturnType1(Class<?> class1) {
        // 获取所有的方法
        Class returnType1;
        Type returnType2;
        Method[] methods = class1.getDeclaredMethods();
        for (int i = 0; i < methods.length; i++) {
            returnType1 = methods[i].getReturnType();
            returnType2 = methods[i].getGenericReturnType();
            System.out.print("方法名称:" + methods[i].getName());
            System.out.print(" 返回值类型1:" + returnType1.getName());
            System.out.println(" 返回值类型2:" + returnType2.getTypeName());
        }
    }

    public static String getMethodReturnTypeName1(Method method) {
        return method.getGenericReturnType().getTypeName();
    }

    public static String getMethodReturnTypeName2(Method method) {
        return method.getReturnType().getName();
    }

}
复制代码

运行结果:

方法名:returnVoidMethod 返回值类型1:void 返回值类型2:void
方法名:returnStringMethod 返回值类型1:java.lang.String 返回值类型2:java.lang.String

 

 

posted @   Bonnie_ξ  阅读(142)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话
点击右上角即可分享
微信分享提示