Polish "Add rule to prevent calls to Objects.requireNonNull()"
See gh-41611
This commit is contained in:
parent
fb22c189f4
commit
4ee24bf9bd
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2022-2023 the original author or authors.
|
* Copyright 2022-2024 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -22,6 +22,7 @@ import java.net.URLDecoder;
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.StandardOpenOption;
|
import java.nio.file.StandardOpenOption;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
@ -49,6 +50,7 @@ import org.gradle.api.file.DirectoryProperty;
|
||||||
import org.gradle.api.file.FileCollection;
|
import org.gradle.api.file.FileCollection;
|
||||||
import org.gradle.api.file.FileTree;
|
import org.gradle.api.file.FileTree;
|
||||||
import org.gradle.api.provider.ListProperty;
|
import org.gradle.api.provider.ListProperty;
|
||||||
|
import org.gradle.api.provider.Property;
|
||||||
import org.gradle.api.tasks.IgnoreEmptyDirectories;
|
import org.gradle.api.tasks.IgnoreEmptyDirectories;
|
||||||
import org.gradle.api.tasks.Input;
|
import org.gradle.api.tasks.Input;
|
||||||
import org.gradle.api.tasks.InputFiles;
|
import org.gradle.api.tasks.InputFiles;
|
||||||
|
@ -65,6 +67,7 @@ import org.gradle.api.tasks.TaskAction;
|
||||||
*
|
*
|
||||||
* @author Andy Wilkinson
|
* @author Andy Wilkinson
|
||||||
* @author Yanming Zhou
|
* @author Yanming Zhou
|
||||||
|
* @author Ivan Malutin
|
||||||
*/
|
*/
|
||||||
public abstract class ArchitectureCheck extends DefaultTask {
|
public abstract class ArchitectureCheck extends DefaultTask {
|
||||||
|
|
||||||
|
@ -72,14 +75,15 @@ public abstract class ArchitectureCheck extends DefaultTask {
|
||||||
|
|
||||||
public ArchitectureCheck() {
|
public ArchitectureCheck() {
|
||||||
getOutputDirectory().convention(getProject().getLayout().getBuildDirectory().dir(getName()));
|
getOutputDirectory().convention(getProject().getLayout().getBuildDirectory().dir(getName()));
|
||||||
|
getProhibitObjectsRequireNonNull().convention(true);
|
||||||
getRules().addAll(allPackagesShouldBeFreeOfTangles(),
|
getRules().addAll(allPackagesShouldBeFreeOfTangles(),
|
||||||
allBeanPostProcessorBeanMethodsShouldBeStaticAndHaveParametersThatWillNotCausePrematureInitialization(),
|
allBeanPostProcessorBeanMethodsShouldBeStaticAndHaveParametersThatWillNotCausePrematureInitialization(),
|
||||||
allBeanFactoryPostProcessorBeanMethodsShouldBeStaticAndHaveNoParameters(),
|
allBeanFactoryPostProcessorBeanMethodsShouldBeStaticAndHaveNoParameters(),
|
||||||
noClassesShouldCallStepVerifierStepVerifyComplete(),
|
noClassesShouldCallStepVerifierStepVerifyComplete(),
|
||||||
noClassesShouldConfigureDefaultStepVerifierTimeout(), noClassesShouldCallCollectorsToList(),
|
noClassesShouldConfigureDefaultStepVerifierTimeout(), noClassesShouldCallCollectorsToList(),
|
||||||
noClassesShouldCallURLEncoderWithStringEncoding(), noClassesShouldCallURLDecoderWithStringEncoding(),
|
noClassesShouldCallURLEncoderWithStringEncoding(), noClassesShouldCallURLDecoderWithStringEncoding());
|
||||||
noClassesShouldCallObjectsRequireNonNullWithMessage(),
|
getRules().addAll(getProhibitObjectsRequireNonNull()
|
||||||
noClassesShouldCallObjectsRequireNonNullWithSupplier());
|
.map((prohibit) -> prohibit ? noClassesShouldCallObjectsRequireNonNull() : Collections.emptyList()));
|
||||||
getRuleDescriptions().set(getRules().map((rules) -> rules.stream().map(ArchRule::getDescription).toList()));
|
getRuleDescriptions().set(getRules().map((rules) -> rules.stream().map(ArchRule::getDescription).toList()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -212,18 +216,16 @@ public abstract class ArchitectureCheck extends DefaultTask {
|
||||||
.because("java.net.URLDecoder.decode(String s, Charset charset) should be used instead");
|
.because("java.net.URLDecoder.decode(String s, Charset charset) should be used instead");
|
||||||
}
|
}
|
||||||
|
|
||||||
private ArchRule noClassesShouldCallObjectsRequireNonNullWithMessage() {
|
private List<ArchRule> noClassesShouldCallObjectsRequireNonNull() {
|
||||||
return ArchRuleDefinition.noClasses()
|
return List.of(
|
||||||
|
ArchRuleDefinition.noClasses()
|
||||||
.should()
|
.should()
|
||||||
.callMethod(Objects.class, "requireNonNull", Object.class, String.class)
|
.callMethod(Objects.class, "requireNonNull", Object.class, String.class)
|
||||||
.because("Use org.springframework.utils.Assert.notNull(Object, String) should be used instead");
|
.because("org.springframework.utils.Assert.notNull(Object, String) should be used instead"),
|
||||||
}
|
ArchRuleDefinition.noClasses()
|
||||||
|
|
||||||
private ArchRule noClassesShouldCallObjectsRequireNonNullWithSupplier() {
|
|
||||||
return ArchRuleDefinition.noClasses()
|
|
||||||
.should()
|
.should()
|
||||||
.callMethod(Objects.class, "requireNonNull", Object.class, Supplier.class)
|
.callMethod(Objects.class, "requireNonNull", Object.class, Supplier.class)
|
||||||
.because("Use org.springframework.utils.Assert.notNull(Object, Supplier) should be used instead");
|
.because("org.springframework.utils.Assert.notNull(Object, Supplier) should be used instead"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setClasses(FileCollection classes) {
|
public void setClasses(FileCollection classes) {
|
||||||
|
@ -254,6 +256,9 @@ public abstract class ArchitectureCheck extends DefaultTask {
|
||||||
@Internal
|
@Internal
|
||||||
public abstract ListProperty<ArchRule> getRules();
|
public abstract ListProperty<ArchRule> getRules();
|
||||||
|
|
||||||
|
@Internal
|
||||||
|
public abstract Property<Boolean> getProhibitObjectsRequireNonNull();
|
||||||
|
|
||||||
@Input
|
@Input
|
||||||
// The rules themselves can't be an input as they aren't serializable so we use
|
// The rules themselves can't be an input as they aren't serializable so we use
|
||||||
// their
|
// their
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2023 the original author or authors.
|
* Copyright 2012-2024 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -36,6 +36,7 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||||
* Tests for {@link ArchitectureCheck}.
|
* Tests for {@link ArchitectureCheck}.
|
||||||
*
|
*
|
||||||
* @author Andy Wilkinson
|
* @author Andy Wilkinson
|
||||||
|
* @author Ivan Malutin
|
||||||
*/
|
*/
|
||||||
class ArchitectureCheckTests {
|
class ArchitectureCheckTests {
|
||||||
|
|
||||||
|
@ -122,18 +123,18 @@ class ArchitectureCheckTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void whenClassCallsObjectsRequireNonNullWithMessageTaskFailsAndWritesReport() throws Exception {
|
void whenClassDoesNotCallObjectsRequireNonNullTaskSucceedsAndWritesAnEmptyReport() throws Exception {
|
||||||
prepareTask("objects/requireNonNullWithMessage", (architectureCheck) -> {
|
prepareTask("objects/noRequireNonNull", (architectureCheck) -> {
|
||||||
assertThatExceptionOfType(GradleException.class).isThrownBy(architectureCheck::checkArchitecture);
|
architectureCheck.checkArchitecture();
|
||||||
assertThat(failureReport(architectureCheck)).isNotEmpty();
|
assertThat(failureReport(architectureCheck)).isEmpty();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void whenClassDoesNotCallObjectsRequireNonNullWithMessageTaskSucceedsAndWritesAnEmptyReport() throws Exception {
|
void whenClassCallsObjectsRequireNonNullWithMessageTaskFailsAndWritesReport() throws Exception {
|
||||||
prepareTask("objects/noRequireNonNullWithMessage", (architectureCheck) -> {
|
prepareTask("objects/requireNonNullWithString", (architectureCheck) -> {
|
||||||
architectureCheck.checkArchitecture();
|
assertThatExceptionOfType(GradleException.class).isThrownBy(architectureCheck::checkArchitecture);
|
||||||
assertThat(failureReport(architectureCheck)).isEmpty();
|
assertThat(failureReport(architectureCheck)).isNotEmpty();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -145,14 +146,6 @@ class ArchitectureCheckTests {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
void whenClassDoesNotCallObjectsRequireNonNullWithSupplierTaskSucceedsAndWritesAnEmptyReport() throws Exception {
|
|
||||||
prepareTask("objects/noRequireNonNullWithSupplier", (architectureCheck) -> {
|
|
||||||
architectureCheck.checkArchitecture();
|
|
||||||
assertThat(failureReport(architectureCheck)).isEmpty();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private void prepareTask(String classes, Callback<ArchitectureCheck> callback) throws Exception {
|
private void prepareTask(String classes, Callback<ArchitectureCheck> callback) throws Exception {
|
||||||
File projectDir = new File(this.temp, "project");
|
File projectDir = new File(this.temp, "project");
|
||||||
projectDir.mkdirs();
|
projectDir.mkdirs();
|
||||||
|
|
|
@ -14,24 +14,19 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.boot.build.architecture.objects.noRequireNonNullWithMessage;
|
package org.springframework.boot.build.architecture.objects.noRequireNonNull;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
/**
|
class NoRequireNonNull {
|
||||||
* This class uses `Assert.notNull(Object, String)` instead of
|
|
||||||
* `Objects.requireNonNull(Object, String)`, and should pass the architecture check.
|
|
||||||
*
|
|
||||||
* @author Ivan Malutin
|
|
||||||
*/
|
|
||||||
public class NoRequireNonNullWithMessageUsage {
|
|
||||||
|
|
||||||
/**
|
void exampleMethod() {
|
||||||
* Example method that uses `Assert.notNull(Object, String)`, which should not be
|
|
||||||
* flagged by the architecture check.
|
|
||||||
*/
|
|
||||||
public void exampleMethod() {
|
|
||||||
Assert.notNull(new Object(), "Object must not be null");
|
Assert.notNull(new Object(), "Object must not be null");
|
||||||
|
// Compilation of a method reference generates code that uses
|
||||||
|
// Objects.requireNonNull(Object). Check that it doesn't cause a failure.
|
||||||
|
Collections.emptyList().forEach(System.out::println);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,37 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright 2012-2024 the original author or authors.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* https://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.springframework.boot.build.architecture.objects.noRequireNonNullWithSupplier;
|
|
||||||
|
|
||||||
import org.springframework.util.Assert;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This class uses `Assert.notNull(Object, Supplier)` instead of
|
|
||||||
* `Objects.requireNonNull(Object, Supplier)`, and should pass the architecture check.
|
|
||||||
*
|
|
||||||
* @author Ivan Malutin
|
|
||||||
*/
|
|
||||||
public class NoRequireNonNullWithSupplierUsage {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Example method that uses `Assert.notNull(Object, Supplier)`, which should not be
|
|
||||||
* flagged by the architecture check.
|
|
||||||
*/
|
|
||||||
public void exampleMethod() {
|
|
||||||
Assert.notNull(new Object(), () -> "Object must not be null");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -14,23 +14,13 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.boot.build.architecture.objects.requireNonNullWithMessage;
|
package org.springframework.boot.build.architecture.objects.requireNonNullWithString;
|
||||||
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
class RequireNonNullWithString {
|
||||||
* This class is intentionally designed to test the use of `Objects.requireNonNull(Object,
|
|
||||||
* String)`, which should trigger a failure in the architecture check.
|
|
||||||
*
|
|
||||||
* @author Ivan Malutin
|
|
||||||
*/
|
|
||||||
public class RequireNonNullWithMessageUsage {
|
|
||||||
|
|
||||||
/**
|
void exampleMethod() {
|
||||||
* Example method that uses `Objects.requireNonNull(Object, String)`, which should be
|
|
||||||
* flagged by the architecture check.
|
|
||||||
*/
|
|
||||||
public void exampleMethod() {
|
|
||||||
Objects.requireNonNull(new Object(), "Object cannot be null");
|
Objects.requireNonNull(new Object(), "Object cannot be null");
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,19 +18,9 @@ package org.springframework.boot.build.architecture.objects.requireNonNullWithSu
|
||||||
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
class RequireNonNullWithSupplier {
|
||||||
* This class is intentionally designed to test the use of `Objects.requireNonNull(Object,
|
|
||||||
* Supplier)`, which should trigger a failure in the architecture check.
|
|
||||||
*
|
|
||||||
* @author Ivan Malutin
|
|
||||||
*/
|
|
||||||
public class RequireNonNullWithSupplierUsage {
|
|
||||||
|
|
||||||
/**
|
void exampleMethod() {
|
||||||
* Example method that uses `Objects.requireNonNull(Object, Supplier)`, which should
|
|
||||||
* be flagged by the architecture check.
|
|
||||||
*/
|
|
||||||
public void exampleMethod() {
|
|
||||||
Objects.requireNonNull(new Object(), () -> "Object cannot be null");
|
Objects.requireNonNull(new Object(), () -> "Object cannot be null");
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,3 +21,7 @@ dependencies {
|
||||||
testRuntimeOnly("org.bouncycastle:bcprov-jdk18on:1.78.1")
|
testRuntimeOnly("org.bouncycastle:bcprov-jdk18on:1.78.1")
|
||||||
testRuntimeOnly("org.springframework:spring-webmvc")
|
testRuntimeOnly("org.springframework:spring-webmvc")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tasks.named("checkArchitectureMain").configure {
|
||||||
|
prohibitObjectsRequireNonNull = false
|
||||||
|
}
|
||||||
|
|
|
@ -21,3 +21,7 @@ dependencies {
|
||||||
testRuntimeOnly("org.bouncycastle:bcprov-jdk18on:1.78.1")
|
testRuntimeOnly("org.bouncycastle:bcprov-jdk18on:1.78.1")
|
||||||
testRuntimeOnly("org.springframework:spring-webmvc")
|
testRuntimeOnly("org.springframework:spring-webmvc")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tasks.named("checkArchitectureMain").configure {
|
||||||
|
prohibitObjectsRequireNonNull = false
|
||||||
|
}
|
||||||
|
|
|
@ -24,7 +24,6 @@ import java.util.ArrayList;
|
||||||
import java.util.EnumMap;
|
import java.util.EnumMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import com.jayway.jsonpath.JsonPath;
|
import com.jayway.jsonpath.JsonPath;
|
||||||
|
@ -616,8 +615,8 @@ class ValueObjectBinderTests {
|
||||||
private final Object value;
|
private final Object value;
|
||||||
|
|
||||||
ExampleFailingConstructorBean(String name, String value) {
|
ExampleFailingConstructorBean(String name, String value) {
|
||||||
Objects.requireNonNull(name, "'name' must be not null.");
|
Assert.notNull(name, "'name' must be not null.");
|
||||||
Objects.requireNonNull(value, "'value' must be not null.");
|
Assert.notNull(value, "'value' must be not null.");
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue