Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3759,7 +3759,7 @@ void checkDeprecated(Supplier<DiagnosticPosition> pos, final Symbol other, final
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));
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,9 @@ public enum DiagnosticFlag {
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;
Expand Down
20 changes: 9 additions & 11 deletions src/jdk.compiler/share/classes/com/sun/tools/javac/util/Log.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
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;
Expand Down Expand Up @@ -679,16 +680,6 @@ protected void directError(String key, Object... args) {
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
Expand Down Expand Up @@ -797,7 +788,14 @@ public void report(JCDiagnostic diagnostic) {
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);
Expand Down
45 changes: 28 additions & 17 deletions test/langtools/tools/javac/options/system/SystemSunProprietary.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,18 @@
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;
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 {

Expand Down Expand Up @@ -110,28 +115,34 @@ private void expectNoSunapi(boolean ignoreSymbolFile, String... options) throws

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");
}
}
}

Expand Down
12 changes: 11 additions & 1 deletion test/langtools/tools/lib/toolbox/JavacTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
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;
Expand All @@ -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;
Expand Down Expand Up @@ -285,6 +287,14 @@ public JavacTask processors(Processor... procs) {
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
Expand Down Expand Up @@ -406,7 +416,7 @@ private int runAPI(PrintWriter pw) throws IOException {
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);
Expand Down