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:
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
.
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);
}
}