The easiest to explain my problem is with an example. I have a compilation problem:
The method setOptions(ArgBuilder<Option<? extends Command>>)
in the type Command is not applicable for the arguments (ArgBuilder<Option<T>>)
but T is declared as a generics that extends Command so I don't understand why this doesn't work.
This compilation problem can be replicated with the 4 classes below, the compilation problem will appear in the Steps
class I pasted at the c.setOptions(options);
line.
I would like to know if there exist any way to workaround this compilation error.
package xyz.com;
import java.lang.reflect.Constructor;
public class Main {
public abstract class Option<T extends Command>{}
public abstract class Command{
protected ArgBuilder<Option<? extends Command>> options;
void setOptions(ArgBuilder<Option<? extends Command>> options){
this.options = options;
}
}
public class ArgBuilder<T extends Option<? extends Command>>{}
public class Steps<T extends Command>{
protected Class<T> newCommandClass;
protected ArgBuilder<Option<T>> options;
public Command build(){
try {
Constructor<T> ctor = newCommandClass.getConstructor();
Command c = ctor.newInstance();
c.setOptions(options);
return c;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
}