views:

197

answers:

1

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.

+2  A: 

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);
    }
}
Tom Hawtin - tackline
If you mean Method#setAccessible, i can't do it, because Exception is thrown before I get access to method instance (it occours on call to FetcherInfo.class.getDeclaredMethods()).
jb
Works for me! What are you doing that is odd? Can you write a small yet complete example that demonstrates the issue.
Tom Hawtin - tackline
Dumb, dumb, dumb me. All works now :) the problem was calling FetcherInfo.class.
jb
I think we all do that sort of thing. Frequently.
Tom Hawtin - tackline