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.