将instanceof与Java枚举一起使用
我遇到了一种情况,我从外部系统接收到一个 enum
,为此我需要返回一个我们自己的 enum
。这两个枚举具有完全相同的文字值:
// externalEnum is guaranteed not to be null
public static MyEnum enumToEnum(final Enum<? extends Enum<?>> externalEnum)
{
if( externalEnum instanceof MyEnum )
{
return (MyEnum)enumType;
}
else
{
return MyEnum.valueOf(externalEnum.name());
}
}
但是,编译器会发出以下警告:
[javac] found : java.lang.Enum<capture#117 of ? extends java.lang.Enum<?>> [javac] required: myEnum [javac] if( externalEnum instanceof MyEnum )
我得到了该函数的一个版本,只需返回 MyEnum.valueOf(externalEnum.name())
-它有效,这才是最重要的。但是,我对编译器错误感到困惑。
在这种情况下,我试图理解泛型的具体化过程。 Enum>
或简单的 Enum
可以是 MyEnum
(假设后者永远不能是子类型以外的任何东西) )
所以 instanceof
测试应该可以工作,但是 Enum
的通用定义中似乎有一些东西(也许是 Enum 无法扩展的事实)导致编译器吐出该特定语句。
对我来说,解决方法很简单,但我喜欢很好地理解这些问题,任何对此的见解都将不胜感激。
- 路易斯.
I have a situation where I'm receiving an enum
from an external system, and for which I need to return an enum
of our own. The two enums have the exact same literal values in them:
// externalEnum is guaranteed not to be null
public static MyEnum enumToEnum(final Enum<? extends Enum<?>> externalEnum)
{
if( externalEnum instanceof MyEnum )
{
return (MyEnum)enumType;
}
else
{
return MyEnum.valueOf(externalEnum.name());
}
}
However, the compiler squeals the following:
[javac] found : java.lang.Enum<capture#117 of ? extends java.lang.Enum<?>> [javac] required: myEnum [javac] if( externalEnum instanceof MyEnum )
I got a version of that function that works by simply returning MyEnum.valueOf(externalEnum.name())
- it works and that's all that matters. However, I'm baffled about the compiler error.
I'm trying to understand the reification process of generics in this case. An instance of Enum<? extends Enum<?>>
or simply Enum<?>
can be a MyEnum
(given that the later can never be anything other than a subtype of the former.)
So the instanceof
test should work, but there seems to be something in the generic definition of Enum
(and perhaps the fact that Enums cannot be extended) that causes the compiler to puke with that particular statement.
The workaround for me is easy, but I like to understand the problems well, and any insight on this will be appreciated.
- Luis.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我对您的问题的(可能有缺陷的)分析是类型定义中的两个通配符彼此分开:
表示“扩展枚举的类型的枚举,该枚举扩展了某些未知类型”。
你想要的可能更像是这样的:
这意味着“扩展枚举自身的类型的枚举”。这是一个奇怪的递归定义(它以某种方式循环回 Comparable),但这就是
Enum
的定义方式。当然,您的静态方法需要采用类型参数
才能正常工作。试试这个:编辑:修复由错误标识符引起的编译器错误使问题中的代码为我编译。所以我的分析绝对是有缺陷的;-)
My (possibly flawed) analysis of your problem is that the two wildcards in your type definition are separate from each other:
means "An Enum of a type that extends Enum that extends some unknown type".
What you want is probably more like this:
which means "An Enum of a type that extends Enum of itself". That is a curious recursive definition (which somehow loops back to
Comparable<T>
), but it's just howEnum
s are defined.Of course your static method would need to take a type parameter
<T>
for this to work. Try this:Edit: fixing the compiler error caused by a wrong identifier makes the code in the question compile for me. So my analysis is definitely flawed ;-)
如果两者相同,为什么还要麻烦呢?
Why even bother if both are the same?
我认为您在第一个返回语句中指的是
externalEnum
而不是enumType
。修复后,它可以在 Eclipse 和 ant 中编译。
I think you mean
externalEnum
rather thanenumType
in your first return statement.That fixed, it compiles in Eclipse and with ant.
如果两个类由不同的类加载器加载和/或具有不同的次要/主要版本,则instanceof将返回false。也要考虑到这一点。
The instanceof will return false if both classes are loaded by different classloader and/or do have a different minor/major version. Take this into account as well.