I have developed a Java agent which uses the Attach API to attach to Eclipse. I want to use reflection so I can get the UI Thread and dispatch some calls. The problem is I can't get the org.eclipse.swt.Display class through reflection. I get the ClassNotFoundException. Any ideas why this happens and how I can successfully use reflection in this case?
Asked
Active
Viewed 67 times
0
-
1Eclipse uses a very elaborate classpath system with each plug-in having its own classpath. I have no idea how you handle that in a Java agent with JNI. – greg-449 Nov 03 '21 at 10:06
-
1“a Java agent which uses the JNI Attach API” sounds like a contradiction. When it uses JNI, it is a native Agent. Or you mean, it uses the Attach API (resp. the Instrumentation API on the other side), without prepending “JNI” to it. Depending on what your software actually is, you need a different approach. A native Agent would have to use JVM TI, a Java Agent would use the Instrumentation API. – Holger Nov 03 '21 at 16:33
-
Thanks @Holger, edited my question, I actually use the Instrumentation API via this https://github.com/apangin/jattach cli tool. – crankedrelic Nov 03 '21 at 16:38
-
2So your best option is to iterate over [all loaded classes](https://docs.oracle.com/en/java/javase/17/docs/api/java.instrument/java/lang/instrument/Instrumentation.html#getAllLoadedClasses()) until you found an SWT specific one. Once you have such a class, you [know its loader](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Class.html#getClassLoader()) and can use it directly to ask for other SWT classes. If no SWT specific class has been loaded yet, there’s nothing in this API that’d help. – Holger Nov 03 '21 at 16:54
-
Well, you could try to find a path from some root to that class - like, where is that class loader for that class stored, and hack your way through the different objects. – Johannes Kuhn Nov 03 '21 at 19:20