GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [java] NPE in UseCollectionIsEmptyRule with enums by foxmason · Pull Request #2968 · pmd/pmd

Conversation

@foxmason

@foxmason foxmason commented Dec 12, 2020

Copy link
Copy Markdown
Contributor

Describe the PR

The UseCollectionIsEmptyRule requires finding the first parent of the class ASTClassOrInterfaceBody when searching for the type of a variable by name. In the case of an enum, the call to fetch the parent returns null, resulting in a NPE. This was fixed by checking for null and finding a parent of type ASTEnumBody instead.

I also refactored, cleared-up, and formatted a few test cases.

Related issues

  • Fixes [java] NPE in UseCollectionIsEmptyRule with enums #2833

Ready?

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@foxmason
[java] NPE in UseCollectionIsEmptyRule with enums #2833
cce0a35
@foxmason foxmason changed the title [java] NPE in UseCollectionIsEmptyRule with enums #2833 [java] NPE in UseCollectionIsEmptyRule with enums Dec 12, 2020
@ghost

ghost commented Dec 12, 2020 •
edited by ghost
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
1 Message
📖 This changeset changes 0 violations,
introduces 3 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
Full report
✅ This changeset changes violations,
introduces new violations, new errors and new configuration errors,
removes violations, errors and configuration errors.
Full report

Generated by 🚫 Danger

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

KroArtem
KroArtem suggested changes Jan 10, 2021
View reviewed changes

@KroArtem KroArtem left a comment

Copy link
Copy Markdown
Contributor

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.

@oowekyala , @adangel , would you be able to review this one?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread ...src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/UseCollectionIsEmptyRule.java
ASTClassOrInterfaceBody classBody = expr.getFirstParentOfType(ASTClassOrInterfaceBody.class);
List<ASTVariableDeclarator> varDeclarators = classBody.findDescendantsOfType(ASTVariableDeclarator.class);
Node classOrEnumBody = expr.getFirstParentOfType(ASTClassOrInterfaceBody.class);
if (classOrEnumBody == null) {

@KroArtem KroArtem Jan 10, 2021

Copy link
Copy Markdown
Contributor

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.

Is it the only case when classOrEnumBody can be null? Unfortunately I don't know.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@oowekyala oowekyala Jan 12, 2021

Copy link
Copy Markdown
Member

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.

Technically it could also be in an annotation type, but I think it's ok, as annotations may not contain code (at least, not code that this rule would pick up on).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@oowekyala oowekyala Jan 12, 2021 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

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.

Technically it could also be in an annotation type, but I think it's ok, as annotations may not contain code (at least, not code that this rule would pick up on).

It could also be an ASTRecordBody

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@adangel adangel Jan 21, 2021

Copy link
Copy Markdown
Member

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 add a test case when merging

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread ...src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/UseCollectionIsEmptyRule.java Outdated
for (ASTVariableDeclarator varDeclarator : varDeclarators) {
if (varDeclarator.getName().equals(varName)) {
return varDeclarator.getFirstDescendantOfType(ASTClassOrInterfaceType.class);
return varDeclarator.getParent().getFirstDescendantOfType(ASTClassOrInterfaceType.class);

@KroArtem KroArtem Jan 10, 2021

Copy link
Copy Markdown
Contributor

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.

What's the reason to go one level up in the hierarchy?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@oowekyala oowekyala Jan 12, 2021

Copy link
Copy Markdown
Member

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 fetches a LocalVariableDeclaration or FieldDeclaration node. VariableDeclarator nodes never have themselves a type node child.

But the actual right way to do that is varDeclarator.getVariableId().getTypeNode().

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@oowekyala oowekyala Jan 12, 2021

Copy link
Copy Markdown
Member

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 fetches a LocalVariableDeclaration or FieldDeclaration node. VariableDeclarator nodes never have themselves a type node child.

But the actual right way to do that is varDeclarator.getVariableId().getTypeNode().

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@adangel adangel Jan 21, 2021

Copy link
Copy Markdown
Member

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 fix that when merging

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread ...test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/UseCollectionIsEmpty.xml Outdated
public enum ComponentSize {

S("s"),
M("m"),

@KroArtem KroArtem Jan 10, 2021

Copy link
Copy Markdown
Contributor

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.

Only one field is enough, IMO. Less code in xml is better :)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

adangel reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Comment thread ...test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/UseCollectionIsEmpty.xml Outdated
public enum ComponentSize {

S("s"),
M("m"),

@KroArtem KroArtem Jan 10, 2021

Copy link
Copy Markdown
Contributor

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 suggest removing several fields here too.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@KroArtem

KroArtem commented Jan 10, 2021

Copy link
Copy Markdown
Contributor

Btw, it seems like danger report is incorrect here, I'm not sure how to regenerate it.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@oowekyala oowekyala added this to the 6.31.0 milestone Jan 18, 2021
@adangel adangel self-assigned this Jan 19, 2021
@adangel
Fixups for #2968
4be2562
- Use better typeres
- Add test cases for records and local var
adangel added a commit that referenced this pull request Jan 21, 2021
@adangel
[doc] Update release notes, refs #2968, refs #2833
248d4d7
adangel added a commit that referenced this pull request Jan 21, 2021
@adangel
Merge pull request #2968 from foxmason:master
b629e26
[java] NPE in UseCollectionIsEmptyRule with enums #2968
@adangel
adangel merged commit 175d535 into pmd:master Jan 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@adangel adangel adangel left review comments
@oowekyala oowekyala oowekyala left review comments
+1 more reviewer
@KroArtem KroArtem KroArtem requested changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@adangel adangel

Labels

None yet

Projects

None yet

Milestone

6.31.0

Development

Successfully merging this pull request may close these issues.

[java] NPE in UseCollectionIsEmptyRule with enums

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@foxmason @KroArtem @adangel @oowekyala
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.