【问题标题】:Cannot stop ant from generating compiler Sun proprietary API warnings无法阻止 ant 生成编译器 Sun 专有 API 警告
【发布时间】:2012-03-08 06:43:17
【问题描述】:

我像这样从我的 ant 脚本中调用 javac:

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

但它仍然会在输出中引发编译器警告:

[javac] Compiling 73 source files to C:\IKOfficeRoot\Java\ERP\Framework\build\classes

[javac] C:\IKOfficeRoot\Java\ERP\Framework\src\de\ikoffice\util\LoggerFactory.java:49: warning: sun.reflect.Reflection is Sun proprietary API and may be removed in a future release
[javac]         return Logger.getLogger(Reflection.getCallerClass(2));
[javac]                                 ^
[javac] Note: C:\IKOfficeRoot\Java\ERP\Framework\src\de\ikoffice\db\SingleShotResultSet.java uses or overrides a deprecated API.
[javac] Note: Recompile with -Xlint:deprecation for details.
[javac] Note: Some input files use unchecked or unsafe operations.
[javac] Note: Recompile with -Xlint:unchecked for details.
[javac] 1 warning

我也试过了

<compilerarg line="-Xlint:-unchecked -Xlint:-deprecation"/>

<compilerarg value="-Xlint:none"/>

但这也没有效果。如何删除警告?

【问题讨论】:

标签: java ant warnings javac


【解决方案1】:

-XDignore.symbol.file 选项添加到javac 命令行对我有用。

【讨论】:

    【解决方案2】:

    最好的方法是使用sunapi 抑制,即-Xlint:-sunapi 用于整个编译,或@SuppressWarnings("sunapi") 在您所需的范围内。

    请注意,使用此抑制的唯一方法是首先使用编译器选项 -XDenableSunApiLintControl 启用它

    enableSunApiLintControl

    【讨论】:

    • 为我工作,谢谢!与我发现的其他解决方案相比,这个不需要从 gradle fork 编译器。
    • 是的,尽管其他消息来源说,这也得到了一切。大大的宽慰。
    【解决方案3】:

    使用 nowarn 属性见下文

    例如

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

    默认情况下 nowarn 属性是关闭的

    【讨论】:

      【解决方案4】:

      你不能这样做:不能从命令行做,所以你也不能从 Ant 做。

      javac 文档说:

      -Xlint:无

      禁用 Java 语言规范未规定的所有警告。

      所以看来这种警告是无法抑制的,因为它们是由 JLS 直接提出的。

      你可以做的是:

      1. 在需要的位置使用@SuppressWarnings({"deprecation"}, {"unchecked"})
      2. 使用一些变通方法将警告消息重定向到/dev/null
      3. 尝试使用-Xlint:-unchecked -Xlint:-deprecation 参数从命令行编译,可能只是与Ant 相关的问题。

      【讨论】:

      • -Xlint:-unchecked 对我没有影响。尽管文档说了什么,但似乎根本没有办法从命令行隐藏警告。 (-Xmaxwarns 0 也没有效果)
      【解决方案5】:

      建议:不要忽视编译器的警告。警告是有原因的。我公司的遗留代码库正朝着“将警告视为错误并导致构建失败”的模型发展,因为我们可以花费精力来修复在编译周期中产生的警告。

      在你的情况下,警告是:

      warning: sun.reflect.Reflection is Sun proprietary API and may be removed in a future release
      

      只要我记得,不要从sun 包导入一直是一个警告,因为它们是非公共 API。甚至在 Oracle Java 官方网站上也有关于它的 FAQ

      您还没有发布您的实际代码,因此很难提供进一步的建议...您在 sun.reflect.Reflection 中使用了哪些您无法通过 java.lang.reflect 中的某些东西来实现的功能?

      【讨论】:

      • 因为我们有一个名为“Crash the Server”的按钮。这个使用 Unsafe 类来修改位置 0 处的内存,这会导致 JVM 崩溃。这个不错的实用程序可以帮助我们测试 JVM 崩溃恢复代码。此外,以这种方式检查堆栈跟踪速度更快。
      【解决方案6】:

      为了防止出现弃用警告,我尝试使用 JDK 6 和 JDK 7。

      在 JDK 7 中,@Deprecated 注释可以工作

      使用 JDK 6,它不起作用,添加 -Xlint:-deprecation 参数也不适合我。我设法消除警告的唯一方法是使用@deprecated Javadoc Tag

        /**
         * @deprecated
         */
        @Override
        public void removeValue(String arg0) {
          // TODO Auto-generated method stub
        }
      

      【讨论】:

        猜你喜欢
        • 2010-11-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-01
        • 1970-01-01
        相关资源
        最近更新 更多