Skip to content

Commit 0e376b3

Browse files
committed
Java: extend typeflow tests to cover union types.
1 parent d0f7052 commit 0e376b3

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import java.util.*;
2+
import java.util.concurrent.*;
3+
4+
public class UnionTypes {
5+
public void m1() {
6+
m1_map_put(new LinkedHashMap<>(), "k", "v");
7+
m1_map_put(new ConcurrentHashMap<>(), "k", "v");
8+
}
9+
10+
private void m1_map_put(Map<String, String> m, String k, String v) {
11+
m.put(k, v);
12+
}
13+
14+
static class Sup { }
15+
interface Inter { }
16+
17+
static class A1 extends Sup implements Inter { }
18+
static class A2 extends Sup implements Inter { }
19+
static class A3 extends Sup { }
20+
static class A4 extends Sup implements Inter { }
21+
static class A2sub extends A2 { }
22+
23+
private void m2(boolean b) {
24+
scc1(new A1(), 10);
25+
Sup x = b ? new A2() : new A3();
26+
scc2(x, 10);
27+
}
28+
29+
private void scc1(Sup x1, int i) {
30+
scc2(x1, i);
31+
}
32+
33+
private void scc2(Sup x2, int i) {
34+
scc3(x2, i);
35+
}
36+
37+
private void scc3(Sup x3, int i) {
38+
next(x3);
39+
if (i > 0)
40+
scc1(x3, --i);
41+
}
42+
43+
private void next(Sup x) {
44+
if (x instanceof Inter) {
45+
x.hashCode();
46+
}
47+
}
48+
49+
void m3(Object d) {
50+
if (d instanceof A1 || d instanceof A2 || d instanceof A3) {
51+
d.hashCode();
52+
}
53+
}
54+
}

java/ql/test/library-tests/typeflow/typeflow.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313
| A.java:67:22:67:22 | x | Integer | false |
1414
| A.java:70:23:70:24 | x2 | Integer | false |
1515
| A.java:92:18:92:18 | n | Integer | false |
16+
| UnionTypes.java:45:7:45:7 | x | Inter | false |
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
| UnionTypes.java:11:5:11:5 | m | 2 | ConcurrentHashMap<String,String> | false |
2+
| UnionTypes.java:11:5:11:5 | m | 2 | LinkedHashMap<String,String> | false |
3+
| UnionTypes.java:26:10:26:10 | x | 2 | A2 | true |
4+
| UnionTypes.java:26:10:26:10 | x | 2 | A3 | false |
5+
| UnionTypes.java:30:10:30:11 | x1 | 3 | A1 | false |
6+
| UnionTypes.java:30:10:30:11 | x1 | 3 | A2 | true |
7+
| UnionTypes.java:30:10:30:11 | x1 | 3 | A3 | false |
8+
| UnionTypes.java:34:10:34:11 | x2 | 3 | A1 | false |
9+
| UnionTypes.java:34:10:34:11 | x2 | 3 | A2 | true |
10+
| UnionTypes.java:34:10:34:11 | x2 | 3 | A3 | false |
11+
| UnionTypes.java:38:10:38:11 | x3 | 3 | A1 | false |
12+
| UnionTypes.java:38:10:38:11 | x3 | 3 | A2 | true |
13+
| UnionTypes.java:38:10:38:11 | x3 | 3 | A3 | false |
14+
| UnionTypes.java:40:12:40:13 | x3 | 3 | A1 | false |
15+
| UnionTypes.java:40:12:40:13 | x3 | 3 | A2 | true |
16+
| UnionTypes.java:40:12:40:13 | x3 | 3 | A3 | false |
17+
| UnionTypes.java:44:9:44:9 | x | 3 | A1 | false |
18+
| UnionTypes.java:44:9:44:9 | x | 3 | A2 | true |
19+
| UnionTypes.java:44:9:44:9 | x | 3 | A3 | false |
20+
| UnionTypes.java:45:7:45:7 | x | 2 | A1 | false |
21+
| UnionTypes.java:45:7:45:7 | x | 2 | A2 | true |
22+
| UnionTypes.java:51:7:51:7 | d | 3 | A1 | false |
23+
| UnionTypes.java:51:7:51:7 | d | 3 | A2 | false |
24+
| UnionTypes.java:51:7:51:7 | d | 3 | A3 | false |
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import java
2+
import semmle.code.java.dataflow.TypeFlow
3+
4+
int countUnionTypes(Expr e) {
5+
result = strictcount(RefType t, boolean exact | exprUnionTypeFlow(e, t, exact))
6+
}
7+
8+
from RValue e, RefType t, boolean exact
9+
where exprUnionTypeFlow(e, t, exact)
10+
select e, countUnionTypes(e), t.toString(), exact

0 commit comments

Comments
 (0)