[英]JRuby proc as a Java interface implementation
My question is related to closure conversion . 我的问题与闭包转换有关 。 I want to test a proc that implements a functional interface from jruby code. 我想测试一个从jruby代码实现功能接口的proc。
I have a simple proc like 我有一个简单的过程
proc { print 'hey!' }
to implement a java functional interface like 实现像
public void doStuff()
Since the client object is a Java object that is 由于客户端对象是Java对象,因此
public class MyClient {
private MyInterface iface;
public MyClient(MyInterface iface) {
this.iface = iface;
}
public void doHighLevelStuff() {
this.iface.doStuff();
}
}
it works right, since jruby manages to "cast" the proc into a java functional interface. 它工作正常,因为jruby设法将proc“投射”到Java功能接口中。 So, this part it's ok. 所以,这部分没问题。
My problem is testing this code, calling it from jruby. 我的问题是测试此代码,从jruby调用它。 I don't know how to tell jruby to treat that proc as an implementation of MyInterface
to be able to call the actual method doStuff
instead of the ruby specification call
. 我不知道如何告诉jruby将该proc当作MyInterface
的实现,以便能够调用实际方法doStuff
而不是ruby规范call
。
So, the question is how to tell jruby to treat a proc as a given java interface. 因此,问题是如何告诉jruby将proc视为给定的java接口。
client = MyClient.new { print 'hey! }
client = MyClient.new { print 'hey! }
and then client.doHighLevelStuff
should work just fine ... if you have a proc instance simply pass it in ( &proc
) as a block into the constructor. client = MyClient.new { print 'hey! }
然后client.doHighLevelStuff
应该只是罚款...如果你有一个进程实例仅在(通过它&proc
)作为一个块到构造。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.