0

For converting a integer into an enum (out of a json file), I declared some HashMaps like this:

final static HashMap<Integer, Types> getType = new HashMap<Integer, Types>() {
    {
        put(1, Types.TYPE1);
        put(2, Types.TYPE2);
        ....
    }
};

Now eclipse gives me the following warning:

The serializable class  does not declare a static final serialVersionUID field of type long

I already found a pretty good explanation for the error message here. However I don't know how this is with GWT. Does GWT use the SerialVersionUID anywhere, or is it actually deleted by the closur compiler? As far as I see it, it isn't needed.

Community
  • 1
  • 1
Stefan
  • 14,826
  • 17
  • 80
  • 143

1 Answers1

1

serialVersionUID is only used for Java serialization (using the JVM Serializable contract), which is (quite obviously) not supported in GWT.

That means you can safely ignore the warning; and you can also safely add a serialVersionUID to appease Eclipse, as it'll be pruned by the compiler.
(I believe you could also configure Eclipse so it doesn't generate the warning)

Also, you shouldn't initialze your HashMap that way (that is, create an anonymous subclass of HashMap that initializes itself in its initializer). You'd better use a static initializer:

final static Map<Integer, Types> getType;
static {
   getType = new HashMap<Integer, types>();
   getType.put(1, Types.TYPE1);
   getType.put(2, Types.TYPE2);
   ...
   // you could even use Collections.unmodifiableMap here
}

or use Guava's ImmutableMap: final static Map getType = ImmutableMap.builder() .put(1, Types.TYPE1) .put(1, Types.TYPE2) ... .build();

Better yet, if your integer values map with the enum constants' declaration order, you can simply use Enum#values() instead of map:

Types typeFromInt(int i) {
   return Types.values()[i];
}
Thomas Broyer
  • 64,353
  • 7
  • 91
  • 164
  • thanks, ImmutableMap was right what I wanted! Types.values()[i]; doesn't work for me, since the numers are not from one to 10 but 1,2, 5, 6...... :/ – Stefan Mar 26 '12 at 10:00