-
Notifications
You must be signed in to change notification settings - Fork 720
Expand file tree
/
Copy pathVolatileVariablesOperationsCheck.java
More file actions
177 lines (162 loc) · 4.45 KB
/
VolatileVariablesOperationsCheck.java
File metadata and controls
177 lines (162 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package checks;
import java.util.function.ObjIntConsumer;
class VolatileVariablesOperationsCheck {
private volatile int count1 = 0;
private volatile long count2 = 0L;
private volatile Integer count3 = 0;
private volatile Long count4 = 0L;
private volatile double noAtomicCounterPart = 0.0;
private int nonVolatileCount1 = 0;
private float nonVolatileCount2 = 0f;
private volatile boolean boo1 = false;
private boolean boo2 = false;
public void incrementCounts() {
count1++; // Noncompliant {{Use an "AtomicInteger" for this field; its operations are atomic.}}
// ^^^^^^
++this.count1; // Noncompliant {{Use an "AtomicInteger" for this field; its operations are atomic.}}
// ^^^^^^
(count2)++; // Noncompliant {{Use an "AtomicLong" for this field; its operations are atomic.}}
// ^^^^^^
count2 = (++count2); // Noncompliant {{Use an "AtomicLong" for this field; its operations are atomic.}}
// ^^^^^^
count3++; // Noncompliant {{Use an "AtomicInteger" for this field; its operations are atomic.}}
// ^^^^^^
++count3; // Noncompliant
// ^^^^^^
count4++; // Noncompliant {{Use an "AtomicLong" for this field; its operations are atomic.}}
// ^^^^^^
++count4; // Noncompliant
// ^^^^^^
nonVolatileCount1++;
++nonVolatileCount1;
nonVolatileCount2++;
++nonVolatileCount2;
noAtomicCounterPart++;
++noAtomicCounterPart;
}
public void decrementCounts() {
count1--; // Noncompliant {{Use an "AtomicInteger" for this field; its operations are atomic.}}
// ^^^^^^
--count1; // Noncompliant
// ^^^^^^
(count2)--; // Noncompliant {{Use an "AtomicLong" for this field; its operations are atomic.}}
// ^^^^^^
count2 = (--count2); // Noncompliant
// ^^^^^^
count3--; // Noncompliant
// ^^^^^^
--count3; // Noncompliant
// ^^^^^^
count4--; // Noncompliant
// ^^^^^^
--count4; // Noncompliant
// ^^^^^^
nonVolatileCount1--;
--nonVolatileCount1;
nonVolatileCount2--;
--nonVolatileCount2;
noAtomicCounterPart--;
--noAtomicCounterPart;
}
public boolean toggleBooleans(){
boo1 = !boo1; // Noncompliant {{Use an "AtomicBoolean" for this field; its operations are atomic.}}
// ^^^^
boo1 = (!boo1); // Noncompliant
// ^^^^
boo1 = !(boo1); // Noncompliant
// ^^^^
this.boo1 = (!this.boo1); // Noncompliant
// ^^^^
boo2 = !boo2;
boo2 = !boo1;
this.boo2 = (!this.boo1);
boo1 = !boo2;
boo1 = !true;
boo1 = boo2 = !false;
return !boo1;
}
void assignments() {
// An example such as the one below definitely shouldn't be applied on a volatile field, but it's probably also poor practice to apply it on an atomic field...
count1 = 3 * count1 + 2; // Noncompliant {{Use an "AtomicInteger" for this field; its operations are atomic.}}
// ^^^^^^
count1 *= 1; // Noncompliant
// ^^^^^^
count1 /= 1; // Noncompliant
// ^^^^^^
count1 %= 1; // Noncompliant
// ^^^^^^
count1 += 1; // Noncompliant
// ^^^^^^
count1 -= 1; // Noncompliant
// ^^^^^^
count1 <<= 1; // Noncompliant
// ^^^^^^
count1 >>= 1; // Noncompliant
// ^^^^^^
count1 >>>= 1; // Noncompliant
// ^^^^^^
count1 ^= 1; // Noncompliant
// ^^^^^^
count1 |= 1; // Noncompliant
// ^^^^^^
boo1 = true && !(boo1 || boo2); // Noncompliant
// ^^^^
boo1 &= true; // Noncompliant
// ^^^^
boo1 |= true; // Noncompliant
// ^^^^
boo1 ^= true; // Noncompliant
// ^^^^
}
synchronized void synchronizedMethod() {
boo1 = !boo1;
count1++;
}
void synchronizedBlock() {
synchronized (this) {
boo1 = !boo1;
count1++;
}
}
void exclusions() {
ObjIntConsumer<Integer> consumer = new ObjIntConsumer<Integer>() {
@Override
public void accept(Integer integer, int value) {
boo1 = !boo1;
count1++;
}
};
ObjIntConsumer<Integer> consumer2 = (integer, value) -> {
boo1 = !boo1;
count1++;
};
}
}
enum anEnum {
VAL;
volatile int value;
void method() {
value++; // Noncompliant
// ^^^^^
}
}
record Vinyl(String singer, String title, int year) {
static volatile int counter = 0;
static int nonVolatile = 0;
void method() {
counter++; // Noncompliant
// ^^^^^^^
nonVolatile++;
}
}
class Container {
static volatile int counter = 0;
static int nonVolatile = 0;
}
interface MyInterface {
default void method() {
Container.counter++; // Noncompliant
// ^^^^^^^
Container.nonVolatile++;
}
}