A library I'm using (and can't change) has a raw Map being returned to me, so I want to suppress the warning about an unchecked cast. It seems that it isn't working when I try it on an instance variable though.
I've enumerated the cases below, and for brevity, combined them into a single code example, even though I tested them individually.
class Foo {
@SuppressWarnings("unchecked") //doesn't work
private Map<String, String> map;
@SuppressWarnings("unchecked") //works
private void doSomething() {
@SuppressWarnings("unchecked") //syntax error
this.map = Library.getMap();
}
}
What is the most specific spot that I can suppress the warning? I would prefer to not do it on the entire method, but that is the only spot where it currently works for me.
You expected to suppress warning when there's an issue, the issue is inside
doSomethingin lineYou can't suppress the assignment itself, so you need to go to the outer scope which is the method or the class
If you assignment would be in initialization, you could suppress it: