It looks like my misconceptions have led me to this question. Please help me sort them out. JLS 9.6.1 states that "Class or an invocation of Class (§4.5)" is covered as an annotation type, chrylis -cautiouslyoptimistic pointed out.
We all agree that a static import is a constant (well, pretend I did not make that statement: It is of course not true https://stackoverflow.com/a/9083023/1236128)
import static path.to.someFunction
I create an annotation like this
@Retention(RetentionPolicy.SOURCE)
@Target({ElementType.METHOD, ElementType.FIELD})
public @interface Testing {
Class<? extends Function> symbol();
}
Given a static function (even declaring this final does not make someFunction
constant by definition in Java, which is throwing me off):
public static Function<Long, Long> someFunction = a -> b;
Excuse me (I realize there is a difference between the function itself and its application),
public static Function<Long, Long> someFunction = a -> 2L;
I create a unit test like this (assuming the aforementioned static import):
@Test
@Testing(symbol = someFunction)
void someFunctionTest() {
....
}
Aside from my mistakes, does anybody recognize how useful this would be at compile time? I could use reflection on all functions marked with say @Tested
to see whether there is indeed a test for it somewhere (on the basis of the @Testing
annotation).
The funny thing is, I am supplying the annotation with a constant, but that is the complaint given by IntelliJ:
Attribute value must be [a] constant.
What's up with that? Is the annotation mechanism trying to execute the function, or will it accept it as a higher order function?