简体   繁体   中英

Why ”Type Bound mismatch: The type ? extends T is not a valid substitute for the bounded parameter <E extends Enum<E>> of the type Enum<E>“?

I am trying to read the hadoop 1.0.0 source code in eclipse. I downloaded the source code first and then use ant eclipse to build the project. After that, I successfully created the project in eclipse. But there is an error Type Bound mismatch: The type ? extends T is not a valid substitute for the bounded parameter <E extends Enum<E>> of the type Enum<E> Type Bound mismatch: The type ? extends T is not a valid substitute for the bounded parameter <E extends Enum<E>> of the type Enum<E> on the line 396 of Gridmix.java . The error code:

private <T> String getEnumValues(Enum<? extends T>[] e) {
  StringBuilder sb = new StringBuilder();
  String sep = "";
  for (Enum<? extends T> v : e) {
    sb.append(sep);
    sb.append(v.name());
    sep = "|";
  }
  return sb.toString();
}

Enum itself is generic (in pure Java) with restriction on the parameter T so:

`Enum<T extends <Enum<T>>`

You don't have any restriction on T in your code, so compiler complains, because You may end up with T = Object , but Enum cannot accept Object as T .

So you have to restrict T in your code as well:

private <T extends Enum<T>> String getEnumValues(Enum<? extends T>[] e) {
    StringBuilder sb = new StringBuilder();
    String sep = "";
    for (Enum<? extends T> v : e) {
        sb.append(sep);
        sb.append(v.name());
        sep = "|";
    }
    return sb.toString();
}

In fact in this case the wildcard wouldn't make sense, because you cannot extend T (because you cannot extend any specific enum ). But that should already compile. If not, drop the wildcard.

I see, it's not your code. So probably some older Java didn't check this properly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM