Skip to Content.
Sympa Menu

xom-interest - [XOM-interest] Type safe enums, class loaders and equals

xom-interest AT lists.ibiblio.org

Subject: XOM API for Processing XML with Java

List archive

Chronological Thread  
  • From: Elliotte Rusty Harold <elharo AT metalab.unc.edu>
  • To: xom-interest <xom-interest AT lists.ibiblio.org>
  • Subject: [XOM-interest] Type safe enums, class loaders and equals
  • Date: Fri, 17 Jan 2003 20:17:10 -0500

Going over old messages about XOM, I thought I had fixed the
problems with class loading and type safety in the
Attribute.Type inner class. Turns out I was wrong.
Here's the currnet equals() method:

public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof Type)) return false;
Type t = (Type) o;
return t.type == this.type;
}

The problem is if two Attribute.Type objects are loaded
by sifferent class loaders, then for one of them
it will not be an instanceof Type and can't be cast to it,
nor can I acces sthe private parts of at least one.

The way this is traditionally fixed for a singleton
is to test the clas sand package name instead of
the content. However, this isn't a singleton. there are
ten legal objects of this class, so the fact that the
class and package names are equal does not prove
that the objects are equal.

I can't see a way around this, short of reverting to
integer type codes, which I hate! The only other
thing I can think of is ten singleton subclasses of
Attribute.Type, one for each type (also yuck.)

Thoughts? Suggestions?
Does anyone know how to write a good equals method
that works in a multi-classloader environment?

--
Elliotte





Archive powered by MHonArc 2.6.24.

Top of Page