Skip to content

Commit e50af6d

Browse files
committed
8354090: Refactor import warning suppression in Check.java
Reviewed-by: mcimadamore
1 parent c4c3edf commit e50af6d

File tree

3 files changed

+23
-21
lines changed

3 files changed

+23
-21
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,12 @@ protected Check(Context context) {
218218
*/
219219
private final boolean allowSealed;
220220

221+
/** Whether to force suppression of deprecation and preview warnings.
222+
* This happens when attributing import statements for JDK 9+.
223+
* @see Feature#DEPRECATION_ON_IMPORT
224+
*/
225+
private boolean importSuppression;
226+
221227
/* *************************************************************************
222228
* Errors and Warnings
223229
**************************************************************************/
@@ -228,6 +234,12 @@ Lint setLint(Lint newLint) {
228234
return prev;
229235
}
230236

237+
boolean setImportSuppression(boolean newImportSuppression) {
238+
boolean prev = importSuppression;
239+
importSuppression = newImportSuppression;
240+
return prev;
241+
}
242+
231243
MethodSymbol setMethod(MethodSymbol newMethod) {
232244
MethodSymbol prev = method;
233245
method = newMethod;
@@ -261,19 +273,10 @@ void warnDeprecated(DiagnosticPosition pos, Symbol sym) {
261273
* @param msg A Warning describing the problem.
262274
*/
263275
public void warnPreviewAPI(DiagnosticPosition pos, LintWarning warnKey) {
264-
if (!lint.isSuppressed(LintCategory.PREVIEW))
276+
if (!importSuppression && !lint.isSuppressed(LintCategory.PREVIEW))
265277
preview.reportPreviewWarning(pos, warnKey);
266278
}
267279

268-
/** Log a preview warning.
269-
* @param pos Position to be used for error reporting.
270-
* @param msg A Warning describing the problem.
271-
*/
272-
public void warnDeclaredUsingPreview(DiagnosticPosition pos, Symbol sym) {
273-
if (!lint.isSuppressed(LintCategory.PREVIEW))
274-
preview.reportPreviewWarning(pos, LintWarnings.DeclaredUsingPreview(kindName(sym), sym));
275-
}
276-
277280
/** Log a preview warning.
278281
* @param pos Position to be used for error reporting.
279282
* @param msg A Warning describing the problem.
@@ -3780,8 +3783,8 @@ void checkDeprecated(final DiagnosticPosition pos, final Symbol other, final Sym
37803783
}
37813784

37823785
void checkDeprecated(Supplier<DiagnosticPosition> pos, final Symbol other, final Symbol s) {
3783-
if ( (s.isDeprecatedForRemoval()
3784-
|| s.isDeprecated() && !other.isDeprecated())
3786+
if (!importSuppression
3787+
&& (s.isDeprecatedForRemoval() || s.isDeprecated() && !other.isDeprecated())
37853788
&& (s.outermostClass() != other.outermostClass() || s.outermostClass() == null)
37863789
&& s.kind != Kind.PCK) {
37873790
deferredLintHandler.report(_l -> warnDeprecated(pos.get(), s));
@@ -3830,10 +3833,10 @@ void checkPreview(DiagnosticPosition pos, Symbol other, Type site, Symbol s) {
38303833
log.error(pos, Errors.IsPreview(s));
38313834
} else {
38323835
preview.markUsesPreview(pos);
3833-
deferredLintHandler.report(_l -> warnPreviewAPI(pos, LintWarnings.IsPreview(s)));
3836+
warnPreviewAPI(pos, LintWarnings.IsPreview(s));
38343837
}
38353838
} else {
3836-
deferredLintHandler.report(_l -> warnPreviewAPI(pos, LintWarnings.IsPreviewReflective(s)));
3839+
warnPreviewAPI(pos, LintWarnings.IsPreviewReflective(s));
38373840
}
38383841
}
38393842
if (preview.declaredUsingPreviewFeature(s)) {
@@ -3842,7 +3845,7 @@ void checkPreview(DiagnosticPosition pos, Symbol other, Type site, Symbol s) {
38423845
//If "s" is compiled from source, then there was an error for it already;
38433846
//if "s" is from classfile, there already was an error for the classfile.
38443847
preview.markUsesPreview(pos);
3845-
deferredLintHandler.report(_l -> warnDeclaredUsingPreview(pos, s));
3848+
warnPreviewAPI(pos, LintWarnings.DeclaredUsingPreview(kindName(s), s));
38463849
}
38473850
}
38483851
}

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -527,16 +527,15 @@ private void doModuleImport(JCModuleImport tree) {
527527

528528
Type attribImportType(JCTree tree, Env<AttrContext> env) {
529529
Assert.check(completionEnabled);
530-
Lint prevLint = chk.setLint(allowDeprecationOnImport ?
531-
lint : lint.suppress(LintCategory.DEPRECATION, LintCategory.REMOVAL, LintCategory.PREVIEW));
530+
boolean prevImportSuppression = chk.setImportSuppression(!allowDeprecationOnImport);
532531
try {
533532
// To prevent deep recursion, suppress completion of some
534533
// types.
535534
completionEnabled = false;
536535
return attr.attribType(tree, env);
537536
} finally {
538537
completionEnabled = true;
539-
chk.setLint(prevLint);
538+
chk.setImportSuppression(prevImportSuppression);
540539
}
541540
}
542541

test/langtools/tools/javac/preview/PreviewAutoSuppress.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -190,8 +190,8 @@ public static class C extends Outer {}
190190
.getOutputLines(Task.OutputKind.DIRECT);
191191

192192
expected =
193-
List.of("Use.java:5:13: compiler.warn.is.preview: preview.api.Outer",
194-
"Use.java:7:35: compiler.warn.is.preview: preview.api.Outer",
193+
List.of("Use.java:7:35: compiler.warn.is.preview: preview.api.Outer",
194+
"Use.java:5:13: compiler.warn.is.preview: preview.api.Outer",
195195
"2 warnings");
196196

197197
if (!log.equals(expected))

0 commit comments

Comments
 (0)