Use pattern matching for instanceof where appropriate

See gh-31475
This commit is contained in:
dreis2211
2022-06-20 18:14:16 +02:00
committed by Andy Wilkinson
parent a7b98e7312
commit 5db04da275
278 changed files with 1049 additions and 1126 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-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.
@@ -94,11 +94,11 @@ class ImportsContextCustomizer implements ContextCustomizer {
}
private BeanDefinitionRegistry getBeanDefinitionRegistry(ApplicationContext context) {
if (context instanceof BeanDefinitionRegistry) {
return (BeanDefinitionRegistry) context;
if (context instanceof BeanDefinitionRegistry beanDefinitionRegistry) {
return beanDefinitionRegistry;
}
if (context instanceof AbstractApplicationContext) {
return (BeanDefinitionRegistry) ((AbstractApplicationContext) context).getBeanFactory();
if (context instanceof AbstractApplicationContext abstractContext) {
return (BeanDefinitionRegistry) abstractContext.getBeanFactory();
}
throw new IllegalStateException("Could not locate BeanDefinitionRegistry");
}
@@ -284,8 +284,8 @@ class ImportsContextCustomizer implements ContextCustomizer {
}
private Class<?>[] getImports(Annotation annotation) {
if (annotation instanceof Import) {
return ((Import) annotation).value();
if (annotation instanceof Import importAnnotation) {
return importAnnotation.value();
}
return NO_IMPORTS;
}

View File

@@ -296,8 +296,8 @@ public class SpringBootContextLoader extends AbstractContextLoader {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
if (applicationContext instanceof ConfigurableWebApplicationContext) {
this.delegate.initialize((ConfigurableWebApplicationContext) applicationContext);
if (applicationContext instanceof ConfigurableWebApplicationContext webApplicationContext) {
this.delegate.initialize(webApplicationContext);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-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,8 +69,8 @@ class SpringBootTestArgs implements ContextCustomizer {
*/
static String[] get(Set<ContextCustomizer> customizers) {
for (ContextCustomizer customizer : customizers) {
if (customizer instanceof SpringBootTestArgs) {
return ((SpringBootTestArgs) customizer).args;
if (customizer instanceof SpringBootTestArgs testArgs) {
return testArgs.args;
}
}
return NO_ARGS;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-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.
@@ -298,8 +298,8 @@ public class ApplicationContextAssert<C extends ApplicationContext>
private boolean isPrimary(String name, Scope scope) {
ApplicationContext context = getApplicationContext();
while (context != null) {
if (context instanceof ConfigurableApplicationContext) {
ConfigurableListableBeanFactory factory = ((ConfigurableApplicationContext) context).getBeanFactory();
if (context instanceof ConfigurableApplicationContext configurableContext) {
ConfigurableListableBeanFactory factory = configurableContext.getBeanFactory();
if (factory.containsBean(name) && factory.getMergedBeanDefinition(name).isPrimary()) {
return true;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-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.
@@ -46,9 +46,9 @@ class AssertProviderApplicationContextInvocationHandler implements InvocationHan
AssertProviderApplicationContextInvocationHandler(Class<?> applicationContextType, Supplier<?> contextSupplier) {
this.applicationContextType = applicationContextType;
Object contextOrStartupFailure = getContextOrStartupFailure(contextSupplier);
if (contextOrStartupFailure instanceof RuntimeException) {
if (contextOrStartupFailure instanceof RuntimeException runtimeException) {
this.applicationContext = null;
this.startupFailure = (RuntimeException) contextOrStartupFailure;
this.startupFailure = runtimeException;
}
else {
this.applicationContext = (ApplicationContext) contextOrStartupFailure;
@@ -136,8 +136,8 @@ class AssertProviderApplicationContextInvocationHandler implements InvocationHan
}
private Object invokeClose() throws IOException {
if (this.applicationContext instanceof Closeable) {
((Closeable) this.applicationContext).close();
if (this.applicationContext instanceof Closeable closeable) {
closeable.close();
}
return null;
}

View File

@@ -392,11 +392,10 @@ public abstract class AbstractApplicationContextRunner<SELF extends AbstractAppl
private C createAndLoadContext(boolean refresh) {
C context = this.runnerConfiguration.contextFactory.get();
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
if (beanFactory instanceof AbstractAutowireCapableBeanFactory) {
((AbstractAutowireCapableBeanFactory) beanFactory)
.setAllowCircularReferences(this.runnerConfiguration.allowCircularReferences);
if (beanFactory instanceof DefaultListableBeanFactory) {
((DefaultListableBeanFactory) beanFactory)
if (beanFactory instanceof AbstractAutowireCapableBeanFactory autowireCapableBeanFactory) {
autowireCapableBeanFactory.setAllowCircularReferences(this.runnerConfiguration.allowCircularReferences);
if (beanFactory instanceof DefaultListableBeanFactory listableBeanFactory) {
listableBeanFactory
.setAllowBeanDefinitionOverriding(this.runnerConfiguration.allowBeanDefinitionOverriding);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-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.
@@ -102,17 +102,17 @@ public class JsonContentAssert extends AbstractAssert<JsonContentAssert, CharSeq
if (expected == null || expected instanceof CharSequence) {
return isEqualToJson((CharSequence) expected);
}
if (expected instanceof byte[]) {
return isEqualToJson((byte[]) expected);
if (expected instanceof byte[] bytes) {
return isEqualToJson(bytes);
}
if (expected instanceof File) {
return isEqualToJson((File) expected);
if (expected instanceof File file) {
return isEqualToJson(file);
}
if (expected instanceof InputStream) {
return isEqualToJson((InputStream) expected);
if (expected instanceof InputStream inputStream) {
return isEqualToJson(inputStream);
}
if (expected instanceof Resource) {
return isEqualToJson((Resource) expected);
if (expected instanceof Resource resource) {
return isEqualToJson(resource);
}
failWithMessage("Unsupported type for JSON assert %s", expected.getClass());
return null;
@@ -430,17 +430,17 @@ public class JsonContentAssert extends AbstractAssert<JsonContentAssert, CharSeq
if (expected == null || expected instanceof CharSequence) {
return isNotEqualToJson((CharSequence) expected);
}
if (expected instanceof byte[]) {
return isNotEqualToJson((byte[]) expected);
if (expected instanceof byte[] bytes) {
return isNotEqualToJson(bytes);
}
if (expected instanceof File) {
return isNotEqualToJson((File) expected);
if (expected instanceof File file) {
return isNotEqualToJson(file);
}
if (expected instanceof InputStream) {
return isNotEqualToJson((InputStream) expected);
if (expected instanceof InputStream inputStream) {
return isNotEqualToJson(inputStream);
}
if (expected instanceof Resource) {
return isNotEqualToJson((Resource) expected);
if (expected instanceof Resource resource) {
return isNotEqualToJson(resource);
}
failWithMessage("Unsupported type for JSON assert %s", expected.getClass());
return null;
@@ -1003,8 +1003,8 @@ public class JsonContentAssert extends AbstractAssert<JsonContentAssert, CharSeq
this.actual.toString(), compareMode);
}
catch (Exception ex) {
if (ex instanceof RuntimeException) {
throw (RuntimeException) ex;
if (ex instanceof RuntimeException runtimeException) {
throw runtimeException;
}
throw new IllegalStateException(ex);
}
@@ -1019,8 +1019,8 @@ public class JsonContentAssert extends AbstractAssert<JsonContentAssert, CharSeq
this.actual.toString(), comparator);
}
catch (Exception ex) {
if (ex instanceof RuntimeException) {
throw (RuntimeException) ex;
if (ex instanceof RuntimeException runtimeException) {
throw runtimeException;
}
throw new IllegalStateException(ex);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-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.
@@ -103,8 +103,7 @@ class DefinitionsParser {
private void addDefinition(AnnotatedElement element, Definition definition, String type) {
boolean isNewDefinition = this.definitions.add(definition);
Assert.state(isNewDefinition, () -> "Duplicate " + type + " definition " + definition);
if (element instanceof Field) {
Field field = (Field) element;
if (element instanceof Field field) {
this.definitionFields.put(definition, field);
}
}
@@ -114,8 +113,7 @@ class DefinitionsParser {
for (Class<?> clazz : value) {
types.add(ResolvableType.forClass(clazz));
}
if (types.isEmpty() && element instanceof Field) {
Field field = (Field) element;
if (types.isEmpty() && element instanceof Field field) {
types.add((field.getGenericType() instanceof TypeVariable) ? ResolvableType.forField(field, source)
: ResolvableType.forField(field));
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-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.
@@ -107,8 +107,8 @@ public enum MockReset {
MockCreationSettings<?> settings = mockingDetails.getMockCreationSettings();
List<InvocationListener> listeners = settings.getInvocationListeners();
for (Object listener : listeners) {
if (listener instanceof ResetInvocationListener) {
reset = ((ResetInvocationListener) listener).getReset();
if (listener instanceof ResetInvocationListener resetInvocationListener) {
reset = resetInvocationListener.getReset();
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-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.
@@ -40,8 +40,8 @@ class MockitoContextCustomizer implements ContextCustomizer {
@Override
public void customizeContext(ConfigurableApplicationContext context,
MergedContextConfiguration mergedContextConfiguration) {
if (context instanceof BeanDefinitionRegistry) {
MockitoPostProcessor.register((BeanDefinitionRegistry) context, this.definitions);
if (context instanceof BeanDefinitionRegistry registry) {
MockitoPostProcessor.register(registry, this.definitions);
}
}

View File

@@ -163,11 +163,11 @@ public class MockitoPostProcessor implements InstantiationAwareBeanPostProcessor
private void register(ConfigurableListableBeanFactory beanFactory, BeanDefinitionRegistry registry,
Definition definition, Field field) {
if (definition instanceof MockDefinition) {
registerMock(beanFactory, registry, (MockDefinition) definition, field);
if (definition instanceof MockDefinition mockDefinition) {
registerMock(beanFactory, registry, mockDefinition, field);
}
else if (definition instanceof SpyDefinition) {
registerSpy(beanFactory, registry, (SpyDefinition) definition, field);
else if (definition instanceof SpyDefinition spyDefinition) {
registerSpy(beanFactory, registry, spyDefinition, field);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-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.
@@ -73,8 +73,8 @@ public class MockitoTestExecutionListener extends AbstractTestExecutionListener
@Override
public void afterTestMethod(TestContext testContext) throws Exception {
Object mocks = testContext.getAttribute(MOCKS_ATTRIBUTE_NAME);
if (mocks instanceof AutoCloseable) {
((AutoCloseable) mocks).close();
if (mocks instanceof AutoCloseable closeable) {
closeable.close();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-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.
@@ -78,8 +78,7 @@ class QualifierDefinition {
}
static QualifierDefinition forElement(AnnotatedElement element) {
if (element != null && element instanceof Field) {
Field field = (Field) element;
if (element != null && element instanceof Field field) {
Set<Annotation> annotations = getQualifierAnnotations(field);
if (!annotations.isEmpty()) {
return new QualifierDefinition(field, annotations);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-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.
@@ -66,8 +66,8 @@ public class ResetMocksTestExecutionListener extends AbstractTestExecutionListen
}
private void resetMocks(ApplicationContext applicationContext, MockReset reset) {
if (applicationContext instanceof ConfigurableApplicationContext) {
resetMocks((ConfigurableApplicationContext) applicationContext, reset);
if (applicationContext instanceof ConfigurableApplicationContext configurableContext) {
resetMocks(configurableContext, reset);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-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.
@@ -208,8 +208,8 @@ class OutputCapture implements CapturedOutput {
}
private static PrintStream getSystemStream(PrintStream printStream) {
while (printStream instanceof PrintStreamCapture) {
printStream = ((PrintStreamCapture) printStream).getParent();
while (printStream instanceof PrintStreamCapture printStreamCapture) {
printStream = printStreamCapture.getParent();
}
return printStream;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-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.
@@ -33,8 +33,8 @@ public abstract class ApplicationContextTestUtils {
*/
public static void closeAll(ApplicationContext context) {
if (context != null) {
if (context instanceof ConfigurableApplicationContext) {
((ConfigurableApplicationContext) context).close();
if (context instanceof ConfigurableApplicationContext configurableContext) {
configurableContext.close();
}
closeAll(context.getParent());
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-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.
@@ -89,8 +89,8 @@ class SpringBootTestRandomPortEnvironmentPostProcessor implements EnvironmentPos
return environment.getConversionService().convert(value, Integer.class);
}
catch (ConversionFailedException ex) {
if (value instanceof String) {
return getResolvedValueIfPossible(environment, (String) value);
if (value instanceof String string) {
return getResolvedValueIfPossible(environment, string);
}
throw ex;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-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.
@@ -96,9 +96,9 @@ public class RootUriRequestExpectationManager implements RequestExpectationManag
URI uri;
try {
uri = new URI(replacementUri);
if (request instanceof MockClientHttpRequest) {
((MockClientHttpRequest) request).setURI(uri);
return request;
if (request instanceof MockClientHttpRequest mockClientHttpRequest) {
mockClientHttpRequest.setURI(uri);
return mockClientHttpRequest;
}
return new ReplaceUriClientHttpRequest(uri, request);
}
@@ -158,9 +158,8 @@ public class RootUriRequestExpectationManager implements RequestExpectationManag
RequestExpectationManager expectationManager) {
Assert.notNull(restTemplate, "RestTemplate must not be null");
UriTemplateHandler templateHandler = restTemplate.getUriTemplateHandler();
if (templateHandler instanceof RootUriTemplateHandler) {
return new RootUriRequestExpectationManager(((RootUriTemplateHandler) templateHandler).getRootUri(),
expectationManager);
if (templateHandler instanceof RootUriTemplateHandler rootHandler) {
return new RootUriRequestExpectationManager(rootHandler.getRootUri(), expectationManager);
}
return expectationManager;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-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.
@@ -163,8 +163,8 @@ public class TestRestTemplate {
*/
public String getRootUri() {
UriTemplateHandler uriTemplateHandler = this.restTemplate.getUriTemplateHandler();
if (uriTemplateHandler instanceof RootUriTemplateHandler) {
return ((RootUriTemplateHandler) uriTemplateHandler).getRootUri();
if (uriTemplateHandler instanceof RootUriTemplateHandler rootHandler) {
return rootHandler.getRootUri();
}
return "";
}
@@ -941,8 +941,8 @@ public class TestRestTemplate {
private URI applyRootUriIfNecessary(URI uri) {
UriTemplateHandler uriTemplateHandler = this.restTemplate.getUriTemplateHandler();
if ((uriTemplateHandler instanceof RootUriTemplateHandler) && uri.toString().startsWith("/")) {
return URI.create(((RootUriTemplateHandler) uriTemplateHandler).getRootUri() + uri.toString());
if ((uriTemplateHandler instanceof RootUriTemplateHandler rootHandler) && uri.toString().startsWith("/")) {
return URI.create(rootHandler.getRootUri() + uri.toString());
}
return uri;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-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.
@@ -61,8 +61,8 @@ class TestRestTemplateContextCustomizer implements ContextCustomizer {
private void registerTestRestTemplate(ConfigurableApplicationContext context) {
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
if (beanFactory instanceof BeanDefinitionRegistry) {
registerTestRestTemplate((BeanDefinitionRegistry) beanFactory);
if (beanFactory instanceof BeanDefinitionRegistry registry) {
registerTestRestTemplate(registry);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-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.
@@ -67,8 +67,8 @@ class WebTestClientContextCustomizer implements ContextCustomizer {
private void registerWebTestClient(ConfigurableApplicationContext context) {
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
if (beanFactory instanceof BeanDefinitionRegistry) {
registerWebTestClient((BeanDefinitionRegistry) beanFactory);
if (beanFactory instanceof BeanDefinitionRegistry registry) {
registerWebTestClient(registry);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-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.
@@ -42,8 +42,7 @@ public class TestDefaultTestExecutionListenersPostProcessor implements DefaultTe
@Override
public void prepareTestInstance(TestContext testContext) throws Exception {
Object testInstance = testContext.getTestInstance();
if (testInstance instanceof SpringBootTestContextBootstrapperIntegrationTests) {
SpringBootTestContextBootstrapperIntegrationTests test = (SpringBootTestContextBootstrapperIntegrationTests) testInstance;
if (testInstance instanceof SpringBootTestContextBootstrapperIntegrationTests test) {
test.defaultTestExecutionListenersPostProcessorCalled = true;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-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.
@@ -56,8 +56,8 @@ class MockBeanContextCachingTests {
Map<MergedContextConfiguration, ApplicationContext> contexts = (Map<MergedContextConfiguration, ApplicationContext>) ReflectionTestUtils
.getField(this.contextCache, "contextMap");
for (ApplicationContext context : contexts.values()) {
if (context instanceof ConfigurableApplicationContext) {
((ConfigurableApplicationContext) context).close();
if (context instanceof ConfigurableApplicationContext configurableContext) {
configurableContext.close();
}
}
this.contextCache.clear();