Skip to content

Commit bf4b13b

Browse files
cushongoogle-java-format Team
authored andcommitted
Require JDK 21 to run google-java-format
And introduce some usages of Java 21 language features to verify. The refactoring changes were generated with https://errorprone.info/bugpattern/PatternMatchingInstanceof PiperOrigin-RevId: 891702291
1 parent 7b0d3a3 commit bf4b13b

File tree

9 files changed

+44
-50
lines changed

9 files changed

+44
-50
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ java -jar /path/to/google-java-format-${GJF_VERSION?}-all-deps.jar <options> [fi
1919
Note that it uses the `jdk.compiler` module to parse the Java source code. The
2020
`java` binary version used must therefore be from a JDK (not JRE) with a version
2121
equal to or newer than the Java language version of the files being formatted.
22-
The minimum Java version can be found in `core/pom.xml` (currently Java 17). An
22+
The minimum Java version can be found in `core/pom.xml` (currently Java 21). An
2323
alternative is to use the available GraalVM based native binaries instead.
2424

2525
The formatter can act on whole files, on limited lines (`--lines`), on specific

core/pom.xml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@
9393
<plugin>
9494
<artifactId>maven-javadoc-plugin</artifactId>
9595
<configuration>
96-
<source>17</source>
9796
<encoding>UTF-8</encoding>
9897
<docencoding>UTF-8</docencoding>
9998
<charset>UTF-8</charset>
@@ -211,14 +210,6 @@
211210
</execution>
212211
</executions>
213212
</plugin>
214-
<plugin>
215-
<groupId>org.apache.maven.plugins</groupId>
216-
<artifactId>maven-compiler-plugin</artifactId>
217-
<configuration>
218-
<source>17</source>
219-
<target>17</target>
220-
</configuration>
221-
</plugin>
222213
</plugins>
223214
</build>
224215

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,8 @@ private static void splitByBreaks(List<Doc> docs, List<List<Doc>> splits, List<B
267267
breaks.clear();
268268
splits.add(new ArrayList<>());
269269
for (Doc doc : docs) {
270-
if (doc instanceof Break) {
271-
breaks.add((Break) doc);
270+
if (doc instanceof Break b) {
271+
breaks.add(b);
272272
splits.add(new ArrayList<>());
273273
} else {
274274
getLast(splits).add(doc);

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,10 @@ public Optional<Boolean> wanted() {
138138

139139
@Override
140140
public BlankLineWanted merge(BlankLineWanted other) {
141-
if (!(other instanceof ConditionalBlankLine)) {
141+
if (!(other instanceof ConditionalBlankLine conditionalBlankLine)) {
142142
return other;
143143
}
144-
return new ConditionalBlankLine(
145-
Iterables.concat(this.tags, ((ConditionalBlankLine) other).tags));
144+
return new ConditionalBlankLine(Iterables.concat(this.tags, conditionalBlankLine.tags));
146145
}
147146
}
148147
}
@@ -495,13 +494,13 @@ public final ImmutableList<Op> build() {
495494
int opsN = ops.size();
496495
for (int i = 0; i < opsN; i++) {
497496
Op op = ops.get(i);
498-
if (op instanceof Doc.Token) {
497+
if (op instanceof Doc.Token tokenOp) {
499498
/*
500499
* Token ops can have associated non-tokens, including comments, which we need to insert.
501500
* They can also cause line breaks, so we insert them before or after the current level,
502501
* when possible.
503502
*/
504-
Doc.Token tokenOp = (Doc.Token) op;
503+
505504
Input.Token token = tokenOp.getToken();
506505
int j = i; // Where to insert toksBefore before.
507506
while (0 < j && ops.get(j - 1) instanceof OpenOp) {
@@ -616,8 +615,8 @@ public final ImmutableList<Op> build() {
616615
Op op = ops.get(i);
617616
if (afterForcedBreak
618617
&& (op instanceof Doc.Space
619-
|| (op instanceof Doc.Break
620-
&& ((Doc.Break) op).getPlusIndent() == 0
618+
|| (op instanceof Doc.Break b
619+
&& b.getPlusIndent() == 0
621620
&& " ".equals(((Doc) op).getFlat())))) {
622621
continue;
623622
}
@@ -636,7 +635,7 @@ public final ImmutableList<Op> build() {
636635
}
637636

638637
private static boolean isForcedBreak(Op op) {
639-
return op instanceof Doc.Break && ((Doc.Break) op).isForced();
638+
return op instanceof Doc.Break b && b.isForced();
640639
}
641640

642641
private static List<Op> makeComment(Input.Tok comment) {

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

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,8 @@ private boolean inExpression() {
366366
public Void scan(Tree tree, Void unused) {
367367
// Pre-visit AST for preview features, since com.sun.source.tree.AnyPattern can't be
368368
// accessed directly without --enable-preview.
369-
if (tree instanceof JCTree.JCAnyPattern) {
370-
visitJcAnyPattern((JCTree.JCAnyPattern) tree);
369+
if (tree instanceof JCTree.JCAnyPattern jcAnyPattern) {
370+
visitJcAnyPattern(jcAnyPattern);
371371
return null;
372372
}
373373
inExpression.addLast(tree instanceof ExpressionTree || inExpression.peekLast());
@@ -890,8 +890,8 @@ public boolean visitEnumDeclaration(ClassTree node) {
890890
ArrayList<VariableTree> enumConstants = new ArrayList<>();
891891
ArrayList<Tree> members = new ArrayList<>();
892892
for (Tree member : node.getMembers()) {
893-
if (member instanceof JCTree.JCVariableDecl) {
894-
JCTree.JCVariableDecl variableDecl = (JCTree.JCVariableDecl) member;
893+
if (member instanceof JCTree.JCVariableDecl variableDecl) {
894+
895895
if ((variableDecl.mods.flags & Flags.ENUM) == Flags.ENUM) {
896896
enumConstants.add(variableDecl);
897897
continue;
@@ -1426,8 +1426,8 @@ public Void visitAnnotation(AnnotationTree node, Void unused) {
14261426
builder.breakOp(" ");
14271427
}
14281428
}
1429-
if (argument instanceof AssignmentTree) {
1430-
visitAnnotationArgument((AssignmentTree) argument);
1429+
if (argument instanceof AssignmentTree assignmentTree) {
1430+
visitAnnotationArgument(assignmentTree);
14311431
} else {
14321432
scan(argument, null);
14331433
}
@@ -1447,11 +1447,11 @@ public Void visitAnnotation(AnnotationTree node, Void unused) {
14471447
}
14481448

14491449
private static boolean isArrayValue(ExpressionTree argument) {
1450-
if (!(argument instanceof AssignmentTree)) {
1450+
if (!(argument instanceof AssignmentTree assignmentTree)) {
14511451
return false;
14521452
}
1453-
ExpressionTree expression = ((AssignmentTree) argument).getExpression();
1454-
return expression instanceof NewArrayTree && ((NewArrayTree) expression).getType() == null;
1453+
ExpressionTree expression = assignmentTree.getExpression();
1454+
return expression instanceof NewArrayTree newArrayTree && newArrayTree.getType() == null;
14551455
}
14561456

14571457
public void visitAnnotationArgument(AssignmentTree node) {
@@ -1474,8 +1474,8 @@ public void visitAnnotationArgument(AssignmentTree node) {
14741474
public Void visitAnnotatedType(AnnotatedTypeTree node, Void unused) {
14751475
sync(node);
14761476
ExpressionTree base = node.getUnderlyingType();
1477-
if (base instanceof MemberSelectTree) {
1478-
MemberSelectTree selectTree = (MemberSelectTree) base;
1477+
if (base instanceof MemberSelectTree selectTree) {
1478+
14791479
scan(selectTree.getExpression(), null);
14801480
token(".");
14811481
visitAnnotations(node.getAnnotations(), BreakOrNot.NO, BreakOrNot.NO);
@@ -1757,10 +1757,10 @@ private static List<Long> handleStream(List<ExpressionTree> parts) {
17571757
return indexes(
17581758
parts.stream(),
17591759
p -> {
1760-
if (!(p instanceof MethodInvocationTree)) {
1760+
if (!(p instanceof MethodInvocationTree methodInvocationTree)) {
17611761
return false;
17621762
}
1763-
Name name = getMethodName((MethodInvocationTree) p);
1763+
Name name = getMethodName(methodInvocationTree);
17641764
return Stream.of("stream", "parallelStream", "toBuilder")
17651765
.anyMatch(name::contentEquals);
17661766
})
@@ -2119,8 +2119,8 @@ public Void visitTry(TryTree node, Void unused) {
21192119
if (afterFirstToken) {
21202120
builder.forcedBreak();
21212121
}
2122-
if (resource instanceof VariableTree) {
2123-
VariableTree variableTree = (VariableTree) resource;
2122+
if (resource instanceof VariableTree variableTree) {
2123+
21242124
declareOne(
21252125
DeclarationKind.PARAMETER,
21262126
fieldAnnotationDirection(variableTree.getModifiers()),
@@ -2642,10 +2642,10 @@ private void formatAnnotationOrModifier(Deque<AnnotationOrModifier> modifiers) {
26422642

26432643
boolean isTypeAnnotation(AnnotationTree annotationTree) {
26442644
Tree annotationType = annotationTree.getAnnotationType();
2645-
if (!(annotationType instanceof IdentifierTree)) {
2645+
if (!(annotationType instanceof IdentifierTree identifierTree)) {
26462646
return false;
26472647
}
2648-
return typeAnnotationSimpleNames.contains(((IdentifierTree) annotationType).getName());
2648+
return typeAnnotationSimpleNames.contains(identifierTree.getName());
26492649
}
26502650

26512651
private static boolean isModifier(String token) {
@@ -2748,8 +2748,8 @@ private static void walkInfix(
27482748
ExpressionTree expression,
27492749
List<ExpressionTree> operands,
27502750
List<String> operators) {
2751-
if (expression instanceof BinaryTree) {
2752-
BinaryTree binaryTree = (BinaryTree) expression;
2751+
if (expression instanceof BinaryTree binaryTree) {
2752+
27532753
if (precedence(binaryTree) == precedence) {
27542754
walkInfix(precedence, binaryTree.getLeftOperand(), operands, operators);
27552755
operators.add(operatorName(expression));
@@ -3325,9 +3325,11 @@ private static ExpressionTree getArrayBase(ExpressionTree node) {
33253325
return node;
33263326
}
33273327

3328-
private static ExpressionTree getMethodReceiver(MethodInvocationTree methodInvocation) {
3328+
private static @Nullable ExpressionTree getMethodReceiver(MethodInvocationTree methodInvocation) {
33293329
ExpressionTree select = methodInvocation.getMethodSelect();
3330-
return select instanceof MemberSelectTree ? ((MemberSelectTree) select).getExpression() : null;
3330+
return select instanceof MemberSelectTree memberSelectTree
3331+
? memberSelectTree.getExpression()
3332+
: null;
33313333
}
33323334

33333335
private void dotExpressionArgsAndParen(

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ public boolean equals(Object o) {
5757
if (o == this) {
5858
return true;
5959
}
60-
if (o instanceof Replacement) {
61-
Replacement that = (Replacement) o;
60+
if (o instanceof Replacement that) {
61+
6262
return replaceRange.equals(that.getReplaceRange())
6363
&& replacementString.equals(that.getReplacementString());
6464
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ public Void visitLiteral(LiteralTree literalTree, Void aVoid) {
155155
return null;
156156
}
157157
Tree parent = getCurrentPath().getParentPath().getLeaf();
158-
if (parent instanceof MemberSelectTree
159-
&& ((MemberSelectTree) parent).getExpression().equals(literalTree)) {
158+
if (parent instanceof MemberSelectTree memberSelectTree
159+
&& memberSelectTree.getExpression().equals(literalTree)) {
160160
return null;
161161
}
162162
int endPosition = getEndPosition(literalTree, unit);

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,17 @@ static String getSourceForNode(Tree node, TreePath path) {
9595
/** Returns the simple name of a (possibly qualified) method invocation expression. */
9696
static Name getMethodName(MethodInvocationTree methodInvocation) {
9797
ExpressionTree select = methodInvocation.getMethodSelect();
98-
return select instanceof MemberSelectTree
99-
? ((MemberSelectTree) select).getIdentifier()
98+
return select instanceof MemberSelectTree memberSelectTree
99+
? memberSelectTree.getIdentifier()
100100
: ((IdentifierTree) select).getName();
101101
}
102102

103103
/** Returns the receiver of a qualified method invocation expression, or {@code null}. */
104-
static ExpressionTree getMethodReceiver(MethodInvocationTree methodInvocation) {
104+
static @Nullable ExpressionTree getMethodReceiver(MethodInvocationTree methodInvocation) {
105105
ExpressionTree select = methodInvocation.getMethodSelect();
106-
return select instanceof MemberSelectTree ? ((MemberSelectTree) select).getExpression() : null;
106+
return select instanceof MemberSelectTree memberSelectTree
107+
? memberSelectTree.getExpression()
108+
: null;
107109
}
108110

109111
/** Returns the string name of an operator, including assignment and compound assignment. */

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585

8686
<properties>
8787
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
88-
<java.version>1.8</java.version>
88+
<java.version>21</java.version>
8989
<guava.version>32.1.3-jre</guava.version>
9090
<truth.version>1.4.0</truth.version>
9191
<jspecify.version>1.0.0</jspecify.version>

0 commit comments

Comments
 (0)