7

I'm using JSON.simple to generate JSON output from Java. But every time I call jsonobj.put("this", "that"), I see a warning in Eclipse:

Type safety: The method put(Object, Object) belongs to the raw type HashMap. References to generic type HashMap should be parameterized

The clean fix would be if JSONObject were genericized, but since it isn't, I can't add any generic type parameters to fix this. I'd like to switch off as few warnings as possible, so adding "@SuppressWarnings("unchecked")" to lots of methods is unappealing, but do I have any other option besides putting up with the warnings?

skaffman
  • 398,947
  • 96
  • 818
  • 769
Paul Crowley
  • 1,656
  • 1
  • 14
  • 26

3 Answers3

7

Here's one option. It's a bit ugly, but allows you to scope the the suppressed warning to only that individual operation.

Add a function which does the unchecked cast, and suppress warnings on it:

@SuppressWarnings("unchecked")
private final static Map<Object,Object> asMap(JSONObject j)
{
  return j;
}

Then you can call it without compiler warnings:

asMap(jsonobj).put("this", "that");

This way, you can be sure that you aren't suppressing any warnings that you actually want to see.

Kip
  • 107,154
  • 87
  • 232
  • 265
  • In Netbeans, that returns ANOTHER set of warnings for me: > Private method aMap is declared final > Private method aMap is declared static – user919860 Apr 04 '13 at 15:26
4

you can have a per project compiler settings and you can change those warnings to ignore.

Omry Yadan
  • 31,280
  • 18
  • 64
  • 87
  • 3
    That's even more extreme than adding @SuppressWarnings("unchecked") to the relevant methods; my issue is that I'd like to see as many warnings as possible while suppressing this one. – Paul Crowley Apr 15 '10 at 15:39
  • you can suppress this specific warning in your legacy project configuration. nothing extreme about it. – Omry Yadan Apr 15 '10 at 16:51
  • it's extreme because *his* project isn't legacy, just the library he is referencing. Your suggestion is for his entire project to suppress warnings. – Kip Apr 15 '10 at 21:39
  • I don't think you get such warnings for code you don't compile. but if you need compile that code as part of your project you will indeed have to use more specific methods. however, I suggest that you just create a jar out of that library instead of compiling as a part of your project. – Omry Yadan Apr 16 '10 at 17:49
  • Omry: I'm not compiling the code for JSONObject as part of my code, I'm including it through Maven. The warning is in my code, which is not legacy code. Kip's comment is correct. The accepted solution addresses my situation exactly - make sure you see how it does that before comparing it to your solution. – Paul Crowley Apr 19 '10 at 16:34
4

Write some helper methods, or wrapper classes for the library. Add the @SuppressWarnings("unchecked") only to those helpers. Then use the helpers to perform the interaction with the library.

Chris Lercher
  • 37,264
  • 20
  • 99
  • 131
  • This looks to be the best solution so far, thanks. Eclipse is just wrong to be warning me about this, isn't it? These errors should be in the JSONObject code, not my code. – Paul Crowley Apr 15 '10 at 15:41
  • @Paul: Eclipse is right, because that's the way it's specified for Java. You get the warning, because you're using a raw method from HashMap (you're forced to do that, because JSONObject extends HashMap). – Chris Lercher Apr 15 '10 at 15:50