Repository navigation
Expand file tree
/
Copy pathCommentSizeRule.java
More file actions
127 lines (105 loc) · 4.55 KB
/
Copy pathCommentSizeRule.java
File metadata and controls
127 lines (105 loc) · 4.55 KB
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
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.rule.documentation;
import static net.sourceforge.pmd.properties.NumericConstraints.positive;
import net.sourceforge.pmd.lang.document.Chars;
import net.sourceforge.pmd.lang.document.FileLocation;
import net.sourceforge.pmd.lang.document.TextPos2d;
import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
import net.sourceforge.pmd.lang.java.ast.JavaComment;
import net.sourceforge.pmd.lang.java.ast.JavaNode;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRulechainRule;
import net.sourceforge.pmd.properties.PropertyDescriptor;
import net.sourceforge.pmd.properties.PropertyFactory;
import net.sourceforge.pmd.reporting.RuleContext;
/**
* A rule to manage those who just can't shut up...
*
* @author Brian Remedios
*/
public class CommentSizeRule extends AbstractJavaRulechainRule {
public static final PropertyDescriptor<Integer> MAX_LINES
= PropertyFactory.intProperty("maxLines")
.desc("Maximum lines")
.require(positive()).defaultValue(6).build();
public static final PropertyDescriptor<Integer> MAX_LINE_LENGTH
= PropertyFactory.intProperty("maxLineLength")
.desc("Maximum line length")
.require(positive()).defaultValue(80).build();
public CommentSizeRule() {
super(ASTCompilationUnit.class);
definePropertyDescriptor(MAX_LINES);
definePropertyDescriptor(MAX_LINE_LENGTH);
}
@Override
public Object visit(ASTCompilationUnit cUnit, Object data) {
// Comments that make up the file header (e.g. a license/copyright header)
// appear before the first declaration and are not subject to the limits,
// see #6270.
TextPos2d firstCodePos = firstCodeTokenStart(cUnit);
for (JavaComment comment : cUnit.getComments()) {
if (firstCodePos != null
&& comment.getReportLocation().getEndPos().compareTo(firstCodePos) <= 0) {
continue;
}
if (hasTooManyLines(comment)) {
asCtx(data).addViolationWithPosition(
comment.getToken(),
cUnit.getAstInfo(),
comment.getReportLocation(),
getMessage() + ": Too many lines");
}
reportLinesTooLong(cUnit, asCtx(data), comment);
}
return null;
}
/**
* Start position of the first code token of the compilation unit, or {@code null}
* if the file has no declaration. Any comment ending before this position is
* considered part of the file header.
*/
private static TextPos2d firstCodeTokenStart(ASTCompilationUnit cUnit) {
if (cUnit.getNumChildren() == 0) {
return null;
}
JavaNode firstDecl = cUnit.getChild(0);
return firstDecl.getFirstToken().getReportLocation().getStartPos();
}
private static boolean hasRealText(Chars line) {
return !JavaComment.removeCommentMarkup(line).isEmpty();
}
private boolean hasTooManyLines(JavaComment comment) {
int firstLineWithText = -1;
int lastLineWithText;
int lineNumberWithinComment = 0;
int maxLines = getProperty(MAX_LINES);
for (Chars line : comment.getText().lines()) {
if (hasRealText(line)) {
lastLineWithText = lineNumberWithinComment;
if (firstLineWithText == -1) {
firstLineWithText = lineNumberWithinComment;
}
if (lastLineWithText - firstLineWithText + 1 > maxLines) {
return true;
}
}
lineNumberWithinComment++;
}
return false;
}
private void reportLinesTooLong(ASTCompilationUnit acu, RuleContext ctx, JavaComment comment) {
int maxLength = getProperty(MAX_LINE_LENGTH);
int lineNumber = comment.getReportLocation().getStartLine();
for (Chars line : comment.getFilteredLines(true)) {
if (line.length() > maxLength) {
FileLocation location = FileLocation.caret(acu.getTextDocument().getFileId(), lineNumber, 1);
ctx.addViolationWithPosition(comment.getToken(),
acu.getAstInfo(),
location,
getMessage() + ": Line too long");
}
lineNumber++;
}
}
}