File tree Expand file tree Collapse file tree
java/ql/test/library-tests
virtual-dispatch-binding-patterns Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ //semmle-extractor-options: --javac-args --release 21
Original file line number Diff line number Diff line change 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+
Original file line number Diff line number Diff line change 1+ //semmle-extractor-options: --javac-args --release 21
Original file line number Diff line number Diff line change 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 |
Original file line number Diff line number Diff line change 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 ( )
You can’t perform that action at this time.
0 commit comments