Skip to content

Commit 482aee2

Browse files
authored
Merge pull request #295 from openjavaformat/abashev/remove-jdk-14-16-reflection
Call the syntax tree APIs of JDK 17 directly
2 parents 3940645 + 0eea448 commit 482aee2

2 files changed

Lines changed: 18 additions & 87 deletions

File tree

‎open-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java‎

Lines changed: 13 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import static com.sun.source.tree.Tree.Kind.ANNOTATION;
3434
import static com.sun.source.tree.Tree.Kind.EXTENDS_WILDCARD;
3535
import static com.sun.source.tree.Tree.Kind.STRING_LITERAL;
36+
import static com.sun.tools.javac.code.Flags.COMPACT_RECORD_CONSTRUCTOR;
37+
import static com.sun.tools.javac.code.Flags.RECORD;
3638

3739
import com.google.common.base.Enums;
3840
import com.google.common.base.MoreObjects;
@@ -75,6 +77,7 @@
7577
import com.sun.source.tree.BindingPatternTree;
7678
import com.sun.source.tree.BlockTree;
7779
import com.sun.source.tree.BreakTree;
80+
import com.sun.source.tree.CaseLabelTree;
7881
import com.sun.source.tree.CaseTree;
7982
import com.sun.source.tree.CatchTree;
8083
import com.sun.source.tree.ClassTree;
@@ -400,16 +403,8 @@ public Void visitCompilationUnit(CompilationUnitTree node, Void unused) {
400403
return null;
401404
}
402405

403-
@Nullable
404-
private static final Method COMPILATION_UNIT_TREE_GET_MODULE =
405-
maybeGetMethod(CompilationUnitTree.class, "getModule");
406-
407406
protected void handleModule(boolean first, CompilationUnitTree node) {
408-
if (COMPILATION_UNIT_TREE_GET_MODULE == null) {
409-
// Java < 17, see https://bugs.openjdk.java.net/browse/JDK-8255464
410-
return;
411-
}
412-
ModuleTree module = (ModuleTree) invoke(COMPILATION_UNIT_TREE_GET_MODULE, node);
407+
ModuleTree module = node.getModule();
413408
if (module != null) {
414409
if (!first) {
415410
builder.blankLineWanted(BlankLineWanted.YES);
@@ -1287,32 +1282,11 @@ public Void visitInstanceOf(InstanceOfTree node, Void unused) {
12871282
return null;
12881283
}
12891284

1290-
@Nullable
1291-
private static final Method BINDING_PATTERN_TREE_GET_VARIABLE =
1292-
maybeGetMethod(BindingPatternTree.class, "getVariable");
1293-
1294-
@Nullable
1295-
private static final Method BINDING_PATTERN_TREE_GET_TYPE = maybeGetMethod(BindingPatternTree.class, "getType");
1296-
1297-
@Nullable
1298-
private static final Method BINDING_PATTERN_TREE_GET_BINDING =
1299-
maybeGetMethod(BindingPatternTree.class, "getBinding");
1300-
13011285
@Override
13021286
public Void visitBindingPattern(BindingPatternTree node, Void unused) {
13031287
sync(node);
1304-
if (BINDING_PATTERN_TREE_GET_VARIABLE != null) {
1305-
VariableTree variableTree = (VariableTree) invoke(BINDING_PATTERN_TREE_GET_VARIABLE, node);
1306-
visitBindingPattern(variableTree.getModifiers(), variableTree.getType(), variableTree.getName());
1307-
} else if (BINDING_PATTERN_TREE_GET_TYPE != null && BINDING_PATTERN_TREE_GET_BINDING != null) {
1308-
Tree type = (Tree) invoke(BINDING_PATTERN_TREE_GET_TYPE, node);
1309-
Name name = (Name) invoke(BINDING_PATTERN_TREE_GET_BINDING, node);
1310-
visitBindingPattern(/* modifiers= */ null, type, name);
1311-
} else {
1312-
throw new RuntimeException(
1313-
"BindingPatternTree must have either getVariable() or both getType() and getBinding(),"
1314-
+ " but does not");
1315-
}
1288+
VariableTree variableTree = node.getVariable();
1289+
visitBindingPattern(variableTree.getModifiers(), variableTree.getType(), variableTree.getName());
13161290
return null;
13171291
}
13181292

@@ -1324,11 +1298,9 @@ private static boolean isVarType(Tree type) {
13241298
return type.getKind().name().equals("VAR_TYPE");
13251299
}
13261300

1327-
private void visitBindingPattern(@Nullable ModifiersTree modifiers, @Nullable Tree type, Name name) {
1301+
private void visitBindingPattern(ModifiersTree modifiers, @Nullable Tree type, Name name) {
13281302
builder.open(plusFour);
1329-
if (modifiers != null) {
1330-
builder.addAll(visitModifiers(modifiers, Direction.HORIZONTAL, Optional.empty()));
1331-
}
1303+
builder.addAll(visitModifiers(modifiers, Direction.HORIZONTAL, Optional.empty()));
13321304
if (type == null || isVarType(type)) {
13331305
token("var");
13341306
} else {
@@ -1533,11 +1505,6 @@ public Void visitAnnotatedType(AnnotatedTypeTree node, Void unused) {
15331505
return null;
15341506
}
15351507

1536-
// TODO(fawind): Use Flags.COMPACT_RECORD_CONSTRUCTOR once if/when we drop support for Java 11
1537-
protected static final long COMPACT_RECORD_CONSTRUCTOR = 1L << 51;
1538-
1539-
protected static final long RECORD = 1L << 61;
1540-
15411508
// TODO: use Flags.IMPLICIT_CLASS once this module compiles with JDK 22 or later (JDK 21 calls
15421509
// bit 19 UNNAMED_CLASS). javac sets it on the class it synthesizes for a compact source file.
15431510
private static final long IMPLICIT_CLASS = 1L << 19;
@@ -1990,24 +1957,14 @@ boolean visitSingleMemberAnnotation(AnnotationTree node) {
19901957
return true;
19911958
}
19921959

1993-
@Nullable
1994-
private static final Method CASE_TREE_GET_LABELS = maybeGetMethod(CaseTree.class, "getLabels");
1995-
19961960
@Override
19971961
public Void visitCase(CaseTree node, Void unused) {
19981962
sync(node);
19991963
markForPartialFormat();
20001964
builder.forcedBreak();
2001-
List<? extends Tree> labels;
2002-
boolean isDefault;
2003-
if (CASE_TREE_GET_LABELS != null) {
2004-
labels = (List<? extends Tree>) invoke(CASE_TREE_GET_LABELS, node);
2005-
isDefault = labels.size() == 1
2006-
&& Iterables.getOnlyElement(labels).getKind().name().equals("DEFAULT_CASE_LABEL");
2007-
} else {
2008-
labels = node.getExpressions();
2009-
isDefault = labels.isEmpty();
2010-
}
1965+
List<? extends CaseLabelTree> labels = node.getLabels();
1966+
boolean isDefault =
1967+
labels.size() == 1 && getOnlyElement(labels).getKind().name().equals("DEFAULT_CASE_LABEL");
20111968
if (isDefault) {
20121969
token("default", ZERO);
20131970
} else {
@@ -4069,17 +4026,9 @@ private void addBodyDeclarations(
40694026
}
40704027
}
40714028

4072-
@Nullable
4073-
private static final Method CLASS_TREE_GET_PERMITS_CLAUSE = maybeGetMethod(ClassTree.class, "getPermitsClause");
4074-
4075-
/** Gets the permits clause for the given node. This is only available in Java 15 and later. */
4029+
/** Gets the permits clause for the given node, empty unless the class or interface is sealed. */
40764030
protected List<? extends Tree> getPermitsClause(ClassTree node) {
4077-
if (CLASS_TREE_GET_PERMITS_CLAUSE != null) {
4078-
return (List<? extends Tree>) invoke(CLASS_TREE_GET_PERMITS_CLAUSE, node);
4079-
} else {
4080-
// Java < 15
4081-
return List.of();
4082-
}
4031+
return node.getPermitsClause();
40834032
}
40844033

40854034
private void classDeclarationTypeList(String token, List<? extends Tree> types) {

‎open-java-format/src/main/java/com/palantir/javaformat/java/RemoveUnusedImports.java‎

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
import com.sun.tools.javac.util.Options;
4747
import java.lang.reflect.Method;
4848
import java.util.LinkedHashSet;
49-
import java.util.List;
5049
import java.util.Map;
5150
import java.util.Set;
5251
import org.jspecify.annotations.Nullable;
@@ -102,32 +101,15 @@ public Void visitIdentifier(IdentifierTree tree, Void unused) {
102101
return null;
103102
}
104103

105-
// TODO(fwindheuser): remove this override when pattern matching in switch is no longer a preview
106-
// feature, and TreePathScanner visits CaseTree#getLabels instead of CaseTree#getExpressions
104+
// The TreeScanner of JDK 17 visits CaseTree#getExpressions, which leave out a pattern label and so the type
105+
// it names; from JDK 21 it visits CaseTree#getLabels, and the labels are scanned twice, which changes nothing.
106+
// TODO: remove this override once no release line runs on a JDK older than 21.
107107
@Override
108108
public Void visitCase(CaseTree tree, Void unused) {
109-
if (CASE_TREE_GET_LABELS != null) {
110-
try {
111-
scan((List<? extends Tree>) CASE_TREE_GET_LABELS.invoke(tree), null);
112-
} catch (ReflectiveOperationException e) {
113-
throw new RuntimeException(e.getMessage(), e);
114-
}
115-
}
109+
scan(tree.getLabels(), null);
116110
return super.visitCase(tree, null);
117111
}
118112

119-
@Nullable
120-
private static final Method CASE_TREE_GET_LABELS = caseTreeGetLabels();
121-
122-
@Nullable
123-
private static Method caseTreeGetLabels() {
124-
try {
125-
return CaseTree.class.getMethod("getLabels");
126-
} catch (NoSuchMethodException e) {
127-
return null;
128-
}
129-
}
130-
131113
@Override
132114
public Void scan(@Nullable Tree tree, Void unused) {
133115
if (tree == null) {
@@ -295,7 +277,7 @@ private static boolean isBlankLineAfter(String contents, int pos, String sep) {
295277
}
296278

297279
// ImportTree#isModule() (JEP 511) exists from JDK 23 on; this module compiles with a JDK 21
298-
// compiler, so it can't be referenced directly. Same idiom as CASE_TREE_GET_LABELS above.
280+
// compiler, so it can't be referenced directly.
299281
@Nullable
300282
private static final Method IMPORT_TREE_IS_MODULE =
301283
JavaInputAstVisitor.maybeGetMethod(ImportTree.class, "isModule");

0 commit comments

Comments
 (0)