Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I suppress Javac warning about preview features?

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?

like image 469
giorgiga Avatar asked Nov 24 '19 16:11

giorgiga


People also ask

What are Java preview features?

A preview feature is a new feature whose design, specification, and implementation are complete, but which is not permanent, which means that the feature may exist in a different form or not at all in future JDK releases.

Is a preview API and is disabled by default?

Preview features are disabled by default. To enable them, we must use the enable-preview argument, which enables all preview features at once. The Java compiler, as well as the JVM, must be of the same Java version that includes the preview feature we want to use.

How to use enable preview Java?

First, you need to go preference, and then Build, Execution, Deployment and then Select Java Compiler. And then go to the run configuration. Select the modify options and Mark the Add VM options. You need to add --enable-preview there as well.

How to enable preview features in IntelliJ?

If you want to use preview features in the latest versions of Java in IntelliJ IDEA, you need to set the language level to "Preview". Go to Project Structure, ⌘; (macOS) or Ctrl+Alt+Shift+S (Windows/Linux), make sure you have the correct JDK selected, and pick the "Preview" option from the Language Level dropdown.


2 Answers

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.

like image 144
Slaw Avatar answered Oct 20 '22 00:10

Slaw


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;
    }
...
}
like image 33
zforgo Avatar answered Oct 20 '22 00:10

zforgo