如何使用Java 8将方法传递给注释? [英] How do I pass a method to an annotation using Java 8?

查看:118
本文介绍了如何使用Java 8将方法传递给注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将方法​​传递给注释.这样可能吗?

I would like to pass a method to an annotation. Is something like this possible?

@MyAnnotation(method = MyClass::myMethod)
private String myVariable;

推荐答案

传递方法不是一种选择.相反,请传递以下内容,该内容应允许您使用反射查找方法.

Passing a method isn't an option. Instead, pass the following which should allow you to find the method using reflection.

@MyAnnotation(clazz=String.class, method="contains", params= {CharSequence.class})

@interface MyAnnotation {
   Class<?> clazz();
   String method();
   Class<?>[] params() default {};
}

MyAnnotation annotation = // get the annotation
annotation.clazz().getMethod(annotation.method(), annotation.params());

这篇关于如何使用Java 8将方法传递给注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆