简体   繁体   English

通过将 class 与 generics 传递,返回正确的 Java 类型 class

[英]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.

相关问题 Java Generics:如何为泛型类指定Class类型? - Java Generics: How to specify a Class type for a generic typed class? 用泛型类型java扩展类 - extending a class with a generics type, java 导致类扩展该类型的Java类泛型? - Java class generics that cause class to extend that type? Java 方法返回 java.lang.Class<!--? extends T--> 用于传递到 generics - Java method returning a java.lang.Class<? extends T> for passing into generics 我们可以通过传递类类型作为参数来使用 Java 泛型创建不同的类实例吗 - Can we create different class instance using Java generics by passing class type as argument Java泛型 - 参数化类与类型化方法 - Java generics - parameterized class vs. typed method Java泛型。 如何正确扩展扩展通用抽象参数类型的类的参数类型的类 - Java Generics. How to properly extend parameter typed class that extends Generic abstract parameter typed class 为什么 Java generics 不允许返回它自己的类型,如果类型是从另一个 class 扩展而来的? - Why does Java generics doesnot allow returning its own type if the type is extended from anotehr class? Java中的泛型-抽象类的类型擦除 - Generics in Java - type erasure with abstract class Java泛型类参数类型推断 - Java Generics Class Parameter Type Inference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM