问题描述
我想知道Java7的新的 invokedynamic
字节码指令是否可以用于实现 Java语言。 java.lang.invoke下的新API是否有助于执行此类操作? 我正在考虑的情况如下。 (这看起来像访客设计模式的应用案例,但可能有原因,这不是一个可行的选择。)
class A {}
class A1 extends A {}
class A2 extends A {}
class SomeHandler {
private void doHandle(A1 a1){.. 。}
private void doHandle(A2 a2){...}
private void doHandle(A a){...}
public void handle(A a){
MultipleDispatch.call(this,doHandle,a);
}
}
库类 MultipleDispatch
然后会做一些类似的事情:
class MultipleDispatch {
public静态对象调用(Object receiver,String method,Object ... arg){
//这样的字节码
#invokeDynamicdoHandlesomeBootstrap
}
static CallSite someBootstrap {
//解析动态方法调用。
}
}
(我知道,但可以以纯Java的方式实现吗?)
由于我没有invokedynamic的经验,我不知道性能和类型安全性会有多好,但只能给一些指针:
- 提供多个通过invokedynamic调度(请参阅多调度/ src / invokedynamicmultipledispatch /);
- Charles Oliver Natter具有。幻灯片根本不详细,但我想我在一个关于JAX2012的视频或播客中看到更多的细节,我现在找不到。
- 是没有invokedynamic的替代方法。 和有很多性能信息/基准测试(在完成您的分析之后,它们是一个很好的阅读);
I wondered if Java7's new invokedynamic
bytecode instruction could be used to implement multiple dispatch for the Java language. Would the new API under java.lang.invoke be helpful to perform such a thing?
The scenario I was thinking about looked as follows. (This looks like an application case for the visitor design pattern, but there may be reasons that this is not a viable option.)
class A {}
class A1 extends A {}
class A2 extends A {}
class SomeHandler {
private void doHandle(A1 a1) { ... }
private void doHandle(A2 a2) { ... }
private void doHandle(A a) { ... }
public void handle(A a) {
MultipleDispatch.call(this, "doHandle", a);
}
}
The library class MultipleDispatch
would then do something of the kind:
class MultipleDispatch {
public static Object call(Object receiver, String method, Object...arg) {
// something like that in byte code
#invokeDynamic "doHandle" "someBootstrap"
}
static CallSite someBootstrap {
// resolve that dynamic method call.
}
}
(I am aware of MultiJava, but can this be achieved in a Java-pure fashion?)
Since I have no experience with invokedynamic, I do not know how good the performance and type-safety would be, but can only give some pointers:
- the Da Vinci Machine Project offers multiple dispatch via invokedynamic (see Multiple Dispatch/src/invokedynamicmultipledispatch/);
- Charles Oliver Natter has talked about applications of invokedynamic on JAX2012. The slides do not go into details at all, but I think I came across more details in a video or podcast talking about JAX2012, which I cannot find right now.
- Christopher Dutchyn's JVM Multiple Dispatch is an alternative approach without invokedynamic. His COOTS '01 Paper and these slides have a lot of performance information/benchmarking (and are a good read after you've finished your disseration ;)
这篇关于使用invokedynamic实现多个调度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!