How to avoid Eclipse warnings when using legacy code without generics?

北城余情 提交于 2019-12-04 04:30:48

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.

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!