Skip to content

Commit 4890b74

Browse files
committed
8354216: Small cleanups relating to Log.DiagnosticHandler
Reviewed-by: mcimadamore
1 parent 7680f70 commit 4890b74

File tree

11 files changed

+59
-77
lines changed

11 files changed

+59
-77
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTrees.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,7 @@ public TypeMirror getType(DocTreePath path) {
357357
if (tree instanceof DCReference dcReference) {
358358
JCTree qexpr = dcReference.qualifierExpression;
359359
if (qexpr != null) {
360-
Log.DeferredDiagnosticHandler deferredDiagnosticHandler =
361-
new Log.DeferredDiagnosticHandler(log);
360+
Log.DeferredDiagnosticHandler deferredDiagnosticHandler = log.new DeferredDiagnosticHandler();
362361
try {
363362
Env<AttrContext> env = getAttrContext(path.getTreePath());
364363
Type t = attr.attribType(dcReference.qualifierExpression, env);
@@ -388,8 +387,7 @@ private Symbol attributeDocReference(TreePath path, DCReference ref) {
388387
// module name and member name without type
389388
return null;
390389
}
391-
Log.DeferredDiagnosticHandler deferredDiagnosticHandler =
392-
new Log.DeferredDiagnosticHandler(log);
390+
Log.DeferredDiagnosticHandler deferredDiagnosticHandler = log.new DeferredDiagnosticHandler();
393391
try {
394392
final TypeSymbol tsym;
395393
final Name memberName;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ class RewritingContext {
730730
* Simple deferred diagnostic handler which filters out all messages and keep track of errors.
731731
*/
732732
Log.DeferredDiagnosticHandler diagHandler() {
733-
return new Log.DeferredDiagnosticHandler(log, d -> {
733+
return log.new DeferredDiagnosticHandler(d -> {
734734
if (d.getType() == DiagnosticType.ERROR) {
735735
erroneous = true;
736736
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2038,7 +2038,7 @@ void checkAutoCloseable(DiagnosticPosition pos, Env<AttrContext> env, Type resou
20382038
types.asSuper(resource, syms.autoCloseableType.tsym) != null &&
20392039
!types.isSameType(resource, syms.autoCloseableType)) { // Don't emit warning for AutoCloseable itself
20402040
Symbol close = syms.noSymbol;
2041-
Log.DiagnosticHandler discardHandler = new Log.DiscardDiagnosticHandler(log);
2041+
Log.DiagnosticHandler discardHandler = log.new DiscardDiagnosticHandler();
20422042
try {
20432043
close = rs.resolveQualifiedMethod(pos,
20442044
env,

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
@@ -3685,7 +3685,7 @@ private Name[] defaultTargetMetaInfo() {
36853685
*/
36863686
public boolean validateAnnotationDeferErrors(JCAnnotation a) {
36873687
boolean res = false;
3688-
final Log.DiagnosticHandler diagHandler = new Log.DiscardDiagnosticHandler(log);
3688+
final Log.DiagnosticHandler diagHandler = log.new DiscardDiagnosticHandler();
36893689
try {
36903690
res = validateAnnotation(a);
36913691
} finally {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 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
@@ -547,7 +547,7 @@ public void scan(JCTree tree) {
547547
}
548548

549549
DeferredAttrDiagHandler(Log log, JCTree newTree) {
550-
super(log, d -> {
550+
log.super(d -> {
551551
PosScanner posScanner = new PosScanner(d.getDiagnosticPosition());
552552
posScanner.scan(newTree);
553553
return posScanner.found;

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ public void analyzeLambda(Env<AttrContext> env, JCLambda that, TreeMaker make, b
239239
//step - if we suppress diagnostics, we won't stop at Attr for flow-analysis
240240
//related errors, which will allow for more errors to be detected
241241
if (!speculative) {
242-
diagHandler = new Log.DiscardDiagnosticHandler(log);
242+
diagHandler = log.new DiscardDiagnosticHandler();
243243
}
244244
try {
245245
new LambdaAliveAnalyzer().analyzeTree(env, that, make);
@@ -257,7 +257,7 @@ public List<Type> analyzeLambdaThrownTypes(final Env<AttrContext> env,
257257
//message will be reported and will cause compilation to skip the flow analysis
258258
//step - if we suppress diagnostics, we won't stop at Attr for flow-analysis
259259
//related errors, which will allow for more errors to be detected
260-
Log.DiagnosticHandler diagHandler = new Log.DiscardDiagnosticHandler(log);
260+
Log.DiagnosticHandler diagHandler = log.new DiscardDiagnosticHandler();
261261
try {
262262
new LambdaAssignAnalyzer(env).analyzeTree(env, that, make);
263263
LambdaFlowAnalyzer flowAnalyzer = new LambdaFlowAnalyzer();
@@ -274,7 +274,7 @@ public boolean aliveAfter(Env<AttrContext> env, JCTree that, TreeMaker make) {
274274
//message will be reported and will cause compilation to skip the flow analysis
275275
//step - if we suppress diagnostics, we won't stop at Attr for flow-analysis
276276
//related errors, which will allow for more errors to be detected
277-
Log.DiagnosticHandler diagHandler = new Log.DiscardDiagnosticHandler(log);
277+
Log.DiagnosticHandler diagHandler = log.new DiscardDiagnosticHandler();
278278
try {
279279
SnippetAliveAnalyzer analyzer = new SnippetAliveAnalyzer();
280280

@@ -291,7 +291,7 @@ public boolean breaksToTree(Env<AttrContext> env, JCTree breakTo, JCTree body, T
291291
//message will be reported and will cause compilation to skip the flow analysis
292292
//step - if we suppress diagnostics, we won't stop at Attr for flow-analysis
293293
//related errors, which will allow for more errors to be detected
294-
Log.DiagnosticHandler diagHandler = new Log.DiscardDiagnosticHandler(log);
294+
Log.DiagnosticHandler diagHandler = log.new DiscardDiagnosticHandler();
295295
try {
296296
SnippetBreakToAnalyzer analyzer = new SnippetBreakToAnalyzer(breakTo);
297297

src/jdk.compiler/share/classes/com/sun/tools/javac/main/JavaCompiler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,7 +1166,7 @@ public void initProcessAnnotations(Iterable<? extends Processor> processors,
11661166
genEndPos = true;
11671167
if (!taskListener.isEmpty())
11681168
taskListener.started(new TaskEvent(TaskEvent.Kind.ANNOTATION_PROCESSING));
1169-
deferredDiagnosticHandler = new Log.DeferredDiagnosticHandler(log);
1169+
deferredDiagnosticHandler = log.new DeferredDiagnosticHandler();
11701170
procEnvImpl.getFiler().setInitialState(initialFiles, initialClassNames);
11711171
}
11721172
} else { // free resources
@@ -1898,7 +1898,7 @@ private Name findPackageInFile(JavaFileObject fo) {
18981898

18991899
private Name parseAndGetName(JavaFileObject fo,
19001900
Function<JCTree.JCCompilationUnit, Name> tree2Name) {
1901-
DiagnosticHandler dh = new DiscardDiagnosticHandler(log);
1901+
DiagnosticHandler dh = log.new DiscardDiagnosticHandler();
19021902
JavaFileObject prevSource = log.useSource(fo);
19031903
try {
19041904
JCTree.JCCompilationUnit t = parse(fo, fo.getCharContent(false), true);

src/jdk.compiler/share/classes/com/sun/tools/javac/parser/ReferenceParser.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 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
@@ -119,7 +119,7 @@ public Reference parse(String sig, Mode mode) throws ParseException {
119119
Name member;
120120
List<JCTree> paramTypes;
121121

122-
Log.DeferredDiagnosticHandler dh = new Log.DeferredDiagnosticHandler(fac.log);
122+
Log.DeferredDiagnosticHandler dh = fac.log.new DeferredDiagnosticHandler();
123123

124124
try {
125125
int slash = sig.indexOf("/");
@@ -278,9 +278,10 @@ private List<JCTree> parseParams(String sig, int beginIndex, int endIndex, Log.D
278278
}
279279

280280
private void checkDiags(Log.DeferredDiagnosticHandler h, int offset) throws ParseException {
281-
JCDiagnostic d = h.getDiagnostics().peek();
282-
if (d != null) {
283-
throw new ParseException(offset + ((int) d.getPosition()), "dc.ref.syntax.error");
281+
java.util.List<JCDiagnostic> diagnostics = h.getDiagnostics();
282+
if (!diagnostics.isEmpty()) {
283+
int pos = offset + (int)diagnostics.get(0).getPosition();
284+
throw new ParseException(pos, "dc.ref.syntax.error");
284285
}
285286
}
286287

src/jdk.compiler/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import javax.lang.model.SourceVersion;
4040
import javax.lang.model.element.*;
4141
import javax.lang.model.util.*;
42+
import javax.tools.Diagnostic;
4243
import javax.tools.JavaFileManager;
4344
import javax.tools.JavaFileObject;
4445
import javax.tools.JavaFileObject.Kind;
@@ -1004,7 +1005,7 @@ private Round(int number, Set<JCCompilationUnit> treesToClean,
10041005
Assert.checkNonNull(deferredDiagnosticHandler);
10051006
this.deferredDiagnosticHandler = deferredDiagnosticHandler;
10061007
} else {
1007-
this.deferredDiagnosticHandler = new Log.DeferredDiagnosticHandler(log);
1008+
this.deferredDiagnosticHandler = log.new DeferredDiagnosticHandler();
10081009
compiler.setDeferredDiagnosticHandler(this.deferredDiagnosticHandler);
10091010
}
10101011

@@ -1107,21 +1108,9 @@ boolean unrecoverableError() {
11071108
if (messager.errorRaised())
11081109
return true;
11091110

1110-
for (JCDiagnostic d: deferredDiagnosticHandler.getDiagnostics()) {
1111-
switch (d.getKind()) {
1112-
case WARNING:
1113-
if (werror)
1114-
return true;
1115-
break;
1116-
1117-
case ERROR:
1118-
if (fatalErrors || !d.isFlagSet(RECOVERABLE))
1119-
return true;
1120-
break;
1121-
}
1122-
}
1123-
1124-
return false;
1111+
return deferredDiagnosticHandler.getDiagnostics().stream()
1112+
.anyMatch(d -> (d.getKind() == Diagnostic.Kind.WARNING && werror) ||
1113+
(d.getKind() == Diagnostic.Kind.ERROR && (fatalErrors || !d.isFlagSet(RECOVERABLE))));
11251114
}
11261115

11271116
/** Find the set of annotations present in the set of top level

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

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@
2626
package com.sun.tools.javac.util;
2727

2828
import java.io.*;
29+
import java.util.ArrayList;
2930
import java.util.Arrays;
3031
import java.util.Comparator;
3132
import java.util.EnumMap;
3233
import java.util.HashSet;
34+
import java.util.List;
3335
import java.util.Map;
34-
import java.util.Queue;
36+
import java.util.Optional;
3537
import java.util.Set;
3638
import java.util.function.Predicate;
3739

@@ -83,26 +85,26 @@ public String key(String k) {
8385
/**
8486
* DiagnosticHandler's provide the initial handling for diagnostics.
8587
* When a diagnostic handler is created and has been initialized, it
86-
* should install itself as the current diagnostic handler. When a
88+
* will install itself as the current diagnostic handler. When a
8789
* client has finished using a handler, the client should call
8890
* {@code log.removeDiagnosticHandler();}
8991
*
9092
* Note that javax.tools.DiagnosticListener (if set) is called later in the
9193
* diagnostic pipeline.
9294
*/
93-
public abstract static class DiagnosticHandler {
95+
public abstract class DiagnosticHandler {
9496
/**
9597
* The previously installed diagnostic handler.
9698
*/
97-
protected DiagnosticHandler prev;
99+
protected final DiagnosticHandler prev;
98100

99101
/**
100102
* Install this diagnostic handler as the current one,
101103
* recording the previous one.
102104
*/
103-
protected void install(Log log) {
104-
prev = log.diagnosticHandler;
105-
log.diagnosticHandler = this;
105+
protected DiagnosticHandler() {
106+
prev = diagnosticHandler;
107+
diagnosticHandler = this;
106108
}
107109

108110
/**
@@ -114,11 +116,7 @@ protected void install(Log log) {
114116
/**
115117
* A DiagnosticHandler that discards all diagnostics.
116118
*/
117-
public static class DiscardDiagnosticHandler extends DiagnosticHandler {
118-
@SuppressWarnings("this-escape")
119-
public DiscardDiagnosticHandler(Log log) {
120-
install(log);
121-
}
119+
public class DiscardDiagnosticHandler extends DiagnosticHandler {
122120

123121
@Override
124122
public void report(JCDiagnostic diag) { }
@@ -131,39 +129,38 @@ public void report(JCDiagnostic diag) { }
131129
* with reportAllDiagnostics(), it will be reported to the previously
132130
* active diagnostic handler.
133131
*/
134-
public static class DeferredDiagnosticHandler extends DiagnosticHandler {
135-
private Queue<JCDiagnostic> deferred = new ListBuffer<>();
132+
public class DeferredDiagnosticHandler extends DiagnosticHandler {
133+
private List<JCDiagnostic> deferred = new ArrayList<>();
136134
private final Predicate<JCDiagnostic> filter;
137135
private final boolean passOnNonDeferrable;
138136

139-
public DeferredDiagnosticHandler(Log log) {
140-
this(log, null);
137+
public DeferredDiagnosticHandler() {
138+
this(null);
141139
}
142140

143-
public DeferredDiagnosticHandler(Log log, Predicate<JCDiagnostic> filter) {
144-
this(log, filter, true);
141+
public DeferredDiagnosticHandler(Predicate<JCDiagnostic> filter) {
142+
this(filter, true);
145143
}
146144

147-
@SuppressWarnings("this-escape")
148-
public DeferredDiagnosticHandler(Log log, Predicate<JCDiagnostic> filter, boolean passOnNonDeferrable) {
149-
this.filter = filter;
145+
public DeferredDiagnosticHandler(Predicate<JCDiagnostic> filter, boolean passOnNonDeferrable) {
146+
this.filter = Optional.ofNullable(filter).orElse(d -> true);
150147
this.passOnNonDeferrable = passOnNonDeferrable;
151-
install(log);
148+
}
149+
150+
private boolean deferrable(JCDiagnostic diag) {
151+
return !(diag.isFlagSet(DiagnosticFlag.NON_DEFERRABLE) && passOnNonDeferrable) && filter.test(diag);
152152
}
153153

154154
@Override
155155
public void report(JCDiagnostic diag) {
156-
boolean deferrable = !passOnNonDeferrable ||
157-
!diag.isFlagSet(JCDiagnostic.DiagnosticFlag.NON_DEFERRABLE);
158-
if (deferrable &&
159-
(filter == null || filter.test(diag))) {
156+
if (deferrable(diag)) {
160157
deferred.add(diag);
161158
} else {
162159
prev.report(diag);
163160
}
164161
}
165162

166-
public Queue<JCDiagnostic> getDiagnostics() {
163+
public List<JCDiagnostic> getDiagnostics() {
167164
return deferred;
168165
}
169166

@@ -174,22 +171,18 @@ public void reportDeferredDiagnostics() {
174171

175172
/** Report selected deferred diagnostics. */
176173
public void reportDeferredDiagnostics(Predicate<JCDiagnostic> accepter) {
177-
JCDiagnostic d;
178-
while ((d = deferred.poll()) != null) {
179-
if (accepter.test(d))
180-
prev.report(d);
181-
}
174+
175+
// Flush matching reports to the previous handler
176+
deferred.stream()
177+
.filter(accepter)
178+
.forEach(prev::report);
182179
deferred = null; // prevent accidental ongoing use
183180
}
184181

185-
/** Report selected deferred diagnostics. */
182+
/** Report all deferred diagnostics in the specified order. */
186183
public void reportDeferredDiagnostics(Comparator<JCDiagnostic> order) {
187-
JCDiagnostic[] diags = deferred.toArray(s -> new JCDiagnostic[s]);
188-
Arrays.sort(diags, order);
189-
for (JCDiagnostic d : diags) {
190-
prev.report(d);
191-
}
192-
deferred = null; // prevent accidental ongoing use
184+
deferred.sort(order); // ok to sort in place: reportDeferredDiagnostics() is going to discard it
185+
reportDeferredDiagnostics();
193186
}
194187
}
195188

@@ -483,6 +476,7 @@ public void setWriters(PrintWriter pw) {
483476
*/
484477
public void popDiagnosticHandler(DiagnosticHandler h) {
485478
Assert.check(diagnosticHandler == h);
479+
Assert.check(h.prev != null);
486480
diagnosticHandler = h.prev;
487481
}
488482

src/jdk.jshell/share/classes/jdk/jshell/TaskFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 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
@@ -717,7 +717,7 @@ private void setVariableType(VarSnippet s) {
717717
//if it was not enhanced yet:
718718
//ignore any errors:
719719
JavaFileObject prev = log.useSource(null);
720-
DiscardDiagnosticHandler h = new DiscardDiagnosticHandler(log);
720+
DiscardDiagnosticHandler h = log.new DiscardDiagnosticHandler();
721721
parserFactory.runPermitIntersectionTypes(() -> {
722722
try {
723723
//parse the type as a cast, i.e. "(<typeName>) x". This is to support

0 commit comments

Comments
 (0)