你能从 JavaScript 扩展抽象 Java 类吗?
我正在开发一个大量使用 javax.script.* 包的项目。我有一种情况,我想创建扩展抽象 Java 类的 JavaScript 对象,就像您可以使用 Invocable.getInterface 来创建实现 Java 接口的 JavaScript 对象一样。这可能吗?如果是这样,你会怎么做?
I'm working on a project that makes really heavy use of the javax.script.* packages. I have a situation where I would like to create JavaScript objects that extend an Abstract Java Class, much like you can use Invocable.getInterface to make JavaScript objects that implement Java interfaces. Is this possible? And, if so, how do you do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,你可以;之前的海报是错误的。请参阅 JavaAdapter 文档。
Yes, you can; previous poster is wrong. See the documentation for JavaAdapter.
除非您想在运行时生成字节码(使用 BCEL 如下),否则不行。您可以使用 代理类 通过接口来完成此操作,但是有与抽象类没有等价物。
如果您确实想尝试 BCEL,最好的策略是这样做:
byte[]
JavaScript 方法。foo.MyAbstractClass
对应于foo.MyAbstractClassDynamicLangWrapper
。Unless you want to go the route of generating bytecode at runtime (using BCEL as below) then no. You can do it with interfaces using proxy classes but there is no equivalent for abstract classes.
If you really want to try BCEL, your best strategy is to do this:
byte[]
of bytecode for a new class that extends the abstract class and delegates every abstract method to JavaScript.foo.MyAbstractClass
corresponds tofoo.MyAbstractClassDynamicLangWrapper
.ClassLoader
that implements findClass to recognize that naming convention and to generate the class bytes and calls defineClass