135

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?

Ed Mazur
  • 3,042
  • 3
  • 21
  • 21
  • 4
    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. – Ed Mazur Dec 07 '09 at 21:00

7 Answers7

224

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.

Kasper van den Berg
  • 8,951
  • 4
  • 48
  • 70
  • 16
    +1 This should be the accepted answer. It has the benefit of not suppressing deprecations on the entire class. – k_g Mar 18 '15 at 03:04
  • 12
    This is the better answer, `@SuppressWarnings` at the class level does not cover imports – Alex Apr 29 '15 at 18:54
  • 8
    This is the best answer until Java 9, where it is finally fixed: https://bugs.openjdk.java.net/browse/JDK-8032211 – Christopher Nov 01 '17 at 22:35
153

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)
Luan Kevin Ferreira
  • 1,184
  • 2
  • 16
  • 40
craigforster
  • 2,589
  • 1
  • 16
  • 10
  • 31
    Does this work with imports? I've used that annotation on methods and such, but it doesn't seem to be recognized with imports. – Ed Mazur Dec 07 '09 at 21:01
  • 2
    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. – craigforster Dec 08 '09 at 03:15
  • 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! – Ed Mazur Dec 08 '09 at 18:12
  • 23
    It doesn't seem to work with command-line "javac" or ant's task. Neither does the -Xlint:-deprecation flag. Seems like a javac bug. – Archie Jul 10 '11 at 15:14
  • @EdMazur you are right it does not seem to work with imports – gyorgyabraham Dec 15 '15 at 10:42
  • 1
    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. – mheinzerling Jul 29 '16 at 06:01
  • 1
    Under IntelliJ IDEA (2017.1.3), the annotation suppresses the visual warning but does not remove it from the build messages. – Urhixidur Jun 12 '17 at 14:51
  • 19
    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://stackoverflow.com/a/20909204/2032701 – Ruslan Sep 26 '17 at 14:12
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.

Steen
  • 6,573
  • 3
  • 39
  • 56
TofuBeer
  • 60,850
  • 18
  • 118
  • 163
11

I solved this by changing the import to:

import package.*

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

Tim
  • 479
  • 5
  • 6
  • 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. – Arundev Jul 11 '16 at 10:33
  • 1
    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? – Ascalonian Jul 25 '18 at 13:10
  • @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? – Paulo Apr 23 '20 at 16:22
  • @Paulo - 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. – Arundev May 20 '20 at 13:31
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.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • This combination of "@SuppressWarnings" and "@Deprecated" also works for calling deprecated methods inside your own method. – Swav Jul 30 '15 at 17:26
  • 1
    I've been looking for that explanation for so long. Thank you! –  May 07 '18 at 17:23
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");.

Lucas Zamboulis
  • 2,494
  • 5
  • 24
  • 27
Amit
  • 21
  • 1
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

Ilja Tarasovs
  • 181
  • 2
  • 13