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不会改变任何东西)。

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

4

2 回答 2

11

您不能禁止“使用预览功能”警告。从JEP 12:预览语言和 VM 功能

无论是启用还是禁用预览语言功能,如果javacJDK在源代码中$N检测到使用 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.

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

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

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

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

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

于 2019-11-24T18:35:12.503 回答
6

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

想象一下,每个人都开始尝试预览功能(或孵化器模块,就此而言),然后传播该代码和工件。当一个特性发生变化时,几个月后它们就会过时,维护这种依赖关系将成为一场噩梦。不过不用担心,有许多安全措施可以防止这种情况发生。好吧,至少不会发生意外。

此附加链接@SuppressWarning显示最新的 Eclipse IDE 支持哪些值

更新

这是 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();
    }
...
}

强制性在's的第三个参数 ( enforceMandatory) 处硬编码。MandatoryWarningHandler

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;
    }
...
}
于 2019-11-24T17:40:24.753 回答