From abf3400c07da1e7e98c44388a5fbbd1a89cea0d8 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Mon, 14 Nov 2022 17:36:11 +0100 Subject: [PATCH] Use Assert.state() where appropriate --- ...AbstractServiceLoaderBasedFactoryBean.java | 4 +-- .../DefaultJCacheOperationSource.java | 4 +-- .../annotation/ProxyAsyncConfiguration.java | 4 +-- .../LocalValidatorFactoryBean.java | 14 +++++----- .../aot/agent/RecordedInvocation.java | 4 +-- .../aot/test/agent/RuntimeHintsRecorder.java | 2 +- .../aot/agent/RecordedInvocationTests.java | 4 +-- .../aot/generate/DefaultMethodReference.java | 7 +++-- .../generate/DefaultMethodReferenceTests.java | 3 +- .../jdbc/datasource/ConnectionHolder.java | 4 +-- .../datasource/SimpleDriverDataSource.java | 4 +-- .../MarshallingMessageConverter.java | 6 ++-- .../r2dbc/connection/ConnectionHolder.java | 5 ++-- .../xml/MarshallingHttpMessageConverter.java | 6 ++-- .../reactive/JettyHttpHandlerAdapter.java | 2 +- .../reactive/ServletHttpHandlerAdapter.java | 4 +-- .../reactive/TomcatHttpHandlerAdapter.java | 2 +- .../async/StandardServletAsyncWebRequest.java | 4 +-- .../AbstractReactiveWebInitializer.java | 10 ++++--- .../client/support/ProxyFactoryBeanTests.java | 28 +++++++------------ .../handler/AbstractUrlHandlerMapping.java | 2 +- .../handler/HandlerMappingIntrospector.java | 4 +-- .../PathPatternMatchableHandlerMapping.java | 4 +-- .../annotation/MvcUriComponentsBuilder.java | 2 +- .../RequestMappingHandlerMapping.java | 2 +- .../resource/ResourceHttpRequestHandler.java | 6 ++-- .../AbstractDispatcherServletInitializer.java | 9 +++--- .../web/servlet/view/xml/MarshallingView.java | 4 +-- .../SubProtocolWebSocketHandler.java | 4 +-- .../handler/SockJsWebSocketHandler.java | 4 +-- 30 files changed, 80 insertions(+), 82 deletions(-) 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 974940a951c8..3ee514663c68 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 void setBeanClassLoader(@Nullable ClassLoader beanClassLoader) { */ @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 77d031dc8585..4bde292d8fa5 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 void setBeanFactory(BeanFactory beanFactory) { 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 e3a5b310e575..fcea2643acda 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 33d53fcb787e..9aa5135e1dcd 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 @@ protected void postProcessConfiguration(Configuration configuration) { @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 c5ce25bd2b4c..c80a82eac105 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 Stream getStackFrames() { */ @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 T getInstance() { * @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 a9471a4c5dc5..31b20241b2a0 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 @@ private 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 bf46e804e86c..35b633dd810f 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 @@ void buildValidStaticInvocation() { @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 b3a3ab117d8e..21e0d47511f6 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 CodeBlock toCodeBlock() { } } + @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 b9643151ca9c..d5c54e75abbe 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 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 @@ void toCodeBlockWithStaticMethod() { 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 4f3d1a785a51..4d13484aa840 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 @@ protected void setConnection(@Nullable Connection connection) { * @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 55b75e02bf73..99a00687e470 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 Driver getDriver() { @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 31e9b8223a0e..782915011cdd 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 @@ protected boolean supports(Class clazz) { @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 @@ private Source getSource(Object payload) { 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 1c713048db17..5ad913d0b813 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.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 @@ protected void setConnection(@Nullable Connection connection) { * @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 b06ddc8e3105..a6d9647e6a17 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 @@ protected boolean supports(Class clazz) { @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 @@ protected Object readFromSource(Class clazz, HttpHeaders headers, Source sour @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 35964ca6d715..db6d2093cd5d 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 JettyHttpHandlerAdapter(HttpHandler httpHandler) { 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 8918aa568f69..d462f7b2e933 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 void service(ServletRequest request, ServletResponse response) throws Ser 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 91f3272a80b0..b4ccef6f03b4 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 TomcatHttpHandlerAdapter(HttpHandler httpHandler) { 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 bc7c6ed76275..7833f0018e04 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 void startAsync() { @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 6e18bec9bbeb..7f3251b0a25e 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.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 @@ protected String getServletName() { 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 0f2f8aa45748..33bda55cad76 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 @@ 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 @@ /** * @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 6c9dce05553b..75bb7a2c3a6f 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 @@ protected void exposeUriTemplateVariables(Map uriTemplateVariabl @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 a10055bfa84f..9df482be905e 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 @@ private T doWithMatchingMapping( 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 89ca01893f69..3057331dfb2c 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 @@ public PathPatternMatchableHandlerMapping(MatchableHandlerMapping delegate) { @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 e0f4fb5a8c5f..df38a974bab4 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 static MethodArgumentBuilder fromMappingName(String mappingName) { */ 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 338ec94fe1d3..6e4c01a8c67f 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 @@ private void updateConsumesCondition(RequestMappingInfo info, Method method) { @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 8d94d796f59f..ab632ddfb6c5 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 @@ protected Resource getResource(HttpServletRequest request) throws IOException { 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 353c9d4a5805..44559ce60bc6 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.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 void onStartup(ServletContext servletContext) throws ServletException { */ 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 36b7f04cbdf2..65c6820265d2 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 void setModelKey(String modelKey) { @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 d629a9a92c64..0a546d80b08b 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 Stats getStats() { @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 f0e914dcf016..fadfc905c310 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 @@ protected SockJsServiceConfig getSockJsConfig() { @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); }