Skip to content

Commit 96f114c

Browse files
cushongoogle-java-format Team
authored and
google-java-format Team
committed
Prepare for an incompatible change to Log diagnostic handlers
DeferredDiagnosticHandler changes from a static to inner class in openjdk/jdk@4890b74, see also b/410556169 Follow-up to 6f46e8a PiperOrigin-RevId: 747516421
1 parent 6f46e8a commit 96f114c

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

core/src/main/java/com/google/googlejavaformat/java/JavaInput.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import com.sun.tools.javac.util.Log.DeferredDiagnosticHandler;
4343
import com.sun.tools.javac.util.Options;
4444
import java.io.IOException;
45+
import java.lang.reflect.Constructor;
4546
import java.lang.reflect.Method;
4647
import java.net.URI;
4748
import java.util.ArrayList;
@@ -365,7 +366,7 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept
365366
return text;
366367
}
367368
});
368-
DeferredDiagnosticHandler diagnostics = new DeferredDiagnosticHandler(log);
369+
DeferredDiagnosticHandler diagnostics = deferredDiagnosticHandler(log);
369370
ImmutableList<RawTok> rawToks = JavacTokens.getTokens(text, context, stopTokens);
370371
Collection<JCDiagnostic> ds;
371372
try {
@@ -482,6 +483,29 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept
482483
return ImmutableList.copyOf(toks);
483484
}
484485

486+
private static final Constructor<DeferredDiagnosticHandler>
487+
DEFERRED_DIAGNOSTIC_HANDLER_CONSTRUCTOR = getDeferredDiagnosticHandlerConstructor();
488+
489+
// Depending on the JDK version, we might have a static class whose constructor has an explicit
490+
// Log parameter, or an inner class whose constructor has an *implicit* Log parameter. They are
491+
// different at the source level, but look the same to reflection.
492+
493+
private static Constructor<DeferredDiagnosticHandler> getDeferredDiagnosticHandlerConstructor() {
494+
try {
495+
return DeferredDiagnosticHandler.class.getConstructor(Log.class);
496+
} catch (NoSuchMethodException e) {
497+
throw new LinkageError(e.getMessage(), e);
498+
}
499+
}
500+
501+
private static DeferredDiagnosticHandler deferredDiagnosticHandler(Log log) {
502+
try {
503+
return DEFERRED_DIAGNOSTIC_HANDLER_CONSTRUCTOR.newInstance(log);
504+
} catch (ReflectiveOperationException e) {
505+
throw new LinkageError(e.getMessage(), e);
506+
}
507+
}
508+
485509
private static final Method GET_DIAGNOSTICS = getGetDiagnostics();
486510

487511
private static @Nullable Method getGetDiagnostics() {

0 commit comments

Comments
 (0)