-1

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

Guilherme
  • 456
  • 4
  • 12
Beginner
  • 1,700
  • 4
  • 22
  • 42
  • Please post an mcve. I'd check it but I want to start from a full working code that includes `ety` and `someValue` – Oleg Dec 09 '19 at 02:37
  • take a look at this https://stackoverflow.com/questions/59209426/chaining-a-method-on-dynamic-call/59210009#59210009 – Beginner Dec 09 '19 at 02:38

2 Answers2

1

Just suppress the warning about unchecked assignment, and don't use raw types.

Either annotate the method:

@SuppressWarnings("unchecked")
void myMethod() {
    // ... code here ...

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

    // ... code here ...
}

Or assign to a local variable and annotate there:

@SuppressWarnings("unchecked")
List<Object> list = (List<Object>) methodVal;

list.addAll((Collection<?>) Objects.requireNonNull(someValue));
Andreas
  • 154,647
  • 11
  • 152
  • 247
  • My question is, is it okay or is it a good practice to use that annotation? – Beginner Dec 09 '19 at 02:45
  • @Beginner [Is it a good practice to use suppress warnings in your code?](https://softwareengineering.stackexchange.com/q/323054/202153) – Andreas Dec 09 '19 at 02:47
  • @Beginner [How do I address unchecked cast warnings?](https://stackoverflow.com/q/509076/5221149) – Andreas Dec 09 '19 at 02:48
  • it really feels like there is something wrong when there is a warning... – Beginner Dec 09 '19 at 02:55
  • @Beginner If there's something definitely wrong, it would be an error. The warning is to let you know that standard Java type-safety is not in effect, and that it is *your responsibility* to ensure that only valid object types are inserted into the collection, i.e. the JVM will not throw an exception is an invalid object is inserted. This may cause issues later, when the source of the error can be difficult to ascertain. – Andreas Dec 09 '19 at 04:30
  • So it is okay to use the suppress warning annotation on unchecked cast? sorry im new to java – Beginner Dec 09 '19 at 04:37
  • 1
    @Beginner when you are new to Java, you perhaps should avoid using Reflection. The point of the warning is, you have a reference of type `Object` and don’t know, which actual type parameter the `List` has. You can’t solve this problem, the best you can do, is to tell the compiler that you understood the problem. Whether this will turn out to be a real problem or not, depends on the lists actual type and what you are adding to the list with this unchecked operation. – Holger Dec 09 '19 at 11:00
0

You can solve this by wrapping the "methodVal" in a new List instance so you can use add all. Here is some example code:

public class Main {


    public static void main(String[] args) throws Exception {
        Test a = new Test();

        Object result = Test.class.getMethod("get").invoke(a);

        List<Object> list = new ArrayList<>((Collection<?>) result);

        list.addAll(List.of(7, 8, 9));

        System.out.println(list);
    }


    static class Test {

        public List<Integer> get() {
            return List.of(1, 2, 3, 4, 5);
        }
    }
}
Guilherme
  • 456
  • 4
  • 12