John K.
John K.

Reputation: 13

Eclipse - generate a warning when using map.get with an unexpected type

Is there a setting in Eclipse Luna (or a newer version) that enables a warning to be displayed in the following case:

Map<K, V> map = new HashMap<>();
map.get(val);

where the type of val is not K? (the warning should be displayed on the second line)

I know that the code above is valid because Map.get takes an Object as an argument, but in almost every case is an error on my part, so I would like a warning to be displayed.

Upvotes: 1

Views: 225

Answers (2)

Ulises
Ulises

Reputation: 9635

IntelliJ Idea will give you a Warning as part of their static code analysis. In eclipse, you can install FindBugs from the marketplace and although that check doesn't come out of the box, you can implement your own bug Detector. Check this out

Upvotes: 0

M A
M A

Reputation: 72854

Eclipse and its compiler are meant to be aligned with the Java compiler and the formal language specification. The javac compiler accepts your code without any warning (even with -Xlint enabled), because the get method takes an Object.

So there is no reason why Eclipse should come up with its own warning. Seems more like the job of a static analysis tool like FindBugs.

Upvotes: 2

Related Questions