Merge pull request #4591 from Monits:fix-4578

[java] Fix #4578 - CommentDefaultAccessModifier comment needs to be before annotation if present #4591
This commit is contained in:
Andreas Dangel 2023-10-06 19:43:57 +02:00
commit e31c77c101
No known key found for this signature in database
GPG Key ID: 93450DF2DF9A3FA3
3 changed files with 33 additions and 2 deletions

View File

@ -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

View File

@ -132,16 +132,27 @@ public class JavaComment implements Reportable {
return line.subSequence(subseqFrom, line.length()).trim();
}
private static Stream<JavaccToken> getSpecialCommentsIn(JjtreeNode<?> node) {
private static Stream<JavaccToken> getSpecialTokensIn(JjtreeNode<?> node) {
return GenericToken.streamRange(node.getFirstToken(), node.getLastToken())
.flatMap(it -> IteratorUtil.toStream(GenericToken.previousSpecials(it).iterator()));
}
public static Stream<JavaComment> getLeadingComments(JavaNode node) {
Stream<JavaccToken> 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);
}

View File

@ -564,6 +564,22 @@ class C {
@AfterSuite
void afterSuite() {}
}
]]></code>
</test-code>
<test-code>
<description>#4578 failure with comment after annotation</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Test {
@SuppressWarnings("")
/* package */ void test1() {
}
/* package */ @SuppressWarnings("")
void test2() {
}
}
]]></code>
</test-code>