[英]Returning right Java type of an typed class by passing the class with generics
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: class哪里出错了:
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:总机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[] {});
operationService.invoke(RepairFetcher.class, new Object[] {});
returns an RepairFetcher, which is not so weird because of the argument in the invoke method:返回一个 RepairFetcher,由于 invoke 方法中的参数,它并不奇怪:
Class invoker
Class 调用者
Which returns R, which is a RepairFetcher.class.返回 R,这是一个 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>.我想要的 我不希望调用方法返回 RepairFetcher class,但我希望它返回声明的类型,即 List<String>。
Is this possible, and if so, how to implement this?这可能吗?如果可以,如何实现?
Thanks in advance!提前致谢!
Not sure, but try:不确定,但尝试:
public <T, R extends Invoker<T>> T invoke(Class<R> invoker, Object... parameters) {
The code you posted doesn't compile in the current shape (it misses some parts), but judging from what you're trying to do, you should be aware that Class
is parameterized with the type of the class;您发布的代码没有以当前的形式编译(它遗漏了一些部分),但是从您尝试做的事情来看,您应该知道
Class
是用 class 的类型参数化的; so, the type of the String.getClass()
is Class<String>
.所以,
String.getClass()
的类型是Class<String>
。
That said, your function definition也就是说,您的 function 定义
public <R> R invoke(Class<R> invoker, Object... parameters) {
should probably be something like可能应该是这样的
public <R> R invoke(Class<Invoker<R>> invoker, Object... parameters) {
so you can be sure to return the same type that your invoker returns.因此您可以确保返回与调用者返回的类型相同的类型。
RepairFetcher implement Invoker<List<String>>
so RepairFetcher is a Invoker<List<String>>
and the return is good. RepairFetcher 实现
Invoker<List<String>>
因此 RepairFetcher 是一个Invoker<List<String>>
并且返回良好。
If you test this:如果你测试这个:
RepairFetcher instanceof Invoker<List<String>>
, the answer must be true. RepairFetcher instanceof
Invoker<List<String>>
,答案必须为真。
PS: I replace < by ( because the editor has a problem with < PS:我把 < 换成 ( 因为编辑器对 <
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.