Skip to content

Commit 8e17a2f

Browse files
eamonnmcmanusgoogle-java-format Team
authored andcommitted
Use type patterns more widely.
No functional change is intended. PiperOrigin-RevId: 890952666
1 parent c4dedd5 commit 8e17a2f

File tree

2 files changed

+58
-56
lines changed

2 files changed

+58
-56
lines changed

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

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,9 +1273,11 @@ private void checkForTypeAnnotation(ImportTree node) {
12731273
}
12741274

12751275
private static Name getSimpleName(ImportTree importTree) {
1276-
return importTree.getQualifiedIdentifier() instanceof IdentifierTree
1277-
? ((IdentifierTree) importTree.getQualifiedIdentifier()).getName()
1278-
: ((MemberSelectTree) importTree.getQualifiedIdentifier()).getIdentifier();
1276+
return switch (importTree.getQualifiedIdentifier()) {
1277+
case IdentifierTree identifierTree -> identifierTree.getName();
1278+
case MemberSelectTree memberSelectTree -> memberSelectTree.getIdentifier();
1279+
case Tree tree -> throw new AssertionError(tree);
1280+
};
12791281
}
12801282

12811283
@Override
@@ -1383,14 +1385,14 @@ public Void visitLambdaExpression(LambdaExpressionTree node, Void unused) {
13831385
} else {
13841386
builder.breakOp(" ");
13851387
}
1386-
if (node.getBody().getKind() == Tree.Kind.BLOCK) {
1387-
visitBlock(
1388-
(BlockTree) node.getBody(),
1389-
CollapseEmptyOrNot.YES,
1390-
AllowLeadingBlankLine.NO,
1391-
AllowTrailingBlankLine.NO);
1392-
} else {
1393-
scan(node.getBody(), null);
1388+
switch (node.getBody()) {
1389+
case BlockTree blockTree ->
1390+
visitBlock(
1391+
blockTree,
1392+
CollapseEmptyOrNot.YES,
1393+
AllowLeadingBlankLine.NO,
1394+
AllowTrailingBlankLine.NO);
1395+
case Tree expressionTree -> scan(expressionTree, null);
13941396
}
13951397
builder.close();
13961398
return null;
@@ -1473,20 +1475,20 @@ public void visitAnnotationArgument(AssignmentTree node) {
14731475
@Override
14741476
public Void visitAnnotatedType(AnnotatedTypeTree node, Void unused) {
14751477
sync(node);
1476-
ExpressionTree base = node.getUnderlyingType();
1477-
if (base instanceof MemberSelectTree selectTree) {
1478-
1479-
scan(selectTree.getExpression(), null);
1480-
token(".");
1481-
visitAnnotations(node.getAnnotations(), BreakOrNot.NO, BreakOrNot.NO);
1482-
builder.breakToFill(" ");
1483-
visit(selectTree.getIdentifier());
1484-
} else if (base instanceof ArrayTypeTree) {
1485-
visitAnnotatedArrayType(node);
1486-
} else {
1487-
visitAnnotations(node.getAnnotations(), BreakOrNot.NO, BreakOrNot.NO);
1488-
builder.breakToFill(" ");
1489-
scan(base, null);
1478+
switch (node.getUnderlyingType()) {
1479+
case MemberSelectTree selectTree -> {
1480+
scan(selectTree.getExpression(), null);
1481+
token(".");
1482+
visitAnnotations(node.getAnnotations(), BreakOrNot.NO, BreakOrNot.NO);
1483+
builder.breakToFill(" ");
1484+
visit(selectTree.getIdentifier());
1485+
}
1486+
case ArrayTypeTree arrayTypeTree -> visitAnnotatedArrayType(node);
1487+
case ExpressionTree base -> {
1488+
visitAnnotations(node.getAnnotations(), BreakOrNot.NO, BreakOrNot.NO);
1489+
builder.breakToFill(" ");
1490+
scan(base, null);
1491+
}
14901492
}
14911493
return null;
14921494
}
@@ -1717,8 +1719,7 @@ private boolean handleLogStatement(MethodInvocationTree node) {
17171719
}
17181720
Deque<ExpressionTree> parts = new ArrayDeque<>();
17191721
ExpressionTree curr = node;
1720-
while (curr instanceof MethodInvocationTree) {
1721-
MethodInvocationTree method = (MethodInvocationTree) curr;
1722+
while (curr instanceof MethodInvocationTree method) {
17221723
parts.addFirst(method);
17231724
if (!LOG_METHODS.contains(getMethodName(method).toString())) {
17241725
return false;
@@ -1913,14 +1914,13 @@ private boolean ambiguousUnaryOperator(UnaryTree node, String operatorName) {
19131914
}
19141915

19151916
private JCTree.Tag unaryTag(ExpressionTree expression) {
1916-
if (expression instanceof UnaryTree) {
1917-
return ((JCTree) expression).getTag();
1918-
}
1919-
if (expression instanceof LiteralTree
1920-
&& isUnaryMinusLiteral(getSourceForNode(expression, getCurrentPath()))) {
1921-
return JCTree.Tag.MINUS;
1922-
}
1923-
return null;
1917+
return switch (expression) {
1918+
case UnaryTree unary -> ((JCTree) expression).getTag();
1919+
case LiteralTree literal
1920+
when isUnaryMinusLiteral(getSourceForNode(literal, getCurrentPath())) ->
1921+
JCTree.Tag.MINUS;
1922+
default -> null;
1923+
};
19241924
}
19251925

19261926
@Override
@@ -2642,10 +2642,11 @@ private void formatAnnotationOrModifier(Deque<AnnotationOrModifier> modifiers) {
26422642

26432643
boolean isTypeAnnotation(AnnotationTree annotationTree) {
26442644
Tree annotationType = annotationTree.getAnnotationType();
2645-
if (!(annotationType instanceof IdentifierTree identifierTree)) {
2646-
return false;
2647-
}
2648-
return typeAnnotationSimpleNames.contains(identifierTree.getName());
2645+
return switch (annotationType) {
2646+
case IdentifierTree identifierTree ->
2647+
typeAnnotationSimpleNames.contains(identifierTree.getName());
2648+
default -> false;
2649+
};
26492650
}
26502651

26512652
private static boolean isModifier(String token) {
@@ -2943,8 +2944,9 @@ public Void visitUses(UsesTree node, Void unused) {
29432944
/** Helper method for import declarations, names, and qualified names. */
29442945
private void visitName(Tree node) {
29452946
Deque<Name> stack = new ArrayDeque<>();
2946-
for (; node instanceof MemberSelectTree; node = ((MemberSelectTree) node).getExpression()) {
2947-
stack.addFirst(((MemberSelectTree) node).getIdentifier());
2947+
while (node instanceof MemberSelectTree memberSelectTree) {
2948+
stack.addFirst(memberSelectTree.getIdentifier());
2949+
node = memberSelectTree.getExpression();
29482950
}
29492951
stack.addFirst(((IdentifierTree) node).getName());
29502952
boolean afterFirstToken = false;
@@ -3114,8 +3116,8 @@ void visitDot(ExpressionTree node0) {
31143116
prefixes.add(firstInvocationIndex);
31153117
}
31163118

3117-
if (prefixes.isEmpty() && items.get(0) instanceof IdentifierTree) {
3118-
switch (((IdentifierTree) items.get(0)).getName().toString()) {
3119+
if (prefixes.isEmpty() && items.get(0) instanceof IdentifierTree identifierTree) {
3120+
switch (identifierTree.getName().toString()) {
31193121
case "this", "super" -> prefixes.add(1);
31203122
default -> {}
31213123
}
@@ -3315,12 +3317,12 @@ private void dotExpressionUpToArgs(ExpressionTree expression, Optional<BreakTag>
33153317
}
33163318

33173319
/**
3318-
* Returns the base expression of an erray access, e.g. given {@code foo[0][0]} returns {@code
3320+
* Returns the base expression of an array access, e.g. given {@code foo[0][0]} returns {@code
33193321
* foo}.
33203322
*/
33213323
private static ExpressionTree getArrayBase(ExpressionTree node) {
3322-
while (node instanceof ArrayAccessTree) {
3323-
node = ((ArrayAccessTree) node).getExpression();
3324+
while (node instanceof ArrayAccessTree arrayAccessTree) {
3325+
node = arrayAccessTree.getExpression();
33243326
}
33253327
return node;
33263328
}
@@ -3369,8 +3371,7 @@ private void formatArrayIndices(Deque<ExpressionTree> indices) {
33693371
*/
33703372
private static Deque<ExpressionTree> getArrayIndices(ExpressionTree expression) {
33713373
Deque<ExpressionTree> indices = new ArrayDeque<>();
3372-
while (expression instanceof ArrayAccessTree) {
3373-
ArrayAccessTree array = (ArrayAccessTree) expression;
3374+
while (expression instanceof ArrayAccessTree array) {
33743375
indices.addLast(array.getIndex());
33753376
expression = array.getExpression();
33763377
}
@@ -3507,7 +3508,7 @@ public void scan(JCTree tree) {
35073508
}
35083509
if (tree.getKind() == STRING_LITERAL) {
35093510
Object value = ((LiteralTree) tree).getValue();
3510-
if (value instanceof String && FORMAT_SPECIFIER.matcher(value.toString()).find()) {
3511+
if (value instanceof String string && FORMAT_SPECIFIER.matcher(string).find()) {
35113512
formatString[0] = true;
35123513
}
35133514
}
@@ -3610,10 +3611,9 @@ private static boolean expressionsAreParallel(
36103611
}
36113612
// Treat UnaryTree expressions as their underlying type for the comparison (so, for example
36123613
// -ve and +ve numeric literals are considered the same).
3613-
if (row.get(column) instanceof UnaryTree) {
3614-
nodeTypes.add(((UnaryTree) row.get(column)).getExpression().getKind());
3615-
} else {
3616-
nodeTypes.add(row.get(column).getKind());
3614+
switch (row.get(column)) {
3615+
case UnaryTree unary -> nodeTypes.add(unary.getExpression().getKind());
3616+
case ExpressionTree expression -> nodeTypes.add(expression.getKind());
36173617
}
36183618
}
36193619
for (Multiset.Entry<Tree.Kind> nodeType : nodeTypes.entrySet()) {

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,11 @@ 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 memberSelectTree
99-
? memberSelectTree.getIdentifier()
100-
: ((IdentifierTree) select).getName();
98+
return switch (select) {
99+
case MemberSelectTree memberSelect -> memberSelect.getIdentifier();
100+
case IdentifierTree identifier -> identifier.getName();
101+
default -> throw new AssertionError(select);
102+
};
101103
}
102104

103105
/** Returns the receiver of a qualified method invocation expression, or {@code null}. */

0 commit comments

Comments
 (0)