1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.checks.indentation;
21
22 import java.util.Optional;
23
24 import com.puppycrawl.tools.checkstyle.api.DetailAST;
25 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
26
27
28
29
30
31 public class MemberDefHandler extends AbstractExpressionHandler {
32
33
34
35
36
37
38
39
40
41 public MemberDefHandler(IndentationCheck indentCheck,
42 DetailAST ast, AbstractExpressionHandler parent) {
43 super(indentCheck, "member def", ast, parent);
44 }
45
46 @Override
47 public void checkIndentation() {
48 final DetailAST modifiersNode = getMainAst().findFirstToken(TokenTypes.MODIFIERS);
49 if (modifiersNode.hasChildren()) {
50 checkModifiers();
51 }
52 else {
53 checkType();
54 }
55 final DetailAST firstNode = getMainAst();
56 final DetailAST lastNode = getArrayInitNode(firstNode)
57 .orElseGet(() -> getVarDefStatementSemicolon(firstNode));
58
59 if (lastNode != null) {
60 checkWrappingIndentation(firstNode, lastNode);
61 }
62 }
63
64
65
66
67
68
69
70 private static Optional<DetailAST> getArrayInitNode(DetailAST firstNode) {
71 return Optional.ofNullable(firstNode.findFirstToken(TokenTypes.ASSIGN))
72 .map(assign -> {
73 return Optional.ofNullable(assign.findFirstToken(TokenTypes.EXPR))
74 .map(expr -> expr.findFirstToken(TokenTypes.LITERAL_NEW))
75 .orElse(assign);
76 })
77 .map(node -> node.findFirstToken(TokenTypes.ARRAY_INIT));
78 }
79
80 @Override
81 public IndentLevel getSuggestedChildIndent(AbstractExpressionHandler child) {
82 return getIndent();
83 }
84
85 @Override
86 protected void checkModifiers() {
87 final DetailAST modifier = getMainAst().findFirstToken(TokenTypes.MODIFIERS);
88 if (isOnStartOfLine(modifier)
89 && !getIndent().isAcceptable(expandedTabsColumnNo(modifier))) {
90 logError(modifier, "modifier", expandedTabsColumnNo(modifier));
91 }
92 }
93
94
95
96
97 private void checkType() {
98 final DetailAST type = getMainAst().findFirstToken(TokenTypes.TYPE);
99 final DetailAST ident = AbstractExpressionHandler.getFirstToken(type);
100 final int columnNo = expandedTabsColumnNo(ident);
101 if (isOnStartOfLine(ident) && !getIndent().isAcceptable(columnNo)) {
102 logError(ident, "type", columnNo);
103 }
104 }
105
106
107
108
109
110
111
112
113 private static DetailAST getVarDefStatementSemicolon(DetailAST variableDef) {
114 DetailAST lastNode = variableDef.getLastChild();
115 if (lastNode.getType() != TokenTypes.SEMI) {
116 lastNode = variableDef.getNextSibling();
117 }
118 return lastNode;
119 }
120
121 }