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 c9735309dea..acfd809ea17 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,10 +3,14 @@ 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.BugInstance; +import edu.umd.cs.findbugs.test.SpotBugsRule; import org.apache.bcel.Const; import static org.hamcrest.MatcherAssert.assertThat; +import org.junit.Rule; import org.junit.Test; import edu.umd.cs.findbugs.AbstractIntegrationTest; @@ -14,7 +18,32 @@ import edu.umd.cs.findbugs.test.matcher.BugInstanceMatcher; import edu.umd.cs.findbugs.test.matcher.BugInstanceMatcherBuilder; +import java.nio.file.Paths; + public class FindOverridableMethodCallTest extends AbstractIntegrationTest { + @Rule + public SpotBugsRule spotbugs = new SpotBugsRule(); + + @Test + public void testIssue2414() { + 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(); + + for (BugInstance bi : bugCollection.getCollection()) { + System.out.println(String.format("%s", bi.getBugPattern())); + } + + assertThat(bugCollection, hasItem(bugInstanceMatcher)); + } + @Test public void testDirectCase() { testCase("DirectCase", 5, 18); diff --git a/spotbugsTestCases/src/java/overridableMethodCall/Issue2414.java b/spotbugsTestCases/src/java/overridableMethodCall/Issue2414.java new file mode 100644 index 00000000000..ed3d5b4e3fa --- /dev/null +++ b/spotbugsTestCases/src/java/overridableMethodCall/Issue2414.java @@ -0,0 +1,43 @@ +package overridableMethodCall; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +public class Issue2414 { + + private List someList; + private List aList = new ArrayList() {{ + add("de"); + add("us"); + }}; + + public Issue2414() { + this.someList = getList(); + } + + public void doit() { + someList.add("test"); + } + + + private List getList() { + + return aList.stream() + .map(String::trim) + .peek(url -> { + if (!url.contains("blabla")) { + throw new IllegalStateException("blub"); + } + }) + .map(test -> test + .replace("https://", "")) + // 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..77d22be9d1d --- /dev/null +++ b/spotbugsTestCases/src/java17/Issue2414.java @@ -0,0 +1,37 @@ +package overridableMethodCall; + +import java.util.List; + +public class Issue2414 { + + private List someList; + private List aList = List.of("de", "us"); + + public Issue2414() { + this.someList = getList(); + } + + public void doit() { + someList.add("test"); + } + + private List getList() { + + return aList.stream() + .map(String::trim) + .peek(url -> { + if (!url.contains("blabla")) { + throw new IllegalStateException("blub"); + } + }) + .map(test -> test + .replace("https://", "")) + // if this peek is commented, it works + .peek(test -> { + if (test.length() != 2) { + throw new IllegalStateException("foobar"); + } + }) + .toList(); + } +} \ No newline at end of file