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

Skip to content

Commit 011eb22

Browse files
committed
Add test for ObjFlow over binding patterns
1 parent 4cf511e commit 011eb22

4 files changed

Lines changed: 46 additions & 0 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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 |
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)