Java 泛型 : How to specify a Class type for a generic typed class?

标签 java generics types

我有一个 POJO 指定为:MyClass<U> , 其中U是泛型类型参数。 我正在尝试编写一个接受类引用的实用方法 Class<T>并填充 Map<String, T> 类型的 map (接受 map 填充)。

这个方法是这样实现的:

static void populateMap(Map<String, T> map, Class<T> type) {

    ...

    // Parses into the specified type and returns an object of that type.
    T obj = parse(..., type);
    map.put (key, obj);
    ...

    return map;
}

这编译得很好。在我的来电者中,我尝试用任何 MyClass 填充 map 实例(不考虑类型)作为值。因此我使用以下代码:

// Loses type information
Map<String, MyClass<?>> m = new HashMap<>();
populateMap(m, MyClass.class);

这不会编译。编译错误:

The method populate(Map<String,T>, Class<T>) in the type ... is not applicable for the arguments (Map<String,MyClass<?>>, Class<MyClass>)

我该如何解决这个问题?

最佳答案

在这种情况下,对 Class<MyClass<?>> 进行未经检查的转换应该是安全的:

// This is okay because we're switching to a type with an unbounded wildcard -
// the behaviors of Class.newInstance and Class.cast are still safe.
@SuppressWarnings("unchecked") 
Class<MyClass<?>> classWithNarrowedType =
        (Class<MyClass<?>>)(Class<?>)MyClass.class;
populateMap(m, classWithNarrowedType);

这是一个笨拙的解决方案,特别是如果您有许多这样的调用站点,但无法回避这样一个事实,即类文字使用原始类型进行参数化,使其用作参数化类型的工厂,如 MyClass<T>天生尴尬。

一个可能更清洁的解决方案将解耦 populateMap来自类文字的使用:

interface Parser<T> {

    T parse();
}

static void populateMap(Map<String, T> map, Parser<T> parser) { ... }

...

Map<String, MyClass<?>> m = new HashMap<>();
Parser<MyClass<?>> myClassParser = new Parser<MyClass<?>>() {
    @Override
    public MyClass<?> parse() {
        return parse(..., MyClass.class);
    }
};
populateMap(m, myClassParser);

顺便说一句,我建议使用更灵活的签名(有关更多信息,请参阅 What is PECS (Producer Extends Consumer Super)?):

static void populateMap(Map<String, ? super T> map, Parser<T> parser)

关于Java 泛型 : How to specify a Class type for a generic typed class?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18940376/

相关文章:

c# - Web 服务中的通用类型 WebMethod

generics - 无法为泛型类型的结构实现fmt::Display

c# - 洗牌堆栈<T>

types - 结构可以在标准 ML 中实现多个签名吗?

Java:公历日期验证问题

javascript - 如何通过 Json 将 Java 方法调用到 JavaScript 中?

java - 计算一个序列

types - 受歧视联盟中的 fsharp 记录

Haskell:Num 是如何保存的?

java - Azure 连接无法连接证书错误