diff --git a/spring-tx/src/main/java/org/springframework/dao/annotation/PersistenceExceptionTranslationPostProcessor.java b/spring-tx/src/main/java/org/springframework/dao/annotation/PersistenceExceptionTranslationPostProcessor.java index a612fe808d0..1c2bd4e7f68 100644 --- a/spring-tx/src/main/java/org/springframework/dao/annotation/PersistenceExceptionTranslationPostProcessor.java +++ b/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"); * 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); } } diff --git a/spring-tx/src/main/java/org/springframework/dao/support/DataAccessUtils.java b/spring-tx/src/main/java/org/springframework/dao/support/DataAccessUtils.java index adb80a3afb7..20720b36e19 100644 --- a/spring-tx/src/main/java/org/springframework/dao/support/DataAccessUtils.java +++ b/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"); * 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) requiredType); + result = NumberUtils.convertNumberToTargetClass(number, (Class) requiredType); } catch (IllegalArgumentException ex) { throw new TypeMismatchDataAccessException(ex.getMessage()); diff --git a/spring-tx/src/main/java/org/springframework/dao/support/PersistenceExceptionTranslationInterceptor.java b/spring-tx/src/main/java/org/springframework/dao/support/PersistenceExceptionTranslationInterceptor.java index 8ddcd40e807..e20bb7aff85 100644 --- a/spring-tx/src/main/java/org/springframework/dao/support/PersistenceExceptionTranslationInterceptor.java +++ b/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"); * 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; } } diff --git a/spring-tx/src/main/java/org/springframework/jca/endpoint/AbstractMessageEndpointFactory.java b/spring-tx/src/main/java/org/springframework/jca/endpoint/AbstractMessageEndpointFactory.java index a8ac4eec51b..e6b8eae942e 100644 --- a/spring-tx/src/main/java/org/springframework/jca/endpoint/AbstractMessageEndpointFactory.java +++ b/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"); * you may not use this file except in compliance with the License. diff --git a/spring-tx/src/main/java/org/springframework/transaction/interceptor/TransactionAspectSupport.java b/spring-tx/src/main/java/org/springframework/transaction/interceptor/TransactionAspectSupport.java index 98bc8be64e1..eaa85305354 100644 --- a/spring-tx/src/main/java/org/springframework/transaction/interceptor/TransactionAspectSupport.java +++ b/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 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( diff --git a/spring-tx/src/main/java/org/springframework/transaction/jta/JtaTransactionManager.java b/spring-tx/src/main/java/org/springframework/transaction/jta/JtaTransactionManager.java index 674b23af75a..b24144f6b62 100644 --- a/spring-tx/src/main/java/org/springframework/transaction/jta/JtaTransactionManager.java +++ b/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"); * you may not use this file except in compliance with the License. diff --git a/spring-tx/src/main/java/org/springframework/transaction/reactive/TransactionSynchronizationUtils.java b/spring-tx/src/main/java/org/springframework/transaction/reactive/TransactionSynchronizationUtils.java index 641b7b51407..7eaa43a0c7b 100644 --- a/spring-tx/src/main/java/org/springframework/transaction/reactive/TransactionSynchronizationUtils.java +++ b/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"); * 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; diff --git a/spring-tx/src/main/java/org/springframework/transaction/reactive/TransactionalOperatorImpl.java b/spring-tx/src/main/java/org/springframework/transaction/reactive/TransactionalOperatorImpl.java index 59843b63cea..09733a83a57 100644 --- a/spring-tx/src/main/java/org/springframework/transaction/reactive/TransactionalOperatorImpl.java +++ b/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"); * you may not use this file except in compliance with the License. diff --git a/spring-tx/src/main/java/org/springframework/transaction/support/DefaultTransactionStatus.java b/spring-tx/src/main/java/org/springframework/transaction/support/DefaultTransactionStatus.java index 41a3597bef8..ff9c95691a6 100644 --- a/spring-tx/src/main/java/org/springframework/transaction/support/DefaultTransactionStatus.java +++ b/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"); * 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(); } } diff --git a/spring-tx/src/main/java/org/springframework/transaction/support/TransactionSynchronizationManager.java b/spring-tx/src/main/java/org/springframework/transaction/support/TransactionSynchronizationManager.java index 55b3b8c788c..0b3a8962ad5 100644 --- a/spring-tx/src/main/java/org/springframework/transaction/support/TransactionSynchronizationManager.java +++ b/spring-tx/src/main/java/org/springframework/transaction/support/TransactionSynchronizationManager.java @@ -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; diff --git a/spring-tx/src/main/java/org/springframework/transaction/support/TransactionSynchronizationUtils.java b/spring-tx/src/main/java/org/springframework/transaction/support/TransactionSynchronizationUtils.java index 00d82c29650..f97dd23285f 100644 --- a/spring-tx/src/main/java/org/springframework/transaction/support/TransactionSynchronizationUtils.java +++ b/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"); * 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 diff --git a/spring-tx/src/main/java/org/springframework/transaction/support/TransactionTemplate.java b/spring-tx/src/main/java/org/springframework/transaction/support/TransactionTemplate.java index d13a489828b..87c37645eaf 100644 --- a/spring-tx/src/main/java/org/springframework/transaction/support/TransactionTemplate.java +++ b/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"); * you may not use this file except in compliance with the License. @@ -130,8 +130,8 @@ public class TransactionTemplate extends DefaultTransactionDefinition public T execute(TransactionCallback 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);