问题描述
比方说,我有一个类class Foo : Base
,并且我想使用签名执行特定的方法调用
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$)
是否可以匹配特定Base
子类(在此示例中为Foo
)的任何种类 Iterable
的参数?
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:
$Instance$.$MethodCall$($Parameter$)
变量
$Instance$
最小/最大计数:0,1
$MethodCall
文本/正则表达式:someStuff
$Parameter$
脚本文本:__context__.type.parameters[0].presentableText.contains("Foo")
,表达式类型:List
variables
$Instance$
min/max count: 0,1
$MethodCall
text/regexp: someStuff
$Parameter$
script text: __context__.type.parameters[0].presentableText.contains("Foo")
, expression type: List
我已经提交了错误报告.
这篇关于结构搜索以使方法调用与通用参数匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!