查找RCP应用程序的显示

时间:2023-01-19 13:01:15

I'm writing a testing Framework which is starting a GUI Application. To be able to test this GUI in the case of an SWT application I need to know it's display. In general, this display is loaded by another classloader, therefore I'm using the method findDisplay(Thread t) of the swt Display class by reflection to accomplish this task. My code looks something like this:

我正在编写一个启动GUI应用程序的测试框架。为了能够在SWT应用程序的情况下测试此GUI,我需要知道它的显示。通常,此显示由另一个类加载器加载,因此我通过反射使用swt Display类的方法findDisplay(Thread t)来完成此任务。我的代码看起来像这样:

Thread[] threads = new Thread[10];
Thread.enumerate(threads);
Object foundObject = null;
for (Thread t : Arrays.asList(threads)){
    foundObject = null;
    Class<?> clazz = t.getContextClassLoader().loadClass("org.eclipse.swt.widgets.Display");
    final Method method = clazz.getMethod("findDisplay", Thread.class);
    foundObject = method.invoke(null, new Object[] {t});
    if (foundObject != null) {
        System.out.println("yeah, found it!");
        break;
    }
}

In my opinion this should find every Object of type Display in the current thread group. However I don't get any for the texteditor RCP example although the GUI is starting up perfectly.

在我看来,这应该在当前线程组中找到Display类型的每个Object。然而,我没有得到任何texteditor RCP示例,尽管GUI完美启动。

Any ideas what is going wrong or how I can debug this in a reasonable way?

什么是错误的想法或我如何以合理的方式调试这个?

2 个解决方案

#1


I figured out what the main problem was: The ContextClassloader had nothing to do with the classloader who actually loaded the classes.

我弄清楚主要问题是什么:ContextClassloader与实际加载类的类加载器无关。

To resolve my problem I took care of having the classloader which loads the swt display class both in the hierarchy of the RCP program and the hierarchy of my framework. This was possible by using the java extension classloader. (I couldn't use the application classloader since my RCP application doesn't work with it as parent, I haven't figured out yet why) It was then just a matter of adding the swt.jar to the java.ext.dirs property.

为了解决我的问题,我负责使用类加载器在RCP程序的层次结构和框架的层次结构中加载swt显示类。这可以通过使用java扩展类加载器来实现。 (我无法使用应用程序类加载器,因为我的RCP应用程序不能作为父项使用它,我还没弄清楚为什么)这只是将swt.jar添加到java.ext.dirs的问题属性。

#2


If you are using Eclipse RCP then maybe you can use:

如果您使用的是Eclipse RCP,那么您可以使用:

PlatformUI.getWorkbench().getDisplay()

#1


I figured out what the main problem was: The ContextClassloader had nothing to do with the classloader who actually loaded the classes.

我弄清楚主要问题是什么:ContextClassloader与实际加载类的类加载器无关。

To resolve my problem I took care of having the classloader which loads the swt display class both in the hierarchy of the RCP program and the hierarchy of my framework. This was possible by using the java extension classloader. (I couldn't use the application classloader since my RCP application doesn't work with it as parent, I haven't figured out yet why) It was then just a matter of adding the swt.jar to the java.ext.dirs property.

为了解决我的问题,我负责使用类加载器在RCP程序的层次结构和框架的层次结构中加载swt显示类。这可以通过使用java扩展类加载器来实现。 (我无法使用应用程序类加载器,因为我的RCP应用程序不能作为父项使用它,我还没弄清楚为什么)这只是将swt.jar添加到java.ext.dirs的问题属性。

#2


If you are using Eclipse RCP then maybe you can use:

如果您使用的是Eclipse RCP,那么您可以使用:

PlatformUI.getWorkbench().getDisplay()