java 指定泛型类型,Java泛型:如何为泛型类型化的类指定类类型?

I have a POJO specified as: MyClass, where U is the generic type parameter.

I am trying to write a utility method which accepts a class reference Class and populates a map of type Map (accepts the map to populate).

This method is implemented like:

static void populateMap(Map map, Class type) {

...

// Parses into the specified type and returns an object of that type.

T obj = parse(..., type);

map.put (key, obj);

...

return map;

}

This compiles fine. In my caller, I attempt to populate a map with any MyClass instance (irrespective of type) as the value. Hence I use the following code:

// Loses type information

Map> m = new HashMap<>();

populateMap(m, MyClass.class);

This does not compile. Compilation error:

The method populate(Map, Class) in the type ... is not applicable for the arguments (Map>, Class)

How can I fix this?

解决方案

In this case it should be safe to do an unchecked cast to Class>:

// 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> classWithNarrowedType =

(Class>)(Class>)MyClass.class;

populateMap(m, classWithNarrowedType);

This is a crufty solution, especially if you have many call sites like this, but there's no getting around the fact that class literals are parameterized with raw types, making their use as factories of parameterized types like MyClass inherently awkward.

A potentially cleaner solution would decouple populateMap from the use of class literals:

interface Parser {

T parse();

}

static void populateMap(Map map, Parser parser) { ... }

...

Map> m = new HashMap<>();

Parser> myClassParser = new Parser>() {

@Override

public MyClass> parse() {

return parse(..., MyClass.class);

}

};

populateMap(m, myClassParser);

As an aside I recommend a more flexible signature (see What is PECS (Producer Extends Consumer Super)? for more info):

static void populateMap(Map map, Parser parser)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值