How can I suppress Javac warning about preview features?
Asked Answered
A

2

9

I'm using Java 13 (13.0.1.hs-adpt) and I enabled the preview language features (mainly because of text blocks).

My code is properly annotated with @SuppressWarnings("preview") wherever relevant, but I still get

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

whenever I compile.

I understand that that's not the compiler printing warnings (in fact, -Xlint:-preview doesn't change anything).

Is there a way to suppress these messages?

Aubervilliers answered 24/11, 2019 at 16:42 Comment(2)
This warning can’t be disabled.Antebi
@Antebi I suggest you make an Answer of your Comment. Cite some documentation if possibleGallstone
E
12

You cannot suppress the "use of preview features" warning. From JEP 12: Preview Language and VM Features:

Whether preview language features are enabled or disabled, javac in JDK $N prints a message if it detects the use of a preview language feature of Java SE $N in source code. This message cannot be turned off by using @SuppressWarnings in source code, because developers must be unfailingly aware of their reliance on the Java SE $N version of a preview language feature; the feature may change subtly in Java SE $N+1. The message looks like this:

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

It also mentions the use of @SuppressWarnings("preview") in the section labeled Relationship to Java SE APIs:

  1. When compiling with preview features enabled, any source code reference to an essential API element associated with a preview feature must generate a warning. This warning is suppressible with @SuppressWarnings("preview"), unlike the warning given by javac when it detects the use of a preview language feature in source code; the use of an essential API element is considered slightly less severe (and hence suppressible) than the use of a preview language feature.

Where the meaning of "essential API" is explained previously in the same section:

  1. Essential. The API exists because code cannot enjoy the preview feature without it. Such an API lives in java.* and the JLS will refer to it in normative text. For example, the enhanced-for statement relies on java.lang.Iterable, and the try-with-resources statement relies on java.lang.AutoCloseable.

Your warning is not from the use of "essential API" but from the use of the preview feature itself, which means @SuppressWarnings("preview") is not applicable to your situation.

Efflorescent answered 24/11, 2019 at 18:35 Comment(0)
A
6

This article Evolving Java With ––enable–preview aka Preview Language Features describes what is the main purpose why this warning cannot be disabled.

Imagine everybody started experimenting with preview features (or incubator modules, for that matter) and then spreading that code and the artifacts around. When a feature changes, they become outdated after just a few months and maintaining such dependencies would become a nightmare. Don’t worry, though, there are a number of safeguards preventing exactly that. Well, from happening accidentally at least.

This additional link shows what @SuppressWarning values are supported by the latest Eclipse IDE

UPDATE

Here is the OpenJDK's source code which is proves this warning is always enabled. 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();
    }
...
}

The mandatoriness was hardcoded at the 3rd parameter (enforceMandatory) of MandatoryWarningHandler's.

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;
    }
...
}
Antebi answered 24/11, 2019 at 17:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.