1

I am migrating a java application from Java7 to Java8. The existing code compiles and runs fine with Java7 but not with Java8. It is basically giving me this ERROR:

incompatible types: inference variable E has incompatible upper bounds java.lang.Enum

Below is the method which results in Compilation Error:

public static <E extends Enum<E>> E checkAndConvertEnum(Class<E> enumClass, String valueToConvert)
{
    checkMandatory(Class.class, enumClass);
    Pair<E, ClassB> value= checkAndConvertEnumInternally(enumClass, valueToConvert);
}
private static <E extends Enum<E>> Pair<E, ClassB> internCheckAndConvertEnum(Class<E> enumClass,
            String valueToConvert) {

        if (null != valueToConvert) {
            // --- First try it with the enum-literal
            try {
                E result = Enum.valueOf(enumClass, valueToConvert);
                if (null != result) {
                    return new Pair<E, ClassB>(result, null);
                } // --- end_if
            } catch (Exception ignore) {
            }
return new Pair<E, ClassB>(null, new Incident(GlobalErrCodes.CANNOT_CONVERT_ENUM,
                enumClass.getSimpleName(), valueToConvert));
}

Any help in this regard would help. Thanks in Advance!

Salonee
  • 21
  • 3
  • 3
    Please post the entire code. – Couper May 11 '20 at 17:16
  • This is the only place where the error occurs, and there are different methods which are doing something else all together – Salonee May 12 '20 at 07:34
  • I *assume* the problem is on the assignment, but in order to comment about the assignment, we need to know what each of the names in that statement refers to, in particular we need the exact signature of `checkAndConvertEnumInternally`. Also the type hierarchy of `ClassB` _could_ be relevant. – Stephan Herrmann May 12 '20 at 16:49
  • Hi Stephan, i have added the code snippets for checkAndConvertEnumInternally and ClassB is just an interface. Please let me know if this helps in identifying the root cause – Salonee May 14 '20 at 16:19

1 Answers1

-1

As per Java convention, T - Type E - Element K - Key N - Number V - Value

So here any class extending Enum should be of type E . Better to go for wildcard instead (?) . This might help you .

How to use Class<T> in Java?