简体   繁体   English

如何抑制有关弃用api的javac警告?

[英]How can I suppress javac warnings about deprecated api?

When I compile, javac outputs: 当我编译时,javac输出:

Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.`

I wish to suppress this warning. 我希望压制这个警告。 Trying -Xlint:none does not seem to help. 尝试-Xlint:none似乎没有帮助。

From what I can tell in the docs, you can't do it on the command-line. 从我在文档中可以看出,您不能在命令行上执行此操作。

According to the javac documentation , -Xlint:none only disables warnings "not mandated by the Java Language Specification". 根据javac文档 ,-Xlint:none仅禁用“Java语言规范未强制要求”的警告。 It appears that warning you of the use of deprecated APIs is managed by the language spec. 似乎警告您使用已弃用的API由语言规范管理。

Your best option would be to fix the use of deprecated APIs. 您最好的选择是修复已弃用API的使用。 However, an option would be to add the @SuppressWarnings("deprecation") annotation to the classes or methods that are using the deprecated APIs. 但是,一个选项是将@SuppressWarnings("deprecation")注释添加到使用不推荐使用的API的类或方法中。

Two possible ways: 两种可能的方式:

  1. don't use deprecated API 不要使用已弃用的API
  2. Use @SuppressWarnings("deprecation") 使用@SuppressWarnings("deprecation")

For others that were Google searching this problem and stumble upon this thread like I did... 谷歌搜索这个问题的其他人偶然发现了这个问题...

Try: -Xlint:-deprecation 尝试:-Xlint:-deprecation

It seems to work on JDK 6... not sure about others. 它似乎适用于JDK 6 ......不确定其他人。

With Java 6, neither the @Depreated annotation, nor a comiler flag will help you here. 使用Java 6,@ Depreated注释和comiler标志都不会对您有所帮助。 The only solution that worked for me was to put a javadoc comment with the @deprecated (small caps) tag on the deprecated method: 对我有用的唯一解决方案是在不推荐使用的方法上添加带有@deprecated (小型大写)标记的javadoc注释

/**
  * @deprecated overriding deprecated method
  */
@Override
public javax.xml.bind.Validator createValidator() throws JAXBException {...}

(The example is from a class that derives from JAXBContext.) (该示例来自一个派生自JAXBContext的类。)

(I didn't import the deprecated Validator class to avoid a warning on the import statement.) (我没有导入已弃用的Validator类以避免对import语句发出警告。)

When using gradle you can configure it easily: 使用gradle时,您可以轻松配置它:

tasks.withType(JavaCompile) {
    options.deprecation = false
}

(tested with Gradle 2 and Java 8) (使用Gradle 2和Java 8测试)

If it's a core Java API, there is almost certainly a replacement that will do what you want. 如果它是一个核心Java API,几乎可以肯定有一个替代品可以做你想要的。 Run the javac with that extra parameter, and then look at the API for the deprecated method and replace as appropriate. 使用该额外参数运行javac,然后查看已弃用方法的API并根据需要进行替换。

use nowarn attribute see below 使用nowarn属性见下文

eg 例如

<javac srcdir="src" 
  destdir="build/classes" source="1.6" 
  target="1.6" debug="true" encoding="Cp1252"
  nowarn="on">

by default nowarn attribute is off 默认情况下,nowarn属性已关闭

@SuppressWarnings("deprecation")不适用于我,而是我用过的

@SuppressWarnings("unchecked")

If you are compiling in the command line you can filter the messages with grep , just filtering out the messages that has unwanted content, like for example grep -v deprecated . 如果您在命令行中进行编译,则可以使用grep过滤消息,只过滤掉包含不需要内容的消息,例如grep -v deprecated You can use | 你可以用| to send the output to grep, 将输出发送到grep,

your compile command | grep -v deprecated

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM