Java Deprecated Class using a Deprecated Class — Can I turn off the compiler warning?

此生再无相见时 提交于 2019-12-11 01:58:47

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/3450356/java-deprecated-class-using-a-deprecated-class-can-i-turn-off-the-compiler-wa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!