使用不推荐使用的类的Java不推荐使用的类-是否可以关闭编译器警告? [英] Java Deprecated Class using a Deprecated Class -- Can I turn off the compiler warning?

查看:64
本文介绍了使用不推荐使用的类的Java不推荐使用的类-是否可以关闭编译器警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力弃用一组Java类,因此不再使用它们.我不想关闭已弃用的编译器警告,但是我发现,如果我的一个不赞成使用的类导入另一个不赞成使用的类,我也会收到警告.我不想修改我不赞成使用的代码,但是我也不想针对这些情况发出警告.有没有办法(a)注释/注释代码以禁用警告(b)在这种情况下关闭编译器警告?我正在使用NetBeans,因此,如果有NetBeans特定的方式,那也可以.

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.

这是一个简单的例子:

头等舱:

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

第二堂课

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

好的,这是允许我重现该问题的最低要求(即使启用了@SupressWarnings:

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

母班:

import another.pack.ChildClass;

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

儿童班:

package another.pack;

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

请注意,甚至JavaDoc @deprecated标记甚至允许编译器引发警告.如果我仅使用@Deprecated批注,则永远不会收到警告(即使没有压力).

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

推荐答案

您可以使用其他注释:

@SuppressWarnings(弃用")

@SuppressWarnings("deprecation")

您只能将此注释放置在父类中.导入OldClass时,您将保留警告,但OldSubClass的递归警告"将被忽略.

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.

这篇关于使用不推荐使用的类的Java不推荐使用的类-是否可以关闭编译器警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆