intellij-ideastructural-search

Structural Search to match method call with generic parameter


Let's say that I have a class class Foo : Base and I want to perform a certain method call with signature

public void someStuf(Iterable<? extends Base> param)

For the search template I just take as starting point the pre-existing one

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

Is it possible to match a parameter of any kind Iterable of a specific Base subclass (Foo in this example)??

List<Foo> fooList = new ArrayList<>();
fooList.add(new Foo("A"));
List<Bar> barList = new ArrayList<>();
barList.add(new Bar(1));

someStuff(fooList); // find this!
someStuff(barList); // don't find this one
someStuff(Collections.singletonList(new Foo("B"))); // also match this one

I've tried several combinations without any luck, is it even possible to do?


Solution

  • This was previously not possible without resorting to a hack. See further below for that. Currently, the code can be found using a Type modifier.

    search template

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

    variables

    $Instance$ Count=[0,1]
    $MethodCall Text=someStuff
    $Parameter$ Type=Iterable<Foo>, within type hierarchy

    hack

    The hack previously needed used a Script modifier and a simple Type modifier on the $Parameter$ variable:

    $Parameter$
    Script=__context__.type.parameters[0].presentableText.contains("Foo")
    Type=Iterable

    The related bug report is fixed since IntelliJ IDEA 2017.3.