在 GWT-RPC 中使用嵌套枚举

发布于 2024-12-11 02:52:46 字数 393 浏览 2 评论 0原文

我有一个带有嵌套枚举的枚举(我想将其设为私有),但是当我这样做时,GWT 告诉我嵌套枚举不可见并引发异常。

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;
    }
}

如果我从嵌套枚举中删除 private 修饰符,那么代码就可以工作。为什么 GWT 不允许嵌套枚举的 private 修饰符?有解决方法吗?

I have an enum with a nested enum (which I want to make private), but when I do so GWT tells me that the nested enum is not visible and throws an exception.

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 the private modifier from the nested enum then the code works. Why does GWT not allow the private modifier for nested enums? Is there a workaround?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

比忠 2024-12-18 02:52:46

序列化工作得很好,至少对于您提供的示例是这样。所有枚举都按以下方式进行序列化/反序列化(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());
}

例如,我并不关心内部使用什么。只有序数通过网络传递。这意味着其他地方有问题,GWT 根本不关心枚举内部的内容,因为它不是通过网络传输的(枚举应该是不可变的,不需要传输其状态)。我认为你的问题可能是这样的:

public class OuterClass implements Serializable{

    private OuterEnum.NestedEnum nested;
    private OuterEnum outer;

    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;
        }
    }
}

这个例子与前一个例子非常不同。假设 GWT-RPC 服务中使用了 OuterClass。由于 NestedEnum 用作 OuterClass 的字段,因此 GWT 需要为其创建一个 TypeSerializer。但由于 TypeSerializer 是一个单独的类,因此它无法访问 NestedEnum(因为它是私有的)。所以编译失败。

这基本上是您的示例不起作用的唯一情况。某些特定的 GWT 版本中可能存在一些错误,但我 100% 确定您的示例适用于 gwt 2.2-2.4 。

Serialization works just fine, at least with example you have provided. All enums a getting serialized/deserialized in following way(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());
}

E.g. i doesn't matter what is used inside. Only ordinal is passed over the network. It means there is a problem in some other place, GWT simply doesn't care what is inside of enum, because it is not transferred over the network (enum's should be immutable there is no need to transfer its state). I think your problem might be something like this:

public class OuterClass implements Serializable{

    private OuterEnum.NestedEnum nested;
    private OuterEnum outer;

    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;
        }
    }
}

This example is VERY different from previous one. Let's assume that OuterClass is used in GWT-RPC service. Since NestedEnum used as field of OuterClass, GWT needs to create a TypeSerializer for it. But since a TypeSerializer is a separate class, it doesn't have ANY access to the NestedEnum (since it is private). So the compilation fails.

This basically the only case when your example will not work. There might be some bugs in some specific GWT versions, but i'm 100% sure that your example works in gwt 2.2-2.4 .

妄断弥空 2024-12-18 02:52:46

这可能与 JavaScript 有关。在翻译成 JavaScript 时,嵌套类可能被写成非嵌套类。因此,如果它是私有的,则其他任何人都无法访问它。解决方法是不将其设为私有,默认范围应该可以正常工作。

This probably has to do with JavaScript. It is possible / probable that in the translation to JavaScript the nested class is being written as non-nested. Therefore if it is private, nothing else has access to it. Workaround is to not make it private, default scope should work fine.

与他有关 2024-12-18 02:52:46

要成为 RPC 参数类类型,它必须具有用于序列化的空构造函数。
所以我想,如果你添加

private OuterEnum(){
// may be you should give a default value to nestedValue
}

它就会起作用!

To be a RPC parameter class type, it must have a empty constructor for serialization.
So I think, if you add

private OuterEnum(){
// may be you should give a default value to nestedValue
}

It will work !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文