Merge branch 'bug-1434'

This commit is contained in:
Andreas Dangel
2015-12-03 20:17:22 +01:00
3 changed files with 79 additions and 1 deletions

View File

@ -59,12 +59,17 @@ public class CommentRequiredRule extends AbstractCommentRule {
"enumCommentRequirement", "Enum comments. Possible values: " + Arrays.toString(CommentRequirement.values()),
CommentRequirement.labels(), CommentRequirement.values(), 0, 5.0f);
public static final EnumeratedProperty<CommentRequirement> SERIAL_VERSION_UID_CMT_REQUIREMENT_DESCRIPTOR = new EnumeratedProperty<CommentRequirement>(
"serialVersionUIDCommentRequired", "serial version UID commts. Possible values: " + Arrays.toString(CommentRequirement.values()),
CommentRequirement.labels(), CommentRequirement.values(), 1, 6.0f);
public CommentRequiredRule() {
definePropertyDescriptor(HEADER_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);
}
private CommentRequirement getCommentRequirement(String label) {
@ -167,7 +172,11 @@ public class CommentRequiredRule extends AbstractCommentRule {
CommentRequirement fieldRequirement = getCommentRequirement(getProperty(
FIELD_CMT_REQUIREMENT_DESCRIPTOR).toString());
if (fieldRequirement != CommentRequirement.Ignored) {
if (isSerialVersionUID(decl)) {
checkSerialVersionUID(decl, data, fieldRequirement);
} else
if (fieldRequirement == CommentRequirement.Required) {
if (decl.comment() == null) {
addViolationWithMessage(data, decl,
@ -188,6 +197,36 @@ public class CommentRequiredRule extends AbstractCommentRule {
return super.visit(decl, data);
}
private void checkSerialVersionUID(ASTFieldDeclaration decl, Object data, CommentRequirement fieldRequirement) {
CommentRequirement serialVersionUIDReq = getCommentRequirement(getProperty(SERIAL_VERSION_UID_CMT_REQUIREMENT_DESCRIPTOR).toString());
if (serialVersionUIDReq != CommentRequirement.Ignored) {
if (fieldRequirement == CommentRequirement.Required) {
if (decl.comment() == null) {
addViolationWithMessage(data, decl,
SERIAL_VERSION_UID_CMT_REQUIREMENT_DESCRIPTOR.name() + " "
+ CommentRequirement.Required,
decl.getBeginLine(), decl.getEndLine());
}
} else {
if (decl.comment() != null) {
addViolationWithMessage(data, decl,
SERIAL_VERSION_UID_CMT_REQUIREMENT_DESCRIPTOR.name() + " "
+ CommentRequirement.Unwanted,
decl.getBeginLine(), decl.getEndLine());
}
}
}
}
private boolean isSerialVersionUID(ASTFieldDeclaration field) {
if ("serialVersionUID".equals(field.getVariableName())
&& field.isStatic() && field.isFinal()
&& field.getType() == long.class) {
return true;
}
return false;
}
@Override
public Object visit(ASTEnumDeclaration decl, Object data) {
@ -227,7 +266,8 @@ public class CommentRequiredRule extends AbstractCommentRule {
return getProperty(HEADER_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(PROT_METHOD_CMT_REQUIREMENT_DESCRIPTOR) == CommentRequirement.Ignored
&& getProperty(SERIAL_VERSION_UID_CMT_REQUIREMENT_DESCRIPTOR) == CommentRequirement.Ignored;
}
/**

View File

@ -116,6 +116,40 @@ public class CommentRequired {
}
};
}
}
]]></code>
</test-code>
<test-code>
<description>#1434 CommentRequired raises violation on serialVersionUID field</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
/** The class comment */
public class CommentRequired implements Serializable {
private static final long serialVersionUID = 42L;
}
]]></code>
</test-code>
<test-code>
<description>comment required on serialVersionUID of wrong type</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>3</expected-linenumbers>
<code><![CDATA[
/** The class comment */
public class CommentRequired implements Serializable {
private static final int serialVersionUID = 42L;
}
]]></code>
</test-code>
<test-code>
<description>serialVersionUID comment required</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>3</expected-linenumbers>
<rule-property name="serialVersionUIDCommentRequired">Required</rule-property>
<code><![CDATA[
/** The class comment */
public class CommentRequired implements Serializable {
private static final long serialVersionUID = 42L;
}
]]></code>
</test-code>

View File

@ -13,6 +13,8 @@
* Java
* Logging Java: **InvalidSlf4jMessageFormat** (rulesets/java/logging-java.xml/InvalidSlf4jMessageFormat)<br/>
Check for invalid message format in slf4j loggers.
* java-comments/CommentRequired: New property `serialVersionUIDCommentRequired` which controls the comment requirements
for *serialVersionUID* fields. By default, no comment is required for this field.
**Pull Requests:**
@ -30,6 +32,8 @@
* java-comments/CommentDefaultAccessModifier
* [#1430](https://sourceforge.net/p/pmd/bugs/1430/): CommentDefaultAccessModifier triggers on field
annotated with @VisibleForTesting
* java-comments/CommentRequired
* [#1434](https://sourceforge.net/p/pmd/bugs/1434/): CommentRequired raises violation on serialVersionUID field
* java-design/UseNotifyAllInsteadOfNotify
* [#1438](https://sourceforge.net/p/pmd/bugs/1438/): UseNotifyAllInsteadOfNotify gives false positive
* java-finalizers/AvoidCallingFinalize