1

I have some problems to compile my codes.

$ mvn clean compile 

notify me like this kinds of errors.

[58,30] type parameters of <D,K>D cannot be determined; no unique maximal instance exists for type variable D with upper bounds DS,

Maybe this problem is caused by recursive bounds of generic types. Right?

References: Generics compiles and runs in Eclipse, but doesn't compile in javac

How could I fix this one?

@SuppressWarnings("unchecked")
public static <D extends DataStore<K, T>, K, T extends Persistent> D createDataStore(Class<T> persistent, Properties properties) throws IOException {
    try {
        return (D) (new HBaseStore<K, T>());
    } catch (Exception e) {
        throw new RuntimeException("cannot initialize a datastore", e);
    }
}

public static <DS extends DataStore<U, P>, U, P extends Persistent> DS createDataStore(Class<P> persistent) throws IOException {
    return createDataStore(persistent, null); // ERROR
}
Community
  • 1
  • 1
Jun Young Kim
  • 39
  • 1
  • 7

2 Answers2

1

This means that type D exists as a generic parameter only. For example T can be resolved from method argument Class<T> persistent. You can solve this problem if you change signature of the method to something like:

public static <D extends DataStore<K, T>, K, T extends Persistent> D createDataStore(Class<T> persistent, Class<D> dataStoreType, Properties properties)
AlexR
  • 114,158
  • 16
  • 130
  • 208
0

When types of arguments do not provide enough info for the compiler to infer generic type parameters, you can explicitly give the types parameters. For non-static methods you would say this.<list of type parameters>methodName(...). For an static method like this, you put class name instead of this

public static <DS extends DataStore<U, P>, U, P extends Persistent> DS createDataStore(Class<P> persistent) throws IOException {
    return NameOfThisClass.<DS, U, P>createDataStore(persistent, null); // ERROR
}
Saintali
  • 4,482
  • 2
  • 29
  • 49