禁止 Java 中不推荐使用的导入警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1858021/
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
Suppress deprecated import warning in Java
提问by Ed Mazur
In Java, if you import a deprecated class:
在 Java 中,如果您导入已弃用的类:
import SomeDeprecatedClass;
You get this warning: The type SomeDeprecatedClass is deprecated
您收到此警告: The type SomeDeprecatedClass is deprecated
Is there a way to suppress this warning?
有没有办法抑制这个警告?
采纳答案by craigforster
Use this annotation on your class or method:
在您的类或方法上使用此注释:
@SuppressWarnings( "deprecation" )
回答by TofuBeer
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.
您也可以尝试 javac -Xlint:-deprecation 不确定是否可以解决它。
回答by Per-?ke Minborg
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.
假设您正在使用不推荐使用的方法(例如 java.sql.ResultSet 中的 getUnicodeStream(String columnLabel))覆盖/实现一个接口,那么您将无法仅通过使用注释 @SuppressWarnings( "deprecation" ) 来消除不推荐使用的警告, 除非您还使用 @Deprecated 注释来注释相同的新方法。这是合乎逻辑的,因为否则您可以通过覆盖其接口描述来“取消推荐”一个方法。
回答by Tim
I solved this by changing the import to:
我通过将导入更改为:
import package.*
then annotating the method that used the deprecated classes with@SuppressWarnings("deprecation")
然后注释使用已弃用类的方法@SuppressWarnings("deprecation")
回答by Kasper van den Berg
To avoid the warning: do notimport the class
为了避免该警告:千万不能导入类
instead use the fully qualified class name
而是使用完全限定的类名
and use it in as few locations as possible.
并在尽可能少的地方使用它。
回答by Amit
you can use:
您可以使用:
javac FileName.java -Xlint:-deprecation
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.
但是这会给你警告,并告诉你导致弃用或使用弃用 API 的代码部分。现在,您可以运行带有这些警告的代码,也可以对代码进行适当的更改。
In my case I was using someListItem.addItem("red color")
whereas the compiler wanted me to use someListItem.add("red color");
.
在我的情况下,我正在使用someListItem.addItem("red color")
而编译器希望我使用someListItem.add("red color");
.