如何在Eclipse RCP应用程序中使用java.lang.instrument? [英] How can I use java.lang.instrument in an Eclipse RCP application?

查看:133
本文介绍了如何在Eclipse RCP应用程序中使用java.lang.instrument?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了使用JDK 5中引入的检测功能,您可以使用传递给JVM的 -javaagent 标志。这会将一个Instrumentation类的实例注入静态 premain 方法。例如,在这样的类中:

In order to use the instrumentation features introduced in JDK 5, you can use the -javaagent flag passed to the JVM. This will inject an instance of an Instrumentation class into the static premain method. For example in a class like this:

public class MyClass {
    public static Instrumentation inst;
    public static void premain(String options, Instrumentation inst) {
        MyClass.inst = inst;
    }
}

使用适当的清单文件,您可以将其作为如下:

With an appropriate manifest file, you can run this as follows:

 java -javaagent:myfiles.jar SomeClass

这从 SomeClass 调用premain方法然后 main 。在 Java.SizeOf Project 中使用此方法来猜测Java对象的大致大小。

This calls the premain method then main from SomeClass. This approach is used in the Java.SizeOf Project to guess at the approximate size of a Java object.

好的,现在在Eclipse RCP中每个捆绑包都有它的自己的类加载器。这意味着我们存储在MyClass中的静态Instrumentation对Eclipse应用程序是不可见的。 javaagent使用一个类加载器,Eclipse bundle加载另一个。当我们从插件中访问 MyClass.inst 时,它是 null ,因为那个类与javaagent加载并且名为 premain 的类不同。

OK, now in Eclipse RCP each bundle has its own classloader. This means that the static Instrumentation that we stored in our MyClass is not visible to an Eclipse application. The javaagent uses one class-loader, the Eclipse bundles get loaded with another. When we access MyClass.inst from within a plugin it is null, as that class is not the same class as the one the javaagent loaded and called premain on.

关于可能的其他线索解决方案是在rcp邮件列表上此主题 。但没有结论。

Other clues as to a possible solution are this thread on the rcp mailing list. But nothing conclusive.

有什么方法可以解决这个问题吗? eclipsezone文章中暗示的 Eclipse-BuddyPolicy 听起来不错。我试过了:

Is there any way to work around this? The Eclipse-BuddyPolicy hinted at in the eclipsezone article sounds good. I tried:

Eclipse-BuddyPolicy: app

在我的插件中没有运气。我需要像 Eclipse-BuddyPolicy:javaagent 。有什么想法?

in my plugins without luck. I need something like Eclipse-BuddyPolicy: javaagent. Any ideas?

推荐答案

我认为最简单的解决方案是使用全局属性对象。将检测对象预先存储为全局属性,然后从任何位置访问它(属性对象在所有类加载器中都是相同的):

I think the simplest solution is to use the global properties object. Have pre-main store the instrumentation object as a global properties and then access it from everywhere (the properties object is the same in all class loaders):

public class MyClass {
    private static final String KEY = "my.instrumentation";
    public static void premain(String options, Instrumentation inst) {
        Properties props = System.getProperties();
        if(props.get(KEY) == null)
           props.put(KEY, inst);
    }

    public static Instrumentation getInstrumentation() { 
       return System.getProperties().get(KEY);
    }
}

这篇关于如何在Eclipse RCP应用程序中使用java.lang.instrument?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆