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

gio*_*iga 4 java javac java-13

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

我的代码会在适当的@SuppressWarnings("preview")地方加上适当的注释,但是我仍然可以

Note: Some input files use preview language features.
Note: Recompile with -Xlint:preview for details.
Run Code Online (Sandbox Code Playgroud)

每当我编译时。

我知道那不是编译器打印警告(实际上,-Xlint:-preview不会更改任何内容)。

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

Sla*_*law 7

您不能禁止“使用预览功能”警告。从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.
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

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


zfo*_*rgo 6

这篇文章Evolving Java With ––enable–preview aka Preview Language Features描述了无法禁用此警告的主要目的是什么。

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

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

更新

这是 OpenJDK 的源代码,它证明此警告始终处于启用状态。 预览类的完整来源

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();
    }
...
}
Run Code Online (Sandbox Code Playgroud)

强制在'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;
    }
...
}
Run Code Online (Sandbox Code Playgroud)