Skip to content

Commit f5e6ca0

Browse files
committed
java: add tests for thread safe initialisation
1 parent 1c2d8bb commit f5e6ca0

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

java/ql/test/query-tests/ThreadSafe/ThreadSafe.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,7 @@
4343
| examples/Test.java:60:5:60:10 | this.y | This field access (publicly accessible via $@) is not protected by any monitor, but the class is annotated as @ThreadSafe. | examples/Test.java:60:5:60:10 | this.y | this expression |
4444
| examples/Test.java:74:5:74:10 | this.y | This field access (publicly accessible via $@) is not protected by any monitor, but the class is annotated as @ThreadSafe. | examples/Test.java:74:5:74:10 | this.y | this expression |
4545
| examples/Test.java:74:14:74:14 | y | This field access (publicly accessible via $@) is not protected by any monitor, but the class is annotated as @ThreadSafe. | examples/Test.java:74:14:74:14 | y | this expression |
46+
| examples/ThreadSafeInitializers.java:29:9:29:16 | sync_map | This field access (publicly accessible via $@) is not protected by any monitor, but the class is annotated as @ThreadSafe. | examples/ThreadSafeInitializers.java:29:9:29:16 | sync_map | this expression |
47+
| examples/ThreadSafeInitializers.java:37:9:37:12 | cmap | This field access (publicly accessible via $@) is not protected by any monitor, but the class is annotated as @ThreadSafe. | examples/ThreadSafeInitializers.java:37:9:37:12 | cmap | this expression |
48+
| examples/ThreadSafeInitializers.java:45:9:45:14 | this.y | This field access (publicly accessible via $@) is not protected by any monitor, but the class is annotated as @ThreadSafe. | examples/ThreadSafeInitializers.java:45:9:45:14 | this.y | this expression |
49+
| examples/ThreadSafeInitializers.java:49:9:49:11 | set | This field access (publicly accessible via $@) is not protected by any monitor, but the class is annotated as @ThreadSafe. | examples/ThreadSafeInitializers.java:49:9:49:11 | set | this expression |
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package examples;
2+
3+
import java.util.Map;
4+
import java.util.Set;
5+
import java.util.HashMap;
6+
import java.util.Collections;
7+
import java.util.concurrent.ConcurrentHashMap;
8+
9+
@ThreadSafe
10+
public class ThreadSafeInitializers {
11+
12+
private int y;
13+
private final Map<Integer, Integer> sync_map;
14+
private final Map<Integer, Integer> sync_map_initialised = Collections.synchronizedMap(new HashMap<Integer, Integer>());
15+
16+
17+
private final Map<String, String> cmap;
18+
private final Map<String, String> cmap_initialised = new ConcurrentHashMap();
19+
private final Set<Integer> set;
20+
private final Set<Integer> set_initialised = ConcurrentHashMap.newKeySet();
21+
22+
public ThreadSafeInitializers() {
23+
sync_map = Collections.synchronizedMap(new HashMap<Integer, Integer>());
24+
cmap = new ConcurrentHashMap();
25+
set = ConcurrentHashMap.newKeySet();
26+
}
27+
28+
public void sync_map_put(Integer i, Integer v) {
29+
sync_map.put(i,v); // $ SPURIOUS: Alert
30+
}
31+
32+
public void sync_map_initialised_put(Integer i, Integer v) {
33+
sync_map_initialised.put(i,v);
34+
}
35+
36+
public void cmap_put(String s1, String s2) {
37+
cmap.put(s1, s2); // $ SPURIOUS: Alert
38+
}
39+
40+
public void cmap_initialised_put(String s1, String s2) {
41+
cmap_initialised.put(s1, s2);
42+
}
43+
44+
public void setY(int y) {
45+
this.y = y; // $ Alert
46+
}
47+
48+
public void set_add(Integer i) {
49+
set.add(i); // $ SPURIOUS: Alert
50+
}
51+
52+
public void set_initialised_add(Integer i) {
53+
set_initialised.add(i);
54+
}
55+
}

0 commit comments

Comments
 (0)