Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Classloader hierarchy for java file with same name and package  RSS feed

 
Mahi Vuppula
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have situation where I I have a implementation file with 6 methods but its interface has only 4 methods. This interface is part of a jar file. I would like to include other 2 methods in the interface.

So I have created a interface with same name and same package structure in my web application project so that I can add other two methods.

Question: I would like to know if there will be a situation interface from the jar file will be picked during run time. My assumption is that classes from web-inf/class will take precedence over the files in the jar.

Please correct me if my assumption is in correct.
 
Jayesh A Lalwani
Rancher
Posts: 2762
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot add methods to an interface. You can however extend an interface, and it sounds like you are trying to do that in a convoluted way. Can you describe in more detail what problem you are trying to solve? It seems to be that you some problem X, and you try to solve it with the wrong solution Y. You are asking questions about solution Y, instead of explaining that your real problem is X.
 
Mahi Vuppula
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Our project uses abc.jar. It has class TestImpl.java which implements TestIntf.java. TestImpl.java has 6 methods in it but the interface exposes only four of methods. I need the other two methods that TestImpl.java has implementation methods.

I do not want to use the implementation class directly in my application. So I have copied source of TestIntf.java and added the other two method signatures that TestImpl.java already has exposed.

abc.jar
TestImpl.java implements TestIntf.java
- methodA -methodB -methodC -methodD -methodE - methodF

TestIntf.java
- methodA -methodB -methodC -methodD

TestIntf.java does not have -methodE - methodF

This is a spring configured project.
My application needs methodE and methodF of TestImpl.java.

so I have duplicated file TestIntf.java with the same package structure that has been defined in abc.jar in my application.

and added all the methods defined in TestImpl.java without changing the actual implementation file.
- methodA -methodB -methodC -methodD -methodE - methodF

so that I can access -methodE - methodF from my application class MyBusinessService. I have autowired TestIntf.java in my class MyBusinessService

I just want to make sure whether will there be any situation where MyBusinessService points to abc.jar- TestIntf.java during run time. This might cause run time exception if it happens in that way.

 
Jayesh A Lalwani
Rancher
Posts: 2762
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that when the classloader finds 2 classes with the same name in the classpath, it will decide to pick one. You don't have control over it. THe classloader is implemented in the JVM. So, in one JVM the classloader might pick one class, and it another one it might pick the other. If you reply on the classloader to load the class from your jar instead of abc.jar, your implementation will become platform dependent.

There are multiple ways you can make your implementation platform independent

a) Just inject TestImpl into your service class rather than trying to trick the classloader. Is there a reason why you can't do this?
b) Ask whoever has made abc.jar for you to add those methods to the interface. This is the least hackiest solution of all
c) Hack the jar and replace testinf.java with your own. If you have to hack, you have to hack. Atleast this hack doesn't break platform independence
d) This might be the most most complicated of all, but is also the cleanest hack. Deifine your own interface that extends TestInf and adds the 2 methods . Let's call it TestInf2 . Implement a "bridge" class that implements TestInf2, and wraps an instance of TestImpl. The wrapper class can just pass all the calls through to TestImpl. Now inject TestInf2 in your service classes.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!