抑制 Java 中已弃用的导入警告
在 Java 中,如果导入已弃用的类:
import SomeDeprecatedClass;
您会收到此警告: The type SomeDeprecatedClass is deprecated
有没有办法抑制此警告?
In Java, if you import a deprecated class:
import SomeDeprecatedClass;
You get this warning: The type SomeDeprecatedClass is deprecated
Is there a way to suppress this warning?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
为了避免警告:
不要导入类
而是使用完全限定的类名
并在尽可能少的位置使用它。
To avoid the warning:
do not import the class
instead use the fully qualified class name
and use it in as few locations as possible.
在您的类或方法上使用此注释:
从 Java 9 开始,您可能需要添加:
如果该类被注释为以下内容:
Use this annotation on your class or method:
Since Java 9, you might need to add:
If the class was annotated with something like:
作为黑客,您无法在代码中进行导入和使用完全限定名称。
您也可以尝试 javac -Xlint:-deprecation 不确定是否可以解决该问题。
As a hack you can not do the import and use the fully qualified name inside the code.
You might also try javac -Xlint:-deprecation not sure if that would address it.
我通过将导入更改为:
然后用
@SuppressWarnings("deprecation") 注释使用已弃用的类的方法来解决此问题
I solved this by changing the import to:
then annotating the method that used the deprecated classes with
@SuppressWarnings("deprecation")
假设您正在使用已弃用的方法(例如
java.sql.ResultSet
中的getUnicodeStream(String columnLabel)
)覆盖/实现接口,那么您将无法摆脱只需使用注释@SuppressWarnings( "deprecation" )
即可发出弃用警告,除非您还使用@Deprecated
注释来注释相同的新方法。这是合乎逻辑的,因为否则您可以通过覆盖方法的接口描述来取消推荐方法。Suppose that you are overriding/implementing an interface with a deprecated method (such as the
getUnicodeStream(String columnLabel)
injava.sql.ResultSet
) then you will not get rid of deprecation warnings just by using the annotation@SuppressWarnings( "deprecation" )
, unless you also annotate the same new method with the@Deprecated
annotation. This is logical, because otherwise you could undeprecate a method by just overriding its interface description.你可以使用:
但是,这会给您警告,并告诉您导致弃用或使用已弃用 API 的代码部分。现在,您可以运行带有这些警告的代码,或者对代码进行适当的更改。
就我而言,我使用的是
someListItem.addItem("red color")
,而编译器希望我使用someListItem.add("red color");
。you can use:
But then this will give you warnings and also tell you the part of the code that is causing deprecation or using deprecated API. Now either you can run your code with these warnings or make appropriate changes in the code.
In my case I was using
someListItem.addItem("red color")
whereas the compiler wanted me to usesomeListItem.add("red color");
.如果 @SuppressWarnings("deprecation") 不像我一样对你有用。您可以在声纳 lint 插件中找到准确的鱿鱼编号。然后您可以简单地抑制警告:
@SuppressWarnings("squid:CallToDeprecatedMethod")
If
@SuppressWarnings("deprecation")
is not working for you like for me. You can find exact squid number in sonar lint plugin. And then you can simply suppress warning:@SuppressWarnings("squid:CallToDeprecatedMethod")