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.
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.
The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Joined: Nov 05, 2008
Thank you mate. The article made much more sense to me after your post.