views:

67

answers:

2

I am working on deprecating a set of Java classes so they aren't used anymore. I don't want to turn off the compiler warnings for deprecated usage, but I'm finding that if one of my deprecated classes imports another deprecated class I get a warning on that, too. I don't want to modify the code I'm deprecating, but I also don't want the warning for those cases. Is there a way to (a) annotate / comment the code to disable the warning (b) turn off compiler warnings in these cases? I'm using NetBeans, so if there is a NetBeans specific way, that works, too.

Here is a quick example:

First class:

/**
 * @deprecated No longer in use.
 **/
@Deprecated
public class OldSubClass {
}

Second class:

import com.old.package.OldSubClass; // Don't want this to create a warning

/**
 * @deprecated No longer in use.
 **/    
@Deprecated
public class OldClass {
       // code that makes use of OldSubClass that I don't want to change ...
       // Any methods that use OldSubClass are also deprecated ...
}

Okay, this is the bare minimum that allows me to reproduce the problem (even with @SupressWarnings turned on:

Mother class:

import another.pack.ChildClass;

@SuppressWarnings("deprecation")
public class MotherClass {
}

Child class:

package another.pack;

/**
 * @deprecated
 */
public class ChildClass {
}

Note that it is the JavaDoc @deprecated tag that even allows the compiler to throw the warning. If I only use the @Deprecated annotation, I never get the warning (even without the supress).

+2  A: 

You can use this other annotation :

@SuppressWarnings(“deprecation”)

You place this annotation in the mother class only. You will keep the warning when importing the OldClass but the "recursive warning" for the OldSubClass will be ignored.

Benoit Courtine
That doesn't seem to work on the import. I see that it does work on the methods themselves, though, so it's helpful from that perspective.
Dante617
By "mother class" in this example, that would be OldClass, correct? If I put the suppress in there at the class level, the warning for the import still comes up.
Dante617
It will work if you put the annotation at the class level (and not on the methods).
Benoit Courtine
Right, I see what you're saying, and I'll do that. It seems like the class level SuppressWarnings should turn the warning off, though.
Dante617
@Benoit - Can you show me an example that you have working. If I put it at the class level, I still get the warning.
Dante617
A: 

NetBeans uses javac (from JDK 7) to determine when and where to show warnings like this, so anything that works with jdk7/bin/javac should work in the IDE's editor just the same way.

Jesse Glick
Made an edit to the original question to show the smallest example where I still get a warning even if I have the @SuppressWarnings("deprecation") turned on. This is the exact warning in the compile logs: ...\src\java\MotherClass.java:1: warning: [deprecation] another.pack.ChildClass in another.pack has been deprecated import another.pack.ChildClass;
Dante617
I also get the same warning when using the java 1.6.0_18 javac command (with -Xlint:deprecated), so it's not just NetBeans.
Dante617
Hate to keep adding to this one, but using just the javac compiler, I also get the warning if the suppress is on in the MotherClass and if the ChildClass has the @Deprecated annotation, and no JavaDoc tag. (I don't get the warning in NetBeans under that construct, though.)
Dante617
Try using the fully-qualified name of a deprecated class rather than importing it. In some situations this is enough for @SuppressWarnings to work the way you expect. But be aware that there are known bugs in javac's treatment of @SW("deprecation") related to when it resolves symbols versus when it processes the annotation.
Jesse Glick
@Jesse - Thanks. That is the way I went, and I got the exceptions out of the way with minor code changes. Certainly, from what I'm seeing there are some issues with the suppress, but for me, it's more a curiosity now.
Dante617