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
Original file line number Diff line number Diff line change
Expand Up @@ -1233,9 +1233,11 @@ private void checkForTypeAnnotation(ImportTree node) {
}

private static Name getSimpleName(ImportTree importTree) {
return importTree.getQualifiedIdentifier() instanceof IdentifierTree
? ((IdentifierTree) importTree.getQualifiedIdentifier()).getName()
: ((MemberSelectTree) importTree.getQualifiedIdentifier()).getIdentifier();
return switch (importTree.getQualifiedIdentifier()) {
case IdentifierTree identifierTree -> identifierTree.getName();
case MemberSelectTree memberSelectTree -> memberSelectTree.getIdentifier();
case Tree tree -> throw new AssertionError(tree);
};
}

@Override
Expand Down Expand Up @@ -1343,14 +1345,14 @@ public Void visitLambdaExpression(LambdaExpressionTree node, Void unused) {
} else {
builder.breakOp(" ");
}
if (node.getBody().getKind() == Tree.Kind.BLOCK) {
visitBlock(
(BlockTree) node.getBody(),
CollapseEmptyOrNot.YES,
AllowLeadingBlankLine.NO,
AllowTrailingBlankLine.NO);
} else {
scan(node.getBody(), null);
switch (node.getBody()) {
case BlockTree blockTree ->
visitBlock(
blockTree,
CollapseEmptyOrNot.YES,
AllowLeadingBlankLine.NO,
AllowTrailingBlankLine.NO);
case Tree expressionTree -> scan(expressionTree, null);
}
builder.close();
return null;
Expand Down Expand Up @@ -1433,20 +1435,20 @@ private void visitAnnotationArgument(AssignmentTree node) {
@Override
public Void visitAnnotatedType(AnnotatedTypeTree node, Void unused) {
sync(node);
ExpressionTree base = node.getUnderlyingType();
if (base instanceof MemberSelectTree selectTree) {

scan(selectTree.getExpression(), null);
token(".");
visitAnnotations(node.getAnnotations(), BreakOrNot.NO, BreakOrNot.NO);
builder.breakToFill(" ");
visit(selectTree.getIdentifier());
} else if (base instanceof ArrayTypeTree) {
visitAnnotatedArrayType(node);
} else {
visitAnnotations(node.getAnnotations(), BreakOrNot.NO, BreakOrNot.NO);
builder.breakToFill(" ");
scan(base, null);
switch (node.getUnderlyingType()) {
case MemberSelectTree selectTree -> {
scan(selectTree.getExpression(), null);
token(".");
visitAnnotations(node.getAnnotations(), BreakOrNot.NO, BreakOrNot.NO);
builder.breakToFill(" ");
visit(selectTree.getIdentifier());
}
case ArrayTypeTree unusedTree -> visitAnnotatedArrayType(node);
case ExpressionTree base -> {
visitAnnotations(node.getAnnotations(), BreakOrNot.NO, BreakOrNot.NO);
builder.breakToFill(" ");
scan(base, null);
}
}
return null;
}
Expand Down Expand Up @@ -1671,8 +1673,7 @@ private boolean handleLogStatement(MethodInvocationTree node) {
}
Deque<ExpressionTree> parts = new ArrayDeque<>();
ExpressionTree curr = node;
while (curr instanceof MethodInvocationTree) {
MethodInvocationTree method = (MethodInvocationTree) curr;
while (curr instanceof MethodInvocationTree method) {
parts.addFirst(method);
if (!LOG_METHODS.contains(getMethodName(method).toString())) {
return false;
Expand Down Expand Up @@ -1867,14 +1868,13 @@ private boolean ambiguousUnaryOperator(UnaryTree node, String operatorName) {
}

private JCTree.Tag unaryTag(ExpressionTree expression) {
if (expression instanceof UnaryTree) {
return ((JCTree) expression).getTag();
}
if (expression instanceof LiteralTree
&& isUnaryMinusLiteral(getSourceForNode(expression, getCurrentPath()))) {
return JCTree.Tag.MINUS;
}
return null;
return switch (expression) {
case UnaryTree unary -> ((JCTree) unary).getTag();
case LiteralTree literal
when isUnaryMinusLiteral(getSourceForNode(literal, getCurrentPath())) ->
JCTree.Tag.MINUS;
default -> null;
};
}

@Override
Expand Down Expand Up @@ -2596,10 +2596,11 @@ private void formatAnnotationOrModifier(Deque<AnnotationOrModifier> modifiers) {

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

private static boolean isModifier(String token) {
Expand Down Expand Up @@ -2897,8 +2898,9 @@ public Void visitUses(UsesTree node, Void unused) {
/** Helper method for import declarations, names, and qualified names. */
private void visitName(Tree node) {
Deque<Name> stack = new ArrayDeque<>();
for (; node instanceof MemberSelectTree; node = ((MemberSelectTree) node).getExpression()) {
stack.addFirst(((MemberSelectTree) node).getIdentifier());
while (node instanceof MemberSelectTree memberSelectTree) {
stack.addFirst(memberSelectTree.getIdentifier());
node = memberSelectTree.getExpression();
}
stack.addFirst(((IdentifierTree) node).getName());
boolean afterFirstToken = false;
Expand Down Expand Up @@ -3068,8 +3070,8 @@ private void visitDot(ExpressionTree node0) {
prefixes.add(firstInvocationIndex);
}

if (prefixes.isEmpty() && items.get(0) instanceof IdentifierTree) {
switch (((IdentifierTree) items.get(0)).getName().toString()) {
if (prefixes.isEmpty() && items.get(0) instanceof IdentifierTree identifierTree) {
switch (identifierTree.getName().toString()) {
case "this", "super" -> prefixes.add(1);
default -> {}
}
Expand Down Expand Up @@ -3269,12 +3271,12 @@ private void dotExpressionUpToArgs(ExpressionTree expression, Optional<BreakTag>
}

/**
* Returns the base expression of an erray access, e.g. given {@code foo[0][0]} returns {@code
* Returns the base expression of an array access, e.g. given {@code foo[0][0]} returns {@code
* foo}.
*/
private static ExpressionTree getArrayBase(ExpressionTree node) {
while (node instanceof ArrayAccessTree) {
node = ((ArrayAccessTree) node).getExpression();
while (node instanceof ArrayAccessTree arrayAccessTree) {
node = arrayAccessTree.getExpression();
}
return node;
}
Expand Down Expand Up @@ -3323,8 +3325,7 @@ private void formatArrayIndices(Deque<ExpressionTree> indices) {
*/
private static Deque<ExpressionTree> getArrayIndices(ExpressionTree expression) {
Deque<ExpressionTree> indices = new ArrayDeque<>();
while (expression instanceof ArrayAccessTree) {
ArrayAccessTree array = (ArrayAccessTree) expression;
while (expression instanceof ArrayAccessTree array) {
indices.addLast(array.getIndex());
expression = array.getExpression();
}
Expand Down Expand Up @@ -3461,7 +3462,7 @@ public void scan(JCTree tree) {
}
if (tree.getKind() == STRING_LITERAL) {
Object value = ((LiteralTree) tree).getValue();
if (value instanceof String && FORMAT_SPECIFIER.matcher(value.toString()).find()) {
if (value instanceof String string && FORMAT_SPECIFIER.matcher(string).find()) {
formatString[0] = true;
}
}
Expand Down Expand Up @@ -3564,10 +3565,9 @@ private static boolean expressionsAreParallel(
}
// Treat UnaryTree expressions as their underlying type for the comparison (so, for example
// -ve and +ve numeric literals are considered the same).
if (row.get(column) instanceof UnaryTree) {
nodeTypes.add(((UnaryTree) row.get(column)).getExpression().getKind());
} else {
nodeTypes.add(row.get(column).getKind());
switch (row.get(column)) {
case UnaryTree unary -> nodeTypes.add(unary.getExpression().getKind());
case ExpressionTree expression -> nodeTypes.add(expression.getKind());
}
}
for (Multiset.Entry<Tree.Kind> nodeType : nodeTypes.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,11 @@ 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
? memberSelectTree.getIdentifier()
: ((IdentifierTree) select).getName();
return switch (select) {
case MemberSelectTree memberSelect -> memberSelect.getIdentifier();
case IdentifierTree identifier -> identifier.getName();
default -> throw new AssertionError(select);
};
}

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