Java通用函数:如何返回泛型类型 [英] Java generic function: how to return Generic type
问题描述
这是一个Java通用模式:
public< T> T getResultData(Class< T> resultClass,other_args){
...
return resultClass.cast(T-thing);
典型的调用如下所示:
DoubleBuffer缓冲区;
buffer = thing.getResultData(DoubleBuffer.class,args);
我从来没有想过如何在需要的返回类型是干净的时候使用这个模式,本身就是通用的。要成为'具体',如果像这样的函数想要返回 Map< String,String>
?由于无法为泛型获取类对象,当然,唯一的选择是传递 Map.class
,然后您需要一个转换和一个 @SuppressWarning
毕竟。
最后得到如下的电话:
映射< String,String> returnedMap;
returnedMap = thing.getResultData(Map.class,some_other_args);
现在我们回到需要强制转换并禁止警告的情况。
我想可以从 java.lang.reflect.Type
系列中取代 Class $ c> $ c>,但这些并不是特别容易编制。看起来像:
class Dummy {
Map< String,String>领域;
}
...
类型typeObject = Dummy.class.getField(field)。getGenericType();
在这种情况下,被调用函数可以提取基类型并将其用于转换或newInstance- ing,但是虚拟领域的业务确实看起来很丑。
请注意,像这样的函数并不总是调用 newInstance
。显然,如果他们这样做,他们不需要调用 resultClass.cast
。
在泛型类中输入
。例如,Google Gson(将JSON转换为完全适用的Javabeans,反之亦然)中,有一个 TypeToken
类。您可以如下使用它: 列表< Person> fromJson(json,new TypeToken< List< Person>>(){} .getType());
这种结构正是您所需要的。您可以找到这里来源sode,您可能会发现它也很有用。它只需要额外的 getResultData()
方法,它可以接受 Type
。
Here's a Java generic pattern:
public <T> T getResultData(Class<T> resultClass, other_args) {
...
return resultClass.cast(T-thing);
}
A typical call looks like:
DoubleBuffer buffer;
buffer = thing.getResultData(DoubleBuffer.class, args);
I've never been able to figure out how to use this pattern cleanly when the desired return type is, itself, generic. To be 'concrete', what if a function like this wants to return Map<String,String>
? Since you can't get a class object for a generic, of course, the only option would be to pass Map.class
, and then you need a cast and an @SuppressWarning
after all.
One ends up with a call like:
Map<String, String> returnedMap;
returnedMap = thing.getResultData(Map.class, some_other_args);
Now one is back to needing casts and suppressing a warning.
I suppose that one could take something from the java.lang.reflect.Type
family instead of the Class
, but those aren't especially easy to concoct. That looks like:
class Dummy {
Map<String, String> field;
}
...
Type typeObject = Dummy.class.getField("field").getGenericType();
Given this, the called function could extract the base type and use that for casting or newInstance-ing, but the dummy field business sure looks ugly.
Note that functions like this are not always calling newInstance
. Obviously, if they do, they don't need to call resultClass.cast
.
You cannot do that in the standard Java API. However, there are utility classes available which can get the Type
out of a generic class. In for example Google Gson (which converts JSON to fullworthy Javabeans and vice versa) there's a TypeToken
class. You can use it as follows:
List<Person> persons = new Gson().fromJson(json, new TypeToken<List<Person>>() {}.getType());
Such a construct is exactly what you need. You can find here the source sode, you may find it useful as well. It only requires an additional getResultData()
method which can accept a Type
.
这篇关于Java通用函数:如何返回泛型类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!