Java – use nested enumerations in gwt-rpc

I have an enumeration with nested enumerations (I want private), but when I do, GWT tells me that nested enumerations are invisible and throw exceptions

public enum OuterEnum {
    A(NestedEnum.X),B(NestedEnum.Y),C(NestedEnum.X);

    NestedEnum nestedValue;
    private OuterEnum(NestedEnum nv) { nestedValue = nv; }

    private enum NestedEnum {
        X,Y;
    }
}

If I remove private modifiers from nested enumerations, the code works Why does GWT not allow nested private modifiers of enumerations? Is there a solution?

Solution

Serialization works well, at least the example you provided All enumerations are serialized / deserialized in the following ways (GWT 2.4, 2.3, 2.2):

public static OuterEnum instantiate(SerializationStreamReader streamReader) throws SerializationException {
            int ordinal = streamReader.readInt();
            OuterEnum[] values = OuterEnum.values();
            assert (ordinal >= 0 && ordinal < values.length);
            return values[ordinal];
}

    public static void serialize(SerializationStreamWriter streamWriter,OuterEnum instance) throws SerializationException {
            assert (instance != null);
            streamWriter.writeInt(instance.ordinal());
}

For example I don't care what I use internally, only sequentially through the network This means that there is a problem elsewhere. GWT doesn't care what is inside the enumeration at all, because it is not transmitted over the network (the enumeration should be immutable and there is no need to transfer its state) I think your question may be as follows:

public class OuterClass implements Serializable{

    private OuterEnum.NestedEnum nested;
    private OuterEnum outer;

    public enum OuterEnum {
        A(NestedEnum.X),C(NestedEnum.X);

        NestedEnum nestedValue;

        private OuterEnum(NestedEnum nv) {
            nestedValue = nv;
        }


        private enum NestedEnum {
            X,Y;
        }
    }
}

This example is very different from the previous one Suppose outerclass is used in the gwt-rpc service Since nestedenum is used as a field of outerclass, GWT needs to create a typeserializer However, since typeserializer is a separate class, it does not have any access to nestedenum (because it is private) So the compilation failed

This is basically the only case where your example doesn't work There may be some errors in certain GWT versions, but I'm sure your example works in GWT 2.2-2.4

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>