访问 sun.awt 包中的非公共类 [特别是:FetcherInfo]

发布于 2024-07-20 02:54:31 字数 1496 浏览 6 评论 0原文

问题

我的应用程序存在一些性能问题 - 瓶颈是 sun.awt.image.ImageFetcher.run,并且我无法获得任何(更多)有意义的结果来自探查器的信息。 所以我想看看 ImageFetcher 正在做的工作会很不错。

我无法访问包含所有 ImageFetcher 作业的 FetcherInfo 类。 要获取 FetcherInfo 实例,我必须调用 FetcherInfo.getFetcherInfo()

我在包 sun.awt.image 中创建了类(就在我的项目中,我没有修改 rt.jar)。

为了获取FetcherInfo,我使用:

try{
   for(Method method : FetcherInfo.class.getDeclaredMethods()){
      method.setAccessible(true);
      if(method.getName().equals("getFetcherInfo")){
         m = method;
      }
   }
}catch (Exception e){
   e.printStackTrace();
}

FetcherInfo info = null;
try {
   info = (FetcherInfo) m.invoke(null);
} catch (IllegalAccessException e) {
   e.printStackTrace();
} catch (InvocationTargetException e) {
   e.printStackTrace();
}

并且我得到异常:线程“IMAGE-FETCHER-WATCHER”中的异常java.lang.IllegalAccessError:尝试从类访问类sun.awt.image.FetcherInfo sun.awt.image.FetcherDebug

堆栈跟踪指向:

for(Method method : FetcherInfo.class.getDeclaredMethods()){

引发了相同的异常:

 FetcherInfo.class.getMethod("getFetcherInfo");

所以任何人都知道如何:

  • 获取 ImageFetcher 实例
  • 找出正在加载的图像

解决方案

问题是我已将我的类放入 sun.java.awt 包中以访问包受保护的成员,而不将其放入 rt.jar 中,并且调用ImageFetcher.class时抛出异常。

Question:

I have some performance problem in my app - and the bottleneck is sun.awt.image.ImageFetcher.run, and I canno't get any (more) meaningfull info from profiler. So I figured that it would be nice to look at jobs that ImageFetcher is doing.

I couldn't get access to FetcherInfo class, that holds all ImageFetcher jobs. To obtain FetcherInfo instance I have to call FetcherInfo.getFetcherInfo().

I created class in package sun.awt.image (just in my project, i didnt tinker with rt.jar).

To get FetcherInfo i use:

try{
   for(Method method : FetcherInfo.class.getDeclaredMethods()){
      method.setAccessible(true);
      if(method.getName().equals("getFetcherInfo")){
         m = method;
      }
   }
}catch (Exception e){
   e.printStackTrace();
}

FetcherInfo info = null;
try {
   info = (FetcherInfo) m.invoke(null);
} catch (IllegalAccessException e) {
   e.printStackTrace();
} catch (InvocationTargetException e) {
   e.printStackTrace();
}

And I get exception: Exception in thread "IMAGE-FETCHER-WATCHER" java.lang.IllegalAccessError: tried to access class sun.awt.image.FetcherInfo from class sun.awt.image.FetcherDebug

And the stack trace points to:

for(Method method : FetcherInfo.class.getDeclaredMethods()){

The same exception was raised by:

 FetcherInfo.class.getMethod("getFetcherInfo");

So anyone has any ideas how to either:

  • Obtain ImageFetcher instance
  • Find out what images are getting loaded

SOLUTION

The problem was that i've put my class into sun.java.awt package to get access to package protected members, without putting it into rt.jar, and exception was thrown hen calling ImageFetcher.class.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

妄司 2024-07-27 02:54:31

要访问不可访问的成员,请使用setAccessible(true)。 (如果没有安全管理器,就不会阻止 sun.* 类与反射一起使用。)

import java.lang.reflect.Method;

public class Access {
    public static void main(String[] args) throws Exception {
        Class<?> imageFetcher = Class.forName("sun.awt.image.FetcherInfo");
        for (Method method : imageFetcher.getDeclaredMethods()) {
            ;
        }
        Method method = imageFetcher.getDeclaredMethod("getFetcherInfo");
        method.setAccessible(true);
        Object fetcher = method.invoke(null);
        System.err.println(fetcher);
    }
}

To access non-accessible members use setAccessible(true). (Without a security manager present, there is no block on sun.* classes from being used with reflection.)

import java.lang.reflect.Method;

public class Access {
    public static void main(String[] args) throws Exception {
        Class<?> imageFetcher = Class.forName("sun.awt.image.FetcherInfo");
        for (Method method : imageFetcher.getDeclaredMethods()) {
            ;
        }
        Method method = imageFetcher.getDeclaredMethod("getFetcherInfo");
        method.setAccessible(true);
        Object fetcher = method.invoke(null);
        System.err.println(fetcher);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文