/
CommentRequiredRule.java
217 lines (181 loc) · 10.3 KB
/
CommentRequiredRule.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
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.rule.documentation;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import net.sourceforge.pmd.lang.java.ast.ASTBodyDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTClassDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTEnumDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTExecutableDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
import net.sourceforge.pmd.lang.java.ast.JavaNode;
import net.sourceforge.pmd.lang.java.ast.JavadocCommentOwner;
import net.sourceforge.pmd.lang.java.ast.ModifierOwner.Visibility;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRulechainRule;
import net.sourceforge.pmd.lang.java.rule.internal.JavaRuleUtil;
import net.sourceforge.pmd.properties.PropertyBuilder.GenericPropertyBuilder;
import net.sourceforge.pmd.properties.PropertyDescriptor;
import net.sourceforge.pmd.properties.PropertyFactory;
import net.sourceforge.pmd.reporting.RuleContext;
import net.sourceforge.pmd.util.CollectionUtil;
/**
* @author Brian Remedios
*/
public class CommentRequiredRule extends AbstractJavaRulechainRule {
// Used to pretty print a message
private static final Map<String, String> DESCRIPTOR_NAME_TO_COMMENT_TYPE = new HashMap<>();
private static final PropertyDescriptor<CommentRequirement> ACCESSOR_CMT_DESCRIPTOR
= requirementPropertyBuilder("accessorCommentRequirement", "Comments on getters and setters\"")
.defaultValue(CommentRequirement.Ignored).build();
private static final PropertyDescriptor<CommentRequirement> OVERRIDE_CMT_DESCRIPTOR
= requirementPropertyBuilder("methodWithOverrideCommentRequirement", "Comments on @Override methods")
.defaultValue(CommentRequirement.Ignored).build();
private static final PropertyDescriptor<CommentRequirement> CLASS_CMT_REQUIREMENT_DESCRIPTOR
= requirementPropertyBuilder("classCommentRequirement", "Class comments").build();
private static final PropertyDescriptor<CommentRequirement> FIELD_CMT_REQUIREMENT_DESCRIPTOR
= requirementPropertyBuilder("fieldCommentRequirement", "Field comments").build();
private static final PropertyDescriptor<CommentRequirement> PUB_METHOD_CMT_REQUIREMENT_DESCRIPTOR
= requirementPropertyBuilder("publicMethodCommentRequirement", "Public method and constructor comments").build();
private static final PropertyDescriptor<CommentRequirement> PROT_METHOD_CMT_REQUIREMENT_DESCRIPTOR
= requirementPropertyBuilder("protectedMethodCommentRequirement", "Protected method constructor comments").build();
private static final PropertyDescriptor<CommentRequirement> ENUM_CMT_REQUIREMENT_DESCRIPTOR
= requirementPropertyBuilder("enumCommentRequirement", "Enum comments").build();
private static final PropertyDescriptor<CommentRequirement> SERIAL_VERSION_UID_CMT_REQUIREMENT_DESCRIPTOR
= requirementPropertyBuilder("serialVersionUIDCommentRequired", "Serial version UID comments")
.defaultValue(CommentRequirement.Ignored).build();
private static final PropertyDescriptor<CommentRequirement> SERIAL_PERSISTENT_FIELDS_CMT_REQUIREMENT_DESCRIPTOR
= requirementPropertyBuilder("serialPersistentFieldsCommentRequired", "Serial persistent fields comments")
.defaultValue(CommentRequirement.Ignored).build();
/** stores the resolved property values. This is necessary in order to transparently use deprecated properties. */
private final Map<PropertyDescriptor<CommentRequirement>, CommentRequirement> propertyValues = new HashMap<>();
public CommentRequiredRule() {
super(ASTBodyDeclaration.class);
definePropertyDescriptor(OVERRIDE_CMT_DESCRIPTOR);
definePropertyDescriptor(ACCESSOR_CMT_DESCRIPTOR);
definePropertyDescriptor(CLASS_CMT_REQUIREMENT_DESCRIPTOR);
definePropertyDescriptor(FIELD_CMT_REQUIREMENT_DESCRIPTOR);
definePropertyDescriptor(PUB_METHOD_CMT_REQUIREMENT_DESCRIPTOR);
definePropertyDescriptor(PROT_METHOD_CMT_REQUIREMENT_DESCRIPTOR);
definePropertyDescriptor(ENUM_CMT_REQUIREMENT_DESCRIPTOR);
definePropertyDescriptor(SERIAL_VERSION_UID_CMT_REQUIREMENT_DESCRIPTOR);
definePropertyDescriptor(SERIAL_PERSISTENT_FIELDS_CMT_REQUIREMENT_DESCRIPTOR);
}
@Override
public void start(RuleContext ctx) {
propertyValues.put(ACCESSOR_CMT_DESCRIPTOR, getProperty(ACCESSOR_CMT_DESCRIPTOR));
propertyValues.put(OVERRIDE_CMT_DESCRIPTOR, getProperty(OVERRIDE_CMT_DESCRIPTOR));
propertyValues.put(FIELD_CMT_REQUIREMENT_DESCRIPTOR, getProperty(FIELD_CMT_REQUIREMENT_DESCRIPTOR));
propertyValues.put(PUB_METHOD_CMT_REQUIREMENT_DESCRIPTOR, getProperty(PUB_METHOD_CMT_REQUIREMENT_DESCRIPTOR));
propertyValues.put(PROT_METHOD_CMT_REQUIREMENT_DESCRIPTOR, getProperty(PROT_METHOD_CMT_REQUIREMENT_DESCRIPTOR));
propertyValues.put(ENUM_CMT_REQUIREMENT_DESCRIPTOR, getProperty(ENUM_CMT_REQUIREMENT_DESCRIPTOR));
propertyValues.put(SERIAL_VERSION_UID_CMT_REQUIREMENT_DESCRIPTOR,
getProperty(SERIAL_VERSION_UID_CMT_REQUIREMENT_DESCRIPTOR));
propertyValues.put(SERIAL_PERSISTENT_FIELDS_CMT_REQUIREMENT_DESCRIPTOR,
getProperty(SERIAL_PERSISTENT_FIELDS_CMT_REQUIREMENT_DESCRIPTOR));
propertyValues.put(CLASS_CMT_REQUIREMENT_DESCRIPTOR, getProperty(CLASS_CMT_REQUIREMENT_DESCRIPTOR));
}
private void checkCommentMeetsRequirement(Object data, JavadocCommentOwner node,
PropertyDescriptor<CommentRequirement> descriptor) {
switch (propertyValues.get(descriptor)) {
case Ignored:
break;
case Required:
if (node.getJavadocComment() == null) {
commentRequiredViolation(data, node, descriptor);
}
break;
case Unwanted:
if (node.getJavadocComment() != null) {
commentRequiredViolation(data, node, descriptor);
}
break;
default:
break;
}
}
// Adds a violation
private void commentRequiredViolation(Object data, JavaNode node,
PropertyDescriptor<CommentRequirement> descriptor) {
asCtx(data).addViolationWithMessage(node,
DESCRIPTOR_NAME_TO_COMMENT_TYPE.get(descriptor.name())
+ " are "
+ getProperty(descriptor).label.toLowerCase(Locale.ROOT));
}
@Override
public Object visit(ASTClassDeclaration decl, Object data) {
checkCommentMeetsRequirement(data, decl, CLASS_CMT_REQUIREMENT_DESCRIPTOR);
return data;
}
@Override
public Object visit(ASTConstructorDeclaration decl, Object data) {
checkMethodOrConstructorComment(decl, data);
return data;
}
@Override
public Object visit(ASTMethodDeclaration decl, Object data) {
if (decl.isOverridden()) {
checkCommentMeetsRequirement(data, decl, OVERRIDE_CMT_DESCRIPTOR);
} else if (JavaRuleUtil.isGetterOrSetter(decl)) {
checkCommentMeetsRequirement(data, decl, ACCESSOR_CMT_DESCRIPTOR);
} else {
checkMethodOrConstructorComment(decl, data);
}
return data;
}
private void checkMethodOrConstructorComment(ASTExecutableDeclaration decl, Object data) {
if (decl.getVisibility() == Visibility.V_PUBLIC) {
checkCommentMeetsRequirement(data, decl, PUB_METHOD_CMT_REQUIREMENT_DESCRIPTOR);
} else if (decl.getVisibility() == Visibility.V_PROTECTED) {
checkCommentMeetsRequirement(data, decl, PROT_METHOD_CMT_REQUIREMENT_DESCRIPTOR);
}
}
@Override
public Object visit(ASTFieldDeclaration decl, Object data) {
if (JavaRuleUtil.isSerialVersionUID(decl)) {
checkCommentMeetsRequirement(data, decl, SERIAL_VERSION_UID_CMT_REQUIREMENT_DESCRIPTOR);
} else if (JavaRuleUtil.isSerialPersistentFields(decl)) {
checkCommentMeetsRequirement(data, decl, SERIAL_PERSISTENT_FIELDS_CMT_REQUIREMENT_DESCRIPTOR);
} else {
checkCommentMeetsRequirement(data, decl, FIELD_CMT_REQUIREMENT_DESCRIPTOR);
}
return data;
}
@Override
public Object visit(ASTEnumDeclaration decl, Object data) {
checkCommentMeetsRequirement(data, decl, ENUM_CMT_REQUIREMENT_DESCRIPTOR);
return data;
}
private boolean allCommentsAreIgnored() {
return getProperty(OVERRIDE_CMT_DESCRIPTOR) == CommentRequirement.Ignored
&& getProperty(ACCESSOR_CMT_DESCRIPTOR) == CommentRequirement.Ignored
&& getProperty(CLASS_CMT_REQUIREMENT_DESCRIPTOR) == CommentRequirement.Ignored
&& getProperty(FIELD_CMT_REQUIREMENT_DESCRIPTOR) == CommentRequirement.Ignored
&& getProperty(PUB_METHOD_CMT_REQUIREMENT_DESCRIPTOR) == CommentRequirement.Ignored
&& getProperty(PROT_METHOD_CMT_REQUIREMENT_DESCRIPTOR) == CommentRequirement.Ignored
&& getProperty(ENUM_CMT_REQUIREMENT_DESCRIPTOR) == CommentRequirement.Ignored
&& getProperty(SERIAL_VERSION_UID_CMT_REQUIREMENT_DESCRIPTOR) == CommentRequirement.Ignored
&& getProperty(SERIAL_PERSISTENT_FIELDS_CMT_REQUIREMENT_DESCRIPTOR) == CommentRequirement.Ignored;
}
@Override
public String dysfunctionReason() {
return allCommentsAreIgnored() ? "All comment types are ignored" : null;
}
private enum CommentRequirement {
Required("Required"), Ignored("Ignored"), Unwanted("Unwanted");
private final String label;
CommentRequirement(String theLabel) {
label = theLabel;
}
}
// pre-filled builder
private static GenericPropertyBuilder<CommentRequirement> requirementPropertyBuilder(String name, String commentType) {
DESCRIPTOR_NAME_TO_COMMENT_TYPE.put(name, commentType);
return PropertyFactory.enumProperty(name, CommentRequirement.class, cr -> cr.label)
.desc(commentType + ". Possible values: " + CollectionUtil.map(CommentRequirement.values(), cr -> cr.label))
.defaultValue(CommentRequirement.Required);
}
}