本文介绍了在没有泛型的情况下使用遗留代码时如何避免Eclipse警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用从Java生成JSON输出。但是每次我调用jsonobj.put(this,that),我都会在Eclipse中看到一个警告:
$ b

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?

解决方案

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.

这篇关于在没有泛型的情况下使用遗留代码时如何避免Eclipse警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 16:41