Skip to content
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

Merged
merged 31 commits into from
Dec 1, 2022

Conversation

oowekyala
Copy link
Member

Describe the PR

  • Clarify how java comments are handled.
    • The classes MultilineComment and SingleLineComment are removed as they added no API
    • The class Comment is renamed to JavaComment and is a concrete class
    • The class FormalComment is renamed JavadocComment and extends JavaComment
  • Java rules that look at comments now take advantage of Chars so as not to copy strings unnecessarily

Related issues

Ready?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Complete build ./mvnw clean verify passes (checked automatically by github actions)
  • Added (in-code) documentation (if needed)

@oowekyala oowekyala added this to the 7.0.0 milestone Nov 26, 2022
@oowekyala oowekyala added this to In progress in PMD 7 via automation Nov 26, 2022
@oowekyala oowekyala mentioned this pull request Nov 26, 2022
6 tasks
@pmd-test
Copy link

2 Messages
📖 Compared to pmd/7.0.x:
This changeset changes 27355 violations,
introduces 21 new violations, 1721 new errors and 0 new configuration errors,
removes 55071 violations, 1 errors and 0 configuration errors.
Full report
📖 Compared to master:
This changeset changes 49604 violations,
introduces 33956 new violations, 1729 new errors and 0 new configuration errors,
removes 194304 violations, 4 errors and 7 configuration errors.
Full report

Generated by 🚫 Danger

@adangel adangel added the in:grammar About the grammar of a lexer or parser, eg, a parse/lex exception label Dec 1, 2022
Copy link
Member

@adangel adangel left a 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)

Comment on lines +19 to +21
* Wraps a comment token to provide some utilities.
* This is not a node, it's not part of the tree anywhere,
* just convenient.
Copy link
Member

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?

Comment on lines +105 to +113
@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("/**"));
}
Copy link
Member

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)

Copy link
Member Author

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")
Copy link
Member

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".

adangel added a commit that referenced this pull request Dec 1, 2022
adangel added a commit that referenced this pull request Dec 1, 2022
[java] Cleanup handling of Java comments #4237
@adangel adangel merged commit 49c7f42 into pmd:pmd/7.0.x Dec 1, 2022
PMD 7 automation moved this from In progress to Done Dec 1, 2022
@oowekyala oowekyala deleted the text-utils-comments branch December 1, 2022 17:18
Copy link
Member Author

@oowekyala oowekyala left a 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);
Copy link
Member Author

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.

Copy link
Member

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())
Copy link
Member Author

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();
Copy link
Member Author

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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

@adangel adangel mentioned this pull request Jan 23, 2023
55 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in:grammar About the grammar of a lexer or parser, eg, a parse/lex exception
Projects
No open projects
PMD 7
  
Done
Development

Successfully merging this pull request may close these issues.

None yet

3 participants