forked from spotbugs/spotbugs
-
Notifications
You must be signed in to change notification settings - Fork 0
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 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))
- Loading branch information
Ádám Balogh
committed
Sep 9, 2022
1 parent
b119457
commit aecf8b7
Showing
5 changed files
with
54 additions
and
0 deletions.
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
32 changes: 32 additions & 0 deletions
32
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,32 @@ | ||
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"); | ||
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); | ||
} | ||
} |