Browse Source

Apply "instanceof pattern matching" in spring-test

pull/28876/head
Sam Brannen 2 years ago
parent
commit
aa7ef79478
  1. 4
      spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultControllerSpec.java
  2. 18
      spring-test/src/main/java/org/springframework/test/web/servlet/setup/StandaloneMockMvcBuilder.java

4
spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultControllerSpec.java

@ -1,5 +1,5 @@ @@ -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.
@ -62,7 +62,7 @@ class DefaultControllerSpec extends AbstractMockServerSpec<WebTestClient.Control @@ -62,7 +62,7 @@ class DefaultControllerSpec extends AbstractMockServerSpec<WebTestClient.Control
private static List<Object> instantiateIfNecessary(Object[] specified) {
List<Object> instances = new ArrayList<>(specified.length);
for (Object obj : specified) {
instances.add(obj instanceof Class ? BeanUtils.instantiateClass((Class<?>) obj) : obj);
instances.add(obj instanceof Class<?> clazz ? BeanUtils.instantiateClass(clazz) : obj);
}
return instances;
}

18
spring-test/src/main/java/org/springframework/test/web/servlet/setup/StandaloneMockMvcBuilder.java

@ -155,7 +155,7 @@ public class StandaloneMockMvcBuilder extends AbstractMockMvcBuilder<StandaloneM @@ -155,7 +155,7 @@ public class StandaloneMockMvcBuilder extends AbstractMockMvcBuilder<StandaloneM
private static List<Object> instantiateIfNecessary(Object[] specified) {
List<Object> instances = new ArrayList<>(specified.length);
for (Object obj : specified) {
instances.add(obj instanceof Class ? BeanUtils.instantiateClass((Class<?>) obj) : obj);
instances.add(obj instanceof Class<?> clazz ? BeanUtils.instantiateClass(clazz) : obj);
}
return instances;
}
@ -445,8 +445,8 @@ public class StandaloneMockMvcBuilder extends AbstractMockMvcBuilder<StandaloneM @@ -445,8 +445,8 @@ public class StandaloneMockMvcBuilder extends AbstractMockMvcBuilder<StandaloneM
this.viewResolvers = (this.viewResolvers != null ? this.viewResolvers :
Collections.singletonList(new InternalResourceViewResolver()));
for (Object viewResolver : this.viewResolvers) {
if (viewResolver instanceof WebApplicationObjectSupport) {
((WebApplicationObjectSupport) viewResolver).setApplicationContext(wac);
if (viewResolver instanceof WebApplicationObjectSupport support) {
support.setApplicationContext(wac);
}
}
return this.viewResolvers;
@ -538,9 +538,9 @@ public class StandaloneMockMvcBuilder extends AbstractMockMvcBuilder<StandaloneM @@ -538,9 +538,9 @@ public class StandaloneMockMvcBuilder extends AbstractMockMvcBuilder<StandaloneM
@Override
public Validator mvcValidator() {
Validator mvcValidator = (validator != null) ? validator : super.mvcValidator();
if (mvcValidator instanceof InitializingBean) {
if (mvcValidator instanceof InitializingBean initializingBean) {
try {
((InitializingBean) mvcValidator).afterPropertiesSet();
initializingBean.afterPropertiesSet();
}
catch (Exception ex) {
throw new BeanInitializationException("Failed to initialize Validator", ex);
@ -555,15 +555,15 @@ public class StandaloneMockMvcBuilder extends AbstractMockMvcBuilder<StandaloneM @@ -555,15 +555,15 @@ public class StandaloneMockMvcBuilder extends AbstractMockMvcBuilder<StandaloneM
return;
}
for (HandlerExceptionResolver resolver : handlerExceptionResolvers) {
if (resolver instanceof ApplicationContextAware) {
if (resolver instanceof ApplicationContextAware applicationContextAware) {
ApplicationContext applicationContext = getApplicationContext();
if (applicationContext != null) {
((ApplicationContextAware) resolver).setApplicationContext(applicationContext);
applicationContextAware.setApplicationContext(applicationContext);
}
}
if (resolver instanceof InitializingBean) {
if (resolver instanceof InitializingBean initializingBean) {
try {
((InitializingBean) resolver).afterPropertiesSet();
initializingBean.afterPropertiesSet();
}
catch (Exception ex) {
throw new IllegalStateException("Failure from afterPropertiesSet", ex);

Loading…
Cancel
Save