Suppress deprecated import warning in Java

前端 未结 6 1916
不思量自难忘°
不思量自难忘° 2020-11-27 06:07

In Java, if you import a deprecated class:

import SomeDeprecatedClass;

You get this warning: The type SomeDeprecatedClass is deprecat

相关标签:
6条回答
  • 2020-11-27 06:32

    I solved this by changing the import to:

    import package.*
    

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

    0 讨论(0)
  • 2020-11-27 06:32

    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.

    0 讨论(0)
  • 2020-11-27 06:41

    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");.

    0 讨论(0)
  • 2020-11-27 06:43

    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.

    0 讨论(0)
  • 2020-11-27 06:44

    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.

    0 讨论(0)
  • 2020-11-27 06:50

    Use this annotation on your class or method:

    @SuppressWarnings( "deprecation" )
    
    0 讨论(0)
提交回复
热议问题