java-反射-Method类走读

背景

之前走读了Class类的函数,能让我们更灵活的获取一个类中的信息。然而仅有类信息还是不够的,想要达到灵活调用类中函数的功能还需要借助于reflect包的Method类,本博文将走读Method类的主要方法。

代码+实战结果

Main.java

public class Main {
    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException {
        // 获取本类所有的方法
        Method[] methods = Main.class.getDeclaredMethods();
        for (Method method : methods) {
            // public static void com.ztjy.Main.main(java.lang.String[]) throws java.lang.NoSuchMethodException
            // public void com.ztjy.Main.demoTest(java.lang.Integer,java.lang.String) throws java.lang.Exception
            System.out.println(method);
        }
        // 2
        System.out.println(methods.length);
        // 获取Method方法对象
        Method method = Main.class.getMethod("demoTest", Integer.class, String.class);
        // demoTest
        System.out.println(method.getName());
        // 获取方法注解(如果没有的话则返回null)
        // @com.ztjy.DemoAnnotation(value=123)
        System.out.println(method.getAnnotation(DemoAnnotation.class));
        // void (包含了泛型)
        System.out.println(method.getGenericReturnType());
        // void
        System.out.println(method.getReturnType());
        // sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@1376c05c 还不太清楚这个东西
        System.out.println(method.getAnnotatedReceiverType());
        // sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@51521cc1 还不太清楚这个东西
        System.out.println(method.getAnnotatedReturnType());
        // void 还不太清楚这个东西
        System.out.println(method.getAnnotatedReturnType().getType());
        // @com.ztjy.DemoAnnotation(value=123)
        for (Annotation annotation : method.getDeclaredAnnotations()) {
            System.out.println(annotation);
        }
        // class com.ztjy.Main
        System.out.println(method.getDeclaringClass());
        // my-defaultValue,这可以获取注解中函数的默认值
        System.out.println(DemoAnnotation.class.getMethod("value").getDefaultValue());
        // class java.lang.Exception
        for (Class<?> exceptionType : method.getExceptionTypes()) {
            System.out.println(exceptionType);
        }
        // class java.lang.Exception
        for (Type genericExceptionType : method.getGenericExceptionTypes()) {
            System.out.println(genericExceptionType);
        }
        // class java.lang.Integer
        // class java.lang.String
        for (Type genericParameterType : method.getGenericParameterTypes()) {
            System.out.println(genericParameterType);
        }
        // true
        System.out.println(method.getModifiers() == Modifier.PUBLIC);
        // 获取参数的注解
        // @javax.validation.constraints.NotNull(message={javax.validation.constraints.NotNull.message}, groups=[], payload=[])
        for (Annotation[] parameterAnnotation : method.getParameterAnnotations()) {
            for (Annotation annotation : parameterAnnotation) {
                System.out.println(annotation);
            }
        }
        // 2
        System.out.println(method.getParameterCount());
        // class java.lang.Integer
        // class java.lang.String
        for (Class<?> parameterType : method.getParameterTypes()) {
            System.out.println(parameterType);
        }
        // java.lang.Integer arg0
        // java.lang.String arg1
        for (Parameter parameter : method.getParameters()) {
            System.out.println(parameter);
        }
        // 参考 https://www.zhihu.com/question/54895701
        System.out.println(method.isBridge());
        // 参考 https://blog.csdn.net/u010003835/article/details/76850242,default关键字只能在接口中使用,具体参考博文
        System.out.println(method.isDefault());
        // 没太明白。。。。参考:https://blog.csdn.net/feier7501/article/details/19088361
        System.out.println(method.isSynthetic());
        // false,可变参数
        System.out.println(method.isVarArgs());
        // 权限是否可访问,false
        System.out.println(method.isAccessible());
        // true
        System.out.println(method.isAnnotationPresent(DemoAnnotation.class));
        // 1123
        method.invoke(Main.class.newInstance(), 1, "123");
    }

    @DemoAnnotation(value = "123")
    public void demoTest(@NotNull Integer integer, String string) throws Exception {
        System.out.println(integer + string);
    }
}

DemoAnnotation.java

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface DemoAnnotation {
    String value() default "my-defaultValue";
}

运行结果如下:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值