From 89e7d0433a4acedd1f0fd539a7b670b111a2d2fb Mon Sep 17 00:00:00 2001 From: Damian Techeira Date: Wed, 22 Jul 2015 14:45:20 -0300 Subject: [PATCH 1/2] Add default access modifier as comment rule Summary: Add rule to look for methods and fields that have a default access modifier Test Plan: mvn test Reviewers: jmsotuyo Reviewed By: jmsotuyo Differential Revision: http://ph.monits.com/D11936 --- .../CommentDefaultAccessModifierRule.java | 92 ++++++++++++ .../main/resources/rulesets/java/comments.xml | 47 ++++++ .../java/rule/comments/CommentRulesTest.java | 1 + .../xml/CommentDefaultAccessModifier.xml | 135 ++++++++++++++++++ 4 files changed, 275 insertions(+) create mode 100644 pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/comments/CommentDefaultAccessModifierRule.java create mode 100755 pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/comments/xml/CommentDefaultAccessModifier.xml diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/comments/CommentDefaultAccessModifierRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/comments/CommentDefaultAccessModifierRule.java new file mode 100644 index 0000000000..d34ee9454b --- /dev/null +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/comments/CommentDefaultAccessModifierRule.java @@ -0,0 +1,92 @@ +/** + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ +package net.sourceforge.pmd.lang.java.rule.comments; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration; +import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit; +import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration; +import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration; +import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator; +import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId; +import net.sourceforge.pmd.lang.java.ast.AbstractJavaAccessNode; +import net.sourceforge.pmd.lang.java.ast.Comment; +import net.sourceforge.pmd.lang.rule.properties.StringProperty; + +/** + * Check for Methods, Fields and Nested Classes that have a default access modifier + * + * @author Damián Techeira + */ +public class CommentDefaultAccessModifierRule extends AbstractCommentRule { + + private static final StringProperty REGEX_DESCRIPTOR = new StringProperty("regex", "Regular expression", "", 1.0f); + private static final String MESSAGE = "To avoid mistakes add a comment " + + "at the beginning of the %s %s if you want a default access modifier"; + private final Set interestingLineNumberComments = new HashSet(); + + public CommentDefaultAccessModifierRule() { + definePropertyDescriptor(REGEX_DESCRIPTOR); + } + + public CommentDefaultAccessModifierRule(final String regex) { + this(); + setRegex(regex); + } + + public void setRegex(final String regex) { + setProperty(CommentDefaultAccessModifierRule.REGEX_DESCRIPTOR, regex); + } + + @Override + public Object visit(final ASTCompilationUnit node, final Object data) { + interestingLineNumberComments.clear(); + final List comments = node.getComments(); + for (final Comment comment : comments) { + if (comment.getImage().matches(getProperty(REGEX_DESCRIPTOR).trim())) { + interestingLineNumberComments.add(comment.getBeginLine()); + } + } + return super.visit(node, data); + } + + @Override + public Object visit(final ASTMethodDeclaration decl, final Object data) { + if (shouldReport(decl)) { + addViolationWithMessage(data, decl, String.format(MESSAGE, + decl.getFirstChildOfType(ASTMethodDeclarator.class).getImage(), "method")); + } + return super.visit(decl, data); + } + + @Override + public Object visit(final ASTFieldDeclaration decl, final Object data) { + if (shouldReport(decl)) { + addViolationWithMessage(data, decl, String.format(MESSAGE, + decl.getFirstDescendantOfType(ASTVariableDeclaratorId.class).getImage(), "field")); + } + return super.visit(decl, data); + } + + @Override + public Object visit(final ASTClassOrInterfaceDeclaration decl, final Object data) { + // check for nested classes + if (decl.isNested() && shouldReport(decl)) { + addViolationWithMessage(data, decl, String.format(MESSAGE, decl.getImage(), "nested class")); + } + return super.visit(decl, data); + } + + private boolean shouldReport(final AbstractJavaAccessNode decl) { + // ignore if is a Interface + return !decl.getParentsOfType(ASTClassOrInterfaceDeclaration.class).get(0).isInterface() + // check if the field/method/nested class has a default access modifier + && decl.isPackagePrivate() + // if is a default access modifier check if there is a comment in this line + && !interestingLineNumberComments.contains(decl.getBeginLine()); + } +} \ No newline at end of file diff --git a/pmd-java/src/main/resources/rulesets/java/comments.xml b/pmd-java/src/main/resources/rulesets/java/comments.xml index 7965200836..9b9a2c03fa 100755 --- a/pmd-java/src/main/resources/rulesets/java/comments.xml +++ b/pmd-java/src/main/resources/rulesets/java/comments.xml @@ -76,5 +76,52 @@ A rule for the politically correct... we don't want to offend anyone. + + + + + 3 + + + + + + + + + + + \ No newline at end of file diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/comments/CommentRulesTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/comments/CommentRulesTest.java index 7e6a22a9b7..87822c7099 100755 --- a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/comments/CommentRulesTest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/comments/CommentRulesTest.java @@ -14,5 +14,6 @@ public class CommentRulesTest extends SimpleAggregatorTst { addRule(RULESET, "CommentRequired"); addRule(RULESET, "CommentSize"); addRule(RULESET, "CommentContent"); + addRule(RULESET, "CommentDefaultAccessModifier"); } } diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/comments/xml/CommentDefaultAccessModifier.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/comments/xml/CommentDefaultAccessModifier.xml new file mode 100755 index 0000000000..094a335de2 --- /dev/null +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/comments/xml/CommentDefaultAccessModifier.xml @@ -0,0 +1,135 @@ + + + + + + Some methods and Fields with default access modifier in a class + 2 + + + + + + + All methods and Field with default access modifier in a class + 6 + + + + + + + All methods and Field without default access modifier in a class + 0 + + + + + + Methods with default access modifier in an Interface + 0 + + + + + + Nested classes with default access modifier + 2 + + + + + + Test own regex to default access modifier rule + \/\*\s+package-private\s+\*\/ + 3 + + + \ No newline at end of file From f963f76fd4849ea9048f01ff356df0c366ab0f69 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Sun, 26 Jul 2015 19:20:47 +0200 Subject: [PATCH 2/2] Update changelog, improve test for new CommentDefaultAccessModifier rule --- pmd-core/src/main/resources/rulesets/releases/540.xml | 1 + pmd-java/src/main/resources/rulesets/java/comments.xml | 2 +- .../java/rule/comments/xml/CommentDefaultAccessModifier.xml | 4 ++++ src/site/markdown/overview/changelog.md | 4 ++++ 4 files changed, 10 insertions(+), 1 deletion(-) diff --git a/pmd-core/src/main/resources/rulesets/releases/540.xml b/pmd-core/src/main/resources/rulesets/releases/540.xml index 1f8ac75d21..2d703b2286 100644 --- a/pmd-core/src/main/resources/rulesets/releases/540.xml +++ b/pmd-core/src/main/resources/rulesets/releases/540.xml @@ -8,6 +8,7 @@ This ruleset contains links to rules that are new in PMD v5.4.0 + diff --git a/pmd-java/src/main/resources/rulesets/java/comments.xml b/pmd-java/src/main/resources/rulesets/java/comments.xml index 9b9a2c03fa..a1ba9cd2e7 100755 --- a/pmd-java/src/main/resources/rulesets/java/comments.xml +++ b/pmd-java/src/main/resources/rulesets/java/comments.xml @@ -77,7 +77,7 @@ A rule for the politically correct... we don't want to offend anyone. diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/comments/xml/CommentDefaultAccessModifier.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/comments/xml/CommentDefaultAccessModifier.xml index 094a335de2..bfe309f774 100755 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/comments/xml/CommentDefaultAccessModifier.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/comments/xml/CommentDefaultAccessModifier.xml @@ -24,6 +24,7 @@ public class Foo { Some methods and Fields with default access modifier in a class 2 + 3,10 @@ -47,6 +48,7 @@ public class Foo { All methods and Field with default access modifier in a class 6 + 2,3,4,6,9,12 @@ -106,6 +108,7 @@ public class Foo { Nested classes with default access modifier 2 + 7,10 @@ -130,6 +133,7 @@ public class Foo { Test own regex to default access modifier rule \/\*\s+package-private\s+\*\/ 3 + 2,4,13 \ No newline at end of file diff --git a/src/site/markdown/overview/changelog.md b/src/site/markdown/overview/changelog.md index 1d9a17280f..e55f0a03ec 100644 --- a/src/site/markdown/overview/changelog.md +++ b/src/site/markdown/overview/changelog.md @@ -29,6 +29,9 @@ the minimum required length of a variable name. * Modified Rule: rulesets/java/naming.xml/ShortMethodName: Additional property `minimum` to configure the minimum required length of a method name. +* New Rule: rulesets/java/comments.xml/CommentDefaultAccessModifier: In order to avoid mistakes with + forgotten access modifiers for methods, this rule ensures, that you explicitly mark the usage of the + default access modifier by placing a comment. **Pull Requests:** @@ -37,6 +40,7 @@ * [#54](https://github.com/pmd/pmd/pull/54): Add a new rulesets for Maven's POM rules * [#55](https://github.com/pmd/pmd/pull/55): Fix run.sh for paths with spaces * [#56](https://github.com/pmd/pmd/pull/56): Adding support for WSDL rules +* [#57](https://github.com/pmd/pmd/pull/57): Add default access modifier as comment rule **Bugfixes:**