Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ java -jar /path/to/google-java-format-${GJF_VERSION?}-all-deps.jar <options> [fi
Note that it uses the `jdk.compiler` module to parse the Java source code. The
`java` binary version used must therefore be from a JDK (not JRE) with a version
equal to or newer than the Java language version of the files being formatted.
The minimum Java version can be found in `core/pom.xml` (currently Java 17). An
The minimum Java version can be found in `core/pom.xml` (currently Java 21). An
alternative is to use the available GraalVM based native binaries instead.

The formatter can act on whole files, on limited lines (`--lines`), on specific
Expand Down
9 changes: 0 additions & 9 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<source>17</source>
<encoding>UTF-8</encoding>
<docencoding>UTF-8</docencoding>
<charset>UTF-8</charset>
Expand Down Expand Up @@ -211,14 +210,6 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/com/google/googlejavaformat/Doc.java
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ private static void splitByBreaks(List<Doc> docs, List<List<Doc>> splits, List<B
breaks.clear();
splits.add(new ArrayList<>());
for (Doc doc : docs) {
if (doc instanceof Break) {
breaks.add((Break) doc);
if (doc instanceof Break b) {
breaks.add(b);
splits.add(new ArrayList<>());
} else {
getLast(splits).add(doc);
Expand Down
15 changes: 7 additions & 8 deletions core/src/main/java/com/google/googlejavaformat/OpsBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,10 @@ public Optional<Boolean> wanted() {

@Override
public BlankLineWanted merge(BlankLineWanted other) {
if (!(other instanceof ConditionalBlankLine)) {
if (!(other instanceof ConditionalBlankLine conditionalBlankLine)) {
return other;
}
return new ConditionalBlankLine(
Iterables.concat(this.tags, ((ConditionalBlankLine) other).tags));
return new ConditionalBlankLine(Iterables.concat(this.tags, conditionalBlankLine.tags));
}
}
}
Expand Down Expand Up @@ -495,13 +494,13 @@ public final ImmutableList<Op> build() {
int opsN = ops.size();
for (int i = 0; i < opsN; i++) {
Op op = ops.get(i);
if (op instanceof Doc.Token) {
if (op instanceof Doc.Token tokenOp) {
/*
* Token ops can have associated non-tokens, including comments, which we need to insert.
* They can also cause line breaks, so we insert them before or after the current level,
* when possible.
*/
Doc.Token tokenOp = (Doc.Token) op;

Input.Token token = tokenOp.getToken();
int j = i; // Where to insert toksBefore before.
while (0 < j && ops.get(j - 1) instanceof OpenOp) {
Expand Down Expand Up @@ -616,8 +615,8 @@ public final ImmutableList<Op> build() {
Op op = ops.get(i);
if (afterForcedBreak
&& (op instanceof Doc.Space
|| (op instanceof Doc.Break
&& ((Doc.Break) op).getPlusIndent() == 0
|| (op instanceof Doc.Break b
&& b.getPlusIndent() == 0
&& " ".equals(((Doc) op).getFlat())))) {
continue;
}
Expand All @@ -636,7 +635,7 @@ public final ImmutableList<Op> build() {
}

private static boolean isForcedBreak(Op op) {
return op instanceof Doc.Break && ((Doc.Break) op).isForced();
return op instanceof Doc.Break b && b.isForced();
}

private static List<Op> makeComment(Input.Tok comment) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,8 @@ private boolean inExpression() {
public Void scan(Tree tree, Void unused) {
// Pre-visit AST for preview features, since com.sun.source.tree.AnyPattern can't be
// accessed directly without --enable-preview.
if (tree instanceof JCTree.JCAnyPattern) {
visitJcAnyPattern((JCTree.JCAnyPattern) tree);
if (tree instanceof JCTree.JCAnyPattern jcAnyPattern) {
visitJcAnyPattern(jcAnyPattern);
return null;
}
inExpression.addLast(tree instanceof ExpressionTree || inExpression.peekLast());
Expand Down Expand Up @@ -890,8 +890,8 @@ public boolean visitEnumDeclaration(ClassTree node) {
ArrayList<VariableTree> enumConstants = new ArrayList<>();
ArrayList<Tree> members = new ArrayList<>();
for (Tree member : node.getMembers()) {
if (member instanceof JCTree.JCVariableDecl) {
JCTree.JCVariableDecl variableDecl = (JCTree.JCVariableDecl) member;
if (member instanceof JCTree.JCVariableDecl variableDecl) {

if ((variableDecl.mods.flags & Flags.ENUM) == Flags.ENUM) {
enumConstants.add(variableDecl);
continue;
Expand Down Expand Up @@ -1426,8 +1426,8 @@ public Void visitAnnotation(AnnotationTree node, Void unused) {
builder.breakOp(" ");
}
}
if (argument instanceof AssignmentTree) {
visitAnnotationArgument((AssignmentTree) argument);
if (argument instanceof AssignmentTree assignmentTree) {
visitAnnotationArgument(assignmentTree);
} else {
scan(argument, null);
}
Expand All @@ -1447,11 +1447,11 @@ public Void visitAnnotation(AnnotationTree node, Void unused) {
}

private static boolean isArrayValue(ExpressionTree argument) {
if (!(argument instanceof AssignmentTree)) {
if (!(argument instanceof AssignmentTree assignmentTree)) {
return false;
}
ExpressionTree expression = ((AssignmentTree) argument).getExpression();
return expression instanceof NewArrayTree && ((NewArrayTree) expression).getType() == null;
ExpressionTree expression = assignmentTree.getExpression();
return expression instanceof NewArrayTree newArrayTree && newArrayTree.getType() == null;
}

public void visitAnnotationArgument(AssignmentTree node) {
Expand All @@ -1474,8 +1474,8 @@ public void visitAnnotationArgument(AssignmentTree node) {
public Void visitAnnotatedType(AnnotatedTypeTree node, Void unused) {
sync(node);
ExpressionTree base = node.getUnderlyingType();
if (base instanceof MemberSelectTree) {
MemberSelectTree selectTree = (MemberSelectTree) base;
if (base instanceof MemberSelectTree selectTree) {

scan(selectTree.getExpression(), null);
token(".");
visitAnnotations(node.getAnnotations(), BreakOrNot.NO, BreakOrNot.NO);
Expand Down Expand Up @@ -1757,10 +1757,10 @@ private static List<Long> handleStream(List<ExpressionTree> parts) {
return indexes(
parts.stream(),
p -> {
if (!(p instanceof MethodInvocationTree)) {
if (!(p instanceof MethodInvocationTree methodInvocationTree)) {
return false;
}
Name name = getMethodName((MethodInvocationTree) p);
Name name = getMethodName(methodInvocationTree);
return Stream.of("stream", "parallelStream", "toBuilder")
.anyMatch(name::contentEquals);
})
Expand Down Expand Up @@ -2119,8 +2119,8 @@ public Void visitTry(TryTree node, Void unused) {
if (afterFirstToken) {
builder.forcedBreak();
}
if (resource instanceof VariableTree) {
VariableTree variableTree = (VariableTree) resource;
if (resource instanceof VariableTree variableTree) {

declareOne(
DeclarationKind.PARAMETER,
fieldAnnotationDirection(variableTree.getModifiers()),
Expand Down Expand Up @@ -2642,10 +2642,10 @@ private void formatAnnotationOrModifier(Deque<AnnotationOrModifier> modifiers) {

boolean isTypeAnnotation(AnnotationTree annotationTree) {
Tree annotationType = annotationTree.getAnnotationType();
if (!(annotationType instanceof IdentifierTree)) {
if (!(annotationType instanceof IdentifierTree identifierTree)) {
return false;
}
return typeAnnotationSimpleNames.contains(((IdentifierTree) annotationType).getName());
return typeAnnotationSimpleNames.contains(identifierTree.getName());
}

private static boolean isModifier(String token) {
Expand Down Expand Up @@ -2748,8 +2748,8 @@ private static void walkInfix(
ExpressionTree expression,
List<ExpressionTree> operands,
List<String> operators) {
if (expression instanceof BinaryTree) {
BinaryTree binaryTree = (BinaryTree) expression;
if (expression instanceof BinaryTree binaryTree) {

if (precedence(binaryTree) == precedence) {
walkInfix(precedence, binaryTree.getLeftOperand(), operands, operators);
operators.add(operatorName(expression));
Expand Down Expand Up @@ -3325,9 +3325,11 @@ private static ExpressionTree getArrayBase(ExpressionTree node) {
return node;
}

private static ExpressionTree getMethodReceiver(MethodInvocationTree methodInvocation) {
private static @Nullable ExpressionTree getMethodReceiver(MethodInvocationTree methodInvocation) {
ExpressionTree select = methodInvocation.getMethodSelect();
return select instanceof MemberSelectTree ? ((MemberSelectTree) select).getExpression() : null;
return select instanceof MemberSelectTree memberSelectTree
? memberSelectTree.getExpression()
: null;
}

private void dotExpressionArgsAndParen(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public boolean equals(Object o) {
if (o == this) {
return true;
}
if (o instanceof Replacement) {
Replacement that = (Replacement) o;
if (o instanceof Replacement that) {

return replaceRange.equals(that.getReplaceRange())
&& replacementString.equals(that.getReplacementString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ public Void visitLiteral(LiteralTree literalTree, Void aVoid) {
return null;
}
Tree parent = getCurrentPath().getParentPath().getLeaf();
if (parent instanceof MemberSelectTree
&& ((MemberSelectTree) parent).getExpression().equals(literalTree)) {
if (parent instanceof MemberSelectTree memberSelectTree
&& memberSelectTree.getExpression().equals(literalTree)) {
return null;
}
int endPosition = getEndPosition(literalTree, unit);
Expand Down
10 changes: 6 additions & 4 deletions core/src/main/java/com/google/googlejavaformat/java/Trees.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,17 @@ static String getSourceForNode(Tree node, TreePath path) {
/** Returns the simple name of a (possibly qualified) method invocation expression. */
static Name getMethodName(MethodInvocationTree methodInvocation) {
ExpressionTree select = methodInvocation.getMethodSelect();
return select instanceof MemberSelectTree
? ((MemberSelectTree) select).getIdentifier()
return select instanceof MemberSelectTree memberSelectTree
? memberSelectTree.getIdentifier()
: ((IdentifierTree) select).getName();
}

/** Returns the receiver of a qualified method invocation expression, or {@code null}. */
static ExpressionTree getMethodReceiver(MethodInvocationTree methodInvocation) {
static @Nullable ExpressionTree getMethodReceiver(MethodInvocationTree methodInvocation) {
ExpressionTree select = methodInvocation.getMethodSelect();
return select instanceof MemberSelectTree ? ((MemberSelectTree) select).getExpression() : null;
return select instanceof MemberSelectTree memberSelectTree
? memberSelectTree.getExpression()
: null;
}

/** Returns the string name of an operator, including assignment and compound assignment. */
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<java.version>21</java.version>
<guava.version>32.1.3-jre</guava.version>
<truth.version>1.4.0</truth.version>
<jspecify.version>1.0.0</jspecify.version>
Expand Down
Loading