Search code examples
intellij-ideafluentstructural-search

Matching chained method calls with structural search


I try to match this kind of method calls of some fluent API. There can be an arbitrary number of method calls (at least 2 calls, but no upper limit). The whole expression should be matched. Actually, the goal is to find chained method calls in a fluent api which omit doIt(), as in this case the fluent API does nothing.

FooClass.some("fluent")
        .api()
        .bar(()->"somelambda")
        .doIt();

I tried something like

FooClass.$a$($b$) 

and use different "occurence counts" like 0,∞ for $a$, and 0,1 for $b$, but this still only matches FooClass.some("fluent")


Solution

  • Assuming all the fluent api methods return an instance of FooClass, the following should work. Start with the existing template method calls and add a semicolon to search for statements:

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

    Click Edit variables:

    1. set Expression type of Instance to FooClass
    2. set Text/Regexp of MethodCall to doit and enable Invert condition