0
HashMap<Integer, Integer> aGenericMap = new HashMap<Integer, Integer>();
Integer x = aGenericMap.get("HeyImInvalid");

In my previous version of Eclipse, the code above was marked as a compile error (as expected). In Eclipse 2019-09 R (4.13.0), this doesn't even raise a warning let alone a compile error.

How do I get Eclipse to mark this as a compile error?

It's a real pain now because I'm using enums (as opposed to a String in the example above) and sometimes I forget to add ".value" to get the enum int/Integer value, a nasty bug that should show up at compile time and not runtime.

tazelee
  • 1
  • 3
  • note: I checked the compiler settings in Eclipse and didn't see any specific setting for forcing correct type access to generics – tazelee Sep 25 '20 at 04:21
  • 2
    It's not a type error, because `Map.get` is a method that _actually accepts `Object`._ See [here](https://stackoverflow.com/q/857420/869736) for why. – Louis Wasserman Sep 25 '20 at 04:22
  • There are a few static analyzer tools that can detect this class of problems, typically called something like "unlikely comparison". This problem also exists more generally when comparing objects with `equals`, which also accepts `Object`, but comparing unrelated classes is rarely useful. – Hulk Sep 25 '20 at 04:25
  • Thanks for comments. Maybe I just imagined Eclipse previously catching it as a compile error. – tazelee Sep 25 '20 at 04:45
  • For example, Sonar has rule ["Inappropriate "Collection" calls should not be made"](https://rules.sonarsource.com/java/type/Bug/RSPEC-2175), which is exactly about that. (and the mentioned related problem with `equals` is handled in ["Silly equality checks should not be made"](https://rules.sonarsource.com/java/type/Bug/RSPEC-2159)) – Hulk Sep 25 '20 at 04:45
  • Hulk, thanks for the tool ref. Appreciated. Maybe it was one of the eclipse plugins I had in the earlier version. – tazelee Sep 25 '20 at 05:00

0 Answers0