简体   繁体   English

Java静态方法,通配符/通用返回类型

[英]Java Static Method, Wildcard / Generic Return Type

I'm building an API, which uses abstract classes, to invoke static methods. 我正在构建一个使用抽象类的API来调用静态方法。 Some Java Pseudocode: 一些Java伪代码:

public abstract class APIBaseClass {

    public static List<?> myMethod(Class<?> clazz, MyConverter converter, List<Object> objects) {
        return objects.stream()
                  .map(object -> converter.convertObjectToType(object, clazz))
                  .collect(Collectors.toList());
    }

}

And, where myMethod is invoked: 并且,在调用myMethod地方:

public abstract class MyAPIClass extends APIBaseClass {

    public static List<MyClassType> invokingMethod(MyConverter converter, List<Object> objects) {
        List<MyClassType> list = myMethod(MyClassType.class, converter, objects);
        ...
        return list;
    }
}

myMethod compiles, as expected. myMethod将按预期myMethod编译。 And, invokingMethod does not compile, also as expected, due to incompatible types: 而且,由于类型不兼容, invokingMethod也无法按预期进行编译:

Required: List<blah.blah.blah.MyClassType>
Found:    List<capture<?>>

It compiles without errors or warnings if I: (1) @SuppressWarnings("unchecked") on invokingMethod , and (2) cast the result of myMethod to a list of MyClassType , when invoked, ie List<MyClassType> list = (List<MyClassType>) myMethod(MyClassType.class, converter, objects); 如果我:(1)invokingMethod上的@SuppressWarnings(“ unchecked”),并且(2) invokingMethodmyMethod的结果MyClassTypeMyClassType的列表,则它可以编译而没有错误或警告,即List<MyClassType> list = (List<MyClassType>) myMethod(MyClassType.class, converter, objects); . However, there's no transparency that this would be necessary to anybody using the API, which feels less than ideal. 但是,没有透明度对于使用API​​的任何人都是必要的,这感觉并不理想。

What are my options for myMethod returning the a list of the type it is invoked with to invokingMethod , while maintaining my abstract classes and static methods, while ideally improving usability within invokingMethod ? 我的myMethod在将其调用的类型的列表返回给invokingMethod ,我有什么选择,同时保留我的抽象类和静态方法,同时理想地提高invokingMethod可用性?

Any suggestions or alternative ideas are welcome. 欢迎任何建议或替代想法。

Thanks in advance. 提前致谢。

The fact that your classes are abstract is completely irrelevant. 您的类是抽象的事实是完全不相关的。 If you make them abstract to prevent their instantiation, and use inheritance to avoid having to specify the class name, then that's an antipattern, which doesn't use abstract classes for polymorphism as they're intended to be used. 如果将它们抽象化以防止其实例化,并使用继承避免指定类名,则这是一种反模式,它不打算将抽象类用于多态性,因为它们打算被使用。 Just add a private constructor to the classes, and use static imports. 只需在类中添加一个私有构造函数,然后使用静态导入即可。

Now, regarding the question itself, you need to make the method generic, and the MyConverter.convertObjectToType() method generic, too: 现在,关于问题本身,您需要使方法通用,并使MyConverter.convertObjectToType()方法也通用:

public static <T> List<T> myMethod(Class<T> clazz, MyConverter converter, List<Object> objects) {
    return objects.stream()
              .map(object -> converter.convertObjectToType(object, clazz))
              .collect(Collectors.toList());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM