Add test for virtual-dispatch flow through binding patterns

This commit is contained in:
Chris Smowton
2023-11-09 18:17:07 +00:00
parent 43c935024a
commit 4cf511e26a
5 changed files with 46 additions and 0 deletions

View File

@@ -0,0 +1 @@
//semmle-extractor-options: --javac-args --release 21

View File

@@ -0,0 +1,29 @@
public class Test {
private interface Intf { String get(); }
private static class Specific implements Intf { public String get() { return "Specific"; } }
private static class Alternative implements Intf { public String get() { return "Alternative"; } }
public static String caller() {
Alternative a = new Alternative(); // Instantiate this somewhere so there are at least two candidate types in general
return test(new Specific());
}
public static String test(Object o) {
if (o instanceof Intf i) {
// So we should know i.get is really Specific.get():
return i.get();
}
switch (o) {
case Intf i -> { return i.get(); } // Same goes for this `i`
default -> { return "Not an Intf"; }
}
}
}

View File

@@ -0,0 +1 @@
//semmle-extractor-options: --javac-args --release 21

View File

@@ -0,0 +1,8 @@
| Test.java:1:14:1:17 | super(...) | java.lang.Object.Object |
| Test.java:4:24:4:31 | super(...) | java.lang.Object.Object |
| Test.java:5:24:5:34 | super(...) | java.lang.Object.Object |
| Test.java:9:21:9:37 | new Alternative(...) | Test$Alternative.Alternative |
| Test.java:10:12:10:31 | test(...) | Test.test |
| Test.java:10:17:10:30 | new Specific(...) | Test$Specific.Specific |
| Test.java:18:14:18:20 | get(...) | Test$Specific.get |
| Test.java:22:31:22:37 | get(...) | Test$Specific.get |

View File

@@ -0,0 +1,7 @@
import java
import semmle.code.java.dispatch.VirtualDispatch
from Call c, Callable c2
where c2 = viableCallable(c)
select c, c2.getQualifiedName()