How do I pass a method to an annotation using Java 8?

Frudisch :

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

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

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());

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=445233&siteId=1