Skip to content

Commit abf3400

Browse files
committed
Use Assert.state() where appropriate
1 parent 2aa7888 commit abf3400

File tree

30 files changed

+80
-82
lines changed

30 files changed

+80
-82
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/serviceloader/AbstractServiceLoaderBasedFactoryBean.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2022 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 @@ public void setBeanClassLoader(@Nullable ClassLoader beanClassLoader) {
6969
*/
7070
@Override
7171
protected Object createInstance() {
72-
Assert.notNull(getServiceType(), "Property 'serviceType' is required");
72+
Assert.state(getServiceType() != null, "Property 'serviceType' is required");
7373
return getObjectToExpose(ServiceLoader.load(getServiceType(), this.beanClassLoader));
7474
}
7575

spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/DefaultJCacheOperationSource.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -166,7 +166,7 @@ public void setBeanFactory(BeanFactory beanFactory) {
166166
public void afterSingletonsInstantiated() {
167167
// Make sure that the cache resolver is initialized. An exception cache resolver is only
168168
// required if the exceptionCacheName attribute is set on an operation.
169-
Assert.notNull(getDefaultCacheResolver(), "Cache resolver should have been initialized");
169+
Assert.state(getDefaultCacheResolver() != null, "Cache resolver should have been initialized");
170170
}
171171

172172

spring-context/src/main/java/org/springframework/scheduling/annotation/ProxyAsyncConfiguration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -44,7 +44,7 @@ public class ProxyAsyncConfiguration extends AbstractAsyncConfiguration {
4444
@Bean(name = TaskManagementConfigUtils.ASYNC_ANNOTATION_PROCESSOR_BEAN_NAME)
4545
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
4646
public AsyncAnnotationBeanPostProcessor asyncAdvisor() {
47-
Assert.notNull(this.enableAsync, "@EnableAsync annotation metadata was not injected");
47+
Assert.state(this.enableAsync != null, "@EnableAsync annotation metadata was not injected");
4848
AsyncAnnotationBeanPostProcessor bpp = new AsyncAnnotationBeanPostProcessor();
4949
bpp.configure(this.executor, this.exceptionHandler);
5050
Class<? extends Annotation> customAsyncAnnotation = this.enableAsync.getClass("annotation");

spring-context/src/main/java/org/springframework/validation/beanvalidation/LocalValidatorFactoryBean.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -378,43 +378,43 @@ protected void postProcessConfiguration(Configuration<?> configuration) {
378378

379379
@Override
380380
public Validator getValidator() {
381-
Assert.notNull(this.validatorFactory, "No target ValidatorFactory set");
381+
Assert.state(this.validatorFactory != null, "No target ValidatorFactory set");
382382
return this.validatorFactory.getValidator();
383383
}
384384

385385
@Override
386386
public ValidatorContext usingContext() {
387-
Assert.notNull(this.validatorFactory, "No target ValidatorFactory set");
387+
Assert.state(this.validatorFactory != null, "No target ValidatorFactory set");
388388
return this.validatorFactory.usingContext();
389389
}
390390

391391
@Override
392392
public MessageInterpolator getMessageInterpolator() {
393-
Assert.notNull(this.validatorFactory, "No target ValidatorFactory set");
393+
Assert.state(this.validatorFactory != null, "No target ValidatorFactory set");
394394
return this.validatorFactory.getMessageInterpolator();
395395
}
396396

397397
@Override
398398
public TraversableResolver getTraversableResolver() {
399-
Assert.notNull(this.validatorFactory, "No target ValidatorFactory set");
399+
Assert.state(this.validatorFactory != null, "No target ValidatorFactory set");
400400
return this.validatorFactory.getTraversableResolver();
401401
}
402402

403403
@Override
404404
public ConstraintValidatorFactory getConstraintValidatorFactory() {
405-
Assert.notNull(this.validatorFactory, "No target ValidatorFactory set");
405+
Assert.state(this.validatorFactory != null, "No target ValidatorFactory set");
406406
return this.validatorFactory.getConstraintValidatorFactory();
407407
}
408408

409409
@Override
410410
public ParameterNameProvider getParameterNameProvider() {
411-
Assert.notNull(this.validatorFactory, "No target ValidatorFactory set");
411+
Assert.state(this.validatorFactory != null, "No target ValidatorFactory set");
412412
return this.validatorFactory.getParameterNameProvider();
413413
}
414414

415415
@Override
416416
public ClockProvider getClockProvider() {
417-
Assert.notNull(this.validatorFactory, "No target ValidatorFactory set");
417+
Assert.state(this.validatorFactory != null, "No target ValidatorFactory set");
418418
return this.validatorFactory.getClockProvider();
419419
}
420420

spring-core-test/src/main/java/org/springframework/aot/agent/RecordedInvocation.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public Stream<StackWalker.StackFrame> getStackFrames() {
9898
*/
9999
@SuppressWarnings("unchecked")
100100
public <T> T getInstance() {
101-
Assert.notNull(this.instance, "Cannot resolve 'this' for static invocations");
101+
Assert.state(this.instance != null, "Cannot resolve 'this' for static invocations");
102102
return (T) this.instance;
103103
}
104104

@@ -108,7 +108,7 @@ public <T> T getInstance() {
108108
* @throws IllegalStateException in case of static invocations (there is no {@code this})
109109
*/
110110
public TypeReference getInstanceTypeReference() {
111-
Assert.notNull(this.instance, "Cannot resolve 'this' for static invocations");
111+
Assert.state(this.instance != null, "Cannot resolve 'this' for static invocations");
112112
return TypeReference.of(this.instance.getClass());
113113
}
114114

spring-core-test/src/main/java/org/springframework/aot/test/agent/RuntimeHintsRecorder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ private RuntimeHintsRecorder() {
4949
*/
5050
public synchronized static RuntimeHintsInvocations record(Runnable action) {
5151
Assert.notNull(action, "Runnable action must not be null");
52-
Assert.isTrue(RuntimeHintsAgent.isLoaded(), "RuntimeHintsAgent should be loaded in the current JVM");
52+
Assert.state(RuntimeHintsAgent.isLoaded(), "RuntimeHintsAgent must be loaded in the current JVM");
5353
RuntimeHintsRecorder recorder = new RuntimeHintsRecorder();
5454
RecordedInvocationsPublisher.addListener(recorder.listener);
5555
try {

spring-core-test/src/test/java/org/springframework/aot/agent/RecordedInvocationTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ void buildValidStaticInvocation() {
6363

6464
@Test
6565
void staticInvocationShouldThrowWhenGetInstance() {
66-
assertThatThrownBy(staticInvocation::getInstance).isInstanceOf(IllegalArgumentException.class);
67-
assertThatThrownBy(staticInvocation::getInstanceTypeReference).isInstanceOf(IllegalArgumentException.class);
66+
assertThatThrownBy(staticInvocation::getInstance).isInstanceOf(IllegalStateException.class);
67+
assertThatThrownBy(staticInvocation::getInstanceTypeReference).isInstanceOf(IllegalStateException.class);
6868
}
6969

7070
@Test

spring-core/src/main/java/org/springframework/aot/generate/DefaultMethodReference.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,29 +42,32 @@ public class DefaultMethodReference implements MethodReference {
4242
@Nullable
4343
private final ClassName declaringClass;
4444

45+
4546
public DefaultMethodReference(MethodSpec method, @Nullable ClassName declaringClass) {
4647
this.method = method;
4748
this.declaringClass = declaringClass;
4849
}
4950

51+
5052
@Override
5153
public CodeBlock toCodeBlock() {
5254
String methodName = this.method.name;
5355
if (isStatic()) {
54-
Assert.notNull(this.declaringClass, "static method reference must define a declaring class");
56+
Assert.state(this.declaringClass != null, "static method reference must define a declaring class");
5557
return CodeBlock.of("$T::$L", this.declaringClass, methodName);
5658
}
5759
else {
5860
return CodeBlock.of("this::$L", methodName);
5961
}
6062
}
6163

64+
@Override
6265
public CodeBlock toInvokeCodeBlock(ArgumentCodeGenerator argumentCodeGenerator,
6366
@Nullable ClassName targetClassName) {
6467
String methodName = this.method.name;
6568
CodeBlock.Builder code = CodeBlock.builder();
6669
if (isStatic()) {
67-
Assert.notNull(this.declaringClass, "static method reference must define a declaring class");
70+
Assert.state(this.declaringClass != null, "static method reference must define a declaring class");
6871
if (isSameDeclaringClass(targetClassName)) {
6972
code.add("$L", methodName);
7073
}

spring-core/src/test/java/org/springframework/aot/generate/DefaultMethodReferenceTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
import static org.assertj.core.api.Assertions.assertThat;
3232
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
33+
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
3334

3435
/**
3536
* Tests for {@link DefaultMethodReference}.
@@ -86,7 +87,7 @@ void toCodeBlockWithStaticMethod() {
8687
void toCodeBlockWithStaticMethodRequiresDeclaringClass() {
8788
MethodSpec method = createTestMethod("methodName", new TypeName[0], Modifier.STATIC);
8889
MethodReference methodReference = new DefaultMethodReference(method, null);
89-
assertThatIllegalArgumentException().isThrownBy(methodReference::toCodeBlock)
90+
assertThatIllegalStateException().isThrownBy(methodReference::toCodeBlock)
9091
.withMessage("static method reference must define a declaring class");
9192
}
9293

spring-jdbc/src/main/java/org/springframework/jdbc/datasource/ConnectionHolder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -157,7 +157,7 @@ protected void setConnection(@Nullable Connection connection) {
157157
* @see #released()
158158
*/
159159
public Connection getConnection() {
160-
Assert.notNull(this.connectionHandle, "Active Connection is required");
160+
Assert.state(this.connectionHandle != null, "Active Connection is required");
161161
if (this.currentConnection == null) {
162162
this.currentConnection = this.connectionHandle.getConnection();
163163
}

0 commit comments

Comments
 (0)