mirror of
https://github.com/github/codeql.git
synced 2026-04-20 14:34:04 +02:00
Add test for virtual-dispatch flow through binding patterns
This commit is contained in:
1
java/ql/test/library-tests/dataflow/collections/options
Normal file
1
java/ql/test/library-tests/dataflow/collections/options
Normal file
@@ -0,0 +1 @@
|
||||
//semmle-extractor-options: --javac-args --release 21
|
||||
@@ -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"; }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
//semmle-extractor-options: --javac-args --release 21
|
||||
@@ -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 |
|
||||
@@ -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()
|
||||
Reference in New Issue
Block a user