Intellij structural search for method invocation where class containing method is marked with specific annotation

330 Views Asked by At

Intellij structural search script to to find all method invocations where method source class is marked with @Internal annotation. As for example bellow - script should return a.a();

package com.interestingpackage.internal
import com.common.Internal;

@Internal
public class A {
    public void a() {}
} 
package com.common

public @interface Internal {}
package com.my.service

public class B  {
    private A a;
    void d() {
        a.a();
    }
};

Template

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

Variables

$MethodCall$

[script] = 
import com.intellij.psi.* 
__context__ instanceof PsiMethodCallExpression && 
__context__.resolveMethod() != null &&
java.util.regex.Pattern.matches("com\\.interestingpackage\\.(?!notfrompackage).+", __context__.resolveMethod().getContainingClass().getQualifiedName()) &&
com.intellij.codeInsight.AnnotationUtil.isAnnotated(__context__.resolveMethod().getContainingClass(),"com.common.Internal", true) 

Result Search doesn't return method call a.a(); as a result.

2

There are 2 best solutions below

1
Bas Leijdekkers On BEST ANSWER

Your pattern works for me when I add a [0,∞] count modifier on $parameter$.

0
romanv-jb On

In your example "a" method does not accept any parameters, so it wound not match

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

try using

$Instance$.$MethodCall$()

instead.