如何禁止有关预览功能的 Javac 警告?
How can I suppress Javac warning about preview features?
我正在使用 Java13 (13.0.1.hs-adpt) 并且启用了预览语言功能(主要是因为文本块) .
我的代码在相关的地方用 @SuppressWarnings("preview")
正确注释,但我仍然得到
Note: Some input files use preview language features.
Note: Recompile with -Xlint:preview for details.
每当我编译时。
我知道那不是编译器打印警告(事实上,-Xlint:-preview
不会改变任何东西)。
有没有办法抑制这些消息?
这篇文章 Evolving Java With ––enable–preview aka Preview Language Features 描述了无法禁用此警告的主要目的。
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 显示最新 Eclipse IDE
支持的 @SuppressWarning
值
更新
这是 OpenJDK 的源代码,证明此警告始终处于启用状态。
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();
}
...
}
MandatoryWarningHandler
的第 3 个参数 (enforceMandatory
) 对强制性进行了硬编码。
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;
}
...
}
您无法抑制 "use of preview features" 警告。来自 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.
它还在标记为 与 Java SE API 的关系的部分中提到了 @SuppressWarnings("preview")
的使用:
- 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.
其中 "essential API" 的含义在前面同一节中有解释:
- 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
.
您的警告不是来自使用 "essential API",而是来自使用预览功能本身,这意味着 @SuppressWarnings("preview")
不适用于您的情况。
我正在使用 Java13 (13.0.1.hs-adpt) 并且启用了预览语言功能(主要是因为文本块) .
我的代码在相关的地方用 @SuppressWarnings("preview")
正确注释,但我仍然得到
Note: Some input files use preview language features.
Note: Recompile with -Xlint:preview for details.
每当我编译时。
我知道那不是编译器打印警告(事实上,-Xlint:-preview
不会改变任何东西)。
有没有办法抑制这些消息?
这篇文章 Evolving Java With ––enable–preview aka Preview Language Features 描述了无法禁用此警告的主要目的。
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 显示最新 Eclipse IDE
支持的@SuppressWarning
值
更新
这是 OpenJDK 的源代码,证明此警告始终处于启用状态。 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();
}
...
}
MandatoryWarningHandler
的第 3 个参数 (enforceMandatory
) 对强制性进行了硬编码。
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;
}
...
}
您无法抑制 "use of preview features" 警告。来自 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.
它还在标记为 与 Java SE API 的关系的部分中提到了 @SuppressWarnings("preview")
的使用:
- 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.
其中 "essential API" 的含义在前面同一节中有解释:
- 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 onjava.lang.Iterable
, and the try-with-resources statement relies onjava.lang.AutoCloseable
.
您的警告不是来自使用 "essential API",而是来自使用预览功能本身,这意味着 @SuppressWarnings("preview")
不适用于您的情况。