【发布时间】:2020-04-02 03:14:11
【问题描述】:
假设我有一个调用某个方法的实体
Object methodVal = ety.getClass().getMethod("someMethod").invoke(ety);
我的目标是将其转换为 List 以便使用 addAll 之类的功能,所以我尝试了
List.class.cast(methodVal).addAll((Collection<?>) Objects.requireNonNull(someValue));
//someValue is an Object and I cast it to Collection<?>)
代码运行良好,应用程序仍然可以运行,但是我收到了警告
Unchecked call to 'addAll(Collection<? extends E>)' as a member of raw type 'java.util.List'
我也试过了
((List<?>) methodVal).addAll((Collection<?>) Objects.requireNonNull(someValue));
但是我收到一个错误提示
Required type: Collection <? extends capture of ?>
Provided: Collection <capture of ?>
关于如何修复警告/错误的任何想法?谢谢
【问题讨论】:
-
请发布一个 mcve。我会检查它,但我想从一个完整的工作代码开始,包括
ety和someValue
标签: java spring-boot reflection