File tree Expand file tree Collapse file tree
java/ql/test/library-tests/object-tostring-flow-binding-patterns Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ public class Test {
2+
3+ interface Intf { }
4+ static class Specific implements Intf { public String toString () { return "Specific" ; } }
5+ static class Alternative implements Intf { public String toString () { 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 Object o2 ) {
17+ // So we should know o2.toString is really Specific.toString():
18+ return o2 .toString ();
19+ }
20+
21+ switch (o ) {
22+ case Object o2 when o2 .hashCode () > 0 -> { return o2 .toString (); } // Same goes for this `o2`
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:16:4:23 | super(...) | java.lang.Object.Object |
3+ | Test.java:5:16:5:26 | 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:26 | toString(...) | Test$Specific.toString |
8+ | Test.java:22:27:22:39 | hashCode(...) | java.lang.Object.hashCode |
9+ | Test.java:22:57:22:69 | toString(...) | Test$Specific.toString |
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