About     Search     Feed

Nilesh D Kapadia

Music     Twitter     Github

Suppress unchecked warnings on method calls in Java

Let’s say you have a method call like this:

List<Object> list = (List<Object>) beanWrapper.getPropertyValue(key);

And beanWrapper.getPropertyValue)</a> returns an Object. If you have the “Unchecked generic type operation” compiler setting set to warning, you will get the following warning:

Type safety: Unchecked cast from Object to List<Object>

If you are using Eclipse, it will give you a quick fix to add a @SuppressWarnings(value = "unchecked") annotation, but only give you the option to add it to the current method.

Unchecked quickfix

(the current method in this case is getBean())

But we may not want to suppress warnings for the entire method. We can just put the annotation on the method call itself:

@SuppressWarnings(value = "unchecked")
List<Object> list = (List<Object>) beanWrapper.getPropertyValue(key);

© 2013 Nilesh D Kapadia