I want to write an Annotation Processor to check that a Method is called only in specific places. For example:
interface Command {
@MustOnlyBeCalledByWorker
void execute();
}
class Worker {
void work(Command cmd) {
cmd.execute(); // This is ok for the annotation processor
}
}
class Hacker {
void work(Command cmd) {
cmd.execute(); // annotation processor gives an error
}
}
I already have an annotation processor with @SupportedAnnotationTypes("*")
that uses the Compiler Tree API to get all MethodInvocationTree
s.
I thought that from there, I could get the Declaration of the called method.
Now I can easily get the method name and the argument expressions.
But say I also want to distinguish between overloaded execute()
methods with the same number of arguments.
Do I need to handle the whole overload resolution myself? I think this would also mean to manually resolve the static types of all arguments, and in some cases even the type arguments of other methods.
So here is my Question: How can I get the correct declaration of a potentially overloaded method? Maybe I can somehow get it out of a JavacTask
?
I am using the IntelliJ IDEA 14 and Oracle's Java 8 compiler. For now Support for Language Level 7 would be sufficient, but a solution with Java 8 Support is preferred.