/
ImmutabilityTest.java
473 lines (426 loc) · 20.7 KB
/
ImmutabilityTest.java
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
///////////////////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
// Copyright (C) 2001-2024 the original author or authors.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
///////////////////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.internal;
import static com.tngtech.archunit.base.DescribedPredicate.doNot;
import static com.tngtech.archunit.base.DescribedPredicate.not;
import static com.tngtech.archunit.lang.conditions.ArchPredicates.are;
import static com.tngtech.archunit.lang.conditions.ArchPredicates.have;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.fields;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;
import com.puppycrawl.tools.checkstyle.FileStatefulCheck;
import com.puppycrawl.tools.checkstyle.GlobalStatefulCheck;
import com.puppycrawl.tools.checkstyle.StatelessCheck;
import com.puppycrawl.tools.checkstyle.meta.ModuleDetails;
import com.puppycrawl.tools.checkstyle.meta.ModulePropertyDetails;
import com.puppycrawl.tools.checkstyle.meta.XmlMetaReader;
import com.puppycrawl.tools.checkstyle.utils.ModuleReflectionUtil;
import com.tngtech.archunit.base.DescribedPredicate;
import com.tngtech.archunit.core.domain.JavaClass;
import com.tngtech.archunit.core.domain.JavaClasses;
import com.tngtech.archunit.core.domain.JavaField;
import com.tngtech.archunit.core.domain.JavaModifier;
import com.tngtech.archunit.core.domain.JavaParameterizedType;
import com.tngtech.archunit.core.domain.JavaType;
import com.tngtech.archunit.core.domain.properties.HasName;
import com.tngtech.archunit.core.importer.ClassFileImporter;
import com.tngtech.archunit.core.importer.ImportOption;
import com.tngtech.archunit.lang.ArchCondition;
import com.tngtech.archunit.lang.ArchRule;
import com.tngtech.archunit.lang.ConditionEvents;
import com.tngtech.archunit.lang.SimpleConditionEvent;
public class ImmutabilityTest {
/**
* Immutable types canonical names.
*/
private static final Set<String> IMMUTABLE_TYPES = Set.of(
"java.lang.String",
"java.lang.Integer",
"java.lang.Byte",
"java.lang.Character",
"java.lang.Short",
"java.lang.Boolean",
"java.lang.Long",
"java.lang.Double",
"java.lang.Float",
"java.lang.StackTraceElement",
"java.math.BigInteger",
"java.math.BigDecimal",
"java.io.File",
"java.util.Locale",
"java.util.UUID",
"java.net.URL",
"java.net.URI",
"java.net.Inet4Address",
"java.net.Inet6Address",
"java.net.InetSocketAddress",
"java.util.regex.Pattern"
);
/**
* Immutable primitive types.
*/
private static final Set<String> PRIMITIVE_TYPES = Set.of(
"byte",
"short",
"int",
"long",
"float",
"double",
"char",
"boolean"
);
/**
* List of fields that are a zero size array. They are immutable by definition.
*/
private static final Set<String> ZERO_SIZE_ARRAY_FIELDS = Set.of(
"com.puppycrawl.tools.checkstyle.utils.CommonUtil.EMPTY_BIT_SET",
"com.puppycrawl.tools.checkstyle.utils.CommonUtil.EMPTY_BYTE_ARRAY",
"com.puppycrawl.tools.checkstyle.utils.CommonUtil.EMPTY_DOUBLE_ARRAY",
"com.puppycrawl.tools.checkstyle.utils.CommonUtil.EMPTY_INTEGER_OBJECT_ARRAY",
"com.puppycrawl.tools.checkstyle.utils.CommonUtil.EMPTY_INT_ARRAY",
"com.puppycrawl.tools.checkstyle.utils.CommonUtil.EMPTY_OBJECT_ARRAY",
"com.puppycrawl.tools.checkstyle.utils.CommonUtil.EMPTY_STRING_ARRAY"
);
/**
* List of fields not following {@link #testUtilClassesImmutability()} rule.
*/
private static final Set<String> SUPPRESSED_FIELDS_IN_UTIL_CLASSES = Set.of(
"com.puppycrawl.tools.checkstyle.utils.TokenUtil.TOKEN_IDS",
"com.puppycrawl.tools.checkstyle.utils.XpathUtil.TOKEN_TYPES_WITH_TEXT_ATTRIBUTE"
);
/**
* List of fields not following {@link #testFieldsInStatelessChecksShouldBeImmutable()} rule.
*/
private static final Set<String> SUPPRESSED_FIELDS_IN_MODULES = Set.of(
"com.puppycrawl.tools.checkstyle.checks.FinalParametersCheck.primitiveDataTypes",
"com.puppycrawl.tools.checkstyle.checks.SuppressWarningsHolder.ENTRIES",
"com.puppycrawl.tools.checkstyle.checks.annotation.MissingDeprecatedCheck.TYPES_HASH_SET",
"com.puppycrawl.tools.checkstyle.checks.coding.AvoidDoubleBraceInitializationCheck"
+ ".HAS_MEMBERS",
"com.puppycrawl.tools.checkstyle.checks.coding.AvoidDoubleBraceInitializationCheck"
+ ".IGNORED_TYPES",
"com.puppycrawl.tools.checkstyle.checks.coding.InnerAssignmentCheck"
+ ".ALLOWED_ASSIGNMENT_CONTEXT",
"com.puppycrawl.tools.checkstyle.checks.coding.InnerAssignmentCheck"
+ ".ALLOWED_ASSIGNMENT_IN_COMPARISON_CONTEXT",
"com.puppycrawl.tools.checkstyle.checks.coding.InnerAssignmentCheck.COMPARISON_TYPES",
"com.puppycrawl.tools.checkstyle.checks.coding.InnerAssignmentCheck.CONTROL_CONTEXT",
"com.puppycrawl.tools.checkstyle.checks.coding.InnerAssignmentCheck"
+ ".LOOP_IDIOM_IGNORED_PARENTS",
"com.puppycrawl.tools.checkstyle.checks.coding.MatchXpathCheck.xpathExpression",
"com.puppycrawl.tools.checkstyle.checks.javadoc.AtclauseOrderCheck.DEFAULT_ORDER",
"com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocBlockTagLocationCheck.DEFAULT_TAGS",
"com.puppycrawl.tools.checkstyle.checks.javadoc.SummaryJavadocCheck.ALLOWED_TYPES",
"com.puppycrawl.tools.checkstyle.checks.modifier.ModifierOrderCheck.JLS_ORDER",
"com.puppycrawl.tools.checkstyle.checks.modifier.RedundantModifierCheck"
+ ".TOKENS_FOR_INTERFACE_MODIFIERS",
"com.puppycrawl.tools.checkstyle.checks.regexp.RegexpMultilineCheck.detector",
"com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineCheck.detector",
"com.puppycrawl.tools.checkstyle.checks.coding.IllegalTokenTextCheck.formatString",
"com.puppycrawl.tools.checkstyle.checks.javadoc.WriteTagCheck.tagRegExp",
"com.puppycrawl.tools.checkstyle.checks.naming.AbstractNameCheck.format",
"com.puppycrawl.tools.checkstyle.checks.whitespace.AbstractParenPadCheck.option",
"com.puppycrawl.tools.checkstyle.checks.coding.ConstructorsDeclarationGroupingCheck"
+ ".parents"
);
/**
* List of classes not following
* {@link #testClassesWithImmutableFieldsShouldBeStateless()} rule.
*/
private static final Set<String> SUPPRESSED_CLASSES_FOR_STATELESS_CHECK_RULE = Set.of(
"com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck",
"com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocMethodCheck",
"com.puppycrawl.tools.checkstyle.checks.metrics.ClassDataAbstractionCouplingCheck",
"com.puppycrawl.tools.checkstyle.checks.metrics.ClassFanOutComplexityCheck",
"com.puppycrawl.tools.checkstyle.checks.naming.CatchParameterNameCheck",
"com.puppycrawl.tools.checkstyle.checks.naming.ClassTypeParameterNameCheck",
"com.puppycrawl.tools.checkstyle.checks.naming.ConstantNameCheck",
"com.puppycrawl.tools.checkstyle.checks.naming.InterfaceTypeParameterNameCheck",
"com.puppycrawl.tools.checkstyle.checks.naming.LambdaParameterNameCheck",
"com.puppycrawl.tools.checkstyle.checks.naming.LocalFinalVariableNameCheck",
"com.puppycrawl.tools.checkstyle.checks.naming.LocalVariableNameCheck",
"com.puppycrawl.tools.checkstyle.checks.naming.MemberNameCheck",
"com.puppycrawl.tools.checkstyle.checks.naming.MethodNameCheck",
"com.puppycrawl.tools.checkstyle.checks.naming.MethodTypeParameterNameCheck",
"com.puppycrawl.tools.checkstyle.checks.naming.ParameterNameCheck",
"com.puppycrawl.tools.checkstyle.checks.naming.PatternVariableNameCheck",
"com.puppycrawl.tools.checkstyle.checks.naming.RecordComponentNameCheck",
"com.puppycrawl.tools.checkstyle.checks.naming.RecordTypeParameterNameCheck",
"com.puppycrawl.tools.checkstyle.checks.naming.StaticVariableNameCheck",
"com.puppycrawl.tools.checkstyle.checks.whitespace.TypecastParenPadCheck",
"com.puppycrawl.tools.checkstyle.checks.naming.TypeNameCheck"
);
/**
* List of classes not following {@link #testClassesWithMutableFieldsShouldBeStateful()} rule.
*/
private static final Set<String> SUPPRESSED_CLASSES_FOR_STATEFUL_CHECK_RULE = Set.of(
"com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheck",
"com.puppycrawl.tools.checkstyle.checks.coding.ConstructorsDeclarationGroupingCheck"
);
private static final JavaClasses CHECKSTYLE_CHECKS = new ClassFileImporter()
.withImportOption(ImportOption.Predefined.DO_NOT_INCLUDE_TESTS)
.importPackages("com.puppycrawl.tools.checkstyle")
.that(new DescribedPredicate<>("are checkstyle modules") {
@Override
public boolean test(JavaClass input) {
final Class<?> clazz = input.reflect();
return ModuleReflectionUtil.isCheckstyleModule(clazz)
&& (ModuleReflectionUtil.isCheckstyleTreeWalkerCheck(clazz)
|| ModuleReflectionUtil.isFileSetModule(clazz));
}
});
/**
* ArchCondition for immutable fields.
*/
private static final ArchCondition<JavaField> BE_IMMUTABLE = new ImmutableFieldArchCondition();
/**
* DescribedPredicate defining condition for a class to have immutable fields.
*/
private static final DescribedPredicate<JavaClass> IMMUTABLE_FIELDS =
new ImmutableFieldsPredicate();
/**
* Map of module full name to module details.
*/
private static final Map<String, ModuleDetails> MODULE_DETAILS_MAP =
XmlMetaReader.readAllModulesIncludingThirdPartyIfAny().stream()
.collect(Collectors.toUnmodifiableMap(ModuleDetails::getFullQualifiedName,
Function.identity()));
/**
* Test to ensure that fields in util classes are immutable.
*/
@Test
public void testUtilClassesImmutability() {
final JavaClasses utilClasses = new ClassFileImporter()
.withImportOption(ImportOption.Predefined.DO_NOT_INCLUDE_TESTS)
.importPackages("com.puppycrawl.tools.checkstyle.utils",
"com.puppycrawl.tools.checkstyle.checks.javadoc.utils");
final ArchCondition<JavaField> beSuppressedField = new SuppressionArchCondition<>(
SUPPRESSED_FIELDS_IN_UTIL_CLASSES, "be suppressed");
final ArchRule fieldsInUtilClassesShouldBeImmutable = fields()
.that()
.areDeclaredInClassesThat()
.haveSimpleNameEndingWith("Util")
.should(BE_IMMUTABLE)
.andShould()
.beFinal()
.andShould()
.beStatic()
.orShould(beSuppressedField);
fieldsInUtilClassesShouldBeImmutable.check(utilClasses);
}
/**
* Test to ensure modules annotated with {@link StatelessCheck} contain immutable fields.
*/
@Test
public void testFieldsInStatelessChecksShouldBeImmutable() {
final DescribedPredicate<JavaField> moduleProperties = new ModulePropertyPredicate();
final ArchCondition<JavaField> beSuppressedField = new SuppressionArchCondition<>(
SUPPRESSED_FIELDS_IN_MODULES, "be suppressed");
final ArchRule fieldsInStatelessChecksShouldBeImmutable = fields()
.that()
.areDeclaredInClassesThat()
.areAnnotatedWith(StatelessCheck.class)
.and(are(not(moduleProperties)))
.should(BE_IMMUTABLE)
.andShould()
.beFinal()
.orShould(beSuppressedField);
fieldsInStatelessChecksShouldBeImmutable.check(CHECKSTYLE_CHECKS);
}
/**
* Test to ensure classes with immutable fields are annotated with {@link StatelessCheck}.
*/
@Test
public void testClassesWithImmutableFieldsShouldBeStateless() {
final ArchCondition<JavaClass> beSuppressedClass = new SuppressionArchCondition<>(
SUPPRESSED_CLASSES_FOR_STATELESS_CHECK_RULE, "be suppressed");
final ArchRule classesWithImmutableFieldsShouldBeStateless = classes()
.that(have(IMMUTABLE_FIELDS))
.and()
.doNotHaveModifier(JavaModifier.ABSTRACT)
.should()
.beAnnotatedWith(StatelessCheck.class)
.orShould(beSuppressedClass);
classesWithImmutableFieldsShouldBeStateless.check(CHECKSTYLE_CHECKS);
}
/**
* Test to ensure classes with mutable fields are annotated with {@link FileStatefulCheck} or
* {@link GlobalStatefulCheck}.
*/
@Test
public void testClassesWithMutableFieldsShouldBeStateful() {
final ArchCondition<JavaClass> beSuppressedClass = new SuppressionArchCondition<>(
SUPPRESSED_CLASSES_FOR_STATEFUL_CHECK_RULE, "be suppressed");
final ArchRule classesWithMutableFieldsShouldBeStateful = classes()
.that(doNot(have(IMMUTABLE_FIELDS)))
.and()
.doNotHaveModifier(JavaModifier.ABSTRACT)
.should()
.beAnnotatedWith(FileStatefulCheck.class)
.orShould()
.beAnnotatedWith(GlobalStatefulCheck.class)
.orShould(beSuppressedClass);
classesWithMutableFieldsShouldBeStateful.check(CHECKSTYLE_CHECKS);
}
/**
* ArchCondition checking fields are immutable.
*/
private static final class ImmutableFieldArchCondition extends ArchCondition<JavaField> {
private ImmutableFieldArchCondition() {
super("be among immutable types");
}
/**
* Whether the raw type of the field is immutable.
*
* @param javaField java field to examine
* @return {@code true} if the raw type of field is immutable.
*/
private static boolean isRawTypeImmutable(JavaField javaField) {
final JavaClass rawType = javaField.getRawType();
final String rawTypeName = rawType.getName();
return PRIMITIVE_TYPES.contains(rawTypeName)
|| IMMUTABLE_TYPES.contains(rawTypeName);
}
/**
* Whether the field is an enum constant or an empty array.
*
* @param javaField java field to examine
* @return {@code true} if the field is an enum constant or an empty array
*/
private static boolean isEnumConstantOrEmptyArray(JavaField javaField) {
final JavaClass rawType = javaField.getRawType();
return rawType.isEnum()
|| ZERO_SIZE_ARRAY_FIELDS.contains(javaField.getFullName());
}
/**
* Whether the parameterized type of a field is immutable if it contains parameterized
* type. Parameterized type refers to the generic type of a field.
* {@code List<String>}, here the concrete type of parameterized field is
* {@code java.lang.String}.
*
* @param javaField java field to examine
* @return {@code true} if the parameterized type of a field is immutable
* if it contains parameterized type
*/
private static boolean isParameterizedTypeImmutable(JavaField javaField) {
boolean isParameterizedTypeImmutable = false;
final JavaType javaType = javaField.getType();
if (javaType instanceof JavaParameterizedType) {
final JavaParameterizedType parameterizedType = (JavaParameterizedType) javaType;
isParameterizedTypeImmutable = parameterizedType.getActualTypeArguments().stream()
.allMatch(actualTypeArgument -> {
return IMMUTABLE_TYPES.contains(actualTypeArgument.toErasure().getName());
});
}
return isParameterizedTypeImmutable;
}
@Override
public void check(JavaField item, ConditionEvents events) {
if (!isRawTypeImmutable(item)
&& !isEnumConstantOrEmptyArray(item)
&& !isParameterizedTypeImmutable(item)) {
final String message = String
.format(Locale.ROOT, "Field <%s> should %s in %s",
item.getFullName(), getDescription(),
item.getSourceCodeLocation());
events.add(SimpleConditionEvent.violated(item, message));
}
}
}
/**
* DescribedPredicate defining condition for a field to be a module property.
*/
private static final class ModulePropertyPredicate extends DescribedPredicate<JavaField> {
private ModulePropertyPredicate() {
super("module properties");
}
/**
* Whether a field is a module property or not.
*
* @param javaField field to check
* @return {@code true} if field is a module property
*/
private static boolean isModuleProperty(JavaField javaField) {
boolean result = false;
final JavaClass containingClass = javaField.getOwner();
final ModuleDetails moduleDetails = MODULE_DETAILS_MAP.get(
containingClass.getFullName());
if (moduleDetails != null) {
final List<ModulePropertyDetails> properties = moduleDetails.getProperties();
result = properties.stream()
.map(ModulePropertyDetails::getName)
.anyMatch(moduleName -> moduleName.equals(javaField.getName()));
}
return result;
}
@Override
public boolean test(JavaField input) {
return isModuleProperty(input);
}
}
/**
* DescribedPredicate defining condition for a class to have immutable fields.
*/
private static final class ImmutableFieldsPredicate extends DescribedPredicate<JavaClass> {
private ImmutableFieldsPredicate() {
super("immutable fields");
}
@Override
public boolean test(JavaClass input) {
final Set<JavaField> fields = input.getFields();
return fields.stream()
.filter(javaField -> {
return !ModulePropertyPredicate.isModuleProperty(javaField)
&& !SUPPRESSED_FIELDS_IN_MODULES.contains(javaField.getFullName());
})
.allMatch(javaField -> {
final Set<JavaModifier> javaFieldModifiers = javaField.getModifiers();
return javaFieldModifiers.contains(JavaModifier.FINAL)
&& (ImmutableFieldArchCondition.isRawTypeImmutable(javaField)
|| ImmutableFieldArchCondition.isEnumConstantOrEmptyArray(javaField)
|| ImmutableFieldArchCondition.isParameterizedTypeImmutable(javaField));
});
}
}
/**
* ArchCondition checking if a type or a member is present in the suppression list.
*/
private static final class SuppressionArchCondition<T extends HasName.AndFullName>
extends ArchCondition<T> {
private final Set<String> suppressions;
private SuppressionArchCondition(Set<String> suppressions, String description) {
super(description);
this.suppressions = suppressions;
}
@Override
public void check(HasName.AndFullName item, ConditionEvents events) {
if (!suppressions.contains(item.getFullName())) {
final String message = String.format(
Locale.ROOT, "should %s or resolved.", getDescription());
events.add(SimpleConditionEvent.violated(item, message));
}
}
}
}