8361401: Warnings for use of Sun APIs should not be mandatory

Reviewed-by: jlahoda, vromero
This commit is contained in:
Maurizio Cimadamore 2025-07-16 09:55:08 +00:00
parent 6b4a5ef105
commit b787ad6f69
6 changed files with 53 additions and 31 deletions

View File

@ -3759,7 +3759,7 @@ public class Check {
void checkSunAPI(final DiagnosticPosition pos, final Symbol s) {
if ((s.flags() & PROPRIETARY) != 0) {
deferredLintHandler.report(_l -> {
log.mandatoryWarning(pos, Warnings.SunProprietary(s));
log.warning(pos, Warnings.SunProprietary(s));
});
}
}

View File

@ -1969,6 +1969,7 @@ compiler.warn.has.been.deprecated.for.removal.module=\
module {0} has been deprecated and marked for removal
# 0: symbol
# flags: strict
compiler.warn.sun.proprietary=\
{0} is internal proprietary API and may be removed in a future release

View File

@ -458,7 +458,9 @@ public class JCDiagnostic implements Diagnostic<JavaFileObject> {
API,
/** Flag for not-supported-in-source-X errors.
*/
SOURCE_LEVEL;
SOURCE_LEVEL,
/** Flag for warnings that cannot be disabled */
STRICT;
}
private final DiagnosticSource source;

View File

@ -48,6 +48,7 @@ import com.sun.tools.javac.code.Source;
import com.sun.tools.javac.main.Main;
import com.sun.tools.javac.main.Option;
import com.sun.tools.javac.tree.EndPosTable;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticInfo;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType;
@ -679,16 +680,6 @@ public class Log extends AbstractLog {
errWriter.flush();
}
/** Report a warning that cannot be suppressed.
* @param pos The source position at which to report the warning.
* @param key The key for the localized warning message.
* @param args Fields of the warning message.
*/
public void strictWarning(DiagnosticPosition pos, String key, Object ... args) {
writeDiagnostic(diags.warning(null, source, pos, key, args));
nwarnings++;
}
/**
* Primary method to report a diagnostic.
* @param diagnostic
@ -797,7 +788,14 @@ public class Log extends AbstractLog {
return;
}
// Emit warning unless not mandatory and warnings are disabled
// Strict warnings are always emitted
if (diagnostic.isFlagSet(DiagnosticFlag.STRICT)) {
writeDiagnostic(diagnostic);
nwarnings++;
return;
}
// Emit other warning unless not mandatory and warnings are disabled
if (emitWarnings || diagnostic.isMandatory()) {
if (nwarnings < MaxWarnings) {
writeDiagnostic(diagnostic);

View File

@ -41,6 +41,10 @@ import toolbox.Task.Expect;
import toolbox.TestRunner;
import toolbox.ToolBox;
import javax.tools.Diagnostic;
import javax.tools.Diagnostic.Kind;
import javax.tools.DiagnosticListener;
import javax.tools.JavaFileObject;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
@ -48,6 +52,7 @@ import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
public class SystemSunProprietary extends TestRunner {
@ -110,28 +115,34 @@ public class SystemSunProprietary extends TestRunner {
private void expectSunapi(boolean expectDiagnostic, boolean ignoreSymbolFile, String... options)
throws IOException {
List<String> expected =
expectDiagnostic
? List.of(
"Test.java:1:43: compiler.warn.sun.proprietary: sun.misc.Unsafe",
"1 warning")
: List.of("");
List<String> allOptions = new ArrayList<>();
allOptions.add("-XDrawDiagnostics");
Collections.addAll(allOptions, options);
JavacFileManager fm = new JavacFileManager(new Context(), false, null);
fm.setSymbolFileEnabled(!ignoreSymbolFile);
List<String> log =
new JavacTask(tb)
.fileManager(fm)
.options(allOptions)
.outdir(classes)
.files(tb.findJavaFiles(src))
.run(Expect.SUCCESS)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
if (!log.equals(expected)) {
throw new AssertionError("expected: " + expected + "\nactual: " + log + "\n");
new JavacTask(tb)
.fileManager(fm)
.options(allOptions)
.diagnosticListener(d -> sunAPIWarningChecker(d, expectDiagnostic))
.outdir(classes)
.files(tb.findJavaFiles(src))
.run(Expect.SUCCESS)
.writeAll();
}
void sunAPIWarningChecker(Diagnostic<?> diag, boolean expectDiagnostic) {
if (!expectDiagnostic) {
throw new AssertionError("Unexpected diagnostic: " + diag.getMessage(Locale.getDefault()));
} else {
if (diag.getKind() != Kind.WARNING) {
throw new AssertionError("Bad diagnostic kind. Expected " + Kind.WARNING + ", found: " + diag.getKind() + "\n");
}
if (!diag.getCode().equals("compiler.warn.sun.proprietary")) {
throw new AssertionError("Bad diagnostic code. Expected \"compiler.warn.sun.proprietary\", found: " + diag.getCode() + "\n");
}
if (diag.getLineNumber() != 1 || diag.getColumnNumber() != 43) {
throw new AssertionError("Bad diagnostic position. Expected 1:43, found: " + diag.getLineNumber() + ":" + diag.getColumnNumber() + "\n");
}
}
}

View File

@ -37,6 +37,7 @@ import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.processing.Processor;
import javax.tools.DiagnosticListener;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;
@ -62,6 +63,7 @@ public class JavacTask extends AbstractTask<JavacTask> {
private JavaFileManager fileManager;
private Consumer<com.sun.source.util.JavacTask> callback;
private List<Processor> procs;
private DiagnosticListener<? super JavaFileObject> diagnosticListener;
private JavaCompiler compiler;
private StandardJavaFileManager internalFileManager;
@ -285,6 +287,14 @@ public class JavacTask extends AbstractTask<JavacTask> {
return this;
}
/**
* Sets the diagnostic listener to be used.
*/
public JavacTask diagnosticListener(DiagnosticListener<? super JavaFileObject> diagnosticListener) {
this.diagnosticListener = diagnosticListener;
return this;
}
/**
* Sets the file manager to be used by this task.
* @param fileManager the file manager
@ -406,7 +416,7 @@ public class JavacTask extends AbstractTask<JavacTask> {
Iterable<? extends JavaFileObject> allFiles = joinFiles(files, fileObjects);
JavaCompiler.CompilationTask task = compiler.getTask(pw,
fileManager,
null, // diagnostic listener; should optionally collect diags
diagnosticListener, // diagnostic listener; should optionally collect diags
allOpts,
classes,
allFiles);