Skip to content

Commit

Permalink
Use Assert.state() where appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Nov 15, 2022
1 parent 2aa7888 commit abf3400
Show file tree
Hide file tree
Showing 30 changed files with 80 additions and 82 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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");
}


Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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<? extends Annotation> customAsyncAnnotation = this.enableAsync.getClass("annotation");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public Stream<StackWalker.StackFrame> getStackFrames() {
*/
@SuppressWarnings("unchecked")
public <T> 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;
}

Expand All @@ -108,7 +108,7 @@ public <T> 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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,32 @@ 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 {
return CodeBlock.of("this::$L", methodName);
}
}

@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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand Down Expand Up @@ -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");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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 + "]");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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,
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down
Loading

0 comments on commit abf3400

Please sign in to comment.