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?
This is currently not possible without resorting to hack. The trick is use a script constraint:
search template
$Instance$.$MethodCall$($Parameter$)
variables
$Instance$
min/max count: 0,1
$MethodCall
text/regexp: someStuff
$Parameter$
script text: __context__.type.parameters[0].presentableText.contains("Foo")
, expression type: List
I have submitted a bug report.
来源:https://stackoverflow.com/questions/45882590/structural-search-to-match-method-call-with-generic-parameter