Repository navigation
Expand file tree
/
Copy pathDuplicateImmutableCollectionArgumentsCheckSample.java
More file actions
289 lines (247 loc) · 10.4 KB
/
Copy pathDuplicateImmutableCollectionArgumentsCheckSample.java
File metadata and controls
289 lines (247 loc) · 10.4 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package checks;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import static java.util.Map.entry;
class DuplicateImmutableCollectionArgumentsCheckSample {
private static final String CONST_A = "keyA";
private static final String CONST_B = "keyB";
private static final String CONST_A_ALIAS = "keyA";
private static final int INT_A = 1;
private static final int INT_B = 2;
private static final int INT_A_ALIAS = 1;
private static final char CHAR_A = 'a';
private static final char CHAR_A_ALIAS = CHAR_A;
private static final char CHAR_FROM_INT = 97;
private static final int INT_FROM_CHAR = 'a';
enum Color {
RED, GREEN, BLUE
}
private static final Color RED_ALIAS = Color.RED;
void testMapOf() {
Map<String, Integer> empty = Map.of(); // Compliant
Map<String, Integer> single = Map.of("a", 1); // Compliant
Map<String, Integer> distinct = Map.of("a", 1, "b", 2, "c", 3); // Compliant
Map<String, Integer> duplicateLiteral = Map.of(
"timeout", 30,
"retries", 3,
"timeout", 60 // Noncompliant {{Remove or rename this duplicate key; "Map.of" throws an "IllegalArgumentException" at runtime when keys are duplicated.}} [[secondary=-2]]
);
Map<String, Integer> multipleDuplicates = Map.of(
"k1", 1,
"k2", 2,
"k1", 3, // Noncompliant {{Remove or rename this duplicate key; "Map.of" throws an "IllegalArgumentException" at runtime when keys are duplicated.}} [[secondary=-2]]
"k2", 4, // Noncompliant {{Remove or rename this duplicate key; "Map.of" throws an "IllegalArgumentException" at runtime when keys are duplicated.}} [[secondary=-2]]
"k1", 5 // Noncompliant {{Remove or rename this duplicate key; "Map.of" throws an "IllegalArgumentException" at runtime when keys are duplicated.}} [[secondary=-4]]
);
Map<String, Integer> duplicateConstants = Map.of(
CONST_A, 1,
CONST_B, 2,
CONST_A_ALIAS, 3 // Noncompliant {{Remove or rename this duplicate key; "Map.of" throws an "IllegalArgumentException" at runtime when keys are duplicated.}} [[secondary=-2]]
);
Map<String, Integer> duplicateConstAndLiteral = Map.of(
CONST_A, 1,
CONST_B, 2,
"keyA", 3 // Noncompliant {{Remove or rename this duplicate key; "Map.of" throws an "IllegalArgumentException" at runtime when keys are duplicated.}} [[secondary=-2]]
);
Map<Integer, String> duplicateIntKeys = Map.of(
1, "one",
2, "two",
1, "uno" // Noncompliant {{Remove or rename this duplicate key; "Map.of" throws an "IllegalArgumentException" at runtime when keys are duplicated.}} [[secondary=-2]]
);
Map<Integer, String> duplicateAliasedInts = Map.of(
INT_A, "one",
INT_B, "two",
INT_A_ALIAS, "uno" // Noncompliant {{Remove or rename this duplicate key; "Map.of" throws an "IllegalArgumentException" at runtime when keys are duplicated.}} [[secondary=-2]]
);
Map<Integer, String> duplicateIntAndConst = Map.of(
INT_A, "one",
INT_B, "two",
1, "uno" // Noncompliant {{Remove or rename this duplicate key; "Map.of" throws an "IllegalArgumentException" at runtime when keys are duplicated.}} [[secondary=-2]]
);
Map<Boolean, String> duplicateBoolKeys = Map.of(
true, "yes",
false, "no",
true, "oui" // Noncompliant {{Remove or rename this duplicate key; "Map.of" throws an "IllegalArgumentException" at runtime when keys are duplicated.}} [[secondary=-2]]
);
Map<String, Integer> parenthesizedKeys = Map.of(
("key"), 1,
"other", 2,
"key", 3 // Noncompliant {{Remove or rename this duplicate key; "Map.of" throws an "IllegalArgumentException" at runtime when keys are duplicated.}} [[secondary=-2]]
);
}
void testMapOfVariables(String varKey1, String varKey2) {
Map<String, Integer> distinctVars = Map.of(varKey1, 1, varKey2, 2); // Compliant
Map<String, Integer> sameVar = Map.of(
varKey1, 1,
varKey2, 2,
varKey1, 3 // Noncompliant {{Remove or rename this duplicate key; "Map.of" throws an "IllegalArgumentException" at runtime when keys are duplicated.}} [[secondary=-2]]
);
}
void testMapOfEntries() {
Map<String, String> empty = Map.ofEntries(); // Compliant
Map<String, String> single = Map.ofEntries(Map.entry("a", "b")); // Compliant
Map<String, String> distinct = Map.ofEntries(
Map.entry("k1", "v1"),
Map.entry("k2", "v2"),
entry("k3", "v3")
); // Compliant
Map<String, String> duplicateEntries = Map.ofEntries(
Map.entry("host", "localhost"),
Map.entry("port", "8080"),
Map.entry("host", "remotehost") // Noncompliant {{Remove or rename this duplicate key; "Map.ofEntries" throws an "IllegalArgumentException" at runtime when keys are duplicated.}} [[secondary=-2]]
);
Map<String, String> duplicateStaticImport = Map.ofEntries(
entry("user", "admin"),
entry("user", "guest") // Noncompliant {{Remove or rename this duplicate key; "Map.ofEntries" throws an "IllegalArgumentException" at runtime when keys are duplicated.}} [[secondary=-1]]
);
Map.Entry<String, String> entryVariable = Map.entry("dynamic", "val");
Map<String, String> entryVarMap = Map.ofEntries(entryVariable); // Compliant
}
void testSetOf() {
Set<String> empty = Set.of(); // Compliant
Set<String> single = Set.of("a"); // Compliant
Set<String> distinct = Set.of("a", "b", "c"); // Compliant
Set<String> duplicateStrings = Set.of(
"read",
"write",
"read" // Noncompliant {{Remove or replace this duplicate element; "Set.of" throws an "IllegalArgumentException" at runtime when elements are duplicated.}} [[secondary=-2]]
);
Set<String> duplicateConcat = Set.of(
"ab",
"cd",
"a" + "b" // Noncompliant {{Remove or replace this duplicate element; "Set.of" throws an "IllegalArgumentException" at runtime when elements are duplicated.}} [[secondary=-2]]
);
Set<Integer> duplicateNumbers = Set.of(
10,
20,
10 // Noncompliant {{Remove or replace this duplicate element; "Set.of" throws an "IllegalArgumentException" at runtime when elements are duplicated.}} [[secondary=-2]]
);
Set<String> duplicateConsts = Set.of(
CONST_A,
CONST_B,
CONST_A // Noncompliant {{Remove or replace this duplicate element; "Set.of" throws an "IllegalArgumentException" at runtime when elements are duplicated.}} [[secondary=-2]]
);
Set<Color> duplicateEnums = Set.of(
Color.RED,
Color.GREEN,
Color.RED // Noncompliant {{Remove or replace this duplicate element; "Set.of" throws an "IllegalArgumentException" at runtime when elements are duplicated.}} [[secondary=-2]]
);
Set<Color> duplicateAliasedEnums = Set.of(
Color.RED,
Color.BLUE,
RED_ALIAS // Noncompliant {{Remove or replace this duplicate element; "Set.of" throws an "IllegalArgumentException" at runtime when elements are duplicated.}} [[secondary=-2]]
);
Set<String> multipleSetDuplicates = Set.of(
"x",
"x", // Noncompliant {{Remove or replace this duplicate element; "Set.of" throws an "IllegalArgumentException" at runtime when elements are duplicated.}} [[secondary=-1]]
"x" // Noncompliant {{Remove or replace this duplicate element; "Set.of" throws an "IllegalArgumentException" at runtime when elements are duplicated.}} [[secondary=-2]]
);
}
void testCharAndIntAreDistinct() {
Set.of('a', 97);
Set.of(97, 'a');
Set.of((CHAR_A), (97));
Set.of(97, CHAR_A_ALIAS);
Set.of(CHAR_FROM_INT, 97);
Set.of('a', INT_FROM_CHAR);
Map.of('a', "first", 97, "second");
Map.of(97, "first", 'a', "second");
Map.of(CHAR_A, "first", 97, "second");
Map.of(97, "first", CHAR_A_ALIAS, "second");
Map.ofEntries(entry('a', "first"), entry(97, "second"));
Map.ofEntries(entry(97, "first"), entry('a', "second"));
Map.ofEntries(entry(CHAR_A, "first"), entry(97, "second"));
Map.ofEntries(entry(97, "first"), entry(CHAR_A_ALIAS, "second"));
}
void testDuplicateChars() {
Set.of(
'a',
// ^^^>
'a' // Noncompliant {{Remove or replace this duplicate element; "Set.of" throws an "IllegalArgumentException" at runtime when elements are duplicated.}}
// ^^^
);
Set.of(
CHAR_A_ALIAS,
// ^^^^^^^^^^^^>
'a' // Noncompliant
// ^^^
);
Set.of(
CHAR_FROM_INT,
// ^^^^^^^^^^^^^>
'a' // Noncompliant
// ^^^
);
Map.of(
'a', "first",
// ^^^>
CHAR_A, "second" // Noncompliant {{Remove or rename this duplicate key; "Map.of" throws an "IllegalArgumentException" at runtime when keys are duplicated.}}
// ^^^^^^
);
Map.ofEntries(
entry(CHAR_A, "first"),
// ^^^^^^>
entry('a', "second") // Noncompliant {{Remove or rename this duplicate key; "Map.ofEntries" throws an "IllegalArgumentException" at runtime when keys are duplicated.}}
// ^^^
);
}
void testPromotedChars() {
Set.of(
+'a',
// ^^^^>
97 // Noncompliant
// ^^
);
Set.of(
INT_FROM_CHAR,
// ^^^^^^^^^^^^^>
97 // Noncompliant
// ^^
);
Set.of(
CHAR_A + 0,
// ^^^^^^^^^^>
97 // Noncompliant
// ^^
);
}
void testSetOfVariables(String v1, String v2) {
Set<String> distinctVars = Set.of(v1, v2); // Compliant
Set<String> duplicateVars = Set.of(
v1,
v2,
v1 // Noncompliant {{Remove or replace this duplicate element; "Set.of" throws an "IllegalArgumentException" at runtime when elements are duplicated.}} [[secondary=-2]]
);
}
void testNonDeterministicExpressionsAreCompliant() {
Set<Object> newObjects = Set.of(new Object(), new Object()); // Compliant: each instance is distinct
Set<String> methodCalls = Set.of(generateId(), generateId()); // Compliant: methods can return distinct values
Set<UUID> randomUuids = Set.of(UUID.randomUUID(), UUID.randomUUID()); // Compliant
}
void testListOfPermitsDuplicates() {
List<String> list = List.of("dup", "dup", "dup"); // Compliant: List.of allows duplicates
}
static class CyclicDeclarations {
int a;
int b;
int self;
void setA() {
a = b;
}
void setB() {
b = a;
}
void setSelf() {
self = self;
}
void testCyclic() {
Map<Integer, String> map = Map.of(a, "a", b, "b"); // Compliant
Set<Integer> set = Set.of(self, 1); // Compliant
}
}
private String generateId() {
return UUID.randomUUID().toString();
}
}