【发布时间】:2018-05-25 14:53:19
【问题描述】:
希望你一切都好。
这篇文章是关于在检索数据库信息时使用 @SuppressWarnings("unchecked") 时的性能注意事项。我向我公司的领导提出了一个问题,这似乎是一个灰色地带。示例如下。
场景 1:
public List<BusinessObject> retrieveInformation(Long id){
List<? extends Object> info = persistenceService.get("namedQuery", params, values);
//... cast the contents one by one to List<BusinessObject> and return
}
场景 2:
public List<BusinessObject> retrieveInformation(Long id){
List<?> info = persistenceService.get("namedQuery", params, values);
//... cast the contents one by one to List<BusinessObject> and return
}
场景 3:
public List<BusinessObject> retrieveInformation(Long id){
List<BusinessObject> info = (List<BusinessObject>)persistenceService.get("namedQuery", params, values);
//... return the List<BusinessObject>
}
场景 4:
@SuppressWarnings("unchecked")
public List<BusinessObject> retrieveInformation(Long id){
List<BusinessObject> info = persistenceService.get("namedQuery", params, values);
//... return the List<BusinessObject>
}
当然,还有很多方法可以做到这一点,但我关心的是一种以最佳性能执行此过程的方法。 所以,我们需要考虑到,在检索信息的时候,我们可能会在查询中出现错误,并且会出现异常;另外,如果没有这样的错误,我们确实已经知道要检索的对象的类型。
所以,从一个大型商业项目的角度考虑这个问题,所有组件都已到位,比如休眠层、DAO 和 DTO,请您帮我确定:
- 具有最佳性能考虑的选项。
- 如果@SuppressWarnings("unchecked") 注释有性能考虑。
- 如果在此范围内摆脱 @SuppressWarnings("unchecked") 是一种很好的做法。
祝你有美好的一天。
【问题讨论】:
-
由于类型擦除,这些在运行时都没有任何影响。
-
注解在编译时。
-
再问一个问题,这种情况下的警告会被认为是代码异味吗?摆脱它会更好吗?
标签: java performance warnings query-performance