Run Eclipse RCP application with Java instrumentation - eclipse

Using Java instrumentation, we can access a class that is loaded by the Java classloader from the JVM and modify its bytecode by inserting our custom code, all these done at runtime. We need not worry about security, these are governed by the same security context applicable for Java classes and respective classloaders.
We are able to access some java application using this as they run in same classloader.
Now what we are trying to do is to access eclipse RCP application using java instrumentation but in RCP each bundle has its own classloader and our instrumentation code runs with java application classloader.
when we are accessing it, it is throwing "Workbench has not been created yet" exception whereas the workbench is up and running.(I hope this is because of diffrent classloaders for both of them).
I have tried doing thing from here but to no success.
Is there any way we can work RCP application out with java instrumentation.

When you instrument a class, the references of the inserted code are resolved by the ClassLoader of the modified class. If that class loader does not delegate to the application loader, e.g. because it is rule based and doesn’t know your instrumentation specific classes you can’t enforce delegation.
What you can do:
Use access override to define classes in the scope of the loader of the instrumented class. Since defineClass is final the bundle class loader can’t intercept it. However, the references of these injected classes are again resolved by the bundle loader so you have to add all required instrumentation classes this way.
Since the bundle class loader will do the parent loader delegation for the official Java API classes, you can instrument one of the core Java classes to add a helper method which will be called by the instrumented classes and delegates to your instrumentation classes which must have been added to the boostrap loader
You can use the trick described in the answer you have already linked to put a MethodHandle into the system properties. The instrumented classes can retrieve and invoke it as MethodHandle is a core class which will be correctly resolved by the bundle loader and the underlying method can be invoked without having access to its defining class (assuming that parameter and return types are all either primitive types or core classes).

Related

Eclipse Plug-in First executes

From where an eclipse plug in will start?
As for as i know, class which extends abstract class plugin will run first.That class will start the bundle and plug-in.xml file will load.Am i correct?
If i am correct, Please tell me in Plug-in.xml which extension point will execute first and how that is determined ?
It's not as simple as you describe. Most importantly, plug-ins/bundles are initially loaded by the platform without actually executing any code from them. The platform reads and evaluates the bundle manifest plugin.xml in order to know what extension points it uses, but only when one of those is actually invoked (usually triggered by user action) does it execute any code from the plug-in (including instantiating the bundle activator class, which usually is an extension of Plugin).
There are exceptions to this (basically plug-in eager start-up, but that's heavily discouraged), but this is the general concept.
Read more details in the Eclipse wiki, help pages, and API docs.

Inheritance in java, casting subclass to superclass

I am writing a software in java and this inheritance problem always crops up. Funny enough, I don't get problems when i run the program in my IDE (Eclipse Kepler) but once i export it to an executable jar, problem starts. I think I am the only one experiencing this problem because all searches on google show the direct opposite.
My problem is I have an interface named VoteType. This interface is implemented by several concrete classes. I save the concrete implementation to a file using XMLEncoder and retrieve it using XMLDecoder.
But when i try to cast it to the interface it implements VoteType, I get a ClassCastException. What could possibly be causing this?
You are not precise enough to track this down, but I assume that the interface and the object implementing the interface and which is created via XMLDecoder are created by different class loaders. Could that be the reason?
See also this problem: Solution for the ClassCastException due to ClassLoader issue

How to add jars properly to a eclipse bundle

I got a little problem. I want to use hibernate in an eclipse rcp. (i'm new to osgi and eclipse rcp). So I added the jar into the plugin-project folder and the build path and the bundle build path, but when I try to use hibernate from my bundle, it crashes with a ClassNotFoundException.
What is the proper way to do this?
Pls look at eclipse buddy policy. This might help you if you are facing class not getting loaded because of osgi classloading.
Hibernate, and many other classic Java programs (ab)use dynamic class loading to to connect the different parts. They classes they use are read from a file and then loaded with Class.forName. This is fundamentally not-modular since these classes are by definition implementation classes, which should be hidden.
Since OSGi is a modularity framework it puts fences around a module (a bundle) and refuses to load anything that is not properly exported and imported. So if Hibernate does its Class.forName it will run right into this fence, as it should be to get the advantages of modularity.
Eclipse Buddy policy is like a huge hole in this fence, moving things back tot he bad old classpath: linear search. With a buddy policy, Eclipse will just start searching if there is a class somewhere that has that name. Since this ignores versions, you can no longer rely on proper version handling. The good news is that it works most of the time. The bad news is that you loose privacy and when it does not work you get weird errors.
With Hibernate, only solution is to not use the text file setup but use the API and give Hibernate the actual classes. In those cases Hibernate will use the class loader of those classes and that works. In OSGi, as long as you follow the Java language rules there are no problems.
To handle the kind of problems that class loading hacks address OSGi uses services.

Using latest Xerces with Eclipse RCP application

I'm trying to use Xerces 2.11.0 in an Eclipse RCP application, but from everything I've tried, I'm at a loss to figure out how. To complicate matters, I'm also trying to use Batik 1.7.
I've created my own bundles for Xerces and the xml-apis, I've added the additional W3C DOM interfaces that Batik uses to my xml-apis bundle.
The first problem that occurs within Batik
java.lang.NoClassDefFoundError: org/w3c/dom/ElementTraversal
The fundamental reason for this appears to be because org.w3c.dom is contained in the JRE and exposed through org.eclipse.osgi (the system.bundle). This appears to trump any other bundle that wants to provide the package.
Trying to influence the system using require-bundle with my bundle before any others or using import-package with an explicit version leads to errors like
java.lang.LinkageError: loader constraint violation: loader
(instance of org/eclipse/osgi/internal/baseadaptor/DefaultClassLoader)
previously initiated loading for a different type with name
"org/w3c/dom/Document"
Changing the org.osgi.framework.system.packages and org.osgi.framework.bootdelegation parameters to remove org.w3c.dom and adding my bundle to osgi.framework.extensions, leads to erros like
java.lang.LinkageError: loader constraint violation in interface itable
initialization: when resolving method
"javax.xml.parsers.DocumentBuilder.setErrorHandler(
Lorg/xml/sax/ErrorHandler;)V" the class loader
(instance of org/eclipse/osgi/internal/baseadaptor/DefaultClassLoader)
of the current class, org/apache/log4j/xml/DOMConfigurator, and the
class loader (instance of <bootloader>) for resolved class,
javax/xml/parsers/DocumentBuilder, have different Class objects for the
type rrorHandler;)V used in the signature
I've also tried using the java.endorsed.dirs, which makes things break very quickly.
Any ideas what I've missed or may be doing wrong?
We used to have similar problems using DOM level 3 on jdk 1.4 and I think endorsed dirs is the only solution that works because you need to override the DOM APIs in the jdk.
We couldn't face going down this route again so instead ripped out references to ElementTraversal and built Xerces by hand and the problem goes away. If Batik uses ElementTraversal that's not an option though.
I think I've found a solution.
I've modified my xml-apis bundle so that it is a fragment, hosted by the system.bundle.
I've also modified my Xerces bundle to import the packages rather than requiring the xml-apis bundle (which was done originally to convince xerces to use the right packages).
From the testing I've done, this appears to be sufficient to allow each of the bundles to locate the same implementation of org.w3c.dom packages, which contains all the correct classes.

JBoss AS Classloader implementation?

I'm migrating a small tool I wrote for our application that was working on WAS and WLS to JBoss AS 5.1. Basically, the tools helps me to troubleshoot classloading issues: it discovers the classloading hierarchy of a specified class, at runtime, and finds out all classes and jars loaded by each classloader. It is a fairly simple tool - it just uses the Classloader.getPArent() method to get the classloader hierarchy.
The trick that made it work for WLS and WAS was that their classloader implementations either had a 'getClassPath()' method which returned a full list of all classpath entries, or they were instances of the URLClassloader class, and I could use its 'getURLs()' method to get the same info.
Now, it looks like JBoss internal classloaders do not follow the same pattern - so I was wondering if there is some other way to get the same information somehow?
Basically, I want to get a full picture of the classloading hierarchy and which classloader loads which classes/jars at runtime. It has to be a runtime information - because I want to be able to find out this information for any instance of a class, be it a servlet, an EJB, a util class or JSP - so that I could see where in the classloader hierarchy those objects are, what their sibling loaded resources are and what their parent classloaders are.
Thanks!
Marina
In fact this dued to VFS used by JBoss, I've written a post relating this issue and the corresponding solution.