Skip to content

Commit

Permalink
recreate issue
Browse files Browse the repository at this point in the history
  • Loading branch information
JuditKnoll committed Oct 11, 2023
1 parent 09d70e1 commit 70506a8
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,47 @@
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;
import edu.umd.cs.findbugs.annotations.Confidence;
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);
Expand Down
43 changes: 43 additions & 0 deletions spotbugsTestCases/src/java/overridableMethodCall/Issue2414.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package overridableMethodCall;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class Issue2414 {

private List<String> someList;
private List<String> aList = new ArrayList<String>() {{
add("de");
add("us");
}};

public Issue2414() {
this.someList = getList();
}

public void doit() {
someList.add("test");
}


private List<String> 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());
}
}
37 changes: 37 additions & 0 deletions spotbugsTestCases/src/java17/Issue2414.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package overridableMethodCall;

import java.util.List;

public class Issue2414 {

private List<String> someList;
private List<String> aList = List.of("de", "us");

public Issue2414() {
this.someList = getList();
}

public void doit() {
someList.add("test");
}

private List<String> 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();
}
}

0 comments on commit 70506a8

Please sign in to comment.