如何抑制Javac有关预览功能的警告?

9

我正在使用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什么也没有改变)。
有没有办法来抑制这些消息?

1
此警告无法禁用。 - zforgo
@zforgo 我建议您将您的评论写成答案。如果可能,引用一些文档。 - Basil Bourque
2个回答

11

您无法抑制“使用预览功能”警告。根据JEP 12:预览语言和VM功能

  

无论启用还是禁用预览语言功能,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元素的源代码引用都必须生成警告。与javac在检测源代码中使用预览语言功能时给出的警告不同,可以使用@SuppressWarnings("preview")来抑制此警告;使用基本API元素被认为比使用预览语言功能略微不严重(因此可抑制)。
其中,“基本 API” 的含义已经在同一部分中解释过:
  1. 基本的。API 存在是因为没有它,代码就无法享受预览功能。这种 API 存在于java.*中,JLS 将在规范性文本中引用它。例如,增强型 for 循环语句依赖于 java.lang.Iterable,而 try-with-resources 语句则依赖于 java.lang.AutoCloseable
你的警告不是来自“基本API”的使用,而是来自预览功能本身的使用,这意味着@SuppressWarnings("preview")不适用于你的情况。

6
这篇文章通过启用预览功能来完善Java介绍了为什么这个警告不能被禁用的主要目的。

想象一下,如果每个人都开始尝试预览功能(或者孵化器模块),然后传播那些代码和工件。当一个功能发生变化时,它们很快就会过时,维护这样的依赖关系将成为一场噩梦。不过,不用担心,有许多保障措施可以防止这种情况发生。至少,防止意外发生。

这个额外的链接展示了最新的Eclipse IDE支持哪些@SuppressWarning值。

更新

这里是OpenJDK的源代码,证明了该警告始终处于启用状态。 Preview类的完整源代码
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)中被硬编码。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接