users@jaxb.java.net

Re: Questions on typesafe enum classes

From: Wiedmann, Jochen <jochen.wiedmann_at_softwareag.com>
Date: Tue, 12 Aug 2003 18:37:06 +0200

Hi, Kohsuke,

first of all, thanks for your reply.

> No, you shouldn't be. So this must be a bug.

Should I file a bug report?


> See spec 5.2.3. In particular, it says "An anonymous simple type
definition
> is never bound to a typesafe enum class by default, but it can be
customized
> as described in Section 6.10, <typesafeEnum> Declaration to bind to a
> typesafe enum class."

I may be misunderstaning the reply, but as I understand it, the question was
not,
*whether* typesafe enum classes are generated. Looking at the attached
schema,
xjc is very well creating me a class "LocalStringCl". However, I would
expect,
that "LocalStringCl" would be an inner class of "LocalEnumElement", or
"LocalEnumElementImpl", but not an outer class, as it is. (I am using wsdp
1.2.)



> > 3.) I wondered that the typesafe enum classes to not implement the
> > java.io.Serializable interface.

> With the <xjc:serializable/> customization, it will. By default, it
doesn't.

The attached schema has an element xjc:serializable in the globalBindings
section,
to no effect. I may be doing a mistake, though.


> Again, I thought this should work in the presence of <xjc:serializable />
> Assuming that it doesn't, overriding equals/hashCode as you suggested is
> a wrong fix to the problem. The idea of the type-safe enum is the ability
> to compare objects by ==.

If the class is serializable, then == won't work as expected and custom
hashCode() and equals() methods are required. See the following example.

Regards,

Jochen


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class Main {
  private static class Singleton implements Serializable {
    private final String s = "1";
    public boolean equals(Object o) {
      return o != null &&
              o instanceof Singleton &&
              ((Singleton) o).s.equals(s);
    }
    public int hashCode() { return s.hashCode(); }
    public static final Singleton THE_INSTANCE = new Singleton();
  }

  public static void main(String[] args) throws Exception {
    Singleton s1 = Singleton.THE_INSTANCE;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(s1);
    oos.close();
    ByteArrayInputStream bais = new
ByteArrayInputStream(baos.toByteArray());
    ObjectInputStream ois = new ObjectInputStream(bais);
    Singleton s2 = (Singleton) ois.readObject();
    if (!s1.equals(s2)) System.err.println("s1 is not equal to s2");
    if (s1 != s2) System.err.println("s1 is another object than s2");
  }
}




---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
For additional commands, e-mail: users-help_at_jaxb.dev.java.net