这篇文章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;
}
...
}