Suppress deprecated import warning in Java
Asked Answered
V

7

138

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?

Vescuso answered 7/12, 2009 at 5:37 Comment(1)
Yeah that would be ideal, but in this case I am using a library for Hadoop that uses a portion of its API that was recently deprecated, so I don't really have a choice if I want to use this library. Plus this is just for a school project, not anything that will need to be maintained.Vescuso
D
231

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.

Dowry answered 3/1, 2014 at 17:20 Comment(3)
+1 This should be the accepted answer. It has the benefit of not suppressing deprecations on the entire class.Lyrebird
This is the better answer, @SuppressWarnings at the class level does not cover importsEpicene
This is the best answer until Java 9, where it is finally fixed: bugs.openjdk.java.net/browse/JDK-8032211Burushaski
T
159

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)
Trollope answered 7/12, 2009 at 5:45 Comment(8)
Does this work with imports? I've used that annotation on methods and such, but it doesn't seem to be recognized with imports.Vescuso
Using the annotation at the class level should cover imports as well. It does this at least within Eclipse (well, Rational Application Developer) for me but I'm not sure about during command-line compilation.Trollope
Ah yes, putting it at the class level did the trick (I'm using Eclipse as well). I'd only tried adding it above the imports, which did not work. Thank you!Vescuso
It doesn't seem to work with command-line "javac" or ant's <javac> task. Neither does the -Xlint:-deprecation flag. Seems like a javac bug.Ellata
@EdMazur you are right it does not seem to work with importsYork
Not working if the deprecation is part of an static field and a local SuppressWarnings. Fully qualified class name seems to be the better approach here.Sexdecillion
Under IntelliJ IDEA (2017.1.3), the annotation suppresses the visual warning but does not remove it from the build messages.Maness
The answer shouldn't be at the top, since the question is about the "import" statement and the fix works only for Eclipse/Intellij (seems so according to the comments), but the ultimate source of truth is javac for which the fix doesn't work. This answer really answers the question: https://mcmap.net/q/66411/-suppress-deprecated-import-warning-in-javaMaharashtra
L
15

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.

Laclair answered 7/12, 2009 at 5:42 Comment(0)
S
11

I solved this by changing the import to:

import package.*

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

Strive answered 29/3, 2012 at 10:45 Comment(4)
Don't import everything from the whole package. It will affect the performance. Unnecessary compiler will load all the classes will result in serious performance issue. Always make use that unused classes are not present in your import.Smithy
Using an encyclopedia as an example here (yes I dated myself) - if you are looking for information about an Orangutan, do you just grab the 'O' book or grab all the books?Selfmade
@Arundev, to be clear, what you're saying is that the compilation could take longer to complete, but at runtime, this will not have any impact on the performance of the execution, right?Pothouse
@Pothouse - thats what am trying to say, it will check for the availability of classes imported, even if its not used. So if you have lot of unused imports always better to remove that.Smithy
F
10

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.

Fenestella answered 20/10, 2011 at 13:43 Comment(2)
This combination of "@SuppressWarnings" and "@Deprecated" also works for calling deprecated methods inside your own method.Radiogram
I've been looking for that explanation for so long. Thank you!Faubion
E
2

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

Endoblast answered 8/3, 2014 at 13:39 Comment(0)
L
1

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

Lorikeet answered 19/4, 2021 at 13:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.