• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Sun explaining interposing

 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Saloon Keeper
Posts: 14314
321
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you mate. The article made much more sense to me after your post.
 
Beauty is in the eye of the tiny ad.
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic