抑制 Java 中已弃用的导入警告

发布于 2024-08-14 15:06:00 字数 173 浏览 2 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

留一抹残留的笑 2024-08-21 15:06:00

为了避免警告:
不要导入类

而是使用完全限定的类名

并在尽可能少的位置使用它。

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.

好多鱼好多余 2024-08-21 15:06:00

在您的类或方法上使用此注释:

@SuppressWarnings("deprecation")

从 Java 9 开始,您可能需要添加:

@SuppressWarnings("removal")

如果该类被注释为以下内容:

@Deprecated(since = "3.14", forRemoval = true)

Use this annotation on your class or method:

@SuppressWarnings("deprecation")

Since Java 9, you might need to add:

@SuppressWarnings("removal")

If the class was annotated with something like:

@Deprecated(since = "3.14", forRemoval = true)
不打扰别人 2024-08-21 15:06:00

作为黑客,您无法在代码中进行导入和使用完全限定名称。

您也可以尝试 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.

懒的傷心 2024-08-21 15:06:00

我通过将导入更改为:

import package.*

然后用 @SuppressWarnings("deprecation") 注释使用已弃用的类的方法来解决此问题

I solved this by changing the import to:

import package.*

then annotating the method that used the deprecated classes with@SuppressWarnings("deprecation")

指尖上的星空 2024-08-21 15:06:00

假设您正在使用已弃用的方法(例如 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) in java.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.

浅浅 2024-08-21 15:06:00

你可以使用:

javac FileName.java -Xlint:-弃用

但是,这会给您警告,并告诉您导致弃用或使用已弃用 API 的代码部分。现在,您可以运行带有这些警告的代码,或者对代码进行适当的更改。

就我而言,我使用的是 someListItem.addItem("red color"),而编译器希望我使用 someListItem.add("red color");

you can use:

javac FileName.java -Xlint:-deprecation

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 use someListItem.add("red color");.

私藏温柔 2024-08-21 15:06:00

如果 @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")

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文