访问 sun.awt 包中的非公共类 [特别是:FetcherInfo]
问题:
我的应用程序存在一些性能问题 - 瓶颈是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要访问不可访问的成员,请使用
setAccessible(true)
。 (如果没有安全管理器,就不会阻止sun.*
类与反射一起使用。)To access non-accessible members use
setAccessible(true)
. (Without a security manager present, there is no block onsun.*
classes from being used with reflection.)