Apply "instanceof pattern matching" Eclipse clean-up in spring-test

This has only been applied to `src/main/java`.
This commit is contained in:
Sam Brannen 2021-10-14 15:50:36 +02:00
parent 4fc359f75e
commit 7e4870577d
15 changed files with 19 additions and 38 deletions

View File

@ -1279,8 +1279,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
public void setSession(HttpSession session) { public void setSession(HttpSession session) {
this.session = session; this.session = session;
if (session instanceof MockHttpSession) { if (session instanceof MockHttpSession mockSession) {
MockHttpSession mockSession = ((MockHttpSession) session);
mockSession.access(); mockSession.access();
} }
} }

View File

@ -444,8 +444,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
if (cookie.isHttpOnly()) { if (cookie.isHttpOnly()) {
buf.append("; HttpOnly"); buf.append("; HttpOnly");
} }
if (cookie instanceof MockCookie) { if (cookie instanceof MockCookie mockCookie) {
MockCookie mockCookie = (MockCookie) cookie;
if (StringUtils.hasText(mockCookie.getSameSite())) { if (StringUtils.hasText(mockCookie.getSameSite())) {
buf.append("; SameSite=").append(mockCookie.getSameSite()); buf.append("; SameSite=").append(mockCookie.getSameSite());
} }

View File

@ -335,10 +335,9 @@ public class ContextConfigurationAttributes {
if (this == other) { if (this == other) {
return true; return true;
} }
if (!(other instanceof ContextConfigurationAttributes)) { if (!(other instanceof ContextConfigurationAttributes otherAttr)) {
return false; return false;
} }
ContextConfigurationAttributes otherAttr = (ContextConfigurationAttributes) other;
return (ObjectUtils.nullSafeEquals(this.declaringClass, otherAttr.declaringClass) && return (ObjectUtils.nullSafeEquals(this.declaringClass, otherAttr.declaringClass) &&
Arrays.equals(this.classes, otherAttr.classes)) && Arrays.equals(this.classes, otherAttr.classes)) &&
Arrays.equals(this.locations, otherAttr.locations) && Arrays.equals(this.locations, otherAttr.locations) &&

View File

@ -94,8 +94,7 @@ public class DefaultCacheAwareContextLoaderDelegate implements CacheAwareContext
ApplicationContext applicationContext; ApplicationContext applicationContext;
if (contextLoader instanceof SmartContextLoader) { if (contextLoader instanceof SmartContextLoader smartContextLoader) {
SmartContextLoader smartContextLoader = (SmartContextLoader) contextLoader;
applicationContext = smartContextLoader.loadContext(mergedContextConfiguration); applicationContext = smartContextLoader.loadContext(mergedContextConfiguration);
} }
else { else {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2020 the original author or authors. * Copyright 2002-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -58,8 +58,8 @@ class DefaultApplicationEvents implements ApplicationEvents {
} }
private Object unwrapPayloadEvent(Object source) { private Object unwrapPayloadEvent(Object source) {
return (PayloadApplicationEvent.class.isInstance(source) ? return ((source instanceof PayloadApplicationEvent<?> payloadApplicationEvent) ?
((PayloadApplicationEvent<?>) source).getPayload() : source); payloadApplicationEvent.getPayload() : source);
} }
} }

View File

@ -352,8 +352,7 @@ public abstract class AbstractTestContextBootstrapper implements TestContextBoot
logger.trace(String.format("Processing locations and classes for context configuration attributes %s", logger.trace(String.format("Processing locations and classes for context configuration attributes %s",
configAttributes)); configAttributes));
} }
if (contextLoader instanceof SmartContextLoader) { if (contextLoader instanceof SmartContextLoader smartContextLoader) {
SmartContextLoader smartContextLoader = (SmartContextLoader) contextLoader;
smartContextLoader.processContextConfiguration(configAttributes); smartContextLoader.processContextConfiguration(configAttributes);
locations.addAll(0, Arrays.asList(configAttributes.getLocations())); locations.addAll(0, Arrays.asList(configAttributes.getLocations()));
classes.addAll(0, Arrays.asList(configAttributes.getClasses())); classes.addAll(0, Arrays.asList(configAttributes.getClasses()));

View File

@ -122,9 +122,7 @@ public class DefaultTestContext implements TestContext {
@Override @Override
public ApplicationContext getApplicationContext() { public ApplicationContext getApplicationContext() {
ApplicationContext context = this.cacheAwareContextLoaderDelegate.loadContext(this.mergedContextConfiguration); ApplicationContext context = this.cacheAwareContextLoaderDelegate.loadContext(this.mergedContextConfiguration);
if (context instanceof ConfigurableApplicationContext) { if (context instanceof ConfigurableApplicationContext cac) {
@SuppressWarnings("resource")
ConfigurableApplicationContext cac = (ConfigurableApplicationContext) context;
Assert.state(cac.isActive(), () -> Assert.state(cac.isActive(), () ->
"The ApplicationContext loaded for [" + this.mergedContextConfiguration + "The ApplicationContext loaded for [" + this.mergedContextConfiguration +
"] is not active. This may be due to one of the following reasons: " + "] is not active. This may be due to one of the following reasons: " +

View File

@ -106,9 +106,7 @@ public abstract class TestContextTransactionUtils {
} }
try { try {
if (bf instanceof ListableBeanFactory) { if (bf instanceof ListableBeanFactory lbf) {
ListableBeanFactory lbf = (ListableBeanFactory) bf;
// Look up single bean by type // Look up single bean by type
Map<String, DataSource> dataSources = Map<String, DataSource> dataSources =
BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, DataSource.class); BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, DataSource.class);
@ -180,9 +178,7 @@ public abstract class TestContextTransactionUtils {
} }
try { try {
if (bf instanceof ListableBeanFactory) { if (bf instanceof ListableBeanFactory lbf) {
ListableBeanFactory lbf = (ListableBeanFactory) bf;
// Look up single TransactionManagementConfigurer // Look up single TransactionManagementConfigurer
Map<String, TransactionManagementConfigurer> configurers = Map<String, TransactionManagementConfigurer> configurers =
BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, TransactionManagementConfigurer.class); BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, TransactionManagementConfigurer.class);

View File

@ -188,8 +188,7 @@ public class ServletTestExecutionListener extends AbstractTestExecutionListener
ApplicationContext context = testContext.getApplicationContext(); ApplicationContext context = testContext.getApplicationContext();
if (context instanceof WebApplicationContext) { if (context instanceof WebApplicationContext wac) {
WebApplicationContext wac = (WebApplicationContext) context;
ServletContext servletContext = wac.getServletContext(); ServletContext servletContext = wac.getServletContext();
Assert.state(servletContext instanceof MockServletContext, () -> String.format( Assert.state(servletContext instanceof MockServletContext, () -> String.format(
"The WebApplicationContext for test context %s must be configured with a MockServletContext.", "The WebApplicationContext for test context %s must be configured with a MockServletContext.",
@ -211,9 +210,7 @@ public class ServletTestExecutionListener extends AbstractTestExecutionListener
testContext.setAttribute(POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE); testContext.setAttribute(POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE);
testContext.setAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE); testContext.setAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE);
if (wac instanceof ConfigurableApplicationContext) { if (wac instanceof ConfigurableApplicationContext configurableApplicationContext) {
@SuppressWarnings("resource")
ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) wac;
ConfigurableListableBeanFactory bf = configurableApplicationContext.getBeanFactory(); ConfigurableListableBeanFactory bf = configurableApplicationContext.getBeanFactory();
bf.registerResolvableDependency(MockHttpServletResponse.class, response); bf.registerResolvableDependency(MockHttpServletResponse.class, response);
bf.registerResolvableDependency(ServletWebRequest.class, servletWebRequest); bf.registerResolvableDependency(ServletWebRequest.class, servletWebRequest);

View File

@ -36,8 +36,7 @@ class MockServerContainerContextCustomizer implements ContextCustomizer {
@Override @Override
public void customizeContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) { public void customizeContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) {
if (context instanceof WebApplicationContext) { if (context instanceof WebApplicationContext wac) {
WebApplicationContext wac = (WebApplicationContext) context;
ServletContext sc = wac.getServletContext(); ServletContext sc = wac.getServletContext();
if (sc != null) { if (sc != null) {
sc.setAttribute("jakarta.websocket.server.ServerContainer", new MockServerContainer()); sc.setAttribute("jakarta.websocket.server.ServerContainer", new MockServerContainer());

View File

@ -45,6 +45,7 @@ abstract class AbstractMockMvcServerSpec<B extends MockMvcWebTestClient.MockMvcS
return self(); return self();
} }
@Override
public final <T extends B> T filter(Filter filter, String... urlPatterns) { public final <T extends B> T filter(Filter filter, String... urlPatterns) {
getMockMvcBuilder().addFilter(filter, urlPatterns); getMockMvcBuilder().addFilter(filter, urlPatterns);
return self(); return self();

View File

@ -370,8 +370,7 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable {
}); });
}); });
for (NameValuePair param : this.webRequest.getRequestParameters()) { for (NameValuePair param : this.webRequest.getRequestParameters()) {
if (param instanceof KeyDataPair) { if (param instanceof KeyDataPair pair) {
KeyDataPair pair = (KeyDataPair) param;
File file = pair.getFile(); File file = pair.getFile();
MockPart part; MockPart part;
if (file != null) { if (file != null) {

View File

@ -561,11 +561,9 @@ public class MockHttpServletRequestBuilder
if (parent == null) { if (parent == null) {
return this; return this;
} }
if (!(parent instanceof MockHttpServletRequestBuilder)) { if (!(parent instanceof MockHttpServletRequestBuilder parentBuilder)) {
throw new IllegalArgumentException("Cannot merge with [" + parent.getClass().getName() + "]"); throw new IllegalArgumentException("Cannot merge with [" + parent.getClass().getName() + "]");
} }
MockHttpServletRequestBuilder parentBuilder = (MockHttpServletRequestBuilder) parent;
if (!StringUtils.hasText(this.contextPath)) { if (!StringUtils.hasText(this.contextPath)) {
this.contextPath = parentBuilder.contextPath; this.contextPath = parentBuilder.contextPath;
} }

View File

@ -122,8 +122,7 @@ public class MockMultipartHttpServletRequestBuilder extends MockHttpServletReque
} }
if (parent instanceof MockHttpServletRequestBuilder) { if (parent instanceof MockHttpServletRequestBuilder) {
super.merge(parent); super.merge(parent);
if (parent instanceof MockMultipartHttpServletRequestBuilder) { if (parent instanceof MockMultipartHttpServletRequestBuilder parentBuilder) {
MockMultipartHttpServletRequestBuilder parentBuilder = (MockMultipartHttpServletRequestBuilder) parent;
this.files.addAll(parentBuilder.files); this.files.addAll(parentBuilder.files);
parentBuilder.parts.keySet().forEach(name -> parentBuilder.parts.keySet().forEach(name ->
this.parts.putIfAbsent(name, parentBuilder.parts.get(name))); this.parts.putIfAbsent(name, parentBuilder.parts.get(name)));

View File

@ -179,8 +179,7 @@ public class PrintingResultHandler implements ResultHandler {
this.printer.printValue("Type", null); this.printer.printValue("Type", null);
} }
else { else {
if (handler instanceof HandlerMethod) { if (handler instanceof HandlerMethod handlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
this.printer.printValue("Type", handlerMethod.getBeanType().getName()); this.printer.printValue("Type", handlerMethod.getBeanType().getName());
this.printer.printValue("Method", handlerMethod); this.printer.printValue("Method", handlerMethod);
} }