Skip to content

Commit 911835c

Browse files
authored
Merge pull request #13392 from yoff/java/test-type-tracking-through-flow-summaries
java: test type tracking through flow summaries
2 parents 6ba7f9a + aec1e4a commit 911835c

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import java.util.*;
2+
3+
public class CallableViaSummary {
4+
public interface Element {
5+
public void handle(String message);
6+
}
7+
8+
public void main(String[] args) {
9+
List<Element> elements = new ArrayList<>();
10+
11+
List<Element> elements2 = new ArrayList<>();
12+
13+
elements.add(new Element() {
14+
@Override
15+
public void handle(String message) {
16+
System.out.println(message);
17+
}
18+
});
19+
20+
elements.add(message -> System.out.println(message));
21+
22+
// This dispatches to the two added elements because
23+
// the summary of ArrayList causes flow via type tracking.
24+
elements.get(0).handle("Hello, world!");
25+
26+
// This does not dispatch to anything, showing that the
27+
// open-world assumption does not apply
28+
// (and hence that type tracking is necessary above).
29+
elements2.get(0).handle("Hello, world!");
30+
}
31+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| CallableViaSummary.java:24:9:24:47 | handle(...) | CallableViaSummary.java:15:25:15:30 | handle |
2+
| CallableViaSummary.java:24:9:24:47 | handle(...) | CallableViaSummary.java:20:22:20:59 | handle |
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import java
2+
import semmle.code.java.dispatch.VirtualDispatch
3+
4+
from MethodAccess ma, Method m
5+
where
6+
m = viableImpl(ma) and
7+
m.fromSource() and
8+
ma.getFile().toString() = "CallableViaSummary"
9+
select ma, m

0 commit comments

Comments
 (0)