|
1 | 1 | /*
|
2 |
| - * Copyright 2022-2023 the original author or authors. |
| 2 | + * Copyright 2022-2024 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
22 | 22 | import java.net.URLEncoder;
|
23 | 23 | import java.nio.file.Files;
|
24 | 24 | import java.nio.file.StandardOpenOption;
|
| 25 | +import java.util.Collections; |
25 | 26 | import java.util.List;
|
| 27 | +import java.util.Objects; |
| 28 | +import java.util.function.Supplier; |
26 | 29 | import java.util.stream.Collectors;
|
27 | 30 |
|
28 | 31 | import com.tngtech.archunit.base.DescribedPredicate;
|
|
47 | 50 | import org.gradle.api.file.FileCollection;
|
48 | 51 | import org.gradle.api.file.FileTree;
|
49 | 52 | import org.gradle.api.provider.ListProperty;
|
| 53 | +import org.gradle.api.provider.Property; |
50 | 54 | import org.gradle.api.tasks.IgnoreEmptyDirectories;
|
51 | 55 | import org.gradle.api.tasks.Input;
|
52 | 56 | import org.gradle.api.tasks.InputFiles;
|
|
63 | 67 | *
|
64 | 68 | * @author Andy Wilkinson
|
65 | 69 | * @author Yanming Zhou
|
| 70 | + * @author Ivan Malutin |
66 | 71 | */
|
67 | 72 | public abstract class ArchitectureCheck extends DefaultTask {
|
68 | 73 |
|
69 | 74 | private FileCollection classes;
|
70 | 75 |
|
71 | 76 | public ArchitectureCheck() {
|
72 | 77 | getOutputDirectory().convention(getProject().getLayout().getBuildDirectory().dir(getName()));
|
| 78 | + getProhibitObjectsRequireNonNull().convention(true); |
73 | 79 | getRules().addAll(allPackagesShouldBeFreeOfTangles(),
|
74 | 80 | allBeanPostProcessorBeanMethodsShouldBeStaticAndHaveParametersThatWillNotCausePrematureInitialization(),
|
75 | 81 | allBeanFactoryPostProcessorBeanMethodsShouldBeStaticAndHaveNoParameters(),
|
76 | 82 | noClassesShouldCallStepVerifierStepVerifyComplete(),
|
77 | 83 | noClassesShouldConfigureDefaultStepVerifierTimeout(), noClassesShouldCallCollectorsToList(),
|
78 | 84 | noClassesShouldCallURLEncoderWithStringEncoding(), noClassesShouldCallURLDecoderWithStringEncoding());
|
| 85 | + getRules().addAll(getProhibitObjectsRequireNonNull() |
| 86 | + .map((prohibit) -> prohibit ? noClassesShouldCallObjectsRequireNonNull() : Collections.emptyList())); |
79 | 87 | getRuleDescriptions().set(getRules().map((rules) -> rules.stream().map(ArchRule::getDescription).toList()));
|
80 | 88 | }
|
81 | 89 |
|
@@ -208,6 +216,18 @@ private ArchRule noClassesShouldCallURLDecoderWithStringEncoding() {
|
208 | 216 | .because("java.net.URLDecoder.decode(String s, Charset charset) should be used instead");
|
209 | 217 | }
|
210 | 218 |
|
| 219 | + private List<ArchRule> noClassesShouldCallObjectsRequireNonNull() { |
| 220 | + return List.of( |
| 221 | + ArchRuleDefinition.noClasses() |
| 222 | + .should() |
| 223 | + .callMethod(Objects.class, "requireNonNull", Object.class, String.class) |
| 224 | + .because("org.springframework.utils.Assert.notNull(Object, String) should be used instead"), |
| 225 | + ArchRuleDefinition.noClasses() |
| 226 | + .should() |
| 227 | + .callMethod(Objects.class, "requireNonNull", Object.class, Supplier.class) |
| 228 | + .because("org.springframework.utils.Assert.notNull(Object, Supplier) should be used instead")); |
| 229 | + } |
| 230 | + |
211 | 231 | public void setClasses(FileCollection classes) {
|
212 | 232 | this.classes = classes;
|
213 | 233 | }
|
@@ -236,8 +256,12 @@ final FileTree getInputClasses() {
|
236 | 256 | @Internal
|
237 | 257 | public abstract ListProperty<ArchRule> getRules();
|
238 | 258 |
|
| 259 | + @Internal |
| 260 | + public abstract Property<Boolean> getProhibitObjectsRequireNonNull(); |
| 261 | + |
239 | 262 | @Input
|
240 |
| - // The rules themselves can't be an input as they aren't serializable so we use their |
| 263 | + // The rules themselves can't be an input as they aren't serializable so we use |
| 264 | + // their |
241 | 265 | // descriptions instead
|
242 | 266 | abstract ListProperty<String> getRuleDescriptions();
|
243 | 267 |
|
|
0 commit comments