Skip to content

Commit c1ea933

Browse files
cushongoogle-java-format Team
authored and
google-java-format Team
committed
Stop supporting JDK 14 in google-java-format, and update to remove uses of reflection for APIs that can be accessed directly in JDK 17
PiperOrigin-RevId: 484376678
1 parent 198cc39 commit c1ea933

File tree

3 files changed

+19
-73
lines changed

3 files changed

+19
-73
lines changed

core/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@
227227
<profile>
228228
<id>jdk11</id>
229229
<activation>
230-
<jdk>(,14)</jdk>
230+
<jdk>(,17)</jdk>
231231
</activation>
232232
<build>
233233
<plugins>
@@ -236,14 +236,14 @@
236236
<artifactId>maven-compiler-plugin</artifactId>
237237
<configuration>
238238
<excludes>
239-
<exclude>**/Java14InputAstVisitor.java</exclude>
239+
<exclude>**/Java17InputAstVisitor.java</exclude>
240240
</excludes>
241241
</configuration>
242242
</plugin>
243243
<plugin>
244244
<artifactId>maven-javadoc-plugin</artifactId>
245245
<configuration>
246-
<excludePackageNames>com.google.googlejavaformat.java.java14</excludePackageNames>
246+
<excludePackageNames>com.google.googlejavaformat.java.java17</excludePackageNames>
247247
</configuration>
248248
</plugin>
249249
</plugins>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,10 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept
151151
OpsBuilder builder = new OpsBuilder(javaInput, javaOutput);
152152
// Output the compilation unit.
153153
JavaInputAstVisitor visitor;
154-
if (Runtime.version().feature() >= 14) {
154+
if (Runtime.version().feature() >= 17) {
155155
try {
156156
visitor =
157-
Class.forName("com.google.googlejavaformat.java.java14.Java14InputAstVisitor")
157+
Class.forName("com.google.googlejavaformat.java.java17.Java17InputAstVisitor")
158158
.asSubclass(JavaInputAstVisitor.class)
159159
.getConstructor(OpsBuilder.class, int.class)
160160
.newInstance(builder, options.indentationMultiplier());

core/src/main/java/com/google/googlejavaformat/java/java14/Java14InputAstVisitor.java renamed to core/src/main/java/com/google/googlejavaformat/java/java17/Java17InputAstVisitor.java

Lines changed: 14 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* the License.
1313
*/
1414

15-
package com.google.googlejavaformat.java.java14;
15+
package com.google.googlejavaformat.java.java17;
1616

1717
import static com.google.common.collect.ImmutableList.toImmutableList;
1818
import static com.google.common.collect.Iterables.getOnlyElement;
@@ -25,6 +25,7 @@
2525
import com.sun.source.tree.AnnotationTree;
2626
import com.sun.source.tree.BindingPatternTree;
2727
import com.sun.source.tree.BlockTree;
28+
import com.sun.source.tree.CaseLabelTree;
2829
import com.sun.source.tree.CaseTree;
2930
import com.sun.source.tree.ClassTree;
3031
import com.sun.source.tree.CompilationUnitTree;
@@ -39,39 +40,23 @@
3940
import com.sun.tools.javac.tree.JCTree;
4041
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
4142
import com.sun.tools.javac.tree.TreeInfo;
42-
import java.lang.reflect.Method;
4343
import java.util.List;
4444
import java.util.Optional;
4545
import javax.lang.model.element.Name;
4646

4747
/**
48-
* Extends {@link JavaInputAstVisitor} with support for AST nodes that were added or modified for
49-
* Java 14.
48+
* Extends {@link JavaInputAstVisitor} with support for AST nodes that were added or modified in
49+
* Java 17.
5050
*/
51-
public class Java14InputAstVisitor extends JavaInputAstVisitor {
52-
private static final Method COMPILATION_UNIT_TREE_GET_MODULE =
53-
maybeGetMethod(CompilationUnitTree.class, "getModule");
54-
private static final Method CLASS_TREE_GET_PERMITS_CLAUSE =
55-
maybeGetMethod(ClassTree.class, "getPermitsClause");
56-
private static final Method BINDING_PATTERN_TREE_GET_VARIABLE =
57-
maybeGetMethod(BindingPatternTree.class, "getVariable");
58-
private static final Method BINDING_PATTERN_TREE_GET_TYPE =
59-
maybeGetMethod(BindingPatternTree.class, "getType");
60-
private static final Method BINDING_PATTERN_TREE_GET_BINDING =
61-
maybeGetMethod(BindingPatternTree.class, "getBinding");
62-
private static final Method CASE_TREE_GET_LABELS = maybeGetMethod(CaseTree.class, "getLabels");
51+
public class Java17InputAstVisitor extends JavaInputAstVisitor {
6352

64-
public Java14InputAstVisitor(OpsBuilder builder, int indentMultiplier) {
53+
public Java17InputAstVisitor(OpsBuilder builder, int indentMultiplier) {
6554
super(builder, indentMultiplier);
6655
}
6756

6857
@Override
6958
protected void handleModule(boolean first, CompilationUnitTree node) {
70-
if (COMPILATION_UNIT_TREE_GET_MODULE == null) {
71-
// Java < 17, see https://bugs.openjdk.java.net/browse/JDK-8255464
72-
return;
73-
}
74-
ModuleTree module = (ModuleTree) invoke(COMPILATION_UNIT_TREE_GET_MODULE, node);
59+
ModuleTree module = node.getModule();
7560
if (module != null) {
7661
if (!first) {
7762
builder.blankLineWanted(BlankLineWanted.YES);
@@ -84,30 +69,15 @@ protected void handleModule(boolean first, CompilationUnitTree node) {
8469

8570
@Override
8671
protected List<? extends Tree> getPermitsClause(ClassTree node) {
87-
if (CLASS_TREE_GET_PERMITS_CLAUSE != null) {
88-
return (List<? extends Tree>) invoke(CLASS_TREE_GET_PERMITS_CLAUSE, node);
89-
} else {
90-
// Java < 15
91-
return super.getPermitsClause(node);
92-
}
72+
return node.getPermitsClause();
9373
}
9474

9575
@Override
9676
public Void visitBindingPattern(BindingPatternTree node, Void unused) {
9777
sync(node);
98-
if (BINDING_PATTERN_TREE_GET_VARIABLE != null) {
99-
VariableTree variableTree = (VariableTree) invoke(BINDING_PATTERN_TREE_GET_VARIABLE, node);
100-
visitBindingPattern(
101-
variableTree.getModifiers(), variableTree.getType(), variableTree.getName());
102-
} else if (BINDING_PATTERN_TREE_GET_TYPE != null && BINDING_PATTERN_TREE_GET_BINDING != null) {
103-
Tree type = (Tree) invoke(BINDING_PATTERN_TREE_GET_TYPE, node);
104-
Name name = (Name) invoke(BINDING_PATTERN_TREE_GET_BINDING, node);
105-
visitBindingPattern(/* modifiers= */ null, type, name);
106-
} else {
107-
throw new LinkageError(
108-
"BindingPatternTree must have either getVariable() or both getType() and getBinding(),"
109-
+ " but does not");
110-
}
78+
VariableTree variableTree = node.getVariable();
79+
visitBindingPattern(
80+
variableTree.getModifiers(), variableTree.getType(), variableTree.getName());
11181
return null;
11282
}
11383

@@ -248,17 +218,9 @@ public Void visitCase(CaseTree node, Void unused) {
248218
sync(node);
249219
markForPartialFormat();
250220
builder.forcedBreak();
251-
List<? extends Tree> labels;
252-
boolean isDefault;
253-
if (CASE_TREE_GET_LABELS != null) {
254-
labels = (List<? extends Tree>) invoke(CASE_TREE_GET_LABELS, node);
255-
isDefault =
256-
labels.size() == 1
257-
&& getOnlyElement(labels).getKind().name().equals("DEFAULT_CASE_LABEL");
258-
} else {
259-
labels = node.getExpressions();
260-
isDefault = labels.isEmpty();
261-
}
221+
List<? extends CaseLabelTree> labels = node.getLabels();
222+
boolean isDefault =
223+
labels.size() == 1 && getOnlyElement(labels).getKind().name().equals("DEFAULT_CASE_LABEL");
262224
if (isDefault) {
263225
token("default", plusTwo);
264226
} else {
@@ -305,20 +267,4 @@ public Void visitCase(CaseTree node, Void unused) {
305267
}
306268
return null;
307269
}
308-
309-
private static Method maybeGetMethod(Class<?> c, String name) {
310-
try {
311-
return c.getMethod(name);
312-
} catch (ReflectiveOperationException e) {
313-
return null;
314-
}
315-
}
316-
317-
private static Object invoke(Method m, Object target) {
318-
try {
319-
return m.invoke(target);
320-
} catch (ReflectiveOperationException e) {
321-
throw new LinkageError(e.getMessage(), e);
322-
}
323-
}
324270
}

0 commit comments

Comments
 (0)