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

1
6 comments

Hello,

Why don't you add @MyAnnotation in search template in structural search?

 

0
Avatar
Permanently deleted user

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

0

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

0
Avatar
Permanently deleted user

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

0

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.

0

Add Structural Search Inspection…

$Instance$.$MethodCall$($Parameter$)

Attach Count=[0,∞] to $Parameter$

Attach script to $MethodCall$

String annotationName = "myPackage.MyAnnotation";
MethodCall.resolveMethod().hasAnnotation(annotationName)

Or, if your annotation defines a boolean inherited();

String annotationName = "myPackage.MyAnnotation";
MethodCall.resolveMethod().hasAnnotation(annotationName) ||
Arrays.stream(MethodCall.resolveMethod().findSuperMethods())
.filter(method -> method.hasAnnotation(annotationName))
.anyMatch(method -> {
    method.getAnnotation(annotationName).findAttributeValue("inherited").getValue() == Boolean.TRUE;
})

Unfortunately I couldn't find a way to set tool tip text via script, very useful for custom warnings supplied by the annotation.

0

Please sign in to leave a comment.