Skip to content

Add rule to prevent calls to Objects.requireNonNull() #41611

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.List;
import java.util.Objects;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import com.tngtech.archunit.base.DescribedPredicate;
Expand Down Expand Up @@ -83,7 +85,8 @@ public ArchitectureCheck() {
noClassesShouldCallStepVerifierStepVerifyComplete(),
noClassesShouldConfigureDefaultStepVerifierTimeout(), noClassesShouldCallCollectorsToList(),
noClassesShouldCallURLEncoderWithStringEncoding(), noClassesShouldCallURLDecoderWithStringEncoding(),
noClassesShouldLoadResourcesUsingResourceUtils());
noClassesShouldLoadResourcesUsingResourceUtils(), noClassesShouldCallObjectsRequireNonNullWithMessage(),
noClassesShouldCallObjectsRequireNonNullWithSupplier());
getRuleDescriptions().set(getRules().map((rules) -> rules.stream().map(ArchRule::getDescription).toList()));
}

Expand Down Expand Up @@ -228,6 +231,20 @@ private ArchRule noClassesShouldLoadResourcesUsingResourceUtils() {
.because("org.springframework.boot.io.ApplicationResourceLoader should be used instead");
}

private ArchRule noClassesShouldCallObjectsRequireNonNullWithMessage() {
return ArchRuleDefinition.noClasses()
.should()
.callMethod(Objects.class, "requireNonNull", Object.class, String.class)
.because("Use org.springframework.utils.Assert.notNull(Object, String) should be used instead");
}

private ArchRule noClassesShouldCallObjectsRequireNonNullWithSupplier() {
return ArchRuleDefinition.noClasses()
.should()
.callMethod(Objects.class, "requireNonNull", Object.class, Supplier.class)
.because("Use org.springframework.utils.Assert.notNull(Object, Supplier) should be used instead");
}

public void setClasses(FileCollection classes) {
this.classes = classes;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,38 @@ void whenClassUsesResourceUtilsWithoutLoadingResourcesTaskSucceedsAndWritesAnEmp
});
}

@Test
void whenClassCallsObjectsRequireNonNullWithMessageTaskFailsAndWritesReport() throws Exception {
prepareTask("objects/requireNonNullWithMessage", (architectureCheck) -> {
assertThatExceptionOfType(GradleException.class).isThrownBy(architectureCheck::checkArchitecture);
assertThat(failureReport(architectureCheck)).isNotEmpty();
});
}

@Test
void whenClassDoesNotCallObjectsRequireNonNullWithMessageTaskSucceedsAndWritesAnEmptyReport() throws Exception {
prepareTask("objects/noRequireNonNullWithMessage", (architectureCheck) -> {
architectureCheck.checkArchitecture();
assertThat(failureReport(architectureCheck)).isEmpty();
});
}

@Test
void whenClassCallsObjectsRequireNonNullWithSupplierTaskFailsAndWritesReport() throws Exception {
prepareTask("objects/requireNonNullWithSupplier", (architectureCheck) -> {
assertThatExceptionOfType(GradleException.class).isThrownBy(architectureCheck::checkArchitecture);
assertThat(failureReport(architectureCheck)).isNotEmpty();
});
}

@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 {
File projectDir = new File(this.temp, "project");
projectDir.mkdirs();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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.noRequireNonNullWithMessage;

import org.springframework.util.Assert;

/**
* 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 {

/**
* 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");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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.requireNonNullWithMessage;

import java.util.Objects;

/**
* 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 {

/**
* 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");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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.requireNonNullWithSupplier;

import java.util.Objects;

/**
* 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 {

/**
* 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");
}

}