|
30 | 30 | import static com.palantir.javaformat.java.Trees.precedence; |
31 | 31 | import static com.palantir.javaformat.java.Trees.skipParen; |
32 | 32 | import static com.sun.source.tree.Tree.Kind.ANNOTATION; |
33 | | -import static com.sun.source.tree.Tree.Kind.ARRAY_ACCESS; |
34 | | -import static com.sun.source.tree.Tree.Kind.ASSIGNMENT; |
35 | 33 | import static com.sun.source.tree.Tree.Kind.BLOCK; |
36 | 34 | import static com.sun.source.tree.Tree.Kind.EXTENDS_WILDCARD; |
37 | | -import static com.sun.source.tree.Tree.Kind.IF; |
38 | 35 | import static com.sun.source.tree.Tree.Kind.METHOD_INVOCATION; |
39 | | -import static com.sun.source.tree.Tree.Kind.NEW_ARRAY; |
40 | | -import static com.sun.source.tree.Tree.Kind.NEW_CLASS; |
41 | 36 | import static com.sun.source.tree.Tree.Kind.STRING_LITERAL; |
42 | | -import static com.sun.source.tree.Tree.Kind.UNION_TYPE; |
43 | | -import static com.sun.source.tree.Tree.Kind.VARIABLE; |
44 | 37 | import static java.util.stream.Collectors.toList; |
45 | 38 |
|
46 | 39 | import com.google.common.base.MoreObjects; |
@@ -370,7 +363,7 @@ public Void visitCompilationUnit(CompilationUnitTree node, Void unused) { |
370 | 363 | } |
371 | 364 | dropEmptyDeclarations(); |
372 | 365 | for (Tree type : node.getTypeDecls()) { |
373 | | - if (type.getKind() == Tree.Kind.IMPORT) { |
| 366 | + if (type instanceof ImportTree) { |
374 | 367 | // javac treats extra semicolons in the import list as type declarations |
375 | 368 | // TODO(cushon): remove this if https://bugs.openjdk.java.net/browse/JDK-8027682 is fixed |
376 | 369 | continue; |
@@ -501,7 +494,7 @@ public boolean visitArrayInitializer(List<? extends ExpressionTree> expressions) |
501 | 494 | if (!first) { |
502 | 495 | builder.forcedBreak(); |
503 | 496 | } |
504 | | - builder.open(row.iterator().next().getKind() == NEW_ARRAY || cols == 1 ? ZERO : plusFour); |
| 497 | + builder.open(row.iterator().next() instanceof NewArrayTree || cols == 1 ? ZERO : plusFour); |
505 | 498 | boolean firstInRow = true; |
506 | 499 | for (ExpressionTree item : row) { |
507 | 500 | if (!firstInRow) { |
@@ -753,7 +746,7 @@ public Void visitDoWhileLoop(DoWhileLoopTree node, Void unused) { |
753 | 746 | token("do"); |
754 | 747 | visitStatement( |
755 | 748 | node.getStatement(), CollapseEmptyOrNot.YES, AllowLeadingBlankLine.YES, AllowTrailingBlankLine.YES); |
756 | | - if (node.getStatement().getKind() == BLOCK) { |
| 749 | + if (node.getStatement() instanceof BlockTree) { |
757 | 750 | builder.space(); |
758 | 751 | } else { |
759 | 752 | builder.breakOp(" "); |
@@ -1006,12 +999,11 @@ public Void visitForLoop(ForLoopTree node, Void unused) { |
1006 | 999 | token("("); |
1007 | 1000 | builder.open(plusFour); |
1008 | 1001 | builder.open( |
1009 | | - node.getInitializer().size() > 1 |
1010 | | - && node.getInitializer().get(0).getKind() == Tree.Kind.EXPRESSION_STATEMENT |
| 1002 | + node.getInitializer().size() > 1 && node.getInitializer().get(0) instanceof ExpressionStatementTree |
1011 | 1003 | ? plusFour |
1012 | 1004 | : ZERO); |
1013 | 1005 | if (!node.getInitializer().isEmpty()) { |
1014 | | - if (node.getInitializer().get(0).getKind() == VARIABLE) { |
| 1006 | + if (node.getInitializer().get(0) instanceof VariableTree) { |
1015 | 1007 | PeekingIterator<StatementTree> it = |
1016 | 1008 | Iterators.peekingIterator(node.getInitializer().iterator()); |
1017 | 1009 | visitVariables(variableFragments(it, it.next()), DeclarationKind.NONE, Direction.HORIZONTAL); |
@@ -1071,7 +1063,7 @@ public Void visitIf(IfTree node, Void unused) { |
1071 | 1063 | while (true) { |
1072 | 1064 | expressions.add(node.getCondition()); |
1073 | 1065 | statements.add(node.getThenStatement()); |
1074 | | - if (node.getElseStatement() != null && node.getElseStatement().getKind() == IF) { |
| 1066 | + if (node.getElseStatement() != null && node.getElseStatement() instanceof IfTree) { |
1075 | 1067 | node = (IfTree) node.getElseStatement(); |
1076 | 1068 | } else { |
1077 | 1069 | break; |
@@ -1105,7 +1097,7 @@ public Void visitIf(IfTree node, Void unused) { |
1105 | 1097 | CollapseEmptyOrNot.valueOf(onlyClause), |
1106 | 1098 | AllowLeadingBlankLine.YES, |
1107 | 1099 | AllowTrailingBlankLine.valueOf(trailingClauses)); |
1108 | | - followingBlock = statements.get(i).getKind() == BLOCK; |
| 1100 | + followingBlock = statements.get(i) instanceof BlockTree; |
1109 | 1101 | first = false; |
1110 | 1102 | } |
1111 | 1103 | if (node.getElseStatement() != null) { |
@@ -1269,7 +1261,7 @@ protected void visitLambdaExpression(LambdaExpressionTree node, boolean statemen |
1269 | 1261 | builder.breakOp(" "); |
1270 | 1262 | } |
1271 | 1263 |
|
1272 | | - if (node.getBody().getKind() == Tree.Kind.BLOCK) { |
| 1264 | + if (node.getBody() instanceof BlockTree) { |
1273 | 1265 | visitBlock( |
1274 | 1266 | (BlockTree) node.getBody(), |
1275 | 1267 | CollapseEmptyOrNot.YES, |
@@ -1339,7 +1331,7 @@ private static boolean isArrayValue(ExpressionTree argument) { |
1339 | 1331 | } |
1340 | 1332 |
|
1341 | 1333 | public void visitAnnotationArgument(AssignmentTree node) { |
1342 | | - boolean isArrayInitializer = node.getExpression().getKind() == NEW_ARRAY; |
| 1334 | + boolean isArrayInitializer = node.getExpression() instanceof NewArrayTree; |
1343 | 1335 | sync(node); |
1344 | 1336 | builder.open( |
1345 | 1337 | isArrayInitializer ? ZERO : plusFour, |
@@ -1494,7 +1486,7 @@ public Void visitMethod(MethodTree node, Void unused) { |
1494 | 1486 | if (node.getDefaultValue() != null) { |
1495 | 1487 | builder.space(); |
1496 | 1488 | token("default"); |
1497 | | - if (node.getDefaultValue().getKind() == Tree.Kind.NEW_ARRAY) { |
| 1489 | + if (node.getDefaultValue() instanceof NewArrayTree) { |
1498 | 1490 | builder.open(minusFour); |
1499 | 1491 | { |
1500 | 1492 | builder.space(); |
@@ -1810,10 +1802,10 @@ boolean visitSingleMemberAnnotation(AnnotationTree node) { |
1810 | 1802 | return false; |
1811 | 1803 | } |
1812 | 1804 | ExpressionTree value = getOnlyElement(node.getArguments()); |
1813 | | - if (value.getKind() == ASSIGNMENT) { |
| 1805 | + if (value instanceof AssignmentTree) { |
1814 | 1806 | return false; |
1815 | 1807 | } |
1816 | | - boolean isArrayInitializer = value.getKind() == NEW_ARRAY; |
| 1808 | + boolean isArrayInitializer = value instanceof NewArrayTree; |
1817 | 1809 | builder.open( |
1818 | 1810 | isArrayInitializer ? ZERO : plusFour, |
1819 | 1811 | BreakBehaviours.preferBreakingLastInnerLevel(true), |
@@ -1845,7 +1837,7 @@ public Void visitCase(CaseTree node, Void unused) { |
1845 | 1837 | token(":"); |
1846 | 1838 | } |
1847 | 1839 | boolean isBlock = |
1848 | | - node.getStatements().size() == 1 && node.getStatements().get(0).getKind() == BLOCK; |
| 1840 | + node.getStatements().size() == 1 && node.getStatements().get(0) instanceof BlockTree; |
1849 | 1841 | builder.open(isBlock ? ZERO : plusTwo); |
1850 | 1842 | if (isBlock) { |
1851 | 1843 | builder.space(); |
@@ -2318,7 +2310,7 @@ private void visitCatchClause(CatchTree node, AllowTrailingBlankLine allowTraili |
2318 | 2310 | token("("); |
2319 | 2311 | builder.open(plusFour); |
2320 | 2312 | VariableTree ex = node.getParameter(); |
2321 | | - if (ex.getType().getKind() == UNION_TYPE) { |
| 2313 | + if (ex.getType() instanceof UnionTypeTree) { |
2322 | 2314 | builder.open(ZERO); |
2323 | 2315 | visitUnionType(ex); |
2324 | 2316 | builder.close(); |
@@ -2641,7 +2633,7 @@ void visitDot(ExpressionTree node0) { |
2641 | 2633 | LOOP: |
2642 | 2634 | do { |
2643 | 2635 | stack.addFirst(node); |
2644 | | - if (node.getKind() == ARRAY_ACCESS) { |
| 2636 | + if (node instanceof ArrayAccessTree) { |
2645 | 2637 | node = getArrayBase(node); |
2646 | 2638 | } |
2647 | 2639 | switch (node.getKind()) { |
@@ -2671,7 +2663,7 @@ void visitDot(ExpressionTree node0) { |
2671 | 2663 | if (node != null) { |
2672 | 2664 | // Exception: if it's an anonymous class declaration, we don't need to |
2673 | 2665 | // break and indent after the trailing '}'. |
2674 | | - if (node.getKind() == NEW_CLASS && ((NewClassTree) node).getClassBody() != null) { |
| 2666 | + if (node instanceof NewClassTree && ((NewClassTree) node).getClassBody() != null) { |
2675 | 2667 | builder.open(ZERO); |
2676 | 2668 | scan(getArrayBase(node), null); |
2677 | 2669 | token("."); |
@@ -2707,7 +2699,7 @@ void visitDot(ExpressionTree node0) { |
2707 | 2699 | { |
2708 | 2700 | for (int i = 0; i < items.size(); i++) { |
2709 | 2701 | ExpressionTree expression = items.get(i); |
2710 | | - if (expression.getKind() == METHOD_INVOCATION) { |
| 2702 | + if (expression instanceof MethodInvocationTree) { |
2711 | 2703 | if (i > 0 || node != null) { |
2712 | 2704 | // we only want dereference invocations |
2713 | 2705 | invocationCount++; |
@@ -2832,7 +2824,7 @@ private boolean fillFirstArgument(ExpressionTree e, List<ExpressionTree> items, |
2832 | 2824 | return false; |
2833 | 2825 | } |
2834 | 2826 | // don't special-case calls nested inside expressions |
2835 | | - if (e.getKind() != METHOD_INVOCATION) { |
| 2827 | + if (!(e instanceof MethodInvocationTree)) { |
2836 | 2828 | return false; |
2837 | 2829 | } |
2838 | 2830 | MethodInvocationTree methodInvocation = (MethodInvocationTree) e; |
@@ -2869,7 +2861,7 @@ private void visitDotWithPrefix( |
2869 | 2861 | @SuppressWarnings("for-rollout:NullAway") |
2870 | 2862 | boolean trailingDereferences = !prefixes.isEmpty() && getLast(prefixes) < items.size() - 1; |
2871 | 2863 |
|
2872 | | - boolean hasMethodInvocations = items.stream().anyMatch(expr -> expr.getKind() == METHOD_INVOCATION); |
| 2864 | + boolean hasMethodInvocations = items.stream().anyMatch(expr -> expr instanceof MethodInvocationTree); |
2873 | 2865 |
|
2874 | 2866 | builder.open(OpenOp.builder() |
2875 | 2867 | .debugName("visitDotWithPrefix") |
@@ -2939,15 +2931,15 @@ private void visitDotWithPrefix( |
2939 | 2931 | * Whereas an expression like a name {@code com.palantir.foo.bar.Baz} would not. |
2940 | 2932 | */ |
2941 | 2933 | private boolean shouldHaveColumnLimit(ExpressionTree expr) { |
2942 | | - return getArrayBase(expr).getKind() == METHOD_INVOCATION; |
| 2934 | + return getArrayBase(expr) instanceof MethodInvocationTree; |
2943 | 2935 | } |
2944 | 2936 |
|
2945 | 2937 | /** Returns the simple names of expressions in a "." chain. */ |
2946 | 2938 | private List<String> simpleNames(Deque<ExpressionTree> stack) { |
2947 | 2939 | ImmutableList.Builder<String> simpleNames = ImmutableList.builder(); |
2948 | 2940 | OUTER: |
2949 | 2941 | for (ExpressionTree expression : stack) { |
2950 | | - boolean isArray = expression.getKind() == ARRAY_ACCESS; |
| 2942 | + boolean isArray = expression instanceof ArrayAccessTree; |
2951 | 2943 | expression = getArrayBase(expression); |
2952 | 2944 | switch (expression.getKind()) { |
2953 | 2945 | case MEMBER_SELECT: |
@@ -3327,7 +3319,7 @@ private int argumentsAreTabular(List<? extends ExpressionTree> arguments) { |
3327 | 3319 | static int rowLength(List<? extends ExpressionTree> row) { |
3328 | 3320 | int size = 0; |
3329 | 3321 | for (ExpressionTree tree : row) { |
3330 | | - if (tree.getKind() != NEW_ARRAY) { |
| 3322 | + if (!(tree instanceof NewArrayTree)) { |
3331 | 3323 | size++; |
3332 | 3324 | continue; |
3333 | 3325 | } |
@@ -3482,8 +3474,7 @@ int declareOne( |
3482 | 3474 | if (initializer.isPresent()) { |
3483 | 3475 | builder.space(); |
3484 | 3476 | token(equals); |
3485 | | - if (initializer.get().getKind() == Tree.Kind.NEW_ARRAY |
3486 | | - && ((NewArrayTree) initializer.get()).getType() == null) { |
| 3477 | + if (initializer.get() instanceof NewArrayTree && ((NewArrayTree) initializer.get()).getType() == null) { |
3487 | 3478 | builder.open(minusFour); |
3488 | 3479 | builder.space(); |
3489 | 3480 | initializer.get().accept(this, null); |
@@ -3659,15 +3650,15 @@ protected void addBodyDeclarations( |
3659 | 3650 | builder.forcedBreak(); |
3660 | 3651 | @SuppressWarnings("for-rollout:NullAway") |
3661 | 3652 | boolean thisOneGetsBlankLineBefore = |
3662 | | - bodyDeclaration.getKind() != VARIABLE || hasJavaDoc(bodyDeclaration); |
| 3653 | + !(bodyDeclaration instanceof VariableTree) || hasJavaDoc(bodyDeclaration); |
3663 | 3654 | if (first) { |
3664 | 3655 | builder.blankLineWanted(PRESERVE); |
3665 | 3656 | } else if (!first && (thisOneGetsBlankLineBefore || lastOneGotBlankLineBefore)) { |
3666 | 3657 | builder.blankLineWanted(YES); |
3667 | 3658 | } |
3668 | 3659 | markForPartialFormat(); |
3669 | 3660 |
|
3670 | | - if (bodyDeclaration.getKind() == VARIABLE) { |
| 3661 | + if (bodyDeclaration instanceof VariableTree) { |
3671 | 3662 | visitVariables( |
3672 | 3663 | variableFragments(it, bodyDeclaration), |
3673 | 3664 | DeclarationKind.FIELD, |
@@ -3727,10 +3718,10 @@ private void classDeclarationTypeList(String token, List<? extends Tree> types) |
3727 | 3718 | @SuppressWarnings("for-rollout:NullAway") |
3728 | 3719 | private List<VariableTree> variableFragments(PeekingIterator<? extends Tree> it, Tree first) { |
3729 | 3720 | List<VariableTree> fragments = new ArrayList<>(); |
3730 | | - if (first.getKind() == VARIABLE) { |
| 3721 | + if (first instanceof VariableTree) { |
3731 | 3722 | int start = getStartPosition(first); |
3732 | 3723 | fragments.add((VariableTree) first); |
3733 | | - while (it.hasNext() && it.peek().getKind() == VARIABLE && getStartPosition(it.peek()) == start) { |
| 3724 | + while (it.hasNext() && it.peek() instanceof VariableTree && getStartPosition(it.peek()) == start) { |
3734 | 3725 | fragments.add((VariableTree) it.next()); |
3735 | 3726 | } |
3736 | 3727 | } |
|
0 commit comments