如果使用遗留库,如何避免 Java 中的未经检查的转换警告?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1130433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to avoid unchecked-conversion-warning in Java, if you use legacy libraries?
提问by Mnementh
I like the generics-feature in java and use it often. But I have a problem, if I use libraries that aren't yet aware of generics. An example are servlets. If you use ServletRequest.getParameterMap()
the result will be a raw map, but it includes only String
as keys and String[]
as values. So I want to assign it to a Map<String, String[]>
. But for this assignment I get an warning. How can I avoid this warning with the language, not by simply suppressing the warning with the @SuppressWarnings
annotation.
我喜欢 java 中的泛型特性并且经常使用它。但是我有一个问题,如果我使用还不知道泛型的库。一个例子是 servlet。如果您使用ServletRequest.getParameterMap()
结果将是一个原始地图,但它仅包括String
作为键和String[]
作为值。所以我想把它分配给一个Map<String, String[]>
. 但是对于这项任务,我收到了警告。我怎样才能通过语言避免这种警告,而不是简单地用@SuppressWarnings
注释抑制警告。
采纳答案by Miserable Variable
As others have said the warnings cannot be avoided except by suppressing them. The issue IMHO is that either you have to litter your code with annotations that apply to small scope or ignore them globally and risk errors.
正如其他人所说,除非压制警告,否则无法避免警告。恕我直言,问题是要么你必须用适用于小范围的注释来乱扔你的代码,要么全局忽略它们并冒错误的风险。
IIRC there is a proposal to generate warnings where the raw types are being returned instead of at the call.
IIRC 有一个建议,在原始类型被返回而不是在调用时生成警告。
Meanwhile, I think the best approach is to use a wrapper method so that the warnings are limited to a single place, where it is safe to ignore them:
同时,我认为最好的方法是使用包装器方法,以便将警告限制在一个地方,在那里可以安全地忽略它们:
class NoWarn {
public static Map<String, String[]> getParameterMap(ServletRequest r)
{
@SuppressWarnings("unchecked")
Map<String, String[]> result = r.getParameterMap();
return result;
}
}
NoteThis answer was edited with a comment that annotations cannot be inside method bodies. That is incorrect, the above is syntactically correct. I have reverted the change.
注意此答案已编辑,注释不能在方法主体内。那是不正确的,以上在语法上是正确的。我已经恢复了更改。
回答by Rich Seller
I don't think you can. The warning will appear unless you suppress it, or filter it from your IDE's warnings list.
我不认为你可以。除非您禁止它,或者从 IDE 的警告列表中过滤它,否则该警告将出现。
回答by Michael Borgwardt
How can I avoid this warning with the language, not by simply suppressing the warning with the SuppressWarnings-annotation.
我怎样才能通过语言避免这种警告,而不是简单地用 SuppressWarnings-annotation 抑制警告。
The annotation isthe way to avoid the warning with the language. There is no other way.
注释是避免语言警告的方法。没有其他办法。
回答by starblue
The cleanest thing you can do is to encapsulate the conversion from legacy to generic code and suppress the warning only there.
您可以做的最干净的事情是封装从遗留代码到通用代码的转换,并仅在那里抑制警告。
E.g. you could put a generic facade on your legacy library, though this might not always be worthwhile.
例如,您可以在遗留库上放置一个通用外观,尽管这可能并不总是值得的。
回答by IAdapter
I had the same problem, i just turned off all generic warnings and im happy :) You could also turn off serialVersionUID warning since many people dont use serialVersionUID.
我遇到了同样的问题,我刚刚关闭了所有通用警告,我很高兴 :) 您也可以关闭 serialVersionUID 警告,因为很多人不使用 serialVersionUID。
in Eclipse - Window/Perferences/Java/Compiler/Errors/Warnings and turn off all Generic types.
在 Eclipse 中 - Window/Perferences/Java/Compiler/Errors/Warnings 并关闭所有通用类型。
P.S. Many bad warnings make you ignore all the warnings and some might be usefull.
PS 许多不好的警告让您忽略所有警告,有些可能有用。
回答by Bardya Momeni
as others said, the only way to get rid of this warning is to suppress it.
正如其他人所说,摆脱这个警告的唯一方法就是压制它。
the best practice is to encapsulate the warning using methods and classes.
最佳实践是使用方法和类封装警告。
but with other warnings, always try to solve the problem that are making them, like remove unused imports and etc... it makes your application leaner and better.
但是对于其他警告,请始终尝试解决造成它们的问题,例如删除未使用的导入等……它使您的应用程序更精简、更好。
happy coding
快乐编码
回答by Gaurav
Stumbled upon this question as I was also trying to figure out a way to avoid using the suppress annotation in such cases. I found another alternative which I thought was worth mentioning:
偶然发现了这个问题,因为我也试图找出一种方法来避免在这种情况下使用抑制注释。我找到了另一个我认为值得一提的替代方案:
Map<?, ?> map = servletRequest.getParameterMap();
String[] values = (String[]) map.get("key");
We are basically using wildcard '?' to indicate that the map can have any type of key and value.
我们基本上使用通配符“?” 指示映射可以具有任何类型的键和值。
Potential downside I see here is that we are doing an explicit cast while fetching values, which I think will result in a slight performance overhead during runtime.
我在这里看到的潜在缺点是我们在获取值时进行了显式转换,我认为这会在运行时导致轻微的性能开销。