intellij-ideastructural-search

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


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.


Solution

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