diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/serviceloader/AbstractServiceLoaderBasedFactoryBean.java b/spring-beans/src/main/java/org/springframework/beans/factory/serviceloader/AbstractServiceLoaderBasedFactoryBean.java index 974940a951c..3ee514663c6 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/serviceloader/AbstractServiceLoaderBasedFactoryBean.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/serviceloader/AbstractServiceLoaderBasedFactoryBean.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2022 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. @@ -69,7 +69,7 @@ public abstract class AbstractServiceLoaderBasedFactoryBean extends AbstractFact */ @Override protected Object createInstance() { - Assert.notNull(getServiceType(), "Property 'serviceType' is required"); + Assert.state(getServiceType() != null, "Property 'serviceType' is required"); return getObjectToExpose(ServiceLoader.load(getServiceType(), this.beanClassLoader)); } diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/DefaultJCacheOperationSource.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/DefaultJCacheOperationSource.java index 77d031dc858..4bde292d8fa 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/DefaultJCacheOperationSource.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/DefaultJCacheOperationSource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2022 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. @@ -166,7 +166,7 @@ public class DefaultJCacheOperationSource extends AnnotationJCacheOperationSourc public void afterSingletonsInstantiated() { // Make sure that the cache resolver is initialized. An exception cache resolver is only // required if the exceptionCacheName attribute is set on an operation. - Assert.notNull(getDefaultCacheResolver(), "Cache resolver should have been initialized"); + Assert.state(getDefaultCacheResolver() != null, "Cache resolver should have been initialized"); } diff --git a/spring-context/src/main/java/org/springframework/scheduling/annotation/ProxyAsyncConfiguration.java b/spring-context/src/main/java/org/springframework/scheduling/annotation/ProxyAsyncConfiguration.java index e3a5b310e57..fcea2643acd 100644 --- a/spring-context/src/main/java/org/springframework/scheduling/annotation/ProxyAsyncConfiguration.java +++ b/spring-context/src/main/java/org/springframework/scheduling/annotation/ProxyAsyncConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2022 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. @@ -44,7 +44,7 @@ public class ProxyAsyncConfiguration extends AbstractAsyncConfiguration { @Bean(name = TaskManagementConfigUtils.ASYNC_ANNOTATION_PROCESSOR_BEAN_NAME) @Role(BeanDefinition.ROLE_INFRASTRUCTURE) public AsyncAnnotationBeanPostProcessor asyncAdvisor() { - Assert.notNull(this.enableAsync, "@EnableAsync annotation metadata was not injected"); + Assert.state(this.enableAsync != null, "@EnableAsync annotation metadata was not injected"); AsyncAnnotationBeanPostProcessor bpp = new AsyncAnnotationBeanPostProcessor(); bpp.configure(this.executor, this.exceptionHandler); Class customAsyncAnnotation = this.enableAsync.getClass("annotation"); diff --git a/spring-context/src/main/java/org/springframework/validation/beanvalidation/LocalValidatorFactoryBean.java b/spring-context/src/main/java/org/springframework/validation/beanvalidation/LocalValidatorFactoryBean.java index 33d53fcb787..9aa5135e1dc 100644 --- a/spring-context/src/main/java/org/springframework/validation/beanvalidation/LocalValidatorFactoryBean.java +++ b/spring-context/src/main/java/org/springframework/validation/beanvalidation/LocalValidatorFactoryBean.java @@ -378,43 +378,43 @@ public class LocalValidatorFactoryBean extends SpringValidatorAdapter @Override public Validator getValidator() { - Assert.notNull(this.validatorFactory, "No target ValidatorFactory set"); + Assert.state(this.validatorFactory != null, "No target ValidatorFactory set"); return this.validatorFactory.getValidator(); } @Override public ValidatorContext usingContext() { - Assert.notNull(this.validatorFactory, "No target ValidatorFactory set"); + Assert.state(this.validatorFactory != null, "No target ValidatorFactory set"); return this.validatorFactory.usingContext(); } @Override public MessageInterpolator getMessageInterpolator() { - Assert.notNull(this.validatorFactory, "No target ValidatorFactory set"); + Assert.state(this.validatorFactory != null, "No target ValidatorFactory set"); return this.validatorFactory.getMessageInterpolator(); } @Override public TraversableResolver getTraversableResolver() { - Assert.notNull(this.validatorFactory, "No target ValidatorFactory set"); + Assert.state(this.validatorFactory != null, "No target ValidatorFactory set"); return this.validatorFactory.getTraversableResolver(); } @Override public ConstraintValidatorFactory getConstraintValidatorFactory() { - Assert.notNull(this.validatorFactory, "No target ValidatorFactory set"); + Assert.state(this.validatorFactory != null, "No target ValidatorFactory set"); return this.validatorFactory.getConstraintValidatorFactory(); } @Override public ParameterNameProvider getParameterNameProvider() { - Assert.notNull(this.validatorFactory, "No target ValidatorFactory set"); + Assert.state(this.validatorFactory != null, "No target ValidatorFactory set"); return this.validatorFactory.getParameterNameProvider(); } @Override public ClockProvider getClockProvider() { - Assert.notNull(this.validatorFactory, "No target ValidatorFactory set"); + Assert.state(this.validatorFactory != null, "No target ValidatorFactory set"); return this.validatorFactory.getClockProvider(); } diff --git a/spring-core-test/src/main/java/org/springframework/aot/agent/RecordedInvocation.java b/spring-core-test/src/main/java/org/springframework/aot/agent/RecordedInvocation.java index c5ce25bd2b4..c80a82eac10 100644 --- a/spring-core-test/src/main/java/org/springframework/aot/agent/RecordedInvocation.java +++ b/spring-core-test/src/main/java/org/springframework/aot/agent/RecordedInvocation.java @@ -98,7 +98,7 @@ public final class RecordedInvocation { */ @SuppressWarnings("unchecked") public T getInstance() { - Assert.notNull(this.instance, "Cannot resolve 'this' for static invocations"); + Assert.state(this.instance != null, "Cannot resolve 'this' for static invocations"); return (T) this.instance; } @@ -108,7 +108,7 @@ public final class RecordedInvocation { * @throws IllegalStateException in case of static invocations (there is no {@code this}) */ public TypeReference getInstanceTypeReference() { - Assert.notNull(this.instance, "Cannot resolve 'this' for static invocations"); + Assert.state(this.instance != null, "Cannot resolve 'this' for static invocations"); return TypeReference.of(this.instance.getClass()); } diff --git a/spring-core-test/src/main/java/org/springframework/aot/test/agent/RuntimeHintsRecorder.java b/spring-core-test/src/main/java/org/springframework/aot/test/agent/RuntimeHintsRecorder.java index a9471a4c5dc..31b20241b2a 100644 --- a/spring-core-test/src/main/java/org/springframework/aot/test/agent/RuntimeHintsRecorder.java +++ b/spring-core-test/src/main/java/org/springframework/aot/test/agent/RuntimeHintsRecorder.java @@ -49,7 +49,7 @@ public final class RuntimeHintsRecorder { */ public synchronized static RuntimeHintsInvocations record(Runnable action) { Assert.notNull(action, "Runnable action must not be null"); - Assert.isTrue(RuntimeHintsAgent.isLoaded(), "RuntimeHintsAgent should be loaded in the current JVM"); + Assert.state(RuntimeHintsAgent.isLoaded(), "RuntimeHintsAgent must be loaded in the current JVM"); RuntimeHintsRecorder recorder = new RuntimeHintsRecorder(); RecordedInvocationsPublisher.addListener(recorder.listener); try { diff --git a/spring-core-test/src/test/java/org/springframework/aot/agent/RecordedInvocationTests.java b/spring-core-test/src/test/java/org/springframework/aot/agent/RecordedInvocationTests.java index bf46e804e86..35b633dd810 100644 --- a/spring-core-test/src/test/java/org/springframework/aot/agent/RecordedInvocationTests.java +++ b/spring-core-test/src/test/java/org/springframework/aot/agent/RecordedInvocationTests.java @@ -63,8 +63,8 @@ class RecordedInvocationTests { @Test void staticInvocationShouldThrowWhenGetInstance() { - assertThatThrownBy(staticInvocation::getInstance).isInstanceOf(IllegalArgumentException.class); - assertThatThrownBy(staticInvocation::getInstanceTypeReference).isInstanceOf(IllegalArgumentException.class); + assertThatThrownBy(staticInvocation::getInstance).isInstanceOf(IllegalStateException.class); + assertThatThrownBy(staticInvocation::getInstanceTypeReference).isInstanceOf(IllegalStateException.class); } @Test diff --git a/spring-core/src/main/java/org/springframework/aot/generate/DefaultMethodReference.java b/spring-core/src/main/java/org/springframework/aot/generate/DefaultMethodReference.java index b3a3ab117d8..21e0d47511f 100644 --- a/spring-core/src/main/java/org/springframework/aot/generate/DefaultMethodReference.java +++ b/spring-core/src/main/java/org/springframework/aot/generate/DefaultMethodReference.java @@ -42,16 +42,18 @@ public class DefaultMethodReference implements MethodReference { @Nullable private final ClassName declaringClass; + public DefaultMethodReference(MethodSpec method, @Nullable ClassName declaringClass) { this.method = method; this.declaringClass = declaringClass; } + @Override public CodeBlock toCodeBlock() { String methodName = this.method.name; if (isStatic()) { - Assert.notNull(this.declaringClass, "static method reference must define a declaring class"); + Assert.state(this.declaringClass != null, "static method reference must define a declaring class"); return CodeBlock.of("$T::$L", this.declaringClass, methodName); } else { @@ -59,12 +61,13 @@ public class DefaultMethodReference implements MethodReference { } } + @Override public CodeBlock toInvokeCodeBlock(ArgumentCodeGenerator argumentCodeGenerator, @Nullable ClassName targetClassName) { String methodName = this.method.name; CodeBlock.Builder code = CodeBlock.builder(); if (isStatic()) { - Assert.notNull(this.declaringClass, "static method reference must define a declaring class"); + Assert.state(this.declaringClass != null, "static method reference must define a declaring class"); if (isSameDeclaringClass(targetClassName)) { code.add("$L", methodName); } diff --git a/spring-core/src/test/java/org/springframework/aot/generate/DefaultMethodReferenceTests.java b/spring-core/src/test/java/org/springframework/aot/generate/DefaultMethodReferenceTests.java index b9643151ca9..d5c54e75abb 100644 --- a/spring-core/src/test/java/org/springframework/aot/generate/DefaultMethodReferenceTests.java +++ b/spring-core/src/test/java/org/springframework/aot/generate/DefaultMethodReferenceTests.java @@ -30,6 +30,7 @@ import org.springframework.lang.Nullable; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; /** * Tests for {@link DefaultMethodReference}. @@ -86,7 +87,7 @@ class DefaultMethodReferenceTests { void toCodeBlockWithStaticMethodRequiresDeclaringClass() { MethodSpec method = createTestMethod("methodName", new TypeName[0], Modifier.STATIC); MethodReference methodReference = new DefaultMethodReference(method, null); - assertThatIllegalArgumentException().isThrownBy(methodReference::toCodeBlock) + assertThatIllegalStateException().isThrownBy(methodReference::toCodeBlock) .withMessage("static method reference must define a declaring class"); } diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/ConnectionHolder.java b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/ConnectionHolder.java index 4f3d1a785a5..4d13484aa84 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/ConnectionHolder.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/ConnectionHolder.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2022 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. @@ -157,7 +157,7 @@ public class ConnectionHolder extends ResourceHolderSupport { * @see #released() */ public Connection getConnection() { - Assert.notNull(this.connectionHandle, "Active Connection is required"); + Assert.state(this.connectionHandle != null, "Active Connection is required"); if (this.currentConnection == null) { this.currentConnection = this.connectionHandle.getConnection(); } diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/SimpleDriverDataSource.java b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/SimpleDriverDataSource.java index 55b75e02bf7..99a00687e47 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/SimpleDriverDataSource.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/SimpleDriverDataSource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2022 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. @@ -136,8 +136,8 @@ public class SimpleDriverDataSource extends AbstractDriverBasedDataSource { @Override protected Connection getConnectionFromDriver(Properties props) throws SQLException { Driver driver = getDriver(); + Assert.state(driver != null, "Driver has not been set"); String url = getUrl(); - Assert.notNull(driver, "Driver must not be null"); if (logger.isDebugEnabled()) { logger.debug("Creating new JDBC Driver Connection to [" + url + "]"); } diff --git a/spring-messaging/src/main/java/org/springframework/messaging/converter/MarshallingMessageConverter.java b/spring-messaging/src/main/java/org/springframework/messaging/converter/MarshallingMessageConverter.java index 31e9b8223a0..782915011cd 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/converter/MarshallingMessageConverter.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/converter/MarshallingMessageConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2022 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. @@ -144,7 +144,7 @@ public class MarshallingMessageConverter extends AbstractMessageConverter { @Override @Nullable protected Object convertFromInternal(Message message, Class targetClass, @Nullable Object conversionHint) { - Assert.notNull(this.unmarshaller, "Property 'unmarshaller' is required"); + Assert.state(this.unmarshaller != null, "Property 'unmarshaller' is required"); try { Source source = getSource(message.getPayload()); Object result = this.unmarshaller.unmarshal(source); @@ -172,7 +172,7 @@ public class MarshallingMessageConverter extends AbstractMessageConverter { protected Object convertToInternal(Object payload, @Nullable MessageHeaders headers, @Nullable Object conversionHint) { - Assert.notNull(this.marshaller, "Property 'marshaller' is required"); + Assert.state(this.marshaller != null, "Property 'marshaller' is required"); try { if (byte[].class == getSerializedPayloadClass()) { ByteArrayOutputStream out = new ByteArrayOutputStream(1024); diff --git a/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/ConnectionHolder.java b/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/ConnectionHolder.java index 1c713048db1..5ad913d0b81 100644 --- a/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/ConnectionHolder.java +++ b/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/ConnectionHolder.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2022 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. @@ -23,7 +23,6 @@ import org.springframework.lang.Nullable; import org.springframework.transaction.support.ResourceHolderSupport; import org.springframework.util.Assert; - /** * Resource holder wrapping a R2DBC {@link Connection}. * {@link R2dbcTransactionManager} binds instances of this class to the subscription, @@ -109,7 +108,7 @@ public class ConnectionHolder extends ResourceHolderSupport { * @see #released() */ public Connection getConnection() { - Assert.notNull(this.currentConnection, "Active Connection is required"); + Assert.state(this.currentConnection != null, "Active Connection is required"); return this.currentConnection; } diff --git a/spring-web/src/main/java/org/springframework/http/converter/xml/MarshallingHttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/xml/MarshallingHttpMessageConverter.java index b06ddc8e310..a6d9647e6a1 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/xml/MarshallingHttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/xml/MarshallingHttpMessageConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2022 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. @@ -121,7 +121,7 @@ public class MarshallingHttpMessageConverter extends AbstractXmlHttpMessageConve @Override protected Object readFromSource(Class clazz, HttpHeaders headers, Source source) throws Exception { - Assert.notNull(this.unmarshaller, "Property 'unmarshaller' is required"); + Assert.state(this.unmarshaller != null, "Property 'unmarshaller' is required"); Object result = this.unmarshaller.unmarshal(source); if (!clazz.isInstance(result)) { throw new TypeMismatchException(result, clazz); @@ -131,7 +131,7 @@ public class MarshallingHttpMessageConverter extends AbstractXmlHttpMessageConve @Override protected void writeToResult(Object o, HttpHeaders headers, Result result) throws Exception { - Assert.notNull(this.marshaller, "Property 'marshaller' is required"); + Assert.state(this.marshaller != null, "Property 'marshaller' is required"); this.marshaller.marshal(o, result); } diff --git a/spring-web/src/main/java/org/springframework/http/server/reactive/JettyHttpHandlerAdapter.java b/spring-web/src/main/java/org/springframework/http/server/reactive/JettyHttpHandlerAdapter.java index 35964ca6d71..db6d2093cd5 100644 --- a/spring-web/src/main/java/org/springframework/http/server/reactive/JettyHttpHandlerAdapter.java +++ b/spring-web/src/main/java/org/springframework/http/server/reactive/JettyHttpHandlerAdapter.java @@ -59,7 +59,7 @@ public class JettyHttpHandlerAdapter extends ServletHttpHandlerAdapter { protected ServletServerHttpRequest createRequest(HttpServletRequest request, AsyncContext context) throws IOException, URISyntaxException { - Assert.notNull(getServletPath(), "Servlet path is not initialized"); + Assert.state(getServletPath() != null, "Servlet path is not initialized"); return new JettyServerHttpRequest( request, context, getServletPath(), getDataBufferFactory(), getBufferSize()); } diff --git a/spring-web/src/main/java/org/springframework/http/server/reactive/ServletHttpHandlerAdapter.java b/spring-web/src/main/java/org/springframework/http/server/reactive/ServletHttpHandlerAdapter.java index 8918aa568f6..d462f7b2e93 100644 --- a/spring-web/src/main/java/org/springframework/http/server/reactive/ServletHttpHandlerAdapter.java +++ b/spring-web/src/main/java/org/springframework/http/server/reactive/ServletHttpHandlerAdapter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 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. @@ -200,7 +200,7 @@ public class ServletHttpHandlerAdapter implements Servlet { protected ServletServerHttpRequest createRequest(HttpServletRequest request, AsyncContext context) throws IOException, URISyntaxException { - Assert.notNull(this.servletPath, "Servlet path is not initialized"); + Assert.state(this.servletPath != null, "Servlet path is not initialized"); return new ServletServerHttpRequest( request, context, this.servletPath, getDataBufferFactory(), getBufferSize()); } diff --git a/spring-web/src/main/java/org/springframework/http/server/reactive/TomcatHttpHandlerAdapter.java b/spring-web/src/main/java/org/springframework/http/server/reactive/TomcatHttpHandlerAdapter.java index 91f3272a80b..b4ccef6f03b 100644 --- a/spring-web/src/main/java/org/springframework/http/server/reactive/TomcatHttpHandlerAdapter.java +++ b/spring-web/src/main/java/org/springframework/http/server/reactive/TomcatHttpHandlerAdapter.java @@ -67,7 +67,7 @@ public class TomcatHttpHandlerAdapter extends ServletHttpHandlerAdapter { protected ServletServerHttpRequest createRequest(HttpServletRequest request, AsyncContext asyncContext) throws IOException, URISyntaxException { - Assert.notNull(getServletPath(), "Servlet path is not initialized"); + Assert.state(getServletPath() != null, "Servlet path is not initialized"); return new TomcatServerHttpRequest( request, asyncContext, getServletPath(), getDataBufferFactory(), getBufferSize()); } diff --git a/spring-web/src/main/java/org/springframework/web/context/request/async/StandardServletAsyncWebRequest.java b/spring-web/src/main/java/org/springframework/web/context/request/async/StandardServletAsyncWebRequest.java index bc7c6ed7627..7833f0018e0 100644 --- a/spring-web/src/main/java/org/springframework/web/context/request/async/StandardServletAsyncWebRequest.java +++ b/spring-web/src/main/java/org/springframework/web/context/request/async/StandardServletAsyncWebRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 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. @@ -128,7 +128,7 @@ public class StandardServletAsyncWebRequest extends ServletWebRequest implements @Override public void dispatch() { - Assert.notNull(this.asyncContext, "Cannot dispatch without an AsyncContext"); + Assert.state(this.asyncContext != null, "Cannot dispatch without an AsyncContext"); this.asyncContext.dispatch(); } diff --git a/spring-web/src/main/java/org/springframework/web/server/adapter/AbstractReactiveWebInitializer.java b/spring-web/src/main/java/org/springframework/web/server/adapter/AbstractReactiveWebInitializer.java index 6e18bec9bbe..7f3251b0a25 100644 --- a/spring-web/src/main/java/org/springframework/web/server/adapter/AbstractReactiveWebInitializer.java +++ b/spring-web/src/main/java/org/springframework/web/server/adapter/AbstractReactiveWebInitializer.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 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. @@ -28,6 +28,8 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext import org.springframework.http.server.reactive.HttpHandler; import org.springframework.http.server.reactive.ServletHttpHandlerAdapter; import org.springframework.util.Assert; +import org.springframework.util.ObjectUtils; +import org.springframework.util.StringUtils; import org.springframework.web.WebApplicationInitializer; /** @@ -55,10 +57,10 @@ public abstract class AbstractReactiveWebInitializer implements WebApplicationIn @Override public void onStartup(ServletContext servletContext) throws ServletException { String servletName = getServletName(); - Assert.hasLength(servletName, "getServletName() must not return null or empty"); + Assert.state(StringUtils.hasLength(servletName), "getServletName() must not return null or empty"); ApplicationContext applicationContext = createApplicationContext(); - Assert.notNull(applicationContext, "createApplicationContext() must not return null"); + Assert.state(applicationContext != null, "createApplicationContext() must not return null"); refreshApplicationContext(applicationContext); registerCloseListener(servletContext, applicationContext); @@ -92,7 +94,7 @@ public abstract class AbstractReactiveWebInitializer implements WebApplicationIn protected ApplicationContext createApplicationContext() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); Class[] configClasses = getConfigClasses(); - Assert.notEmpty(configClasses, "No Spring configuration provided through getConfigClasses()"); + Assert.state(!ObjectUtils.isEmpty(configClasses), "No Spring configuration provided through getConfigClasses()"); context.register(configClasses); return context; } diff --git a/spring-web/src/test/java/org/springframework/http/client/support/ProxyFactoryBeanTests.java b/spring-web/src/test/java/org/springframework/http/client/support/ProxyFactoryBeanTests.java index 0f2f8aa4574..33bda55cad7 100644 --- a/spring-web/src/test/java/org/springframework/http/client/support/ProxyFactoryBeanTests.java +++ b/spring-web/src/test/java/org/springframework/http/client/support/ProxyFactoryBeanTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2022 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. @@ -19,7 +19,6 @@ package org.springframework.http.client.support; import java.net.InetSocketAddress; import java.net.Proxy; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; @@ -28,38 +27,31 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException /** * @author Arjen Poutsma */ -public class ProxyFactoryBeanTests { +class ProxyFactoryBeanTests { - ProxyFactoryBean factoryBean; + private final ProxyFactoryBean factoryBean = new ProxyFactoryBean(); - @BeforeEach - public void setUp() { - factoryBean = new ProxyFactoryBean(); - } @Test - public void noType() { + void noType() { factoryBean.setType(null); - assertThatIllegalArgumentException().isThrownBy( - factoryBean::afterPropertiesSet); + assertThatIllegalArgumentException().isThrownBy(factoryBean::afterPropertiesSet); } @Test - public void noHostname() { + void noHostname() { factoryBean.setHostname(""); - assertThatIllegalArgumentException().isThrownBy( - factoryBean::afterPropertiesSet); + assertThatIllegalArgumentException().isThrownBy(factoryBean::afterPropertiesSet); } @Test - public void noPort() { + void noPort() { factoryBean.setHostname("example.com"); - assertThatIllegalArgumentException().isThrownBy( - factoryBean::afterPropertiesSet); + assertThatIllegalArgumentException().isThrownBy(factoryBean::afterPropertiesSet); } @Test - public void normal() { + void normal() { Proxy.Type type = Proxy.Type.HTTP; factoryBean.setType(type); String hostname = "example.com"; diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractUrlHandlerMapping.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractUrlHandlerMapping.java index 6c9dce05553..75bb7a2c3a6 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractUrlHandlerMapping.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractUrlHandlerMapping.java @@ -374,7 +374,7 @@ public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping i @Override @Nullable public RequestMatchResult match(HttpServletRequest request, String pattern) { - Assert.isNull(getPatternParser(), "This HandlerMapping uses PathPatterns."); + Assert.state(getPatternParser() == null, "This HandlerMapping uses PathPatterns."); String lookupPath = UrlPathHelper.getResolvedLookupPath(request); if (getPathMatcher().match(pattern, lookupPath)) { return new RequestMatchResult(pattern, lookupPath, getPathMatcher()); diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/HandlerMappingIntrospector.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/HandlerMappingIntrospector.java index a10055bfa84..9df482be905 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/HandlerMappingIntrospector.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/HandlerMappingIntrospector.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 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. @@ -158,7 +158,7 @@ public class HandlerMappingIntrospector HttpServletRequest request, boolean ignoreException, BiFunction matchHandler) throws Exception { - Assert.notNull(this.handlerMappings, "Handler mappings not initialized"); + Assert.state(this.handlerMappings != null, "Handler mappings not initialized"); boolean parseRequestPath = !this.pathPatternHandlerMappings.isEmpty(); RequestPath previousPath = null; diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/PathPatternMatchableHandlerMapping.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/PathPatternMatchableHandlerMapping.java index 89ca01893f6..3057331dfb2 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/PathPatternMatchableHandlerMapping.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/PathPatternMatchableHandlerMapping.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 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. @@ -59,7 +59,7 @@ class PathPatternMatchableHandlerMapping implements MatchableHandlerMapping { @Override public RequestMatchResult match(HttpServletRequest request, String pattern) { PathPattern pathPattern = this.pathPatternCache.computeIfAbsent(pattern, value -> { - Assert.isTrue(this.pathPatternCache.size() < MAX_PATTERNS, "Max size for pattern cache exceeded."); + Assert.state(this.pathPatternCache.size() < MAX_PATTERNS, "Max size for pattern cache exceeded."); return this.parser.parse(pattern); }); PathContainer path = ServletRequestPathUtils.getParsedRequestPath(request).pathWithinApplication(); diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.java index e0f4fb5a8c5..df38a974bab 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.java @@ -448,7 +448,7 @@ public class MvcUriComponentsBuilder { */ public static MethodArgumentBuilder fromMappingName(@Nullable UriComponentsBuilder builder, String name) { WebApplicationContext wac = getWebApplicationContext(); - Assert.notNull(wac, "No WebApplicationContext"); + Assert.state(wac != null, "No WebApplicationContext"); Map map = wac.getBeansOfType(RequestMappingInfoHandlerMapping.class); List handlerMethods = null; for (RequestMappingInfoHandlerMapping mapping : map.values()) { diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMapping.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMapping.java index 338ec94fe1d..6e4c01a8c67 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMapping.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMapping.java @@ -454,7 +454,7 @@ public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMappi @Override public RequestMatchResult match(HttpServletRequest request, String pattern) { - Assert.isNull(getPatternParser(), "This HandlerMapping requires a PathPattern"); + Assert.state(getPatternParser() == null, "This HandlerMapping uses PathPatterns."); RequestMappingInfo info = RequestMappingInfo.paths(pattern).options(this.config).build(); RequestMappingInfo match = info.getMatchingCondition(request); return (match != null && match.getPatternsCondition() != null ? diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java index 8d94d796f59..ab632ddfb6c 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 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. @@ -625,8 +625,8 @@ public class ResourceHttpRequestHandler extends WebContentGenerator return null; } - Assert.notNull(this.resolverChain, "ResourceResolverChain not initialized."); - Assert.notNull(this.transformerChain, "ResourceTransformerChain not initialized."); + Assert.state(this.resolverChain != null, "ResourceResolverChain not initialized."); + Assert.state(this.transformerChain != null, "ResourceTransformerChain not initialized."); Resource resource = this.resolverChain.resolveResource(request, path, getLocations()); if (resource != null) { diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/support/AbstractDispatcherServletInitializer.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/support/AbstractDispatcherServletInitializer.java index 353c9d4a580..44559ce60bc 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/support/AbstractDispatcherServletInitializer.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/support/AbstractDispatcherServletInitializer.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2022 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. @@ -31,6 +31,7 @@ import org.springframework.core.Conventions; import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.ObjectUtils; +import org.springframework.util.StringUtils; import org.springframework.web.context.AbstractContextLoaderInitializer; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.servlet.DispatcherServlet; @@ -77,13 +78,13 @@ public abstract class AbstractDispatcherServletInitializer extends AbstractConte */ protected void registerDispatcherServlet(ServletContext servletContext) { String servletName = getServletName(); - Assert.hasLength(servletName, "getServletName() must not return null or empty"); + Assert.state(StringUtils.hasLength(servletName), "getServletName() must not return null or empty"); WebApplicationContext servletAppContext = createServletApplicationContext(); - Assert.notNull(servletAppContext, "createServletApplicationContext() must not return null"); + Assert.state(servletAppContext != null, "createServletApplicationContext() must not return null"); FrameworkServlet dispatcherServlet = createDispatcherServlet(servletAppContext); - Assert.notNull(dispatcherServlet, "createDispatcherServlet(WebApplicationContext) must not return null"); + Assert.state(dispatcherServlet != null, "createDispatcherServlet(WebApplicationContext) must not return null"); dispatcherServlet.setContextInitializers(getServletApplicationContextInitializers()); ServletRegistration.Dynamic registration = servletContext.addServlet(servletName, dispatcherServlet); diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/view/xml/MarshallingView.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/view/xml/MarshallingView.java index 36b7f04cbdf..65c6820265d 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/view/xml/MarshallingView.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/view/xml/MarshallingView.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2022 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. @@ -97,7 +97,7 @@ public class MarshallingView extends AbstractView { @Override protected void initApplicationContext() { - Assert.notNull(this.marshaller, "Property 'marshaller' is required"); + Assert.state(this.marshaller != null, "Property 'marshaller' is required"); } diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/messaging/SubProtocolWebSocketHandler.java b/spring-websocket/src/main/java/org/springframework/web/socket/messaging/SubProtocolWebSocketHandler.java index d629a9a92c6..0a546d80b08 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/messaging/SubProtocolWebSocketHandler.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/messaging/SubProtocolWebSocketHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2022 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. @@ -267,7 +267,7 @@ public class SubProtocolWebSocketHandler @Override public final void start() { - Assert.isTrue(this.defaultProtocolHandler != null || !this.protocolHandlers.isEmpty(), "No handlers"); + Assert.state(this.defaultProtocolHandler != null || !this.protocolHandlers.isEmpty(), "No handlers"); synchronized (this.lifecycleMonitor) { this.clientOutboundChannel.subscribe(this); diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/SockJsWebSocketHandler.java b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/SockJsWebSocketHandler.java index f0e914dcf01..fadfc905c31 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/SockJsWebSocketHandler.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/SockJsWebSocketHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2022 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. @@ -84,7 +84,7 @@ public class SockJsWebSocketHandler extends TextWebSocketHandler implements SubP @Override public void afterConnectionEstablished(WebSocketSession wsSession) throws Exception { - Assert.isTrue(this.sessionCount.compareAndSet(0, 1), "Unexpected connection"); + Assert.state(this.sessionCount.compareAndSet(0, 1), "Unexpected connection"); this.sockJsSession.initializeDelegateSession(wsSession); }