-
Notifications
You must be signed in to change notification settings - Fork 578
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix for detector
UncallableMethodOfAnonymousClass
to not report unu…
…sed methods of method-local enumerations and records (#2170) * Fix for detector `UncallableMethodOfAnonymousClass` to not report unused 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 ([#2120](#2120)) * Fix the test case * Solution changed to only disregard auto-generated methods of enums and records Co-authored-by: Kengo TODA <skypencil@gmail.com>
- Loading branch information
1 parent
96d50b5
commit 119956b
Showing
5 changed files
with
65 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
spotbugs-tests/src/test/java/edu/umd/cs/findbugs/detect/Issue2120Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package edu.umd.cs.findbugs.detect; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import edu.umd.cs.findbugs.AbstractIntegrationTest; | ||
import edu.umd.cs.findbugs.test.matcher.BugInstanceMatcher; | ||
import edu.umd.cs.findbugs.test.matcher.BugInstanceMatcherBuilder; | ||
|
||
import static edu.umd.cs.findbugs.test.CountMatcher.containsExactly; | ||
import static org.junit.Assume.assumeFalse; | ||
import static org.junit.Assume.assumeThat; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.number.OrderingComparison.greaterThanOrEqualTo; | ||
|
||
public class Issue2120Test extends AbstractIntegrationTest { | ||
@Before | ||
public void verifyJavaVersion() { | ||
assumeFalse(System.getProperty("java.specification.version").startsWith("1.")); | ||
int javaVersion = Integer.parseInt(System.getProperty("java.specification.version")); | ||
assumeThat(javaVersion, is(greaterThanOrEqualTo(11))); | ||
} | ||
|
||
@Test | ||
public void test() { | ||
performAnalysis("../java14/Issue2120.class", | ||
"../java14/Issue2120$1MyEnum.class", | ||
"../java14/Issue2120$1MyRecord.class"); | ||
BugInstanceMatcher matcher = new BugInstanceMatcherBuilder() | ||
.bugType("UMAC_UNCALLABLE_METHOD_OF_ANONYMOUS_CLASS").build(); | ||
assertThat(getBugCollection(), containsExactly(0, matcher)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
public class Issue2120 { | ||
static void method() { | ||
record MyRecord(int from, int to) { /**/ } | ||
|
||
enum MyEnum { | ||
RED, BLUE; | ||
} | ||
|
||
System.out.println(new MyRecord(1, 2)); | ||
System.out.println(MyEnum.RED); | ||
} | ||
} |