How to solve unchecked cast

110 Views Asked by At

I am trying to setget the ENUM value of a class' ENUM field. "value" is actually a string value read from file. Retrieving, but retrieving it as below throws me an unchecked cast warning:

Object value = "BAR";
Field field = Test.class.getField("bar");

if (field.getType().isEnum()) {
   value = Enum.valueOf((Class<Enum>) field.getType(), (String) value);
   Test test = new Test();
   field.set(test, value);                  
}

...

public class Test {
   public Foo bar;
}

...

public enum Foo {
   BAR;
}

The warning is clear, but I am confused how to address/solve this warning, any thoughts?

[unchecked] unchecked method invocation: method valueOf in class Enum is applied to given types
  required: Class<T>,String
  found:    Class<Enum>,String
  where T is a type-variable:
    T extends Enum<T> declared in method <T>valueOf(Class<T>,String)
0

There are 0 best solutions below