diff --git a/spotbugs-tests/src/test/java/edu/umd/cs/findbugs/detect/FindOverridableMethodCallTest.java b/spotbugs-tests/src/test/java/edu/umd/cs/findbugs/detect/FindOverridableMethodCallTest.java index a764a865f21..6fb0cc9ae14 100644 --- a/spotbugs-tests/src/test/java/edu/umd/cs/findbugs/detect/FindOverridableMethodCallTest.java +++ b/spotbugs-tests/src/test/java/edu/umd/cs/findbugs/detect/FindOverridableMethodCallTest.java @@ -3,6 +3,9 @@ import static edu.umd.cs.findbugs.test.CountMatcher.containsExactly; import static org.hamcrest.Matchers.hasItem; +import edu.umd.cs.findbugs.BugCollection; +import edu.umd.cs.findbugs.test.SpotBugsExtension; +import edu.umd.cs.findbugs.test.SpotBugsRunner; import org.apache.bcel.Const; import static org.hamcrest.MatcherAssert.assertThat; @@ -13,8 +16,45 @@ import edu.umd.cs.findbugs.annotations.Confidence; import edu.umd.cs.findbugs.test.matcher.BugInstanceMatcher; import edu.umd.cs.findbugs.test.matcher.BugInstanceMatcherBuilder; +import org.junit.jupiter.api.condition.DisabledOnJre; +import org.junit.jupiter.api.condition.JRE; +import org.junit.jupiter.api.extension.ExtendWith; +import java.nio.file.Paths; + +@ExtendWith(SpotBugsExtension.class) class FindOverridableMethodCallTest extends AbstractIntegrationTest { + @Test + @DisabledOnJre({ JRE.JAVA_8 }) + void testIssue2414_java11(SpotBugsRunner spotbugs) { + BugCollection bugCollection = spotbugs.performAnalysis(Paths.get( + "../spotbugsTestCases/build/classes/java/java17/overridableMethodCall/Issue2414.class")); + + final BugInstanceMatcher bugInstanceMatcher = new BugInstanceMatcherBuilder() + .bugType("MC_OVERRIDABLE_METHOD_CALL_IN_CONSTRUCTOR") + // .inClass("Issue2414") + // .inMethod(Const.CONSTRUCTOR_NAME) + // .withConfidence(Confidence.LOW) + // .atLine(line) + .build(); + + assertThat(bugCollection, containsExactly(1, bugInstanceMatcher)); + } + + @Test + void testIssue2414_java8() { + performAnalysis("overridableMethodCall/Issue2414.class"); + + final BugInstanceMatcher bugInstanceMatcher = new BugInstanceMatcherBuilder() + .bugType("MC_OVERRIDABLE_METHOD_CALL_IN_CONSTRUCTOR") + // .inClass("Issue2414") + // .inMethod(Const.CONSTRUCTOR_NAME) + // .withConfidence(Confidence.LOW) + // .atLine(line) + .build(); + + assertThat(getBugCollection(), containsExactly(0, bugInstanceMatcher)); + } @Test void testDirectCase() { diff --git a/spotbugsTestCases/src/java/overridableMethodCall/Issue2414.java b/spotbugsTestCases/src/java/overridableMethodCall/Issue2414.java new file mode 100644 index 00000000000..f9c9e457609 --- /dev/null +++ b/spotbugsTestCases/src/java/overridableMethodCall/Issue2414.java @@ -0,0 +1,26 @@ +package overridableMethodCall; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class Issue2414 { + private final static List aList = new ArrayList<>(Arrays.asList("de", "us")); + + public Issue2414() { + getList(); + } + + private List getList() { + + return aList.stream() + // if this peek is commented, it works + .peek(test -> { + if (test.length() != 2) { + throw new IllegalStateException("foobar"); + } + }) + .collect(Collectors.toList()); + } +} \ No newline at end of file diff --git a/spotbugsTestCases/src/java17/Issue2414.java b/spotbugsTestCases/src/java17/Issue2414.java new file mode 100644 index 00000000000..d97f587d21e --- /dev/null +++ b/spotbugsTestCases/src/java17/Issue2414.java @@ -0,0 +1,23 @@ +package overridableMethodCall; + +import java.util.List; +import java.util.stream.Collectors; + +public class Issue2414 { + private final static List aList = List.of("de", "us"); + + public Issue2414() { + getList(); + } + + private List getList() { + return aList.stream() + // if this peek is commented, it works + .peek(test -> { + if (test.length() != 2) { + throw new IllegalStateException("foobar"); + } + }) + .collect(Collectors.toList()); + } +} \ No newline at end of file