【问题标题】:How can I suppress Javac warning about preview features?如何抑制有关预览功能的 Javac 警告?
【发布时间】:2019-11-24 16:42:42
【问题描述】:

我使用的是 Java 13 (13.0.1.hs-adpt),并且启用了预览语言功能(主要是因为文本块)。

我的代码在相关的地方用@SuppressWarnings("preview") 正确注释,但我仍然得到

Note: Some input files use preview language features.
Note: Recompile with -Xlint:preview for details.

每当我编译时。

我知道那不是编译器打印警告(事实上,-Xlint:-preview 并没有改变任何东西)。

有没有办法抑制这些消息?

【问题讨论】:

  • 无法禁用此警告。
  • @zforgo 我建议您回答您的评论。如果可能的话,引用一些文档

标签: java javac java-13


【解决方案1】:

您不能禁止“使用预览功能”警告。来自JEP 12: Preview Language and VM Features:

无论是启用还是禁用预览语言功能,如果JDK $N 中的javac 在源代码中检测到使用Java SE $N 的预览语言功能,它就会打印一条消息。无法通过在源代码中使用@SuppressWarnings 来关闭此消息,因为开发人员必须始终意识到他们对Java SE $N 版本的预览语言功能的依赖;该功能在 Java SE $N+1 中可能会发生微妙的变化。消息如下所示:

Note: Some input files use a preview language feature.
Note: Recompile with -Xlint:preview for details.

它还在标记为与 Java SE API 的关系的部分中提到了@SuppressWarnings("preview") 的使用:

  1. 在启用预览功能的情况下编译时,对与预览功能关联的基本 API 元素的任何源代码引用都必须生成警告。使用@SuppressWarnings("preview") 可以抑制此警告,这与 javac 在检测到源代码中使用了预览语言功能时发出的警告不同;与使用预览语言功能相比,基本 API 元素的使用被认为稍微不那么严重(因此可以抑制)。

“基本 API”的含义在前面的同一部分中进行了解释:

  1. 必不可少的。 API 之所以存在,是因为没有它,代码就无法享受预览功能。这样的 API 位于 java.* 中,JLS 将在规范文本中引用它。例如,the enhanced-for statement 依赖于 java.lang.Iterable,the try-with-resources statement 依赖于 java.lang.AutoCloseable。

您的警告不是来自“基本 API”的使用,而是来自预览功能本身的使用,这意味着 @SuppressWarnings("preview") 不适用于您的情况。

【讨论】:

    【解决方案2】:

    这篇文章Evolving Java With ––enable–preview aka Preview Language Features描述了为什么这个警告不能被禁用的主要目的。

    想象一下每个人都开始尝试预览功能(或 孵化器模块,就此而言),然后传播该代码和 周围的文物。当功能发生变化时,它们就会过时 仅仅几个月后,维护这种依赖关系就变成了 一个噩梦。不过不用担心,有很多保障措施 正是防止了这一点。好吧,至少不会发生意外。

    This additional link 显示最新的 Eclipse IDE 支持哪些 @SuppressWarning 值

    更新

    这是 OpenJDK 的源代码,证明此警告始终处于启用状态。 full source of Preview class

    public class Preview {
    
        /** flag: are preview features enabled */
        private final boolean enabled;
    
        /** the diag handler to manage preview feature usage diagnostics */
        private final MandatoryWarningHandler previewHandler;
    
        /** test flag: should all features be considered as preview features? */
        private final boolean forcePreview;
    
        /** a mapping from classfile numbers to Java SE versions */
        private final Map<Integer, Source> majorVersionToSource;
    
    
        private final Lint lint;
        private final Log log;
    
        private static final Context.Key<Preview> previewKey = new Context.Key<>();
    
        public static Preview instance(Context context) {
            Preview instance = context.get(previewKey);
            if (instance == null) {
                instance = new Preview(context);
            }
            return instance;
        }
    
        Preview(Context context) {
            context.put(previewKey, this);
            Options options = Options.instance(context);
            enabled = options.isSet(PREVIEW);
            log = Log.instance(context);
            lint = Lint.instance(context);
            this.previewHandler =
                    new MandatoryWarningHandler(log, lint.isEnabled(LintCategory.PREVIEW), true, "preview", LintCategory.PREVIEW);
            forcePreview = options.isSet("forcePreview");
            majorVersionToSource = initMajorVersionToSourceMap();
        }
    ...
    }
    

    强制在MandatoryWarningHandler 的第三个参数 (enforceMandatory) 中硬编码。

    Full source of MandatoryWarningHandler

    
    public class MandatoryWarningHandler {
    ...
        /**
         * Create a handler for mandatory warnings.
         * @param log     The log on which to generate any diagnostics
         * @param verbose Specify whether or not detailed messages about
         *                individual instances should be given, or whether an aggregate
         *                message should be generated at the end of the compilation.
         *                Typically set via  -Xlint:option.
         * @param enforceMandatory
         *                True if mandatory warnings and notes are being enforced.
         * @param prefix  A common prefix for the set of message keys for
         *                the messages that may be generated.
         * @param lc      An associated lint category for the warnings, or null if none.
         */
        public MandatoryWarningHandler(Log log, boolean verbose,
                                       boolean enforceMandatory, String prefix,
                                       LintCategory lc) {
            this.log = log;
            this.verbose = verbose;
            this.prefix = prefix;
            this.enforceMandatory = enforceMandatory;
            this.lintCategory = lc;
        }
    ...
    }
    

    【讨论】:

      猜你喜欢
      • 2010-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-19
      • 1970-01-01
      • 2018-08-26
      • 1970-01-01
      • 2019-02-25
      相关资源
      最近更新 更多