Skip to content

Commit b787ad6

Browse files
committed
8361401: Warnings for use of Sun APIs should not be mandatory
Reviewed-by: jlahoda, vromero
1 parent 6b4a5ef commit b787ad6

File tree

6 files changed

+53
-31
lines changed

6 files changed

+53
-31
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3759,7 +3759,7 @@ void checkDeprecated(Supplier<DiagnosticPosition> pos, final Symbol other, final
37593759
void checkSunAPI(final DiagnosticPosition pos, final Symbol s) {
37603760
if ((s.flags() & PROPRIETARY) != 0) {
37613761
deferredLintHandler.report(_l -> {
3762-
log.mandatoryWarning(pos, Warnings.SunProprietary(s));
3762+
log.warning(pos, Warnings.SunProprietary(s));
37633763
});
37643764
}
37653765
}

src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1969,6 +1969,7 @@ compiler.warn.has.been.deprecated.for.removal.module=\
19691969
module {0} has been deprecated and marked for removal
19701970

19711971
# 0: symbol
1972+
# flags: strict
19721973
compiler.warn.sun.proprietary=\
19731974
{0} is internal proprietary API and may be removed in a future release
19741975

src/jdk.compiler/share/classes/com/sun/tools/javac/util/JCDiagnostic.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,9 @@ public enum DiagnosticFlag {
458458
API,
459459
/** Flag for not-supported-in-source-X errors.
460460
*/
461-
SOURCE_LEVEL;
461+
SOURCE_LEVEL,
462+
/** Flag for warnings that cannot be disabled */
463+
STRICT;
462464
}
463465

464466
private final DiagnosticSource source;

src/jdk.compiler/share/classes/com/sun/tools/javac/util/Log.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import com.sun.tools.javac.main.Main;
4949
import com.sun.tools.javac.main.Option;
5050
import com.sun.tools.javac.tree.EndPosTable;
51+
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
5152
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticInfo;
5253
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
5354
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType;
@@ -679,16 +680,6 @@ protected void directError(String key, Object... args) {
679680
errWriter.flush();
680681
}
681682

682-
/** Report a warning that cannot be suppressed.
683-
* @param pos The source position at which to report the warning.
684-
* @param key The key for the localized warning message.
685-
* @param args Fields of the warning message.
686-
*/
687-
public void strictWarning(DiagnosticPosition pos, String key, Object ... args) {
688-
writeDiagnostic(diags.warning(null, source, pos, key, args));
689-
nwarnings++;
690-
}
691-
692683
/**
693684
* Primary method to report a diagnostic.
694685
* @param diagnostic
@@ -797,7 +788,14 @@ public void report(JCDiagnostic diagnostic) {
797788
return;
798789
}
799790

800-
// Emit warning unless not mandatory and warnings are disabled
791+
// Strict warnings are always emitted
792+
if (diagnostic.isFlagSet(DiagnosticFlag.STRICT)) {
793+
writeDiagnostic(diagnostic);
794+
nwarnings++;
795+
return;
796+
}
797+
798+
// Emit other warning unless not mandatory and warnings are disabled
801799
if (emitWarnings || diagnostic.isMandatory()) {
802800
if (nwarnings < MaxWarnings) {
803801
writeDiagnostic(diagnostic);

test/langtools/tools/javac/options/system/SystemSunProprietary.java

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,18 @@
4141
import toolbox.TestRunner;
4242
import toolbox.ToolBox;
4343

44+
import javax.tools.Diagnostic;
45+
import javax.tools.Diagnostic.Kind;
46+
import javax.tools.DiagnosticListener;
47+
import javax.tools.JavaFileObject;
4448
import java.io.IOException;
4549
import java.nio.file.Files;
4650
import java.nio.file.Path;
4751
import java.nio.file.Paths;
4852
import java.util.ArrayList;
4953
import java.util.Collections;
5054
import java.util.List;
55+
import java.util.Locale;
5156

5257
public class SystemSunProprietary extends TestRunner {
5358

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

111116
private void expectSunapi(boolean expectDiagnostic, boolean ignoreSymbolFile, String... options)
112117
throws IOException {
113-
List<String> expected =
114-
expectDiagnostic
115-
? List.of(
116-
"Test.java:1:43: compiler.warn.sun.proprietary: sun.misc.Unsafe",
117-
"1 warning")
118-
: List.of("");
119118
List<String> allOptions = new ArrayList<>();
120119
allOptions.add("-XDrawDiagnostics");
121120
Collections.addAll(allOptions, options);
122121
JavacFileManager fm = new JavacFileManager(new Context(), false, null);
123122
fm.setSymbolFileEnabled(!ignoreSymbolFile);
124-
List<String> log =
125-
new JavacTask(tb)
126-
.fileManager(fm)
127-
.options(allOptions)
128-
.outdir(classes)
129-
.files(tb.findJavaFiles(src))
130-
.run(Expect.SUCCESS)
131-
.writeAll()
132-
.getOutputLines(Task.OutputKind.DIRECT);
133-
if (!log.equals(expected)) {
134-
throw new AssertionError("expected: " + expected + "\nactual: " + log + "\n");
123+
new JavacTask(tb)
124+
.fileManager(fm)
125+
.options(allOptions)
126+
.diagnosticListener(d -> sunAPIWarningChecker(d, expectDiagnostic))
127+
.outdir(classes)
128+
.files(tb.findJavaFiles(src))
129+
.run(Expect.SUCCESS)
130+
.writeAll();
131+
}
132+
133+
void sunAPIWarningChecker(Diagnostic<?> diag, boolean expectDiagnostic) {
134+
if (!expectDiagnostic) {
135+
throw new AssertionError("Unexpected diagnostic: " + diag.getMessage(Locale.getDefault()));
136+
} else {
137+
if (diag.getKind() != Kind.WARNING) {
138+
throw new AssertionError("Bad diagnostic kind. Expected " + Kind.WARNING + ", found: " + diag.getKind() + "\n");
139+
}
140+
if (!diag.getCode().equals("compiler.warn.sun.proprietary")) {
141+
throw new AssertionError("Bad diagnostic code. Expected \"compiler.warn.sun.proprietary\", found: " + diag.getCode() + "\n");
142+
}
143+
if (diag.getLineNumber() != 1 || diag.getColumnNumber() != 43) {
144+
throw new AssertionError("Bad diagnostic position. Expected 1:43, found: " + diag.getLineNumber() + ":" + diag.getColumnNumber() + "\n");
145+
}
135146
}
136147
}
137148

test/langtools/tools/lib/toolbox/JavacTask.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.util.stream.Collectors;
3838
import java.util.stream.Stream;
3939
import javax.annotation.processing.Processor;
40+
import javax.tools.DiagnosticListener;
4041
import javax.tools.JavaCompiler;
4142
import javax.tools.JavaFileManager;
4243
import javax.tools.JavaFileObject;
@@ -62,6 +63,7 @@ public class JavacTask extends AbstractTask<JavacTask> {
6263
private JavaFileManager fileManager;
6364
private Consumer<com.sun.source.util.JavacTask> callback;
6465
private List<Processor> procs;
66+
private DiagnosticListener<? super JavaFileObject> diagnosticListener;
6567

6668
private JavaCompiler compiler;
6769
private StandardJavaFileManager internalFileManager;
@@ -285,6 +287,14 @@ public JavacTask processors(Processor... procs) {
285287
return this;
286288
}
287289

290+
/**
291+
* Sets the diagnostic listener to be used.
292+
*/
293+
public JavacTask diagnosticListener(DiagnosticListener<? super JavaFileObject> diagnosticListener) {
294+
this.diagnosticListener = diagnosticListener;
295+
return this;
296+
}
297+
288298
/**
289299
* Sets the file manager to be used by this task.
290300
* @param fileManager the file manager
@@ -406,7 +416,7 @@ private int runAPI(PrintWriter pw) throws IOException {
406416
Iterable<? extends JavaFileObject> allFiles = joinFiles(files, fileObjects);
407417
JavaCompiler.CompilationTask task = compiler.getTask(pw,
408418
fileManager,
409-
null, // diagnostic listener; should optionally collect diags
419+
diagnosticListener, // diagnostic listener; should optionally collect diags
410420
allOpts,
411421
classes,
412422
allFiles);

0 commit comments

Comments
 (0)