dimanche 8 décembre 2019

Java - List cast to be able to use addAll function

Supposed I have an entity that invokes some method

Object methodVal = ety.getClass().getMethod("someMethod").invoke(ety);

My goal is to cast it to List in order to user the function like addAll, so I tried

List.class.cast(methodVal).addAll((Collection<?>) Objects.requireNonNull(someValue)); 

//someValue is an Object and I cast it to Collection<?>)

The code is working fine and the app is still can run, however I'm getting a warning saying

Unchecked call to 'addAll(Collection<? extends E>)' as a member of raw type 'java.util.List'

and also I tried

((List<?>) methodVal).addAll((Collection<?>) Objects.requireNonNull(someValue));

however I'm getting an error saying

Required type: Collection <? extends capture of ?>
Provided: Collection <capture of ?>

Any idea on how can I fix the warning / error? Thanks





Aucun commentaire:

Enregistrer un commentaire