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

Fix for detector UncallableMethodOfAnonymousClass to not report unused methods of method-local enumerations and records #2170

Merged
merged 6 commits into from
Oct 11, 2022

Conversation

baloghadamsoftware
Copy link
Contributor

During compilation, enumerations and records are turned into Java classes with some auto-generated methods. Some of these methods are called by the code but some of them not. However, this is not a programming error or bad practice, these methods must not be considered as dead code because their generation cannot be prevented. Therefore no bug report UMAC_UNCALLABLE_METHOD_OF_ANONYMOUS_CLASS must be issued to such classes even if they are defined inside a method. See issue (#2120)


Make sure these boxes are checked before submitting your PR -- thank you!

  • Added an entry into CHANGELOG.md if you have changed SpotBugs code

…sed methods of method-local enumerations and records

During compilation, enumerations and records are turned into Java classes with some auto-generated methods. Some of these methods are called by the code but some of them not. However, this is not a programming error or bad practice, these methods must not be considered as dead code because their generation cannot be prevented. Therefore no bug report `UMAC_UNCALLABLE_METHOD_OF_ANONYMOUS_CLASS` must be issued to such classes even if they are defined inside a method. See issue ([spotbugs#2120](spotbugs#2120))
Copy link
Member

@KengoTODA KengoTODA left a comment

Choose a reason for hiding this comment

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

From next, please consider to make two commits in one PR: one is only for failing test cases, and next is fix with documents. Then we can use GitHub Actions to verify that test and fix are enough or not.

Comment on lines 62 to 66
if (Values.DOTTED_JAVA_LANG_ENUM.equals(obj.getSuperclassName()) ||
Values.DOTTED_JAVA_LANG_RECORD.equals(obj.getSuperclassName())) {
return;
}

Copy link
Member

Choose a reason for hiding this comment

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

The test passes even when I revert this change. I assume that the test case is not enough to reproduce the reported problem.

$ ./gradlew clean spotbugs-tests:test --tests=Issue2120Test

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for noticing this. I forgot to add the inner classes to the test. (I always do the tests first, run them manually to see them fail, and then I do the fix.)

@baloghadamsoftware
Copy link
Contributor Author

From next, please consider to make two commits in one PR: one is only for failing test cases, and next is fix with documents. Then we can use GitHub Actions to verify that test and fix are enough or not.

Good idea! Thank you! I will do as you suggested.

@@ -59,6 +59,11 @@ public UncallableMethodOfAnonymousClass(BugReporter bugReporter) {

@Override
public void visitJavaClass(JavaClass obj) {
if (Values.DOTTED_JAVA_LANG_ENUM.equals(obj.getSuperclassName()) ||
Values.DOTTED_JAVA_LANG_RECORD.equals(obj.getSuperclassName())) {
return;
Copy link
Contributor

Choose a reason for hiding this comment

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

There could be extra methods added to an Enum that would validly benefit from the rule, though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, you are right. I will change this to only disregard auto-generated methods.

Copy link
Contributor

@ThrawnCA ThrawnCA left a comment

Choose a reason for hiding this comment

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

Changelog has a conflict.

@baloghadamsoftware baloghadamsoftware requested review from ThrawnCA and KengoTODA and removed request for ThrawnCA and KengoTODA September 21, 2022 10:32
ThrawnCA
ThrawnCA previously approved these changes Sep 26, 2022
@baloghadamsoftware
Copy link
Contributor Author

No new false positives or crashes on some open-source projects.

Copy link
Member

@KengoTODA KengoTODA left a comment

Choose a reason for hiding this comment

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

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants