forked from checkstyle/checkstyle
/
BlockCommentPosition.java
293 lines (266 loc) · 11.8 KB
/
BlockCommentPosition.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2020 the original author or authors.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.utils;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
/**
* Utility class that has methods to check javadoc comment position in java file.
*
*/
public final class BlockCommentPosition {
/**
* Forbid new instances.
*/
private BlockCommentPosition() {
}
/**
* Node is on type definition.
* @param blockComment DetailAST
* @return true if node is before class, interface, enum or annotation.
*/
public static boolean isOnType(DetailAST blockComment) {
return isOnClass(blockComment)
|| isOnInterface(blockComment)
|| isOnEnum(blockComment)
|| isOnAnnotationDef(blockComment);
}
/**
* Node is on class definition.
* @param blockComment DetailAST
* @return true if node is before class
*/
public static boolean isOnClass(DetailAST blockComment) {
return isOnPlainToken(blockComment, TokenTypes.CLASS_DEF, TokenTypes.LITERAL_CLASS)
|| isOnTokenWithModifiers(blockComment, TokenTypes.CLASS_DEF)
|| isOnTokenWithAnnotation(blockComment, TokenTypes.CLASS_DEF);
}
/**
* Node is on package definition.
* @param blockComment DetailAST
* @return true if node is before package
*/
public static boolean isOnPackage(DetailAST blockComment) {
boolean result = isOnTokenWithAnnotation(blockComment, TokenTypes.PACKAGE_DEF);
if (!result) {
DetailAST nextSibling = blockComment.getNextSibling();
while (nextSibling != null
&& nextSibling.getType() == TokenTypes.SINGLE_LINE_COMMENT) {
nextSibling = nextSibling.getNextSibling();
}
result = nextSibling != null && nextSibling.getType() == TokenTypes.PACKAGE_DEF;
}
return result;
}
/**
* Node is on interface definition.
* @param blockComment DetailAST
* @return true if node is before interface
*/
public static boolean isOnInterface(DetailAST blockComment) {
return isOnPlainToken(blockComment, TokenTypes.INTERFACE_DEF, TokenTypes.LITERAL_INTERFACE)
|| isOnTokenWithModifiers(blockComment, TokenTypes.INTERFACE_DEF)
|| isOnTokenWithAnnotation(blockComment, TokenTypes.INTERFACE_DEF);
}
/**
* Node is on enum definition.
* @param blockComment DetailAST
* @return true if node is before enum
*/
public static boolean isOnEnum(DetailAST blockComment) {
return isOnPlainToken(blockComment, TokenTypes.ENUM_DEF, TokenTypes.ENUM)
|| isOnTokenWithModifiers(blockComment, TokenTypes.ENUM_DEF)
|| isOnTokenWithAnnotation(blockComment, TokenTypes.ENUM_DEF);
}
/**
* Node is on annotation definition.
* @param blockComment DetailAST
* @return true if node is before annotation
*/
public static boolean isOnAnnotationDef(DetailAST blockComment) {
return isOnPlainToken(blockComment, TokenTypes.ANNOTATION_DEF, TokenTypes.AT)
|| isOnTokenWithModifiers(blockComment, TokenTypes.ANNOTATION_DEF)
|| isOnTokenWithAnnotation(blockComment, TokenTypes.ANNOTATION_DEF);
}
/**
* Node is on type member declaration.
* @param blockComment DetailAST
* @return true if node is before method, field, constructor, enum constant
* or annotation field
*/
public static boolean isOnMember(DetailAST blockComment) {
return isOnMethod(blockComment)
|| isOnField(blockComment)
|| isOnConstructor(blockComment)
|| isOnEnumConstant(blockComment)
|| isOnAnnotationField(blockComment);
}
/**
* Node is on method declaration.
* @param blockComment DetailAST
* @return true if node is before method
*/
public static boolean isOnMethod(DetailAST blockComment) {
return isOnPlainClassMember(blockComment, TokenTypes.METHOD_DEF)
|| isOnTokenWithModifiers(blockComment, TokenTypes.METHOD_DEF)
|| isOnTokenWithAnnotation(blockComment, TokenTypes.METHOD_DEF);
}
/**
* Node is on field declaration.
* @param blockComment DetailAST
* @return true if node is before field
*/
public static boolean isOnField(DetailAST blockComment) {
return isOnPlainClassMember(blockComment, TokenTypes.VARIABLE_DEF)
|| isOnTokenWithModifiers(blockComment, TokenTypes.VARIABLE_DEF)
&& blockComment.getParent().getParent().getParent()
.getType() == TokenTypes.OBJBLOCK
|| isOnTokenWithAnnotation(blockComment, TokenTypes.VARIABLE_DEF)
&& blockComment.getParent().getParent().getParent()
.getParent().getType() == TokenTypes.OBJBLOCK;
}
/**
* Node is on constructor.
* @param blockComment DetailAST
* @return true if node is before constructor
*/
public static boolean isOnConstructor(DetailAST blockComment) {
return isOnPlainToken(blockComment, TokenTypes.CTOR_DEF, TokenTypes.IDENT)
|| isOnTokenWithModifiers(blockComment, TokenTypes.CTOR_DEF)
|| isOnTokenWithAnnotation(blockComment, TokenTypes.CTOR_DEF);
}
/**
* Node is on enum constant.
* @param blockComment DetailAST
* @return true if node is before enum constant
*/
public static boolean isOnEnumConstant(DetailAST blockComment) {
final DetailAST parent = blockComment.getParent();
boolean result = false;
if (parent != null) {
if (parent.getType() == TokenTypes.ENUM_CONSTANT_DEF) {
final DetailAST prevSibling = getPrevSiblingSkipComments(blockComment);
if (prevSibling.getType() == TokenTypes.ANNOTATIONS && !prevSibling.hasChildren()) {
result = true;
}
}
else if (parent.getType() == TokenTypes.ANNOTATION
&& parent.getParent().getParent().getType() == TokenTypes.ENUM_CONSTANT_DEF) {
result = true;
}
}
return result;
}
/**
* Node is on annotation field declaration.
* @param blockComment DetailAST
* @return true if node is before annotation field
*/
public static boolean isOnAnnotationField(DetailAST blockComment) {
return isOnPlainClassMember(blockComment, TokenTypes.ANNOTATION_FIELD_DEF)
|| isOnTokenWithModifiers(blockComment, TokenTypes.ANNOTATION_FIELD_DEF)
|| isOnTokenWithAnnotation(blockComment, TokenTypes.ANNOTATION_FIELD_DEF);
}
/**
* Checks that block comment is on specified token without any modifiers.
* @param blockComment block comment start DetailAST
* @param parentTokenType parent token type
* @param nextTokenType next token type
* @return true if block comment is on specified token without modifiers
*/
private static boolean isOnPlainToken(DetailAST blockComment,
int parentTokenType, int nextTokenType) {
return blockComment.getParent() != null
&& blockComment.getParent().getType() == parentTokenType
&& !getPrevSiblingSkipComments(blockComment).hasChildren()
&& getNextSiblingSkipComments(blockComment).getType() == nextTokenType;
}
/**
* Checks that block comment is on specified token with modifiers.
* @param blockComment block comment start DetailAST
* @param tokenType parent token type
* @return true if block comment is on specified token with modifiers
*/
private static boolean isOnTokenWithModifiers(DetailAST blockComment, int tokenType) {
return blockComment.getParent() != null
&& blockComment.getParent().getType() == TokenTypes.MODIFIERS
&& blockComment.getParent().getParent().getType() == tokenType
&& getPrevSiblingSkipComments(blockComment) == null;
}
/**
* Checks that block comment is on specified token with annotation.
* @param blockComment block comment start DetailAST
* @param tokenType parent token type
* @return true if block comment is on specified token with annotation
*/
private static boolean isOnTokenWithAnnotation(DetailAST blockComment, int tokenType) {
return blockComment.getParent() != null
&& blockComment.getParent().getType() == TokenTypes.ANNOTATION
&& getPrevSiblingSkipComments(blockComment.getParent()) == null
&& blockComment.getParent().getParent().getParent().getType() == tokenType
&& getPrevSiblingSkipComments(blockComment) == null;
}
/**
* Checks that block comment is on specified class member without any modifiers.
* @param blockComment block comment start DetailAST
* @param memberType parent token type
* @return true if block comment is on specified token without modifiers
*/
private static boolean isOnPlainClassMember(DetailAST blockComment, int memberType) {
DetailAST parent = blockComment.getParent();
// type could be in fully qualified form, so we go up to Type token
while (parent != null && (parent.getType() == TokenTypes.DOT
|| parent.getType() == TokenTypes.ARRAY_DECLARATOR)) {
parent = parent.getParent();
}
return parent != null
&& (parent.getType() == TokenTypes.TYPE
|| parent.getType() == TokenTypes.TYPE_PARAMETERS)
&& parent.getParent().getType() == memberType
// previous parent sibling is always TokenTypes.MODIFIERS
&& !parent.getPreviousSibling().hasChildren()
&& parent.getParent().getParent().getType() == TokenTypes.OBJBLOCK;
}
/**
* Get next sibling node skipping any comment nodes.
* @param node current node
* @return next sibling
*/
private static DetailAST getNextSiblingSkipComments(DetailAST node) {
DetailAST result = node.getNextSibling();
while (result.getType() == TokenTypes.SINGLE_LINE_COMMENT
|| result.getType() == TokenTypes.BLOCK_COMMENT_BEGIN) {
result = result.getNextSibling();
}
return result;
}
/**
* Get previous sibling node skipping any comments.
* @param node current node
* @return previous sibling
*/
private static DetailAST getPrevSiblingSkipComments(DetailAST node) {
DetailAST result = node.getPreviousSibling();
while (result != null
&& (result.getType() == TokenTypes.SINGLE_LINE_COMMENT
|| result.getType() == TokenTypes.BLOCK_COMMENT_BEGIN)) {
result = result.getPreviousSibling();
}
return result;
}
}