From 86501e6bc0bf9c2ab28d689849bb50e7813a3363 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Mart=C3=ADn=20Sotuyo=20Dodero?= Date: Wed, 2 Aug 2017 23:56:34 -0300 Subject: [PATCH 1/2] [java] CommentDefaultAccessModifierRule should consider constructors too - Resolves #536 --- .../CommentDefaultAccessModifierRule.java | 9 ++++++++ .../main/resources/rulesets/java/comments.xml | 6 ++--- .../xml/CommentDefaultAccessModifier.xml | 22 +++++++++++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) 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 index a219be226..9ea78854e 100644 --- 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 @@ -12,6 +12,7 @@ import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBodyDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit; +import net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator; @@ -86,6 +87,14 @@ public Object visit(final ASTClassOrInterfaceDeclaration decl, final Object data return super.visit(decl, data); } + @Override + public Object visit(final ASTConstructorDeclaration decl, Object data) { + if (shouldReport(decl)) { + addViolationWithMessage(data, decl, String.format(MESSAGE, decl.getImage(), "constructor")); + } + return super.visit(decl, data); + } + private boolean shouldReport(final AbstractJavaAccessNode decl) { List parentClassOrInterface = decl .getParentsOfType(ASTClassOrInterfaceDeclaration.class); diff --git a/pmd-java/src/main/resources/rulesets/java/comments.xml b/pmd-java/src/main/resources/rulesets/java/comments.xml index 9462a4862..42dad25e1 100755 --- a/pmd-java/src/main/resources/rulesets/java/comments.xml +++ b/pmd-java/src/main/resources/rulesets/java/comments.xml @@ -82,9 +82,9 @@ A rule for the politically correct... we don't want to offend anyone. message="Missing commented default access modifier" externalInfoUrl="${pmd.website.baseurl}/rules/java/comments.html#CommentDefaultAccessModifier"> - To avoid mistakes if we want that a Method, Field or Nested class have a default access modifier - we must add a comment at the beginning of the Method, Field or Nested class. - By default the comment must be /* default */, if you want another, you have to provide a regex. + To avoid mistakes if we want that a Method, Constructor, Field or Nested class have a default access modifier + we must add a comment at the beginning of it's declaration. + By default the comment must be /* default */, if you want another, you have to provide a regexp. 3 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 4b8474bef..ee56ce392 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 @@ -155,6 +155,28 @@ public enum TestEnum { + + + + #536 Constructor with default access modifier should trigger + 1 + + + + + #536 Enum constructor with implicit private modifier should not trigger + 0 + From 0b2389c7989b550148b8ae5bd63b7daca85b2477 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Mart=C3=ADn=20Sotuyo=20Dodero?= Date: Wed, 2 Aug 2017 23:58:57 -0300 Subject: [PATCH 2/2] Update changelog, refs #536 --- src/site/markdown/overview/changelog.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/site/markdown/overview/changelog.md b/src/site/markdown/overview/changelog.md index 2c02b85ce..d16381439 100644 --- a/src/site/markdown/overview/changelog.md +++ b/src/site/markdown/overview/changelog.md @@ -91,6 +91,8 @@ and include them to such reports. * [#487](https://github.com/pmd/pmd/pull/487): \[java] Fix typeresolution for anonymous extending object * [#496](https://github.com/pmd/pmd/issues/496): \[java] processing error on generics inherited from enclosing class * [#527](https://github.com/pmd/pmd/issues/527): \[java] Lombok getter annotation on enum is not recognized correctly +* java-comments + * [#536](https://github.com/pmd/pmd/issues/536): \[java] CommentDefaultAccessModifierRule ignores constructors * java-controversial * [#408](https://github.com/pmd/pmd/issues/408): \[java] DFA not analyzing asserts * java-sunsecure