-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[java] Cleanup handling of Java comments #4237
Conversation
Generated by 🚫 Danger |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
I'll update the release notes about the changed properties of rule CommentContent and merge it.
(My other comments are just informational and outside of the scope of this PR)
* Wraps a comment token to provide some utilities. | ||
* This is not a node, it's not part of the tree anywhere, | ||
* just convenient. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think, this would make sense at some point in the future? To add the comments as nodes to the tree?
@SuppressWarnings("PMD.LiteralsFirstInComparisons") // a fp | ||
public static boolean isMarkupWord(Chars word) { | ||
return word.length() <= 3 | ||
&& (word.contentEquals("*") | ||
|| word.contentEquals("//") | ||
|| word.contentEquals("/*") | ||
|| word.contentEquals("*/") | ||
|| word.contentEquals("/**")); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to understand, why this is a FP...
is it, because you would get a NP anyway with word.length()
, so putting the literals first in contentEquals doesn't make sense here? (or in other words: word
can't be null when calling contentEquals
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know if that's relevant anymore, I wrote this code before we updated the rule in pmd 7. The problem at the time was that the rule saw a method name that ends with equals (contentEquals) and reported the method call. However the literal and the word have different types, so swapping the operands would call String::contentEquals, when the method I want to call is Chars::contentEquals.
NON_REGEX_PROPERTIES.add(CASE_SENSITIVE_DESCRIPTOR); | ||
} | ||
private static final PropertyDescriptor<Pattern> DISSALLOWED_TERMS_DESCRIPTOR = | ||
regexProperty("forbiddenRegex") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should add a note in 7_0_0_release_notes.md under "Changed Rules": For "CommentContent" the properties "caseSensitive" and "disallowedTerms" are replaced by the regex property "fobiddenRegex".
[java] Cleanup handling of Java comments #4237
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll open a followup PR for these comments
int i = 0; | ||
for (Chars line : comment.getFilteredLines(true)) { | ||
if (violationRegex.matcher(line).find()) { | ||
lines.add(i); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks like this doesn't work, i
is never incremented.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
private boolean shouldReportTypeDeclaration(ASTAnyTypeDeclaration decl) { | ||
// don't report on interfaces | ||
return !decl.isRegularInterface() | ||
return !(decl.isRegularInterface() && !decl.isAnnotation()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this could be replaced with !decl.isInterface()
. I don't think it makes sense to whitelist interfaces though. If they have package visibility then they should be reported. Maybe this was originally an attempt to whitelist interface members, because they're are all public anyway...
@@ -63,10 +63,10 @@ public Sequence call(XPathContext context, Sequence[] arguments) { | |||
int codeBeginLine = contextNode.getBeginLine(); | |||
int codeEndLine = contextNode.getEndLine(); | |||
|
|||
List<Comment> commentList = contextNode.getFirstParentOfType(ASTCompilationUnit.class).getComments(); | |||
for (Comment comment : commentList) { | |||
List<JavaComment> commentList = contextNode.getFirstParentOfType(ASTCompilationUnit.class).getComments(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this could use getRoot
int i = 0; | ||
for (Chars line : comment.getFilteredLines(true)) { | ||
if (line.length() > maxLength) { | ||
indices.add(i); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here
Describe the PR
Related issues
Ready?
./mvnw clean verify
passes (checked automatically by github actions)