You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: java-checks-test-sources/default/src/main/java/checks/VolatileVariablesOperationsCheck.java
+45-1Lines changed: 45 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -16,13 +16,21 @@ class VolatileVariablesOperationsCheck {
16
16
17
17
publicvoidincrementCounts() {
18
18
count1++; // Noncompliant {{Use an "AtomicInteger" for this field; its operations are atomic.}}
19
+
// ^^^^^^
19
20
++this.count1; // Noncompliant {{Use an "AtomicInteger" for this field; its operations are atomic.}}
21
+
// ^^^^^^
20
22
(count2)++; // Noncompliant {{Use an "AtomicLong" for this field; its operations are atomic.}}
23
+
// ^^^^^^
21
24
count2 = (++count2); // Noncompliant {{Use an "AtomicLong" for this field; its operations are atomic.}}
25
+
// ^^^^^^
22
26
count3++; // Noncompliant {{Use an "AtomicInteger" for this field; its operations are atomic.}}
27
+
// ^^^^^^
23
28
++count3; // Noncompliant
29
+
// ^^^^^^
24
30
count4++; // Noncompliant {{Use an "AtomicLong" for this field; its operations are atomic.}}
31
+
// ^^^^^^
25
32
++count4; // Noncompliant
33
+
// ^^^^^^
26
34
nonVolatileCount1++;
27
35
++nonVolatileCount1;
28
36
nonVolatileCount2++;
@@ -33,13 +41,21 @@ public void incrementCounts() {
33
41
34
42
publicvoiddecrementCounts() {
35
43
count1--; // Noncompliant {{Use an "AtomicInteger" for this field; its operations are atomic.}}
44
+
// ^^^^^^
36
45
--count1; // Noncompliant
46
+
// ^^^^^^
37
47
(count2)--; // Noncompliant {{Use an "AtomicLong" for this field; its operations are atomic.}}
48
+
// ^^^^^^
38
49
count2 = (--count2); // Noncompliant
50
+
// ^^^^^^
39
51
count3--; // Noncompliant
52
+
// ^^^^^^
40
53
--count3; // Noncompliant
54
+
// ^^^^^^
41
55
count4--; // Noncompliant
56
+
// ^^^^^^
42
57
--count4; // Noncompliant
58
+
// ^^^^^^
43
59
nonVolatileCount1--;
44
60
--nonVolatileCount1;
45
61
nonVolatileCount2--;
@@ -50,9 +66,13 @@ public void decrementCounts() {
50
66
51
67
publicbooleantoggleBooleans(){
52
68
boo1 = !boo1; // Noncompliant {{Use an "AtomicBoolean" for this field; its operations are atomic.}}
69
+
// ^^^^
53
70
boo1 = (!boo1); // Noncompliant
71
+
// ^^^^
54
72
boo1 = !(boo1); // Noncompliant
73
+
// ^^^^
55
74
this.boo1 = (!this.boo1); // Noncompliant
75
+
// ^^^^
56
76
boo2 = !boo2;
57
77
boo2 = !boo1;
58
78
this.boo2 = (!this.boo1);
@@ -62,17 +82,38 @@ public boolean toggleBooleans(){
62
82
return !boo1;
63
83
}
64
84
65
-
voidbinaryOperations() {
85
+
voidassignments() {
86
+
// 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...
87
+
count1 = 3 * count1 + 2; // Noncompliant {{Use an "AtomicInteger" for this field; its operations are atomic.}}
88
+
// ^^^^^^
66
89
count1 *= 1; // Noncompliant
90
+
// ^^^^^^
67
91
count1 /= 1; // Noncompliant
92
+
// ^^^^^^
68
93
count1 %= 1; // Noncompliant
94
+
// ^^^^^^
69
95
count1 += 1; // Noncompliant
96
+
// ^^^^^^
70
97
count1 -= 1; // Noncompliant
98
+
// ^^^^^^
71
99
count1 <<= 1; // Noncompliant
100
+
// ^^^^^^
72
101
count1 >>= 1; // Noncompliant
102
+
// ^^^^^^
73
103
count1 >>>= 1; // Noncompliant
104
+
// ^^^^^^
74
105
count1 ^= 1; // Noncompliant
106
+
// ^^^^^^
75
107
count1 |= 1; // Noncompliant
108
+
// ^^^^^^
109
+
boo1 = true && !(boo1 || boo2); // Noncompliant
110
+
// ^^^^
111
+
boo1 &= true; // Noncompliant
112
+
// ^^^^
113
+
boo1 |= true; // Noncompliant
114
+
// ^^^^
115
+
boo1 ^= true; // Noncompliant
116
+
// ^^^^
76
117
}
77
118
78
119
synchronizedvoidsynchronizedMethod() {
@@ -107,6 +148,7 @@ enum anEnum {
107
148
108
149
voidmethod() {
109
150
value++; // Noncompliant
151
+
// ^^^^^
110
152
}
111
153
}
112
154
@@ -116,6 +158,7 @@ record Vinyl(String singer, String title, int year) {
0 commit comments