For a better understanding of my question, see the following classes:
public interface Invoker<R> {
public String getName();
public R getResult();
}
Implementation:
public class RepairFetcher implements Invoker<List<String>> {
@Override
public String getName() {
return this.getClass().getSimpleName();
}
@Override
public List<String> getResult() {
return new ArrayList<String>();
}
}
The class where it goes wrong:
public class OperationService {
public <R> R invoke(Class<R> invoker, Object... parameters) {
WebserviceOperation<R> operation = new WebserviceOperation<R>(invoker);
Invoker<R> instance = operation.getInstance();
return instance.getResult();
}
}
The main class:
public class Main {
public static void main(String[] args) {
OperationService operationService = new OperationService();
operationService.invoke(RepairFetcher.class, new Object[] {});
}
}
The problem:
At this moment the method
operationService.invoke(RepairFetcher.class, new Object[] {});
returns an RepairFetcher, which is not so weird because of the argument in the invoke method:
Class invoker
Which returns R, which is a RepairFetcher.class.
What I want
I don’t want the invoke method to return the RepairFetcher class, but I want it to return the declared Type, which is List< String>.
Is this possible, and if so, how to implement this?
Thanks in advance!
Not sure, but try: