使用Rhino编写Eclipse脚本:类加载器属于提供Rhino的插件,而不是使用它的插件
我正在使用 Rhino 编写 Eclipse (RCP) 应用程序的脚本。问题是,从 Javascript 中,我只能访问提供 Rhino 的插件可用的类,而不能访问运行脚本的插件可用的所有类。
显而易见的答案是将 Rhino 放入脚本插件中,但这不起作用,因为它已经由应用程序自己的插件之一提供(它也提供了我需要编写脚本的东西),并且 Eclipse 始终使用此版本而不是版本离手更近。
- 有没有一种方法可以更改 Rhino 使用的类加载器
- ,或者是否可以确保 Eclipse 从一个插件而不是另一个插件加载 Rhino 类?
感谢 Thilo 的回答,我用了这个:
import net.weissmann.tom.rhino.Activator; // Plugin activator class
import org.mozilla.javascript.tools.shell.Main;
public class JSServer extends Thread {
//[...]
public void run() {
// recent versions of the Main class kindly export
// the context factory
Main.shellContextFactory.initApplicationClassLoader(
Activator.class.getClassLoader()
) ;
//[...]
}
I am using Rhino to script an Eclipse (RCP) application. The problem is that from Javascript I only have access to classes available to the plugin that provides Rhino, and not to all the classes available to the plugin that runs the scripts.
The obvious answer would be to put Rhino in the scripting plugin, but this doesn't work because it's already provided by one of the application's own plugins (which also provides things I need to script) and Eclipse always uses this version instead of the version closer to hand.
- Is there a way to change the classloader used by Rhino
- or is it possible to ensure that Eclipse loads the Rhino classes from one plugin rather than another?
Thanks to Thilo's answer I used this:
import net.weissmann.tom.rhino.Activator; // Plugin activator class
import org.mozilla.javascript.tools.shell.Main;
public class JSServer extends Thread {
//[...]
public void run() {
// recent versions of the Main class kindly export
// the context factory
Main.shellContextFactory.initApplicationClassLoader(
Activator.class.getClassLoader()
) ;
//[...]
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Rhino应该使用当前线程的ContextClassLoader。尝试 Thread.setContextClassLoader(不要忘记恢复它)。
如果这不起作用,也许您可以 创建您自己的 Rhino ContextFactory:
Rhino should be using the current Thread's ContextClassLoader. Try Thread.setContextClassLoader (don't forget to restore it).
If that does not work, maybe you can create your own Rhino ContextFactory:
我不知道 Rhino 的具体情况,但您可以考虑使用 Eclipse“伙伴类加载”和“注册”策略。
Rhino 的插件(例如,
net.weissmann.tom.rhino
)将通过在其MANIFEST 中指定
。带有 Rhino 应该能够看到的类的插件将指定Eclipse-BuddyPolicy: Registered
来声明自身“开放扩展” .MFEclipse-RegisterBuddy: net.weissmann.tom.rhino
并且需要对net.weissmann.tom 的捆绑包级别依赖。犀牛。
I don't know Rhino specifics, but you could consider using Eclipse "buddy classloading" with the "registered" policy.
Rhino's plug-in (
net.weissmann.tom.rhino
, say) would declare itself "open to extension" by specifyingEclipse-BuddyPolicy: registered
in itsMANIFEST.MF
. Plug-ins with classes that Rhino should be able to see would specifyEclipse-RegisterBuddy: net.weissmann.tom.rhino
and would need a bundle-level dependency onnet.weissmann.tom.rhino
.