使用Rhino编写Eclipse脚本:类加载器属于提供Rhino的插件,而不是使用它的插件

发布于 2024-08-20 10:39:08 字数 835 浏览 5 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

戏剧牡丹亭 2024-08-27 10:39:08

有没有办法改变Rhino使用的类加载器

Rhino应该使用当前线程的ContextClassLoader。尝试 Thread.setContextClassLoader(不要忘记恢复它)。

如果这不起作用,也许您可​​以 创建您自己的 Rhino ContextFactory

公共最终无效 initApplicationClassLoader(java.lang.ClassLoader 加载器)

设置搜索 Java 类时要使用的显式类加载器。

Is there a way to change the classloader used by Rhino

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:

public final void initApplicationClassLoader(java.lang.ClassLoader loader)

Set explicit class loader to use when searching for Java classes.

不气馁 2024-08-27 10:39:08

我不知道 Rhino 的具体情况,但您可以考虑使用 Eclipse“伙伴类加载”和“注册”策略。

Rhino 的插件(例如,net.weissmann.tom.rhino)将通过在其 MANIFEST 中指定 Eclipse-BuddyPolicy: Registered 来声明自身“开放扩展” .MF。带有 Rhino 应该能够看到的类的插件将指定 Eclipse-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 specifying Eclipse-BuddyPolicy: registered in its MANIFEST.MF. Plug-ins with classes that Rhino should be able to see would specify Eclipse-RegisterBuddy: net.weissmann.tom.rhino and would need a bundle-level dependency on net.weissmann.tom.rhino.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文