Inspection: Show all method call to a method with a specific custom annotation
Answered
Hello
I am looking for an Inspection to show all method call to a method with a custom annotation. I try the structural search inspection with the following template: "$Instance$.$MethodCall$($Parameter$)" my problem, this template show all method call. But I would like to have only calls to methods with the annotation @MyAnnotation. Have you any idea?
Thank for help
Please sign in to leave a comment.
Hello,
Why don't you add @MyAnnotation in search template in structural search?
Hello Yaroslav
Thank you for your fast answer.
Du you mean I should insert the following in the Structural Search?
@MyAnnotation
$Instance$.$MethodCall$($Parameter$)
In my following code example:
public class MyClass {
public static void main(String[] arg){
MyClass myClass = new MyClass();
myClass.sayHello();
myClass.getName();
}
@MyAnnotation
public void sayHello(){
System.out.println("Hello");
}
public String getName(){
return "xxx";
}
}
The search result are not only myClass.sayHello(). Unfortunately I get also myClass.getName() and System.out.println("Hello").
I don’t see what I do wrong.
Thank you for your help
Sorry, I misunderstood your case. There is no need to add annotation.
Have you configured filters for your template: https://www.jetbrains.com/help/idea/search-templates.html ?
You need to set "Instance" to "MyClass" type and "MethodCall" to "sayHello" text
Hello Yaroslav
Thank you very much for your answer.
No, I think I need the annotation. I want to find the line myClass.sayHello() because the method is annotated with @MyAnnotation and not because it has a certain name. I want to add the annotation to various places and all these places should be found then.
Have a good day
There is "Reference Filter" where you could select "annotated methods": https://www.jetbrains.com/help/idea/search-templates.html
The only limitation is that it's not possible to set exact annotation.
Feel free to create feature request for that on YouTrack: http://youtrack.jetbrains.com/issues/IDEA.
Add Structural Search Inspection…
Attach
Count=[0,∞]to$Parameter$Attach script to
$MethodCall$Or, if your annotation defines a
boolean inherited();Unfortunately I couldn't find a way to set tool tip text via script, very useful for custom warnings supplied by the annotation.