It's not a secret anymore!
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

Sun explaining interposing

Mustafa Garhi
Ranch Hand

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

Please refer to

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 and create an instance of class Bar from package Creating a file in another directory, such as fake/pub/bar and prepending /fake to the classpath, ensures that the JVM will load our definition of class rather than the original. In this way, we can interpose our definitions of Bar's methods over the originals.

In the example that follows 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/ 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

Joined: Sep 20, 2010
Posts: 4627

There are two versions of 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/, which has the same name as (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.
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:
subject: Sun explaining interposing
jQuery in Action, 3rd edition