Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4cf511e

Browse files
committed
Add test for virtual-dispatch flow through binding patterns
1 parent 43c9350 commit 4cf511e

5 files changed

Lines changed: 46 additions & 0 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
//semmle-extractor-options: --javac-args --release 21
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
public class Test {
2+
3+
private interface Intf { String get(); }
4+
private static class Specific implements Intf { public String get() { return "Specific"; } }
5+
private static class Alternative implements Intf { public String get() { return "Alternative"; } }
6+
7+
public static String caller() {
8+
9+
Alternative a = new Alternative(); // Instantiate this somewhere so there are at least two candidate types in general
10+
return test(new Specific());
11+
12+
}
13+
14+
public static String test(Object o) {
15+
16+
if (o instanceof Intf i) {
17+
// So we should know i.get is really Specific.get():
18+
return i.get();
19+
}
20+
21+
switch (o) {
22+
case Intf i -> { return i.get(); } // Same goes for this `i`
23+
default -> { return "Not an Intf"; }
24+
}
25+
26+
}
27+
28+
}
29+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
//semmle-extractor-options: --javac-args --release 21
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
| Test.java:1:14:1:17 | super(...) | java.lang.Object.Object |
2+
| Test.java:4:24:4:31 | super(...) | java.lang.Object.Object |
3+
| Test.java:5:24:5:34 | super(...) | java.lang.Object.Object |
4+
| Test.java:9:21:9:37 | new Alternative(...) | Test$Alternative.Alternative |
5+
| Test.java:10:12:10:31 | test(...) | Test.test |
6+
| Test.java:10:17:10:30 | new Specific(...) | Test$Specific.Specific |
7+
| Test.java:18:14:18:20 | get(...) | Test$Specific.get |
8+
| Test.java:22:31:22:37 | get(...) | Test$Specific.get |
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import java
2+
3+
import semmle.code.java.dispatch.VirtualDispatch
4+
5+
from Call c, Callable c2
6+
where c2 = viableCallable(c)
7+
select c, c2.getQualifiedName()

0 commit comments

Comments
 (0)