Browse Source

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
pull/30062/head
Sam Brannen 3 years ago
parent
commit
b1cf832c28
  1. 7
      spring-tx/src/main/java/org/springframework/dao/annotation/PersistenceExceptionTranslationPostProcessor.java
  2. 6
      spring-tx/src/main/java/org/springframework/dao/support/DataAccessUtils.java
  3. 6
      spring-tx/src/main/java/org/springframework/dao/support/PersistenceExceptionTranslationInterceptor.java
  4. 2
      spring-tx/src/main/java/org/springframework/jca/endpoint/AbstractMessageEndpointFactory.java
  5. 19
      spring-tx/src/main/java/org/springframework/transaction/interceptor/TransactionAspectSupport.java
  6. 2
      spring-tx/src/main/java/org/springframework/transaction/jta/JtaTransactionManager.java
  7. 10
      spring-tx/src/main/java/org/springframework/transaction/reactive/TransactionSynchronizationUtils.java
  8. 2
      spring-tx/src/main/java/org/springframework/transaction/reactive/TransactionalOperatorImpl.java
  9. 14
      spring-tx/src/main/java/org/springframework/transaction/support/DefaultTransactionStatus.java
  10. 6
      spring-tx/src/main/java/org/springframework/transaction/support/TransactionSynchronizationManager.java
  11. 6
      spring-tx/src/main/java/org/springframework/transaction/support/TransactionSynchronizationUtils.java
  12. 6
      spring-tx/src/main/java/org/springframework/transaction/support/TransactionTemplate.java

7
spring-tx/src/main/java/org/springframework/dao/annotation/PersistenceExceptionTranslationPostProcessor.java

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

6
spring-tx/src/main/java/org/springframework/dao/support/DataAccessUtils.java

@ -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"); * 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.
@ -169,9 +169,9 @@ public abstract class DataAccessUtils {
if (String.class == requiredType) { if (String.class == requiredType) {
result = result.toString(); result = result.toString();
} }
else if (Number.class.isAssignableFrom(requiredType) && result instanceof Number) { else if (Number.class.isAssignableFrom(requiredType) && result instanceof Number number) {
try { try {
result = NumberUtils.convertNumberToTargetClass(((Number) result), (Class<? extends Number>) requiredType); result = NumberUtils.convertNumberToTargetClass(number, (Class<? extends Number>) requiredType);
} }
catch (IllegalArgumentException ex) { catch (IllegalArgumentException ex) {
throw new TypeMismatchDataAccessException(ex.getMessage()); throw new TypeMismatchDataAccessException(ex.getMessage());

6
spring-tx/src/main/java/org/springframework/dao/support/PersistenceExceptionTranslationInterceptor.java

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

2
spring-tx/src/main/java/org/springframework/jca/endpoint/AbstractMessageEndpointFactory.java

@ -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"); * 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.

19
spring-tx/src/main/java/org/springframework/transaction/interceptor/TransactionAspectSupport.java

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

2
spring-tx/src/main/java/org/springframework/transaction/jta/JtaTransactionManager.java

@ -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"); * 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.

10
spring-tx/src/main/java/org/springframework/transaction/reactive/TransactionSynchronizationUtils.java

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

2
spring-tx/src/main/java/org/springframework/transaction/reactive/TransactionalOperatorImpl.java

@ -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"); * 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.

14
spring-tx/src/main/java/org/springframework/transaction/support/DefaultTransactionStatus.java

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

6
spring-tx/src/main/java/org/springframework/transaction/support/TransactionSynchronizationManager.java

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

6
spring-tx/src/main/java/org/springframework/transaction/support/TransactionSynchronizationUtils.java

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

6
spring-tx/src/main/java/org/springframework/transaction/support/TransactionTemplate.java

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

Loading…
Cancel
Save