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 java.util.Arrays;
23 import java.util.Locale;
24
25 import com.puppycrawl.tools.checkstyle.StatelessCheck;
26 import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
27 import com.puppycrawl.tools.checkstyle.api.DetailAST;
28 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
29 import com.puppycrawl.tools.checkstyle.utils.CodePointUtil;
30 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72 @StatelessCheck
73 public class SeparatorWrapCheck
74 extends AbstractCheck {
75
76
77
78
79
80 public static final String MSG_LINE_PREVIOUS = "line.previous";
81
82
83
84
85
86 public static final String MSG_LINE_NEW = "line.new";
87
88
89 private WrapOption option = WrapOption.EOL;
90
91
92
93
94
95
96
97
98 public void setOption(String optionStr) {
99 option = WrapOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH));
100 }
101
102 @Override
103 public int[] getDefaultTokens() {
104 return new int[] {
105 TokenTypes.DOT,
106 TokenTypes.COMMA,
107 };
108 }
109
110 @Override
111 public int[] getAcceptableTokens() {
112 return new int[] {
113 TokenTypes.DOT,
114 TokenTypes.COMMA,
115 TokenTypes.SEMI,
116 TokenTypes.ELLIPSIS,
117 TokenTypes.AT,
118 TokenTypes.LPAREN,
119 TokenTypes.RPAREN,
120 TokenTypes.ARRAY_DECLARATOR,
121 TokenTypes.RBRACK,
122 TokenTypes.METHOD_REF,
123 };
124 }
125
126 @Override
127 public int[] getRequiredTokens() {
128 return CommonUtil.EMPTY_INT_ARRAY;
129 }
130
131 @Override
132 public void visitToken(DetailAST ast) {
133 final String text = ast.getText();
134 final int colNo = ast.getColumnNo();
135 final int lineNo = ast.getLineNo();
136 final int[] currentLine = getLineCodePoints(lineNo - 1);
137 final boolean isLineEmptyAfterToken = CodePointUtil.isBlank(
138 Arrays.copyOfRange(currentLine, colNo + text.length(), currentLine.length)
139 );
140 final boolean isLineEmptyBeforeToken = CodePointUtil.isBlank(
141 Arrays.copyOfRange(currentLine, 0, colNo)
142 );
143
144 if (option == WrapOption.NL
145 && isLineEmptyAfterToken) {
146 log(ast, MSG_LINE_NEW, text);
147 }
148 else if (option == WrapOption.EOL
149 && isLineEmptyBeforeToken) {
150 log(ast, MSG_LINE_PREVIOUS, text);
151 }
152 }
153
154 }