|
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;
|
|
51 | 54 | import org.gradle.api.file.FileCollection;
|
52 | 55 | import org.gradle.api.file.FileTree;
|
53 | 56 | import org.gradle.api.provider.ListProperty;
|
| 57 | +import org.gradle.api.provider.Property; |
54 | 58 | import org.gradle.api.tasks.IgnoreEmptyDirectories;
|
55 | 59 | import org.gradle.api.tasks.Input;
|
56 | 60 | import org.gradle.api.tasks.InputFiles;
|
|
70 | 74 | * @author Andy Wilkinson
|
71 | 75 | * @author Yanming Zhou
|
72 | 76 | * @author Scott Frederick
|
| 77 | + * @author Ivan Malutin |
73 | 78 | */
|
74 | 79 | public abstract class ArchitectureCheck extends DefaultTask {
|
75 | 80 |
|
76 | 81 | private FileCollection classes;
|
77 | 82 |
|
78 | 83 | public ArchitectureCheck() {
|
79 | 84 | getOutputDirectory().convention(getProject().getLayout().getBuildDirectory().dir(getName()));
|
| 85 | + getProhibitObjectsRequireNonNull().convention(true); |
80 | 86 | getRules().addAll(allPackagesShouldBeFreeOfTangles(),
|
81 | 87 | allBeanPostProcessorBeanMethodsShouldBeStaticAndHaveParametersThatWillNotCausePrematureInitialization(),
|
82 | 88 | allBeanFactoryPostProcessorBeanMethodsShouldBeStaticAndHaveNoParameters(),
|
83 | 89 | noClassesShouldCallStepVerifierStepVerifyComplete(),
|
84 | 90 | noClassesShouldConfigureDefaultStepVerifierTimeout(), noClassesShouldCallCollectorsToList(),
|
85 | 91 | noClassesShouldCallURLEncoderWithStringEncoding(), noClassesShouldCallURLDecoderWithStringEncoding(),
|
86 | 92 | noClassesShouldLoadResourcesUsingResourceUtils());
|
| 93 | + getRules().addAll(getProhibitObjectsRequireNonNull() |
| 94 | + .map((prohibit) -> prohibit ? noClassesShouldCallObjectsRequireNonNull() : Collections.emptyList())); |
87 | 95 | getRuleDescriptions().set(getRules().map((rules) -> rules.stream().map(ArchRule::getDescription).toList()));
|
88 | 96 | }
|
89 | 97 |
|
@@ -228,6 +236,18 @@ private ArchRule noClassesShouldLoadResourcesUsingResourceUtils() {
|
228 | 236 | .because("org.springframework.boot.io.ApplicationResourceLoader should be used instead");
|
229 | 237 | }
|
230 | 238 |
|
| 239 | + private List<ArchRule> noClassesShouldCallObjectsRequireNonNull() { |
| 240 | + return List.of( |
| 241 | + ArchRuleDefinition.noClasses() |
| 242 | + .should() |
| 243 | + .callMethod(Objects.class, "requireNonNull", Object.class, String.class) |
| 244 | + .because("org.springframework.utils.Assert.notNull(Object, String) should be used instead"), |
| 245 | + ArchRuleDefinition.noClasses() |
| 246 | + .should() |
| 247 | + .callMethod(Objects.class, "requireNonNull", Object.class, Supplier.class) |
| 248 | + .because("org.springframework.utils.Assert.notNull(Object, Supplier) should be used instead")); |
| 249 | + } |
| 250 | + |
231 | 251 | public void setClasses(FileCollection classes) {
|
232 | 252 | this.classes = classes;
|
233 | 253 | }
|
@@ -256,9 +276,12 @@ final FileTree getInputClasses() {
|
256 | 276 | @Internal
|
257 | 277 | public abstract ListProperty<ArchRule> getRules();
|
258 | 278 |
|
| 279 | + @Internal |
| 280 | + public abstract Property<Boolean> getProhibitObjectsRequireNonNull(); |
| 281 | + |
259 | 282 | @Input
|
260 |
| - // The rules themselves can't be an input as they aren't serializable so we use their |
261 |
| - // descriptions instead |
| 283 | + // The rules themselves can't be an input as they aren't serializable so we use |
| 284 | + // their descriptions instead |
262 | 285 | abstract ListProperty<String> getRuleDescriptions();
|
263 | 286 |
|
264 | 287 | }
|
0 commit comments