Suppress deprecated import warning in Java

JavaImportDeprecatedSuppress Warnings

Java Problem Overview

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?

Java Solutions

Solution 1 - Java

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.

Solution 2 - Java

Use this annotation on your class or method:

@SuppressWarnings( "deprecation" )

Solution 3 - Java

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.

Solution 4 - Java

I solved this by changing the import to:

import package.*

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

Solution 5 - Java

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.

Solution 6 - Java

you can use:

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

Solution 7 - Java

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


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEd MazurView Question on Stackoverflow
Solution 1 - JavaKasper van den BergView Answer on Stackoverflow
Solution 2 - JavacraigforsterView Answer on Stackoverflow
Solution 3 - JavaTofuBeerView Answer on Stackoverflow
Solution 4 - JavaTimView Answer on Stackoverflow
Solution 5 - JavaPer-Åke MinborgView Answer on Stackoverflow
Solution 6 - JavaAmitView Answer on Stackoverflow
Solution 7 - JavaIlja TarasovsView Answer on Stackoverflow