aspose file tools*
The moose likes Java in General and the fly likes Sun explaining interposing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sun explaining interposing" Watch "Sun explaining interposing" New topic
Author

Sun explaining interposing

Mustafa Garhi
Ranch Hand

Joined: Nov 05, 2008
Posts: 111
Hello everyone,

Please refer to http://java.sun.com/developer/technicalArticles/JavaLP/Interposing/

And then the following explanation:

At class load time, the interposed class must be found before the original one...The interposed class must have the same full name as the original. For example, a class Foo may exist in package pub.foo and create an instance of class Bar from package pub.bar. Creating a file Bar.java in another directory, such as fake/pub/bar and prepending /fake to the classpath, ensures that the JVM will load our definition of class pub.bar.Bar rather than the original. In this way, we can interpose our definitions of Bar's methods over the originals.



In the example that follows Bar.java is the interface - the object of which would be interposed.

I cannot understand what use case is Sun explaining in the quote above. Specific doubts:
1. Which is the original class being mentioned?
2. In what case would I need to have my own fake/pub.bar.Bar and not use the original one (in case i have interpreted the meaning of original correctly). Please explain with example if possible.

Thanks in advane
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  16

There are two versions of pub.bar.Bar: The original one and the one you're using to interpose the original. The second one is located in the /fake folder, while the original is stored normally. There's a problem here though: If the ClassLoader loads /fake/pub.bar.Bar, which has the same name as pub.bar.Bar (the original), then how is the ClassLoader also going to load the original? We need both class definitions, because the fake version just builds on top of the original. The solution is not to use a class with the same name (but loaded from a different location). Instead, the article goes on to explain that the java.lang.reflect.Proxy class can be used to create a fake double of the original class, without running into naming conflicts.

Now, what's the point of all this? The article says that you can use this to add tracing and debugging code to classes, if you don't have their original code available, without drastically changing the code that makes use of these classes.
Mustafa Garhi
Ranch Hand

Joined: Nov 05, 2008
Posts: 111
Thank you mate. The article made much more sense to me after your post.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sun explaining interposing