3

Give this:

Class<? extends Enum> enumClass = ...; // being passed in from a constructor
Enum e = Enum.valueOf(enumClass, aString); // produces a warning that looks like

[unchecked] unchecked method invocation: valueOf(java.lang.Class,java.lang.String) in java.lang.Enum is applied to (java.lang.Class,java.lang.String)

I don't want to use generics because that's a major change. I don't want to supress. I don't understand why this warning happens. I imagine it is because one cannot extend an Enum type. I get that. But I don't get why the wildcard class is throwing this weird error. Is there a way to fix this without using @SupressWarning or using generics?

Edit: To Clarify, the following code using generics makes the warning go away.

class Foo<T extends Enum<T>>{
    Class<T> enumClass;
    Enum e = Enum.valueOf(enumClass, aString);
}

The usage of <T> is what I mean by using generics. I can't do that because it would be a huge cascading change.

Amir Raminfar
  • 33,777
  • 7
  • 93
  • 123
  • Are you initializing `enumClass`? Also, I don't understand how you would use generics to resolve this; what did you have in mind? (By the way, you're already using generics in the declaration of `enumClass`; why would it be "a major change" to use generics?) – Ted Hopp Aug 12 '11 at 15:36
  • So final answer, this is not possible without using @SupressWarning. – Amir Raminfar Aug 12 '11 at 16:14

3 Answers3

4

Both Enum and Class are generic. So if you don't want any warnings:

class Foo<T extends Enum<T>>{
    Class<T> enumClass;
    T e = Enum.valueOf(enumClass, str);
}

Or you can have a generic method:

public <T extends Enum<T>> T getEnumValue(Class<T> clazz, String name) {
    T e = Enum.valueOf(clazz, name);
    return e;
}

But if you don't use generics, you are using raw types, and therefore the compiler issues warnings - there is no option apart from suppressing them.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • And just how is "foo" supposed to get resolved by `Enum.valueOf`? – Ted Hopp Aug 12 '11 at 15:49
  • it's a placeholder, of course. I'll replace it with a variable – Bozho Aug 12 '11 at 15:51
  • 1
    But I said I can't use generics for reasons that are not in my control. So how is this helping me? – Amir Raminfar Aug 12 '11 at 15:57
  • @Amir Raminfar but you are using generics in your code - ` etends Enum>`. And if you use raw types - you have warnings. No way around it (check my last paragraph) – Bozho Aug 12 '11 at 16:03
  • I think OP wants to avoid declaring the entire class as generic. A generic method approach might do the trick, though. – Ted Hopp Aug 12 '11 at 16:05
  • technically, you can configure the compiler not to warn you about raw types. But I wouldn't do that. – Bozho Aug 12 '11 at 16:07
  • 1
    Yea, I have spent a good deal of time trying to get around this without creating a warning. Using a generic type is the correct way but I can't because I am modifying someone else's code. I think supressing the warning might be the only solution. It makes sense because enums can't be extended so javac is complaining about not knowing what that type is. I wish there was a better approach. – Amir Raminfar Aug 12 '11 at 16:08
2

This seems to be a compiler bug - it should be an error, not a warning.

When compiling the method invocation expression Enum.valueOf(enumClass...), first, capture conversion is applied to the argument types.

<W extends Enum> // a new type parameter 
Class<W> enumClass; // the type of the argument after capture conversion

Then, type inference is done for Enum.<T>valueOf(enumClass...), the result is T=W.

Then, check the bound of T after substitution, i.e. whether W is subtype of Enum<W>.

(this process is the same for 15.12.2.2 and 15.12.2.3; and 15.12.2.7 definitely yields T=W)

Here, the check should fail. All compiler knows is that W is a subtype of Enum, it cannot deduce that W is a subtype of Enum<W>. (Well, we know that it's true, barring W=Enum; but this knowledge is not present in subtyping rules, so compiler does not use it - we can verify this by playing this example with a MyEnum hierarchy, the compiler will behave the same.)

So why does the compiler pass the bound check with just a warning? There is another rule that allows assignment from Raw to Raw<X> with an unchecked warning. Why this is allowed is another question (it shouldn't be), but compiler does have a sense that Raw is assignable to Raw<X>. Apparently this rule is mistakenly mixed into the above subtype checking step, compiler thinks that since W is Enum, it is somehow also a Enum<W>, the compiler pass the subtype checking with just a warning, in violation of the spec.

If such method invocation shouldn't compile, what is the correct way? I can't see any - as long as the type of the argument enumClass is not already in the recursive form of Class<X extends Enum<X>>, no amount of castings/conversions can make it into that form, therefore there is no way to match the signature of Enum.valueOf method. Maybe javac guys deliberately violated the spec just to make this kind of code compile!

irreputable
  • 44,725
  • 9
  • 65
  • 93
0

If you think about what has to happen inside valueOf, you'll realize that your code cannot possibly work as written. Enum.valueOf needs as an argument an instance of an actual enum class; it then simply iterates through the values() for that class looking for a match.

Because of type erasure, generics won't work in your code. No actual type is being passed into Enum.valueOf.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521