在 GWT-RPC 中使用嵌套枚举
我有一个带有嵌套枚举的枚举(我想将其设为私有),但是当我这样做时,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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
序列化工作得很好,至少对于您提供的示例是这样。所有枚举都按以下方式进行序列化/反序列化(GWT 2.4、2.3、2.2):
例如,我并不关心内部使用什么。只有序数通过网络传递。这意味着其他地方有问题,GWT 根本不关心枚举内部的内容,因为它不是通过网络传输的(枚举应该是不可变的,不需要传输其状态)。我认为你的问题可能是这样的:
这个例子与前一个例子非常不同。假设 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):
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:
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 .
这可能与 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.
要成为 RPC 参数类类型,它必须具有用于序列化的空构造函数。
所以我想,如果你添加
它就会起作用!
To be a RPC parameter class type, it must have a empty constructor for serialization.
So I think, if you add
It will work !