Skip to content

Commit f08188d

Browse files
committed
Change relevant Assert calls to throw IllegalStateException
Change certain Assert class from `assert...` to `assertState` so that a more appropriate `IllegalStateException` is thrown. Fixes gh-43779
1 parent 29baaf3 commit f08188d

File tree

76 files changed

+201
-188
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+201
-188
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/availability/AvailabilityStateHealthIndicator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -69,7 +69,7 @@ private <S extends AvailabilityState> void assertAllEnumsMapped(Class<S> stateTy
6969
if (!this.statusMappings.containsKey(null) && Enum.class.isAssignableFrom(stateType)) {
7070
EnumSet elements = EnumSet.allOf((Class) stateType);
7171
for (Object element : elements) {
72-
Assert.isTrue(this.statusMappings.containsKey(element),
72+
Assert.state(this.statusMappings.containsKey(element),
7373
() -> "StatusMappings does not include " + element);
7474
}
7575
}

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/management/HeapDumpWebEndpoint.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -209,7 +209,7 @@ private OpenJ9DiagnosticsMXBeanHeapDumper() {
209209

210210
@Override
211211
public File dumpHeap(Boolean live) throws IOException, InterruptedException {
212-
Assert.isNull(live, "OpenJ9DiagnosticsMXBean does not support live parameter when dumping the heap");
212+
Assert.state(live == null, "OpenJ9DiagnosticsMXBean does not support live parameter when dumping the heap");
213213
return new File(
214214
(String) ReflectionUtils.invokeMethod(this.dumpHeapMethod, this.diagnosticMXBean, "heap", null));
215215
}

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/availability/AvailabilityStateHealthIndicatorTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@
2828

2929
import static org.assertj.core.api.Assertions.assertThat;
3030
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
31+
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
3132
import static org.mockito.BDDMockito.given;
3233

3334
/**
@@ -67,7 +68,7 @@ void createWhenStatusMappingIsNullThrowsException() {
6768

6869
@Test
6970
void createWhenStatusMappingDoesNotCoverAllEnumsThrowsException() {
70-
assertThatIllegalArgumentException()
71+
assertThatIllegalStateException()
7172
.isThrownBy(() -> new AvailabilityStateHealthIndicator(this.applicationAvailability, LivenessState.class,
7273
(statusMappings) -> statusMappings.add(LivenessState.CORRECT, Status.UP)))
7374
.withMessage("StatusMappings does not include BROKEN");

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AutoConfigurationImportSelector.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -57,6 +57,7 @@
5757
import org.springframework.core.type.classreading.MetadataReaderFactory;
5858
import org.springframework.util.Assert;
5959
import org.springframework.util.ClassUtils;
60+
import org.springframework.util.CollectionUtils;
6061
import org.springframework.util.StringUtils;
6162

6263
/**
@@ -170,7 +171,7 @@ protected boolean isEnabled(AnnotationMetadata metadata) {
170171
protected AnnotationAttributes getAttributes(AnnotationMetadata metadata) {
171172
String name = getAnnotationClass().getName();
172173
AnnotationAttributes attributes = AnnotationAttributes.fromMap(metadata.getAnnotationAttributes(name, true));
173-
Assert.notNull(attributes, () -> "No auto-configuration attributes found. Is " + metadata.getClassName()
174+
Assert.state(attributes != null, () -> "No auto-configuration attributes found. Is " + metadata.getClassName()
174175
+ " annotated with " + ClassUtils.getShortName(name) + "?");
175176
return attributes;
176177
}
@@ -195,7 +196,7 @@ protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, A
195196
ImportCandidates importCandidates = ImportCandidates.load(this.autoConfigurationAnnotation,
196197
getBeanClassLoader());
197198
List<String> configurations = importCandidates.getCandidates();
198-
Assert.notEmpty(configurations,
199+
Assert.state(!CollectionUtils.isEmpty(configurations),
199200
"No auto configuration classes found in " + "META-INF/spring/"
200201
+ this.autoConfigurationAnnotation.getName() + ".imports. If you "
201202
+ "are using a custom packaging, make sure that file is correct.");

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/JobLauncherApplicationRunner.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -112,10 +112,10 @@ public JobLauncherApplicationRunner(JobLauncher jobLauncher, JobExplorer jobExpl
112112

113113
@Override
114114
public void afterPropertiesSet() {
115-
Assert.isTrue(this.jobs.size() <= 1 || StringUtils.hasText(this.jobName),
115+
Assert.state(this.jobs.size() <= 1 || StringUtils.hasText(this.jobName),
116116
"Job name must be specified in case of multiple jobs");
117117
if (StringUtils.hasText(this.jobName)) {
118-
Assert.isTrue(isLocalJob(this.jobName) || isRegisteredJob(this.jobName),
118+
Assert.state(isLocalJob(this.jobName) || isRegisteredJob(this.jobName),
119119
() -> "No job found with name '" + this.jobName + "'");
120120
}
121121
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfiguration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -102,7 +102,7 @@ static class CacheManagerValidator implements InitializingBean {
102102

103103
@Override
104104
public void afterPropertiesSet() {
105-
Assert.notNull(this.cacheManager.getIfAvailable(),
105+
Assert.state(this.cacheManager.getIfAvailable() != null,
106106
() -> "No cache manager could be auto-configured, check your configuration (caching type is '"
107107
+ this.cacheProperties.getType() + "')");
108108
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/OnBeanCondition.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -250,8 +250,8 @@ private ConfigurableListableBeanFactory getSearchBeanFactory(Spec<?> spec) {
250250
ConfigurableListableBeanFactory beanFactory = spec.getContext().getBeanFactory();
251251
if (spec.getStrategy() == SearchStrategy.ANCESTORS) {
252252
BeanFactory parent = beanFactory.getParentBeanFactory();
253-
Assert.isInstanceOf(ConfigurableListableBeanFactory.class, parent,
254-
"Unable to use SearchStrategy.ANCESTORS");
253+
Assert.state(parent instanceof ConfigurableListableBeanFactory,
254+
"Unable to use SearchStrategy.ANCESTORS without ConfigurableListableBeanFactory");
255255
beanFactory = (ConfigurableListableBeanFactory) parent;
256256
}
257257
return beanFactory;

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/OnResourceCondition.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -45,7 +45,7 @@ public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeM
4545
ResourceLoader loader = context.getResourceLoader();
4646
List<String> locations = new ArrayList<>();
4747
collectValues(locations, attributes.get("resources"));
48-
Assert.isTrue(!locations.isEmpty(),
48+
Assert.state(!locations.isEmpty(),
4949
"@ConditionalOnResource annotations must specify at least one resource location");
5050
List<String> missing = new ArrayList<>();
5151
for (String location : locations) {

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ static class Extension<E extends ConfigurationExtension> {
583583
Extension(FluentConfiguration configuration, Class<E> type, String name) {
584584
this.extension = SingletonSupplier.of(() -> {
585585
E extension = configuration.getPluginRegister().getPlugin(type);
586-
Assert.notNull(extension, () -> "Flyway %s extension missing".formatted(name));
586+
Assert.state(extension != null, () -> "Flyway %s extension missing".formatted(name));
587587
return extension;
588588
});
589589
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastProperties.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -52,7 +52,7 @@ public Resource resolveConfigLocation() {
5252
if (this.config == null) {
5353
return null;
5454
}
55-
Assert.isTrue(this.config.exists(),
55+
Assert.state(this.config.exists(),
5656
() -> "Hazelcast configuration does not exist '" + this.config.getDescription() + "'");
5757
return this.config;
5858
}

0 commit comments

Comments
 (0)