From e342bfd7f8d23ea8cfba6dc3bf26c2d8943da36a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Mart=C3=ADn=20Sotuyo=20Dodero?= Date: Mon, 5 Jun 2023 19:49:26 -0300 Subject: [PATCH 1/4] Add failing use case for #4578 --- .../xml/CommentDefaultAccessModifier.xml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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 + From d1daf9e3fd061256191530fb7e8de23e35ac09af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Mart=C3=ADn=20Sotuyo=20Dodero?= Date: Mon, 5 Jun 2023 19:49:53 -0300 Subject: [PATCH 2/4] Allow comment parsing to include comments after MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - This is safe even for the last token - I don't love the semantics of this… get leading comments vs token in, and actually allowing tokens "around"… a lot of this is for the AccessNode special case and what we consider "leading" to be for those --- .../net/sourceforge/pmd/lang/java/ast/JavaComment.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) 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..b4afdfbf77 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,8 +132,9 @@ public class JavaComment implements Reportable { return line.subSequence(subseqFrom, line.length()).trim(); } - private static Stream getSpecialCommentsIn(JjtreeNode node) { - return GenericToken.streamRange(node.getFirstToken(), node.getLastToken()) + private static Stream getSpecialTokensIn(JjtreeNode node) { + // Consider one more token to include also comments immediately after the node + return GenericToken.streamRange(node.getFirstToken(), node.getLastToken().getNext()) .flatMap(it -> IteratorUtil.toStream(GenericToken.previousSpecials(it).iterator())); } @@ -141,7 +142,7 @@ public class JavaComment implements Reportable { if (node instanceof AccessNode) { node = ((AccessNode) node).getModifiers(); } - return getSpecialCommentsIn(node).filter(JavaComment::isComment) + return getSpecialTokensIn(node).filter(JavaComment::isComment) .map(JavaComment::toComment); } From 1d6f0a43b53af29d200f049fa077a978562a9d36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Mart=C3=ADn=20Sotuyo=20Dodero?= Date: Mon, 5 Jun 2023 23:02:59 -0300 Subject: [PATCH 3/4] Be more explicit as to when we want to extend the range --- .../pmd/lang/java/ast/JavaComment.java | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) 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 b4afdfbf77..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 @@ -133,16 +133,26 @@ public class JavaComment implements Reportable { } private static Stream getSpecialTokensIn(JjtreeNode node) { - // Consider one more token to include also comments immediately after the node - return GenericToken.streamRange(node.getFirstToken(), node.getLastToken().getNext()) + 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 getSpecialTokensIn(node).filter(JavaComment::isComment) + + return specialTokens.filter(JavaComment::isComment) .map(JavaComment::toComment); } From e9ee4a94c601e3d490e6cfa4699b22ff4d9691a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Mart=C3=ADn=20Sotuyo=20Dodero?= Date: Mon, 5 Jun 2023 23:05:07 -0300 Subject: [PATCH 4/4] Update changelog, refs #4578 --- docs/pages/release_notes.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index 3e25409b69..c76f9c9c0f 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -45,6 +45,8 @@ The remaining section describes the complete release notes for 7.0.0. * [#4582](https://github.com/pmd/pmd/issues/4582): \[dist] Download link broken * java * [#4401](https://github.com/pmd/pmd/issues/4401): \[java] PMD 7 fails to build under Java 19 +* java-codestyle + * [#4578](https://github.com/pmd/pmd/issues/4578): \[java] CommentDefaultAccessModifier comment needs to be before annotation if present #### API Changes