Skip to content

Commit fb22c18

Browse files
ivamlywilkinsona
authored andcommitted
Add rule to prevent calls to Objects.requireNonNull()
See gh-41611
1 parent 1a8e9c1 commit fb22c18

File tree

6 files changed

+201
-2
lines changed

6 files changed

+201
-2
lines changed

buildSrc/src/main/java/org/springframework/boot/build/architecture/ArchitectureCheck.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import java.nio.file.Files;
2424
import java.nio.file.StandardOpenOption;
2525
import java.util.List;
26+
import java.util.Objects;
27+
import java.util.function.Supplier;
2628
import java.util.stream.Collectors;
2729

2830
import com.tngtech.archunit.base.DescribedPredicate;
@@ -75,7 +77,9 @@ public ArchitectureCheck() {
7577
allBeanFactoryPostProcessorBeanMethodsShouldBeStaticAndHaveNoParameters(),
7678
noClassesShouldCallStepVerifierStepVerifyComplete(),
7779
noClassesShouldConfigureDefaultStepVerifierTimeout(), noClassesShouldCallCollectorsToList(),
78-
noClassesShouldCallURLEncoderWithStringEncoding(), noClassesShouldCallURLDecoderWithStringEncoding());
80+
noClassesShouldCallURLEncoderWithStringEncoding(), noClassesShouldCallURLDecoderWithStringEncoding(),
81+
noClassesShouldCallObjectsRequireNonNullWithMessage(),
82+
noClassesShouldCallObjectsRequireNonNullWithSupplier());
7983
getRuleDescriptions().set(getRules().map((rules) -> rules.stream().map(ArchRule::getDescription).toList()));
8084
}
8185

@@ -208,6 +212,20 @@ private ArchRule noClassesShouldCallURLDecoderWithStringEncoding() {
208212
.because("java.net.URLDecoder.decode(String s, Charset charset) should be used instead");
209213
}
210214

215+
private ArchRule noClassesShouldCallObjectsRequireNonNullWithMessage() {
216+
return ArchRuleDefinition.noClasses()
217+
.should()
218+
.callMethod(Objects.class, "requireNonNull", Object.class, String.class)
219+
.because("Use org.springframework.utils.Assert.notNull(Object, String) should be used instead");
220+
}
221+
222+
private ArchRule noClassesShouldCallObjectsRequireNonNullWithSupplier() {
223+
return ArchRuleDefinition.noClasses()
224+
.should()
225+
.callMethod(Objects.class, "requireNonNull", Object.class, Supplier.class)
226+
.because("Use org.springframework.utils.Assert.notNull(Object, Supplier) should be used instead");
227+
}
228+
211229
public void setClasses(FileCollection classes) {
212230
this.classes = classes;
213231
}
@@ -237,7 +255,8 @@ final FileTree getInputClasses() {
237255
public abstract ListProperty<ArchRule> getRules();
238256

239257
@Input
240-
// The rules themselves can't be an input as they aren't serializable so we use their
258+
// The rules themselves can't be an input as they aren't serializable so we use
259+
// their
241260
// descriptions instead
242261
abstract ListProperty<String> getRuleDescriptions();
243262

buildSrc/src/test/java/org/springframework/boot/build/architecture/ArchitectureCheckTests.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,38 @@ void whenBeanFactoryPostProcessorBeanMethodIsStaticAndHasNoParametersTaskSucceed
121121
});
122122
}
123123

124+
@Test
125+
void whenClassCallsObjectsRequireNonNullWithMessageTaskFailsAndWritesReport() throws Exception {
126+
prepareTask("objects/requireNonNullWithMessage", (architectureCheck) -> {
127+
assertThatExceptionOfType(GradleException.class).isThrownBy(architectureCheck::checkArchitecture);
128+
assertThat(failureReport(architectureCheck)).isNotEmpty();
129+
});
130+
}
131+
132+
@Test
133+
void whenClassDoesNotCallObjectsRequireNonNullWithMessageTaskSucceedsAndWritesAnEmptyReport() throws Exception {
134+
prepareTask("objects/noRequireNonNullWithMessage", (architectureCheck) -> {
135+
architectureCheck.checkArchitecture();
136+
assertThat(failureReport(architectureCheck)).isEmpty();
137+
});
138+
}
139+
140+
@Test
141+
void whenClassCallsObjectsRequireNonNullWithSupplierTaskFailsAndWritesReport() throws Exception {
142+
prepareTask("objects/requireNonNullWithSupplier", (architectureCheck) -> {
143+
assertThatExceptionOfType(GradleException.class).isThrownBy(architectureCheck::checkArchitecture);
144+
assertThat(failureReport(architectureCheck)).isNotEmpty();
145+
});
146+
}
147+
148+
@Test
149+
void whenClassDoesNotCallObjectsRequireNonNullWithSupplierTaskSucceedsAndWritesAnEmptyReport() throws Exception {
150+
prepareTask("objects/noRequireNonNullWithSupplier", (architectureCheck) -> {
151+
architectureCheck.checkArchitecture();
152+
assertThat(failureReport(architectureCheck)).isEmpty();
153+
});
154+
}
155+
124156
private void prepareTask(String classes, Callback<ArchitectureCheck> callback) throws Exception {
125157
File projectDir = new File(this.temp, "project");
126158
projectDir.mkdirs();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.build.architecture.objects.noRequireNonNullWithMessage;
18+
19+
import org.springframework.util.Assert;
20+
21+
/**
22+
* This class uses `Assert.notNull(Object, String)` instead of
23+
* `Objects.requireNonNull(Object, String)`, and should pass the architecture check.
24+
*
25+
* @author Ivan Malutin
26+
*/
27+
public class NoRequireNonNullWithMessageUsage {
28+
29+
/**
30+
* Example method that uses `Assert.notNull(Object, String)`, which should not be
31+
* flagged by the architecture check.
32+
*/
33+
public void exampleMethod() {
34+
Assert.notNull(new Object(), "Object must not be null");
35+
}
36+
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.build.architecture.objects.noRequireNonNullWithSupplier;
18+
19+
import org.springframework.util.Assert;
20+
21+
/**
22+
* This class uses `Assert.notNull(Object, Supplier)` instead of
23+
* `Objects.requireNonNull(Object, Supplier)`, and should pass the architecture check.
24+
*
25+
* @author Ivan Malutin
26+
*/
27+
public class NoRequireNonNullWithSupplierUsage {
28+
29+
/**
30+
* Example method that uses `Assert.notNull(Object, Supplier)`, which should not be
31+
* flagged by the architecture check.
32+
*/
33+
public void exampleMethod() {
34+
Assert.notNull(new Object(), () -> "Object must not be null");
35+
}
36+
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.build.architecture.objects.requireNonNullWithMessage;
18+
19+
import java.util.Objects;
20+
21+
/**
22+
* This class is intentionally designed to test the use of `Objects.requireNonNull(Object,
23+
* String)`, which should trigger a failure in the architecture check.
24+
*
25+
* @author Ivan Malutin
26+
*/
27+
public class RequireNonNullWithMessageUsage {
28+
29+
/**
30+
* Example method that uses `Objects.requireNonNull(Object, String)`, which should be
31+
* flagged by the architecture check.
32+
*/
33+
public void exampleMethod() {
34+
Objects.requireNonNull(new Object(), "Object cannot be null");
35+
}
36+
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.build.architecture.objects.requireNonNullWithSupplier;
18+
19+
import java.util.Objects;
20+
21+
/**
22+
* This class is intentionally designed to test the use of `Objects.requireNonNull(Object,
23+
* Supplier)`, which should trigger a failure in the architecture check.
24+
*
25+
* @author Ivan Malutin
26+
*/
27+
public class RequireNonNullWithSupplierUsage {
28+
29+
/**
30+
* Example method that uses `Objects.requireNonNull(Object, Supplier)`, which should
31+
* be flagged by the architecture check.
32+
*/
33+
public void exampleMethod() {
34+
Objects.requireNonNull(new Object(), () -> "Object cannot be null");
35+
}
36+
37+
}

0 commit comments

Comments
 (0)