diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index f6853072df..1a217974fd 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -43,6 +43,9 @@ The remaining section describes the complete release notes for 7.0.0. #### Fixed issues +* java-codestyle + * [#4578](https://github.com/pmd/pmd/issues/4578): \[java] CommentDefaultAccessModifier comment needs to be before annotation if present + #### API Changes #### External Contributions @@ -558,6 +561,7 @@ Language specific fixes: * [#4511](https://github.com/pmd/pmd/issues/4511): \[java] LocalVariableCouldBeFinal shouldn't report unused variables * [#4512](https://github.com/pmd/pmd/issues/4512): \[java] MethodArgumentCouldBeFinal shouldn't report unused parameters * [#4557](https://github.com/pmd/pmd/issues/4557): \[java] UnnecessaryImport FP with static imports of overloaded methods + * [#4578](https://github.com/pmd/pmd/issues/4578): \[java] CommentDefaultAccessModifier comment needs to be before annotation if present * java-design * [#1014](https://github.com/pmd/pmd/issues/1014): \[java] LawOfDemeter: False positive with lambda expression * [#1605](https://github.com/pmd/pmd/issues/1605): \[java] LawOfDemeter: False positive for standard UTF-8 charset name diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/JavaComment.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/JavaComment.java index d88681e3ab..d2655ae5e0 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/JavaComment.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/JavaComment.java @@ -132,16 +132,27 @@ public class JavaComment implements Reportable { return line.subSequence(subseqFrom, line.length()).trim(); } - private static Stream getSpecialCommentsIn(JjtreeNode node) { + private static Stream getSpecialTokensIn(JjtreeNode node) { return GenericToken.streamRange(node.getFirstToken(), node.getLastToken()) .flatMap(it -> IteratorUtil.toStream(GenericToken.previousSpecials(it).iterator())); } public static Stream getLeadingComments(JavaNode node) { + Stream specialTokens; + if (node instanceof AccessNode) { node = ((AccessNode) node).getModifiers(); + specialTokens = getSpecialTokensIn(node); + + // if this was a non-implicit empty modifier node, we should also consider comments immediately after + if (!node.getFirstToken().isImplicit()) { + specialTokens = Stream.concat(specialTokens, getSpecialTokensIn(node.getNextSibling())); + } + } else { + specialTokens = getSpecialTokensIn(node); } - return getSpecialCommentsIn(node).filter(JavaComment::isComment) + + return specialTokens.filter(JavaComment::isComment) .map(JavaComment::toComment); } diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/CommentDefaultAccessModifier.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/CommentDefaultAccessModifier.xml index 6a42aa94f3..8ce22f0ebe 100755 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/CommentDefaultAccessModifier.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/CommentDefaultAccessModifier.xml @@ -564,6 +564,22 @@ class C { @AfterSuite void afterSuite() {} +} + ]]> + + + + #4578 failure with comment after annotation + 0 +