Check if function passed to a method marked with annotation

Viewed 60

Is there a way to check if function passed to a method marked with annotation or no?

Here is a code snippet below for better understanding what I want to get.

public class AnnotationExample {

    static void method(Function f) {
        // how to check here is 'f' annotated with @Example or no?
        System.out.println(f.check(5));
    }

    @Example
    static boolean testWithAnnotation(int k) {
        return k == 5;
    }

    static boolean testWithoutAnnotation(int k) {
        return k != 5;
    }

    public static void main(String[] args) {
        method(AnnotationExample::testWithAnnotation);
        method(AnnotationExample::testWithoutAnnotation);
    }
}

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface Example {
    String value() default "example";
}

@FunctionalInterface
interface Function {
    boolean check(int k);
}
0 Answers
Related