[英]How to use generics in a java method
I'm having trouble adding generics to a piece of code that I'm working on, I've searched around and none of the examples I've seen so far quite capture what I've trying to do so I'm reaching out for help. 我在向正在处理的一段代码中添加泛型时遇到了麻烦,我进行了搜索,到目前为止,我所看到的所有示例都没有捕捉到我正在尝试做的事情,因此我正在尝试求助。
Currently I have this code which processes a list of UserDTO
objects that have only their ID's populated and gets the full details of each User
from a restful web-service: 当前,我有这段代码可以处理
UserDTO
对象的列表, UserDTO
对象仅填充了其ID,并从一个宁静的Web服务获取每个User
的完整详细信息:
//userIds and userDetails declared previously
for (UserDTO user : userIds) {
UserDTO populatedUser = webResource.path(REST_USER_PATH).path(user.getId().
toString()).type(MediaType.APPLICATION_XML).get(ClientResponse.class).
getEntity(UserDTO.class);
userDetails.add(populatedUser);
}
Now this is going to be a pattern for the piece of work I'm currently undertaking, I'll need to be able to convert lists of DTO
objects which have just the Id's populated, to lists of fully populated DTO
objects by calling a web service. 现在,这将是为这块我目前正在开展工作的方式,我需要能够转换列表
DTO
具有对象只是标识的人口,到完全填充的名单DTO
对象通过调用一个Web服务。 What I'd like to do is create a generic method to let me do this. 我想做的是创建一个通用方法让我做到这一点。
All the different DTO
objects that I need to do this for extend our BaseDTO
so I came up with the following, unfortunately it does not compile but hopefully it will show what I'm trying to accomplish: 我需要执行此操作以扩展
BaseDTO
所有不同DTO
对象,因此我BaseDTO
了以下内容,不幸的是它没有编译,但希望它将显示我要完成的工作:
public <T extends BaseDTO> getListOfPopulatedDTOs(
List <T extends BaseDTO> unpopulatedDTOs, String restPath) {
List<BaseDTO> populatedDTOs = new ArrayList<BaseDTO>();
for (BaseDTO unpopulatedDTO : unpopulatedDTOs) {
BaseDTO populatedDTO = webResource.path(restPath).path(
unpopulatedDTO.getId().toString()).type(MediaType.APPLICATION_XML).
get(ClientResponse.class).getEntity(T.class);
populatedDTOs.add(populatedDTO);
}
return populatedDTOs;
}
Any help or advice would be gratefully received. 任何帮助或建议将不胜感激。 Many thanks in advance :)
提前谢谢了 :)
You don't need to give the bounds again in the formal parameter. 您无需在形式参数中再次给出界限。 And also
T.class
is not valid. 而且
T.class
无效。 You've to pass the Class<T>
explicitly to the method. 您必须将
Class<T>
显式传递给方法。 You can change your method to something like this: 您可以将方法更改为如下所示:
public <T extends BaseDTO> List<T> populateDTOList(List<T> unpopulatedDTOs, Class<T> clazz, String restPath) {
List<T> populatedDTOs = new ArrayList<T>();
for (T unpopulatedDTO : unpopulatedDTOs) {
T populatedDTO = webResource.path(restPath)
.path(unpopulatedDTO.getId().toString())
.type(MediaType.APPLICATION_XML)
.get(ClientResponse.class)
.getEntity(clazz);
populatedDTOs.add(populatedDTO);
}
return populatedDTOs;
}
Just make sure you have the getId()
method declared in type BaseDTO
, else you won't be able to access it. 只要确保您具有在
BaseDTO
类型中声明的getId()
方法,否则您将无法访问它。
Also, you're missing the return type of the method. 另外,您缺少该方法的返回类型。 I've put it here.
我把它放在这里了。 And the method is better named as
populateDTOList
. 而且该方法更好地命名为
populateDTOList
。
You need to declare type information only one time.In a generic method,you do this before teh return type,not in the parameter itself.Just change it to this :- 您只需要声明一次类型信息。在泛型方法中,您可以在返回类型之前执行此操作,而不是在参数本身中执行。只需将其更改为:
public <T extends BaseDTO> getListOfPopulatedDTOs(List <T> unpopulatedDTOs, String restPath) {
List<BaseDTO> populatedDTOs = new ArrayList<BaseDTO>();
for (BaseDTO unpopulatedDTO : unpopulatedDTOs) {
BaseDTO populatedDTO = webResource.path(restPath).path(unpopulatedDTO.getId().toString()).type(MediaType.APPLICATION_XML)
.get(ClientResponse.class).getEntity(T.class);
populatedDTOs.add(populatedDTO);
}
return populatedDTOs;
Generics are only compile time checks. 泛型仅是编译时检查。 So you can't do
T.class
because T and all other generic information gets removed during compiling. 因此,您无法执行
T.class
因为T和所有其他常规信息在编译期间会被删除。
Your interface could be something like: 您的界面可能类似于:
public <T extends BaseDTO> List<T> getListOfPopulatedDTOs(List <T> unpopulatedDTOs, String restPath)
And you can do getEntity(unpopulatedDTOs.get(0).getClass())
(check that the list is not null or 0 size) 您可以执行
getEntity(unpopulatedDTOs.get(0).getClass())
(检查列表是否不为null或0大小)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.