Skip to Content.
Sympa Menu

xom-interest - Re: [XOM-interest] ClassLoader issues

xom-interest AT lists.ibiblio.org

Subject: XOM API for Processing XML with Java

List archive

Chronological Thread  
  • From: Remko Popma <remko.popma AT azzurri.jp>
  • To: John Cowan <jcowan AT reutershealth.com>
  • Cc: XOM-interest <XOM-interest AT lists.ibiblio.org>
  • Subject: Re: [XOM-interest] ClassLoader issues
  • Date: Fri, 20 Sep 2002 03:39:52 +0900 (JST)

I understand the problem of the == operator not working as expected.
What I meant was, in situations where you have different classloaders,
not only typesafe enums behave unexpectedly, *every class* suddenly
has problems with their equals() method.

It may be possible to work around this (your solution is an example),
but the standard/recommended equals() implementation (according to Bloch)
will not work:

class Foo {
// "standard" implementation
// (will unexpectedly return false if o is a Foo instance
// loaded by different class loader, even if it has identical field
values)
public boolean equals(Object o) {
if (o == this) {
return true;
}
if ((o instanceof Foo) == false) {
return false;
}
Foo other = (Foo) o;
return (mField1 == null ? other.mField1 == null :
mField1.equals(other.mField1))
&& (mField2 == null ? other.mField2 == null :
mField2.equals(other.mField2))
;
}
}

In that respect, the classloader issue affects every class
and is not a typesafe enum issue per se.
The question is whether working in an environment with multiple classloaders
is a design goal, as it will affect design and implementation considerably,
not only whether typesafe enums should be used or not.

Remko


--- John Cowan <jcowan AT reutershealth.com> wrote:
>
> Remko Popma scripsit:
>
> > 1) Is this really a typesafe enum issue?
> > Basically, as far as I understand it, neither the == operator nor the
> > equals() method works if you compare two instances loaded by separate
> > classloaders. So eg. someNode.equals(sameNodeLoadedByOtherClassloader)
> > would also return false.
>
> Typesafe enums are in competition with plain old integers, which don't care
> about class loaders. So yes, it's a typesafe enum issue in that you don't
> get the expected results with enums whereas you do with ints.
>
> Here's a possibility:
>
> int theCode;
>
> private SomeEnumClass(int code) {theCode = code;}
> public int hashCode {return theCode;}
> public boolean equals(Object o) {
> if
> (!o.getClass().getName().equals("blah.blah.SomeEnumClass")) {
> return false;
> }
> else {
> return theCode == o.hashCode();
> }
>
> Note the importance of testing the class *name* for equality, not the
> class object. That way, we do the hashCode check iff this is the same
> class independent of class loaders.
>
> --
> John Cowan <jcowan AT reutershealth.com>
> http://www.reutershealth.com http://www.ccil.org/~cowan
> Yakka foob mog. Grug pubbawup zink wattoom gazork. Chumble spuzz.
> -- Calvin, giving Newton's First Law "in his own words"
>




Archive powered by MHonArc 2.6.24.

Top of Page