File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Comparison between classloaders? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Comparison between classloaders?" Watch "Comparison between classloaders?" New topic

Comparison between classloaders?

Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

I have been trying to accomplish this for a while now, and there seems to be on easy way to accomplish it.

I have a class

Instances of this class may be loaded by seperate class loaders. The requirement is that no matter what classloader its loaded from, the instances are equal if the IDs are equal.

I have not found a good way to perform this comparison. The only way I have found so far is to serialize the instances into the same classloader, and perform the comparison there.

Alternately I could compare the class name. But classes could have the same name and be loaded from different library files. I suppose this is unavoidable since the only difference in this case IS the classloader possibly. I also found that its probably sensible to use some special technique to compare the SerialVersionID of the class too. Which I can do.

The bottom line is that I seem to be relegated to comparying class name, SerialVersionID, and the ID variable of the class. This is the only way without serializing the class.

Hmm, perhaps I could look at comparator interface?

Any tips?
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
Surf over to and look for Java Tip 133. It will give you pointers for solving this problem.
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Thats a novel idea, I didnt think about enums. I'm familiar with the techniques. However, they won't work here. The comparison already works when the class I have is in the same classloader. The problem is it fails when the classes are in seperate classloaders. So does their technique. Not only that, they bail calling it too much work and probably unnecessary

ahh well. ILl just have to serialize before I compare.
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

Can't you load the class in a higher classloader so each section sees the same version of the loaded class? The other option is the 'Jini solution', where you can parse for and test the ID without deserialising the stream, but that's probably more effort than it's worth unless you do it a billion times a day.
jQuery in Action, 3rd edition
subject: Comparison between classloaders?
It's not a secret anymore!