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.whitespace;
21
22 import com.puppycrawl.tools.checkstyle.StatelessCheck;
23 import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
24 import com.puppycrawl.tools.checkstyle.api.DetailAST;
25 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
26 import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 @StatelessCheck
49 public class NoWhitespaceBeforeCaseDefaultColonCheck
50 extends AbstractCheck {
51
52
53
54
55
56 public static final String MSG_KEY = "ws.preceded";
57
58 @Override
59 public int[] getDefaultTokens() {
60 return getRequiredTokens();
61 }
62
63 @Override
64 public int[] getAcceptableTokens() {
65 return getRequiredTokens();
66 }
67
68 @Override
69 public int[] getRequiredTokens() {
70 return new int[] {TokenTypes.COLON};
71 }
72
73 @Override
74 public boolean isCommentNodesRequired() {
75 return true;
76 }
77
78 @Override
79 public void visitToken(DetailAST ast) {
80 if (isInSwitch(ast) && isWhiteSpaceBeforeColon(ast)) {
81 log(ast, MSG_KEY, ast.getText());
82 }
83 }
84
85
86
87
88
89
90
91 private static boolean isInSwitch(DetailAST colonAst) {
92 return TokenUtil.isOfType(colonAst.getParent(), TokenTypes.LITERAL_CASE,
93 TokenTypes.LITERAL_DEFAULT);
94 }
95
96
97
98
99
100
101
102 private static boolean isWhiteSpaceBeforeColon(DetailAST colonAst) {
103 final DetailAST parent = colonAst.getParent();
104 final boolean result;
105 if (isOnDifferentLineWithPreviousToken(colonAst)) {
106 result = true;
107 }
108 else if (parent.getType() == TokenTypes.LITERAL_CASE) {
109 result = isWhitespaceBeforeColonOfCase(colonAst);
110 }
111 else {
112 result = isWhitespaceBeforeColonOfDefault(colonAst);
113 }
114 return result;
115 }
116
117
118
119
120
121
122
123 private static boolean isWhitespaceBeforeColonOfCase(DetailAST colonAst) {
124 final DetailAST previousSibling = colonAst.getPreviousSibling();
125 int offset = 0;
126 if (previousSibling.getType() == TokenTypes.BLOCK_COMMENT_BEGIN) {
127 offset = 1;
128 }
129 return colonAst.getColumnNo() != getLastColumnNumberOf(previousSibling) + offset;
130 }
131
132
133
134
135
136
137
138 private static boolean isWhitespaceBeforeColonOfDefault(DetailAST colonAst) {
139 final boolean result;
140 final DetailAST previousSibling = colonAst.getPreviousSibling();
141 if (previousSibling == null) {
142 final DetailAST literalDefault = colonAst.getParent();
143 result = colonAst.getColumnNo()
144 != literalDefault.getColumnNo() + literalDefault.getText().length();
145 }
146 else {
147 result =
148 colonAst.getColumnNo() != getLastColumnNumberOf(previousSibling) + 1;
149 }
150 return result;
151 }
152
153
154
155
156
157
158
159 private static boolean isOnDifferentLineWithPreviousToken(DetailAST colonAst) {
160 final DetailAST previousSibling;
161 final DetailAST parent = colonAst.getParent();
162 if (parent.getType() == TokenTypes.LITERAL_CASE) {
163 previousSibling = colonAst.getPreviousSibling();
164 }
165 else {
166 previousSibling = colonAst.getParent();
167 }
168 return !TokenUtil.areOnSameLine(previousSibling, colonAst);
169 }
170
171
172
173
174
175
176
177 private static int getLastColumnNumberOf(DetailAST ast) {
178 DetailAST lastChild = ast;
179 while (lastChild.hasChildren()) {
180 lastChild = lastChild.getLastChild();
181 }
182 return lastChild.getColumnNo() + lastChild.getText().length();
183 }
184
185 }