From 0eea448e8f8e00fde3e40f30ddb425f4a00eaf89 Mon Sep 17 00:00:00 2001 From: Alex Abashev Date: Sun, 29 Nov 2026 23:02:14 +0300 Subject: [PATCH] Call the syntax tree APIs of JDK 17 directly JavaInputAstVisitor and RemoveUnusedImports looked up through reflection the tree methods that JDKs older than 17 lack: CompilationUnitTree.getModule, ClassTree.getPermitsClause, BindingPatternTree.getVariable and CaseTree.getLabels, and fell back to the getType and getBinding of the binding patterns of JDK 14 and 15, and to CaseTree.getExpressions. No JDK the formatter runs on is older than 17, so they are called directly now and the fallbacks are gone, as google-java-format did in c1ea933ba. Flags.RECORD and Flags.COMPACT_RECORD_CONSTRUCTOR replace the copies kept for Java 11; their values are the same on JDK 17, 21 and 27. RemoveUnusedImports keeps its own visit of a case's labels: the TreeScanner of JDK 17 visits CaseTree.getExpressions, which leave out a pattern label, while that of JDK 21 visits the labels, so for a while they are visited twice. javac 17 compiles the direct calls; getLabels and CaseLabelTree are a reflective preview API there, which gives a warning only. The reflection for APIs newer than JDK 21, ImportTree.isModule and the value modifier, stays. The tests pass on JDK 21, 25 and 27, and a corpus of 58,017 files formats to the same bytes as with 21.1.2. This prepares a release line for Java 17 (#293). --- .../javaformat/java/JavaInputAstVisitor.java | 77 ++++--------------- .../javaformat/java/RemoveUnusedImports.java | 28 ++----- 2 files changed, 18 insertions(+), 87 deletions(-) diff --git a/open-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java b/open-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java index 50b81f44e..6ee336f5e 100644 --- a/open-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java +++ b/open-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java @@ -33,6 +33,8 @@ import static com.sun.source.tree.Tree.Kind.ANNOTATION; import static com.sun.source.tree.Tree.Kind.EXTENDS_WILDCARD; import static com.sun.source.tree.Tree.Kind.STRING_LITERAL; +import static com.sun.tools.javac.code.Flags.COMPACT_RECORD_CONSTRUCTOR; +import static com.sun.tools.javac.code.Flags.RECORD; import com.google.common.base.Enums; import com.google.common.base.MoreObjects; @@ -75,6 +77,7 @@ import com.sun.source.tree.BindingPatternTree; import com.sun.source.tree.BlockTree; import com.sun.source.tree.BreakTree; +import com.sun.source.tree.CaseLabelTree; import com.sun.source.tree.CaseTree; import com.sun.source.tree.CatchTree; import com.sun.source.tree.ClassTree; @@ -400,16 +403,8 @@ public Void visitCompilationUnit(CompilationUnitTree node, Void unused) { return null; } - @Nullable - private static final Method COMPILATION_UNIT_TREE_GET_MODULE = - maybeGetMethod(CompilationUnitTree.class, "getModule"); - protected void handleModule(boolean first, CompilationUnitTree node) { - if (COMPILATION_UNIT_TREE_GET_MODULE == null) { - // Java < 17, see https://bugs.openjdk.java.net/browse/JDK-8255464 - return; - } - ModuleTree module = (ModuleTree) invoke(COMPILATION_UNIT_TREE_GET_MODULE, node); + ModuleTree module = node.getModule(); if (module != null) { if (!first) { builder.blankLineWanted(BlankLineWanted.YES); @@ -1287,32 +1282,11 @@ public Void visitInstanceOf(InstanceOfTree node, Void unused) { return null; } - @Nullable - private static final Method BINDING_PATTERN_TREE_GET_VARIABLE = - maybeGetMethod(BindingPatternTree.class, "getVariable"); - - @Nullable - private static final Method BINDING_PATTERN_TREE_GET_TYPE = maybeGetMethod(BindingPatternTree.class, "getType"); - - @Nullable - private static final Method BINDING_PATTERN_TREE_GET_BINDING = - maybeGetMethod(BindingPatternTree.class, "getBinding"); - @Override public Void visitBindingPattern(BindingPatternTree node, Void unused) { sync(node); - if (BINDING_PATTERN_TREE_GET_VARIABLE != null) { - VariableTree variableTree = (VariableTree) invoke(BINDING_PATTERN_TREE_GET_VARIABLE, node); - visitBindingPattern(variableTree.getModifiers(), variableTree.getType(), variableTree.getName()); - } else if (BINDING_PATTERN_TREE_GET_TYPE != null && BINDING_PATTERN_TREE_GET_BINDING != null) { - Tree type = (Tree) invoke(BINDING_PATTERN_TREE_GET_TYPE, node); - Name name = (Name) invoke(BINDING_PATTERN_TREE_GET_BINDING, node); - visitBindingPattern(/* modifiers= */ null, type, name); - } else { - throw new RuntimeException( - "BindingPatternTree must have either getVariable() or both getType() and getBinding()," - + " but does not"); - } + VariableTree variableTree = node.getVariable(); + visitBindingPattern(variableTree.getModifiers(), variableTree.getType(), variableTree.getName()); return null; } @@ -1324,11 +1298,9 @@ private static boolean isVarType(Tree type) { return type.getKind().name().equals("VAR_TYPE"); } - private void visitBindingPattern(@Nullable ModifiersTree modifiers, @Nullable Tree type, Name name) { + private void visitBindingPattern(ModifiersTree modifiers, @Nullable Tree type, Name name) { builder.open(plusFour); - if (modifiers != null) { - builder.addAll(visitModifiers(modifiers, Direction.HORIZONTAL, Optional.empty())); - } + builder.addAll(visitModifiers(modifiers, Direction.HORIZONTAL, Optional.empty())); if (type == null || isVarType(type)) { token("var"); } else { @@ -1533,11 +1505,6 @@ public Void visitAnnotatedType(AnnotatedTypeTree node, Void unused) { return null; } - // TODO(fawind): Use Flags.COMPACT_RECORD_CONSTRUCTOR once if/when we drop support for Java 11 - protected static final long COMPACT_RECORD_CONSTRUCTOR = 1L << 51; - - protected static final long RECORD = 1L << 61; - // TODO: use Flags.IMPLICIT_CLASS once this module compiles with JDK 22 or later (JDK 21 calls // bit 19 UNNAMED_CLASS). javac sets it on the class it synthesizes for a compact source file. private static final long IMPLICIT_CLASS = 1L << 19; @@ -1990,24 +1957,14 @@ boolean visitSingleMemberAnnotation(AnnotationTree node) { return true; } - @Nullable - private static final Method CASE_TREE_GET_LABELS = maybeGetMethod(CaseTree.class, "getLabels"); - @Override public Void visitCase(CaseTree node, Void unused) { sync(node); markForPartialFormat(); builder.forcedBreak(); - List labels; - boolean isDefault; - if (CASE_TREE_GET_LABELS != null) { - labels = (List) invoke(CASE_TREE_GET_LABELS, node); - isDefault = labels.size() == 1 - && Iterables.getOnlyElement(labels).getKind().name().equals("DEFAULT_CASE_LABEL"); - } else { - labels = node.getExpressions(); - isDefault = labels.isEmpty(); - } + List labels = node.getLabels(); + boolean isDefault = + labels.size() == 1 && getOnlyElement(labels).getKind().name().equals("DEFAULT_CASE_LABEL"); if (isDefault) { token("default", ZERO); } else { @@ -4069,17 +4026,9 @@ private void addBodyDeclarations( } } - @Nullable - private static final Method CLASS_TREE_GET_PERMITS_CLAUSE = maybeGetMethod(ClassTree.class, "getPermitsClause"); - - /** Gets the permits clause for the given node. This is only available in Java 15 and later. */ + /** Gets the permits clause for the given node, empty unless the class or interface is sealed. */ protected List getPermitsClause(ClassTree node) { - if (CLASS_TREE_GET_PERMITS_CLAUSE != null) { - return (List) invoke(CLASS_TREE_GET_PERMITS_CLAUSE, node); - } else { - // Java < 15 - return List.of(); - } + return node.getPermitsClause(); } private void classDeclarationTypeList(String token, List types) { diff --git a/open-java-format/src/main/java/com/palantir/javaformat/java/RemoveUnusedImports.java b/open-java-format/src/main/java/com/palantir/javaformat/java/RemoveUnusedImports.java index a5441eb24..d3594d4bc 100644 --- a/open-java-format/src/main/java/com/palantir/javaformat/java/RemoveUnusedImports.java +++ b/open-java-format/src/main/java/com/palantir/javaformat/java/RemoveUnusedImports.java @@ -46,7 +46,6 @@ import com.sun.tools.javac.util.Options; import java.lang.reflect.Method; import java.util.LinkedHashSet; -import java.util.List; import java.util.Map; import java.util.Set; import org.jspecify.annotations.Nullable; @@ -102,32 +101,15 @@ public Void visitIdentifier(IdentifierTree tree, Void unused) { return null; } - // TODO(fwindheuser): remove this override when pattern matching in switch is no longer a preview - // feature, and TreePathScanner visits CaseTree#getLabels instead of CaseTree#getExpressions + // The TreeScanner of JDK 17 visits CaseTree#getExpressions, which leave out a pattern label and so the type + // it names; from JDK 21 it visits CaseTree#getLabels, and the labels are scanned twice, which changes nothing. + // TODO: remove this override once no release line runs on a JDK older than 21. @Override public Void visitCase(CaseTree tree, Void unused) { - if (CASE_TREE_GET_LABELS != null) { - try { - scan((List) CASE_TREE_GET_LABELS.invoke(tree), null); - } catch (ReflectiveOperationException e) { - throw new RuntimeException(e.getMessage(), e); - } - } + scan(tree.getLabels(), null); return super.visitCase(tree, null); } - @Nullable - private static final Method CASE_TREE_GET_LABELS = caseTreeGetLabels(); - - @Nullable - private static Method caseTreeGetLabels() { - try { - return CaseTree.class.getMethod("getLabels"); - } catch (NoSuchMethodException e) { - return null; - } - } - @Override public Void scan(@Nullable Tree tree, Void unused) { if (tree == null) { @@ -295,7 +277,7 @@ private static boolean isBlankLineAfter(String contents, int pos, String sep) { } // ImportTree#isModule() (JEP 511) exists from JDK 23 on; this module compiles with a JDK 21 - // compiler, so it can't be referenced directly. Same idiom as CASE_TREE_GET_LABELS above. + // compiler, so it can't be referenced directly. @Nullable private static final Method IMPORT_TREE_IS_MODULE = JavaInputAstVisitor.maybeGetMethod(ImportTree.class, "isModule");