Apply "instanceof pattern matching" in spring-tx

This commit applies "instanceof pattern matching" to the rest of the
code in spring-tx.

See gh-30019
This commit is contained in:
Sam Brannen 2023-02-28 15:55:52 +01:00
parent 34e5ce9360
commit b1cf832c28
12 changed files with 44 additions and 42 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2023 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.
@ -82,12 +82,11 @@ public class PersistenceExceptionTranslationPostProcessor extends AbstractBeanFa
public void setBeanFactory(BeanFactory beanFactory) {
super.setBeanFactory(beanFactory);
if (!(beanFactory instanceof ListableBeanFactory)) {
if (!(beanFactory instanceof ListableBeanFactory lbf)) {
throw new IllegalArgumentException(
"Cannot use PersistenceExceptionTranslator autodetection without ListableBeanFactory");
}
this.advisor = new PersistenceExceptionTranslationAdvisor(
(ListableBeanFactory) beanFactory, this.repositoryAnnotationType);
this.advisor = new PersistenceExceptionTranslationAdvisor(lbf, this.repositoryAnnotationType);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2023 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.
@ -169,9 +169,9 @@ public abstract class DataAccessUtils {
if (String.class == requiredType) {
result = result.toString();
}
else if (Number.class.isAssignableFrom(requiredType) && result instanceof Number) {
else if (Number.class.isAssignableFrom(requiredType) && result instanceof Number number) {
try {
result = NumberUtils.convertNumberToTargetClass(((Number) result), (Class<? extends Number>) requiredType);
result = NumberUtils.convertNumberToTargetClass(number, (Class<? extends Number>) requiredType);
}
catch (IllegalArgumentException ex) {
throw new TypeMismatchDataAccessException(ex.getMessage());

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2023 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.
@ -114,11 +114,11 @@ public class PersistenceExceptionTranslationInterceptor
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
if (this.persistenceExceptionTranslator == null) {
// No explicit exception translator specified - perform autodetection.
if (!(beanFactory instanceof ListableBeanFactory)) {
if (!(beanFactory instanceof ListableBeanFactory lbf)) {
throw new IllegalArgumentException(
"Cannot use PersistenceExceptionTranslator autodetection without ListableBeanFactory");
}
this.beanFactory = (ListableBeanFactory) beanFactory;
this.beanFactory = lbf;
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2023 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.

View File

@ -343,7 +343,7 @@ public abstract class TransactionAspectSupport implements BeanFactoryAware, Init
final TransactionAttribute txAttr = (tas != null ? tas.getTransactionAttribute(method, targetClass) : null);
final TransactionManager tm = determineTransactionManager(txAttr);
if (this.reactiveAdapterRegistry != null && tm instanceof ReactiveTransactionManager) {
if (this.reactiveAdapterRegistry != null && tm instanceof ReactiveTransactionManager rtm) {
boolean isSuspendingFunction = KotlinDetector.isSuspendingFunction(method);
boolean hasSuspendingFlowReturnType = isSuspendingFunction &&
COROUTINES_FLOW_CLASS_NAME.equals(new MethodParameter(method, -1).getParameterType().getName());
@ -367,7 +367,7 @@ public abstract class TransactionAspectSupport implements BeanFactoryAware, Init
if (corInv != null) {
callback = () -> KotlinDelegate.invokeSuspendingFunction(method, corInv);
}
Object result = txSupport.invokeWithinTransaction(method, targetClass, callback, txAttr, (ReactiveTransactionManager) tm);
Object result = txSupport.invokeWithinTransaction(method, targetClass, callback, txAttr, rtm);
if (corInv != null) {
Publisher<?> pr = (Publisher<?>) result;
return (hasSuspendingFlowReturnType ? KotlinDelegate.asFlow(pr) :
@ -379,7 +379,7 @@ public abstract class TransactionAspectSupport implements BeanFactoryAware, Init
PlatformTransactionManager ptm = asPlatformTransactionManager(tm);
final String joinpointIdentification = methodIdentification(method, targetClass, txAttr);
if (txAttr == null || !(ptm instanceof CallbackPreferringPlatformTransactionManager)) {
if (txAttr == null || !(ptm instanceof CallbackPreferringPlatformTransactionManager cpptm)) {
// Standard transaction demarcation with getTransaction and commit/rollback calls.
TransactionInfo txInfo = createTransactionIfNecessary(ptm, txAttr, joinpointIdentification);
@ -416,7 +416,7 @@ public abstract class TransactionAspectSupport implements BeanFactoryAware, Init
// It's a CallbackPreferringPlatformTransactionManager: pass a TransactionCallback in.
try {
result = ((CallbackPreferringPlatformTransactionManager) ptm).execute(txAttr, status -> {
result = cpptm.execute(txAttr, status -> {
TransactionInfo txInfo = prepareTransactionInfo(ptm, txAttr, joinpointIdentification, status);
try {
Object retVal = invocation.proceedWithInvocation();
@ -429,8 +429,8 @@ public abstract class TransactionAspectSupport implements BeanFactoryAware, Init
catch (Throwable ex) {
if (txAttr.rollbackOn(ex)) {
// A RuntimeException: will lead to a rollback.
if (ex instanceof RuntimeException rex) {
throw rex;
if (ex instanceof RuntimeException runtimeException) {
throw runtimeException;
}
else {
throw new ThrowableHolderException(ex);
@ -524,8 +524,11 @@ public abstract class TransactionAspectSupport implements BeanFactoryAware, Init
@Nullable
private PlatformTransactionManager asPlatformTransactionManager(@Nullable Object transactionManager) {
if (transactionManager == null || transactionManager instanceof PlatformTransactionManager) {
return (PlatformTransactionManager) transactionManager;
if (transactionManager == null) {
return null;
}
if (transactionManager instanceof PlatformTransactionManager ptm) {
return ptm;
}
else {
throw new IllegalStateException(

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2023 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.

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2023 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.
@ -55,8 +55,8 @@ abstract class TransactionSynchronizationUtils {
Assert.notNull(resource, "Resource must not be null");
Object resourceRef = resource;
// unwrap infrastructure proxy
if (resourceRef instanceof InfrastructureProxy infraProxy) {
resourceRef = infraProxy.getWrappedObject();
if (resourceRef instanceof InfrastructureProxy infrastructureProxy) {
resourceRef = infrastructureProxy.getWrappedObject();
}
if (aopAvailable) {
// now unwrap scoped proxy
@ -125,8 +125,8 @@ abstract class TransactionSynchronizationUtils {
private static class ScopedProxyUnwrapper {
public static Object unwrapIfNecessary(Object resource) {
if (resource instanceof ScopedObject so) {
return so.getTargetObject();
if (resource instanceof ScopedObject scopedObject) {
return scopedObject.getTargetObject();
}
else {
return resource;

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2023 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.
@ -161,8 +161,8 @@ public class DefaultTransactionStatus extends AbstractTransactionStatus {
*/
@Override
public boolean isGlobalRollbackOnly() {
return ((this.transaction instanceof SmartTransactionObject) &&
((SmartTransactionObject) this.transaction).isRollbackOnly());
return (this.transaction instanceof SmartTransactionObject smartTransactionObject &&
smartTransactionObject.isRollbackOnly());
}
/**
@ -174,11 +174,11 @@ public class DefaultTransactionStatus extends AbstractTransactionStatus {
@Override
protected SavepointManager getSavepointManager() {
Object transaction = this.transaction;
if (!(transaction instanceof SavepointManager manager)) {
if (!(transaction instanceof SavepointManager savepointManager)) {
throw new NestedTransactionNotSupportedException(
"Transaction object [" + this.transaction + "] does not support savepoints");
}
return manager;
return savepointManager;
}
/**
@ -198,8 +198,8 @@ public class DefaultTransactionStatus extends AbstractTransactionStatus {
*/
@Override
public void flush() {
if (this.transaction instanceof SmartTransactionObject transactionObject) {
transactionObject.flush();
if (this.transaction instanceof SmartTransactionObject smartTransactionObject) {
smartTransactionObject.flush();
}
}

View File

@ -146,7 +146,7 @@ public abstract class TransactionSynchronizationManager {
}
Object value = map.get(actualKey);
// Transparently remove ResourceHolder that was marked as void...
if (value instanceof ResourceHolder && ((ResourceHolder) value).isVoid()) {
if (value instanceof ResourceHolder resourceHolder && resourceHolder.isVoid()) {
map.remove(actualKey);
// Remove entire ThreadLocal if empty...
if (map.isEmpty()) {
@ -175,7 +175,7 @@ public abstract class TransactionSynchronizationManager {
}
Object oldValue = map.put(actualKey, value);
// Transparently suppress a ResourceHolder that was marked as void...
if (oldValue instanceof ResourceHolder && ((ResourceHolder) oldValue).isVoid()) {
if (oldValue instanceof ResourceHolder resourceHolder && resourceHolder.isVoid()) {
oldValue = null;
}
if (oldValue != null) {
@ -226,7 +226,7 @@ public abstract class TransactionSynchronizationManager {
resources.remove();
}
// Transparently suppress a ResourceHolder that was marked as void...
if (value instanceof ResourceHolder && ((ResourceHolder) value).isVoid()) {
if (value instanceof ResourceHolder resourceHolder && resourceHolder.isVoid()) {
value = null;
}
return value;

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2023 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.
@ -64,8 +64,8 @@ public abstract class TransactionSynchronizationUtils {
Assert.notNull(resource, "Resource must not be null");
Object resourceRef = resource;
// unwrap infrastructure proxy
if (resourceRef instanceof InfrastructureProxy infrasProxy) {
resourceRef = infrasProxy.getWrappedObject();
if (resourceRef instanceof InfrastructureProxy infrastructureProxy) {
resourceRef = infrastructureProxy.getWrappedObject();
}
if (aopAvailable) {
// now unwrap scoped proxy

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2023 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.
@ -130,8 +130,8 @@ public class TransactionTemplate extends DefaultTransactionDefinition
public <T> T execute(TransactionCallback<T> action) throws TransactionException {
Assert.state(this.transactionManager != null, "No PlatformTransactionManager set");
if (this.transactionManager instanceof CallbackPreferringPlatformTransactionManager) {
return ((CallbackPreferringPlatformTransactionManager) this.transactionManager).execute(this, action);
if (this.transactionManager instanceof CallbackPreferringPlatformTransactionManager cpptm) {
return cpptm.execute(this, action);
}
else {
TransactionStatus status = this.transactionManager.getTransaction(this);