Browse Source

Apply "instanceof pattern matching" in remainder of spring-orm module

See gh-30067
pull/30074/head
Sam Brannen 2 years ago
parent
commit
b7288a4073
  1. 8
      spring-orm/src/main/java/org/springframework/orm/ObjectOptimisticLockingFailureException.java
  2. 8
      spring-orm/src/main/java/org/springframework/orm/ObjectRetrievalFailureException.java
  3. 10
      spring-orm/src/main/java/org/springframework/orm/hibernate5/HibernateExceptionTranslator.java
  4. 22
      spring-orm/src/main/java/org/springframework/orm/hibernate5/HibernateTemplate.java
  5. 20
      spring-orm/src/main/java/org/springframework/orm/hibernate5/HibernateTransactionManager.java
  6. 6
      spring-orm/src/main/java/org/springframework/orm/hibernate5/LocalSessionFactoryBean.java
  7. 19
      spring-orm/src/main/java/org/springframework/orm/hibernate5/LocalSessionFactoryBuilder.java
  8. 64
      spring-orm/src/main/java/org/springframework/orm/hibernate5/SessionFactoryUtils.java
  9. 7
      spring-orm/src/main/java/org/springframework/orm/hibernate5/SpringFlushSynchronization.java
  10. 21
      spring-orm/src/main/java/org/springframework/orm/jpa/EntityManagerFactoryUtils.java
  11. 16
      spring-orm/src/main/java/org/springframework/orm/jpa/JpaTransactionManager.java
  12. 4
      spring-orm/src/main/java/org/springframework/orm/jpa/LocalContainerEntityManagerFactoryBean.java
  13. 20
      spring-orm/src/main/java/org/springframework/orm/jpa/SharedEntityManagerCreator.java
  14. 4
      spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/SpringPersistenceUnitInfo.java
  15. 43
      spring-orm/src/main/java/org/springframework/orm/jpa/vendor/HibernateJpaDialect.java
  16. 6
      spring-orm/src/main/java/org/springframework/orm/jpa/vendor/SpringHibernateJpaPersistenceProvider.java

8
spring-orm/src/main/java/org/springframework/orm/ObjectOptimisticLockingFailureException.java

@ -1,5 +1,5 @@ @@ -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.
@ -137,7 +137,7 @@ public class ObjectOptimisticLockingFailureException extends OptimisticLockingFa @@ -137,7 +137,7 @@ public class ObjectOptimisticLockingFailureException extends OptimisticLockingFa
*/
@Nullable
public Class<?> getPersistentClass() {
return (this.persistentClass instanceof Class ? (Class<?>) this.persistentClass : null);
return (this.persistentClass instanceof Class<?> clazz ? clazz : null);
}
/**
@ -146,8 +146,8 @@ public class ObjectOptimisticLockingFailureException extends OptimisticLockingFa @@ -146,8 +146,8 @@ public class ObjectOptimisticLockingFailureException extends OptimisticLockingFa
*/
@Nullable
public String getPersistentClassName() {
if (this.persistentClass instanceof Class) {
return ((Class<?>) this.persistentClass).getName();
if (this.persistentClass instanceof Class<?> clazz) {
return clazz.getName();
}
return (this.persistentClass != null ? this.persistentClass.toString() : null);
}

8
spring-orm/src/main/java/org/springframework/orm/ObjectRetrievalFailureException.java

@ -1,5 +1,5 @@ @@ -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.
@ -111,7 +111,7 @@ public class ObjectRetrievalFailureException extends DataRetrievalFailureExcepti @@ -111,7 +111,7 @@ public class ObjectRetrievalFailureException extends DataRetrievalFailureExcepti
*/
@Nullable
public Class<?> getPersistentClass() {
return (this.persistentClass instanceof Class ? (Class<?>) this.persistentClass : null);
return (this.persistentClass instanceof Class<?> clazz ? clazz : null);
}
/**
@ -120,8 +120,8 @@ public class ObjectRetrievalFailureException extends DataRetrievalFailureExcepti @@ -120,8 +120,8 @@ public class ObjectRetrievalFailureException extends DataRetrievalFailureExcepti
*/
@Nullable
public String getPersistentClassName() {
if (this.persistentClass instanceof Class) {
return ((Class<?>) this.persistentClass).getName();
if (this.persistentClass instanceof Class<?> clazz) {
return clazz.getName();
}
return (this.persistentClass != null ? this.persistentClass.toString() : null);
}

10
spring-orm/src/main/java/org/springframework/orm/hibernate5/HibernateExceptionTranslator.java

@ -1,5 +1,5 @@ @@ -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.
@ -68,12 +68,12 @@ public class HibernateExceptionTranslator implements PersistenceExceptionTransla @@ -68,12 +68,12 @@ public class HibernateExceptionTranslator implements PersistenceExceptionTransla
@Override
@Nullable
public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
if (ex instanceof HibernateException) {
return convertHibernateAccessException((HibernateException) ex);
if (ex instanceof HibernateException hibernateEx) {
return convertHibernateAccessException(hibernateEx);
}
if (ex instanceof PersistenceException) {
if (ex.getCause() instanceof HibernateException) {
return convertHibernateAccessException((HibernateException) ex.getCause());
if (ex.getCause() instanceof HibernateException hibernateEx) {
return convertHibernateAccessException(hibernateEx);
}
return EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(ex);
}

22
spring-orm/src/main/java/org/springframework/orm/hibernate5/HibernateTemplate.java

@ -1,5 +1,5 @@ @@ -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.
@ -369,8 +369,8 @@ public class HibernateTemplate implements HibernateOperations, InitializingBean @@ -369,8 +369,8 @@ public class HibernateTemplate implements HibernateOperations, InitializingBean
throw SessionFactoryUtils.convertHibernateAccessException(ex);
}
catch (PersistenceException ex) {
if (ex.getCause() instanceof HibernateException) {
throw SessionFactoryUtils.convertHibernateAccessException((HibernateException) ex.getCause());
if (ex.getCause() instanceof HibernateException hibernateEx) {
throw SessionFactoryUtils.convertHibernateAccessException(hibernateEx);
}
throw ex;
}
@ -1115,11 +1115,11 @@ public class HibernateTemplate implements HibernateOperations, InitializingBean @@ -1115,11 +1115,11 @@ public class HibernateTemplate implements HibernateOperations, InitializingBean
protected void applyNamedParameterToQuery(Query<?> queryObject, String paramName, Object value)
throws HibernateException {
if (value instanceof Collection) {
queryObject.setParameterList(paramName, (Collection<?>) value);
if (value instanceof Collection<?> collection) {
queryObject.setParameterList(paramName, collection);
}
else if (value instanceof Object[]) {
queryObject.setParameterList(paramName, (Object[]) value);
else if (value instanceof Object[] array) {
queryObject.setParameterList(paramName, array);
}
else {
queryObject.setParameter(paramName, value);
@ -1168,11 +1168,11 @@ public class HibernateTemplate implements HibernateOperations, InitializingBean @@ -1168,11 +1168,11 @@ public class HibernateTemplate implements HibernateOperations, InitializingBean
// If return value is a Query or Criteria, apply transaction timeout.
// Applies to createQuery, getNamedQuery, createCriteria.
if (retVal instanceof Criteria) {
prepareCriteria(((Criteria) retVal));
if (retVal instanceof Criteria criteria) {
prepareCriteria(criteria);
}
else if (retVal instanceof Query) {
prepareQuery(((Query<?>) retVal));
else if (retVal instanceof Query<?> query) {
prepareQuery(query);
}
return retVal;

20
spring-orm/src/main/java/org/springframework/orm/hibernate5/HibernateTransactionManager.java

@ -215,11 +215,11 @@ public class HibernateTransactionManager extends AbstractPlatformTransactionMana @@ -215,11 +215,11 @@ public class HibernateTransactionManager extends AbstractPlatformTransactionMana
* @see org.springframework.jdbc.core.JdbcTemplate
*/
public void setDataSource(@Nullable DataSource dataSource) {
if (dataSource instanceof TransactionAwareDataSourceProxy) {
if (dataSource instanceof TransactionAwareDataSourceProxy proxy) {
// If we got a TransactionAwareDataSourceProxy, we need to perform transactions
// for its underlying target DataSource, else data access code won't see
// properly exposed transactions (i.e. transactions for the target DataSource).
this.dataSource = ((TransactionAwareDataSourceProxy) dataSource).getTargetDataSource();
this.dataSource = proxy.getTargetDataSource();
}
else {
this.dataSource = dataSource;
@ -359,8 +359,8 @@ public class HibernateTransactionManager extends AbstractPlatformTransactionMana @@ -359,8 +359,8 @@ public class HibernateTransactionManager extends AbstractPlatformTransactionMana
*/
@Nullable
public Interceptor getEntityInterceptor() throws IllegalStateException, BeansException {
if (this.entityInterceptor instanceof Interceptor) {
return (Interceptor) this.entityInterceptor;
if (this.entityInterceptor instanceof Interceptor interceptor) {
return interceptor;
}
else if (this.entityInterceptor instanceof String beanName) {
if (this.beanFactory == null) {
@ -652,8 +652,8 @@ public class HibernateTransactionManager extends AbstractPlatformTransactionMana @@ -652,8 +652,8 @@ public class HibernateTransactionManager extends AbstractPlatformTransactionMana
throw convertHibernateAccessException(ex);
}
catch (PersistenceException ex) {
if (ex.getCause() instanceof HibernateException) {
throw convertHibernateAccessException((HibernateException) ex.getCause());
if (ex.getCause() instanceof HibernateException hibernateEx) {
throw convertHibernateAccessException(hibernateEx);
}
throw ex;
}
@ -680,8 +680,8 @@ public class HibernateTransactionManager extends AbstractPlatformTransactionMana @@ -680,8 +680,8 @@ public class HibernateTransactionManager extends AbstractPlatformTransactionMana
throw convertHibernateAccessException(ex);
}
catch (PersistenceException ex) {
if (ex.getCause() instanceof HibernateException) {
throw convertHibernateAccessException((HibernateException) ex.getCause());
if (ex.getCause() instanceof HibernateException hibernateEx) {
throw convertHibernateAccessException(hibernateEx);
}
throw ex;
}
@ -890,8 +890,8 @@ public class HibernateTransactionManager extends AbstractPlatformTransactionMana @@ -890,8 +890,8 @@ public class HibernateTransactionManager extends AbstractPlatformTransactionMana
throw convertHibernateAccessException(ex);
}
catch (PersistenceException ex) {
if (ex.getCause() instanceof HibernateException) {
throw convertHibernateAccessException((HibernateException) ex.getCause());
if (ex.getCause() instanceof HibernateException hibernateEx) {
throw convertHibernateAccessException(hibernateEx);
}
throw ex;
}

6
spring-orm/src/main/java/org/springframework/orm/hibernate5/LocalSessionFactoryBean.java

@ -1,5 +1,5 @@ @@ -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.
@ -475,8 +475,8 @@ public class LocalSessionFactoryBean extends HibernateExceptionTranslator @@ -475,8 +475,8 @@ public class LocalSessionFactoryBean extends HibernateExceptionTranslator
*/
@Override
public void setBeanFactory(BeanFactory beanFactory) {
if (beanFactory instanceof ConfigurableListableBeanFactory) {
this.beanFactory = (ConfigurableListableBeanFactory) beanFactory;
if (beanFactory instanceof ConfigurableListableBeanFactory clbf) {
this.beanFactory = clbf;
}
}

19
spring-orm/src/main/java/org/springframework/orm/hibernate5/LocalSessionFactoryBuilder.java

@ -1,5 +1,5 @@ @@ -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.
@ -186,26 +186,25 @@ public class LocalSessionFactoryBuilder extends Configuration { @@ -186,26 +186,25 @@ public class LocalSessionFactoryBuilder extends Configuration {
public LocalSessionFactoryBuilder setJtaTransactionManager(Object jtaTransactionManager) {
Assert.notNull(jtaTransactionManager, "Transaction manager reference must not be null");
if (jtaTransactionManager instanceof JtaTransactionManager) {
if (jtaTransactionManager instanceof JtaTransactionManager springJtaTm) {
boolean webspherePresent = ClassUtils.isPresent("com.ibm.wsspi.uow.UOWManager", getClass().getClassLoader());
if (webspherePresent) {
getProperties().put(AvailableSettings.JTA_PLATFORM,
"org.hibernate.engine.transaction.jta.platform.internal.WebSphereExtendedJtaPlatform");
}
else {
JtaTransactionManager jtaTm = (JtaTransactionManager) jtaTransactionManager;
if (jtaTm.getTransactionManager() == null) {
if (springJtaTm.getTransactionManager() == null) {
throw new IllegalArgumentException(
"Can only apply JtaTransactionManager which has a TransactionManager reference set");
}
getProperties().put(AvailableSettings.JTA_PLATFORM,
new ConfigurableJtaPlatform(jtaTm.getTransactionManager(), jtaTm.getUserTransaction(),
jtaTm.getTransactionSynchronizationRegistry()));
new ConfigurableJtaPlatform(springJtaTm.getTransactionManager(), springJtaTm.getUserTransaction(),
springJtaTm.getTransactionSynchronizationRegistry()));
}
}
else if (jtaTransactionManager instanceof TransactionManager) {
else if (jtaTransactionManager instanceof TransactionManager jtaTm) {
getProperties().put(AvailableSettings.JTA_PLATFORM,
new ConfigurableJtaPlatform((TransactionManager) jtaTransactionManager, null, null));
new ConfigurableJtaPlatform(jtaTm, null, null));
}
else {
throw new IllegalArgumentException(
@ -447,9 +446,9 @@ public class LocalSessionFactoryBuilder extends Configuration { @@ -447,9 +446,9 @@ public class LocalSessionFactoryBuilder extends Configuration {
}
catch (ExecutionException ex) {
Throwable cause = ex.getCause();
if (cause instanceof HibernateException) {
if (cause instanceof HibernateException hibernateException) {
// Rethrow a provider configuration exception (possibly with a nested cause) directly
throw (HibernateException) cause;
throw hibernateException;
}
throw new IllegalStateException("Failed to asynchronously initialize Hibernate SessionFactory: " +
ex.getMessage(), cause);

64
spring-orm/src/main/java/org/springframework/orm/hibernate5/SessionFactoryUtils.java

@ -1,5 +1,5 @@ @@ -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.
@ -116,8 +116,8 @@ public abstract class SessionFactoryUtils { @@ -116,8 +116,8 @@ public abstract class SessionFactoryUtils {
throw convertHibernateAccessException(ex);
}
catch (PersistenceException ex) {
if (ex.getCause() instanceof HibernateException) {
throw convertHibernateAccessException((HibernateException) ex.getCause());
if (ex.getCause() instanceof HibernateException hibernateException) {
throw convertHibernateAccessException(hibernateException);
}
throw ex;
}
@ -156,8 +156,8 @@ public abstract class SessionFactoryUtils { @@ -156,8 +156,8 @@ public abstract class SessionFactoryUtils {
Map<?, ?> props = (Map<?, ?>) ReflectionUtils.invokeMethod(getProperties, sessionFactory);
if (props != null) {
Object dataSourceValue = props.get(Environment.DATASOURCE);
if (dataSourceValue instanceof DataSource) {
return (DataSource) dataSourceValue;
if (dataSourceValue instanceof DataSource dataSource) {
return dataSource;
}
}
}
@ -189,32 +189,32 @@ public abstract class SessionFactoryUtils { @@ -189,32 +189,32 @@ public abstract class SessionFactoryUtils {
if (ex instanceof JDBCConnectionException) {
return new DataAccessResourceFailureException(ex.getMessage(), ex);
}
if (ex instanceof SQLGrammarException jdbcEx) {
return new InvalidDataAccessResourceUsageException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex);
if (ex instanceof SQLGrammarException hibJdbcEx) {
return new InvalidDataAccessResourceUsageException(ex.getMessage() + "; SQL [" + hibJdbcEx.getSQL() + "]", ex);
}
if (ex instanceof QueryTimeoutException jdbcEx) {
return new org.springframework.dao.QueryTimeoutException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex);
if (ex instanceof QueryTimeoutException hibJdbcEx) {
return new org.springframework.dao.QueryTimeoutException(ex.getMessage() + "; SQL [" + hibJdbcEx.getSQL() + "]", ex);
}
if (ex instanceof LockAcquisitionException jdbcEx) {
return new CannotAcquireLockException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex);
if (ex instanceof LockAcquisitionException hibJdbcEx) {
return new CannotAcquireLockException(ex.getMessage() + "; SQL [" + hibJdbcEx.getSQL() + "]", ex);
}
if (ex instanceof PessimisticLockException jdbcEx) {
return new PessimisticLockingFailureException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex);
if (ex instanceof PessimisticLockException hibJdbcEx) {
return new PessimisticLockingFailureException(ex.getMessage() + "; SQL [" + hibJdbcEx.getSQL() + "]", ex);
}
if (ex instanceof ConstraintViolationException jdbcEx) {
return new DataIntegrityViolationException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() +
"]; constraint [" + jdbcEx.getConstraintName() + "]", ex);
if (ex instanceof ConstraintViolationException hibJdbcEx) {
return new DataIntegrityViolationException(ex.getMessage() + "; SQL [" + hibJdbcEx.getSQL() +
"]; constraint [" + hibJdbcEx.getConstraintName() + "]", ex);
}
if (ex instanceof DataException jdbcEx) {
return new DataIntegrityViolationException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex);
if (ex instanceof DataException hibJdbcEx) {
return new DataIntegrityViolationException(ex.getMessage() + "; SQL [" + hibJdbcEx.getSQL() + "]", ex);
}
if (ex instanceof JDBCException) {
return new HibernateJdbcException((JDBCException) ex);
if (ex instanceof JDBCException hibJdbcEx) {
return new HibernateJdbcException(hibJdbcEx);
}
// end of JDBCException (subclass) handling
if (ex instanceof QueryException) {
return new HibernateQueryException((QueryException) ex);
if (ex instanceof QueryException queryException) {
return new HibernateQueryException(queryException);
}
if (ex instanceof NonUniqueResultException) {
return new IncorrectResultSizeDataAccessException(ex.getMessage(), 1, ex);
@ -234,20 +234,20 @@ public abstract class SessionFactoryUtils { @@ -234,20 +234,20 @@ public abstract class SessionFactoryUtils {
if (ex instanceof ObjectDeletedException) {
return new InvalidDataAccessApiUsageException(ex.getMessage(), ex);
}
if (ex instanceof UnresolvableObjectException) {
return new HibernateObjectRetrievalFailureException((UnresolvableObjectException) ex);
if (ex instanceof UnresolvableObjectException unresolvableObjectException) {
return new HibernateObjectRetrievalFailureException(unresolvableObjectException);
}
if (ex instanceof WrongClassException) {
return new HibernateObjectRetrievalFailureException((WrongClassException) ex);
if (ex instanceof WrongClassException wrongClassException) {
return new HibernateObjectRetrievalFailureException(wrongClassException);
}
if (ex instanceof StaleObjectStateException) {
return new HibernateOptimisticLockingFailureException((StaleObjectStateException) ex);
if (ex instanceof StaleObjectStateException staleObjectStateException) {
return new HibernateOptimisticLockingFailureException(staleObjectStateException);
}
if (ex instanceof StaleStateException) {
return new HibernateOptimisticLockingFailureException((StaleStateException) ex);
if (ex instanceof StaleStateException staleStateException) {
return new HibernateOptimisticLockingFailureException(staleStateException);
}
if (ex instanceof OptimisticEntityLockException) {
return new HibernateOptimisticLockingFailureException((OptimisticEntityLockException) ex);
if (ex instanceof OptimisticEntityLockException optimisticEntityLockException) {
return new HibernateOptimisticLockingFailureException(optimisticEntityLockException);
}
if (ex instanceof PessimisticEntityLockException) {
if (ex.getCause() instanceof LockAcquisitionException) {

7
spring-orm/src/main/java/org/springframework/orm/hibernate5/SpringFlushSynchronization.java

@ -1,5 +1,5 @@ @@ -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.
@ -45,9 +45,8 @@ public class SpringFlushSynchronization implements TransactionSynchronization { @@ -45,9 +45,8 @@ public class SpringFlushSynchronization implements TransactionSynchronization {
@Override
public boolean equals(@Nullable Object other) {
return (this == other || (other instanceof SpringFlushSynchronization &&
this.session == ((SpringFlushSynchronization) other).session));
public boolean equals(@Nullable Object obj) {
return (this == obj || (obj instanceof SpringFlushSynchronization that && this.session == that.session));
}
@Override

21
spring-orm/src/main/java/org/springframework/orm/jpa/EntityManagerFactoryUtils.java

@ -1,5 +1,5 @@ @@ -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.
@ -106,8 +106,8 @@ public abstract class EntityManagerFactoryUtils { @@ -106,8 +106,8 @@ public abstract class EntityManagerFactoryUtils {
BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory, EntityManagerFactory.class);
for (String candidateName : candidateNames) {
EntityManagerFactory emf = (EntityManagerFactory) beanFactory.getBean(candidateName);
if (emf instanceof EntityManagerFactoryInfo &&
unitName.equals(((EntityManagerFactoryInfo) emf).getPersistenceUnitName())) {
if (emf instanceof EntityManagerFactoryInfo emfInfo &&
unitName.equals(emfInfo.getPersistenceUnitName())) {
return emf;
}
}
@ -372,8 +372,8 @@ public abstract class EntityManagerFactoryUtils { @@ -372,8 +372,8 @@ public abstract class EntityManagerFactoryUtils {
}
// Check for well-known PersistenceException subclasses.
if (ex instanceof EntityNotFoundException) {
return new JpaObjectRetrievalFailureException((EntityNotFoundException) ex);
if (ex instanceof EntityNotFoundException entityNotFoundException) {
return new JpaObjectRetrievalFailureException(entityNotFoundException);
}
if (ex instanceof NoResultException) {
return new EmptyResultDataAccessException(ex.getMessage(), 1, ex);
@ -390,8 +390,8 @@ public abstract class EntityManagerFactoryUtils { @@ -390,8 +390,8 @@ public abstract class EntityManagerFactoryUtils {
if (ex instanceof PessimisticLockException) {
return new PessimisticLockingFailureException(ex.getMessage(), ex);
}
if (ex instanceof OptimisticLockException) {
return new JpaOptimisticLockingFailureException((OptimisticLockException) ex);
if (ex instanceof OptimisticLockException optimisticLockException) {
return new JpaOptimisticLockingFailureException(optimisticLockException);
}
if (ex instanceof EntityExistsException) {
return new DataIntegrityViolationException(ex.getMessage(), ex);
@ -454,8 +454,7 @@ public abstract class EntityManagerFactoryUtils { @@ -454,8 +454,7 @@ public abstract class EntityManagerFactoryUtils {
super(emHolder, emf);
this.transactionData = txData;
this.jpaDialect = (emf instanceof EntityManagerFactoryInfo ?
((EntityManagerFactoryInfo) emf).getJpaDialect() : null);
this.jpaDialect = (emf instanceof EntityManagerFactoryInfo emfInfo ? emfInfo.getJpaDialect() : null);
this.newEntityManager = newEm;
}
@ -467,8 +466,8 @@ public abstract class EntityManagerFactoryUtils { @@ -467,8 +466,8 @@ public abstract class EntityManagerFactoryUtils {
@Override
protected void flushResource(EntityManagerHolder resourceHolder) {
EntityManager em = resourceHolder.getEntityManager();
if (em instanceof EntityManagerProxy) {
EntityManager target = ((EntityManagerProxy) em).getTargetEntityManager();
if (em instanceof EntityManagerProxy emProxy) {
EntityManager target = emProxy.getTargetEntityManager();
if (TransactionSynchronizationManager.hasResource(target)) {
// ExtendedEntityManagerSynchronization active after joinTransaction() call:
// flush synchronization already registered.

16
spring-orm/src/main/java/org/springframework/orm/jpa/JpaTransactionManager.java

@ -262,11 +262,11 @@ public class JpaTransactionManager extends AbstractPlatformTransactionManager @@ -262,11 +262,11 @@ public class JpaTransactionManager extends AbstractPlatformTransactionManager
* @see org.springframework.jdbc.core.JdbcTemplate
*/
public void setDataSource(@Nullable DataSource dataSource) {
if (dataSource instanceof TransactionAwareDataSourceProxy) {
if (dataSource instanceof TransactionAwareDataSourceProxy proxy) {
// If we got a TransactionAwareDataSourceProxy, we need to perform transactions
// for its underlying target DataSource, else data access code won't see
// properly exposed transactions (i.e. transactions for the target DataSource).
this.dataSource = ((TransactionAwareDataSourceProxy) dataSource).getTargetDataSource();
this.dataSource = proxy.getTargetDataSource();
}
else {
this.dataSource = dataSource;
@ -480,8 +480,8 @@ public class JpaTransactionManager extends AbstractPlatformTransactionManager @@ -480,8 +480,8 @@ public class JpaTransactionManager extends AbstractPlatformTransactionManager
EntityManagerFactory emf = obtainEntityManagerFactory();
Map<String, Object> properties = getJpaPropertyMap();
EntityManager em;
if (emf instanceof EntityManagerFactoryInfo) {
em = ((EntityManagerFactoryInfo) emf).createNativeEntityManager(properties);
if (emf instanceof EntityManagerFactoryInfo emfInfo) {
em = emfInfo.createNativeEntityManager(properties);
}
else {
em = (!CollectionUtils.isEmpty(properties) ?
@ -561,8 +561,8 @@ public class JpaTransactionManager extends AbstractPlatformTransactionManager @@ -561,8 +561,8 @@ public class JpaTransactionManager extends AbstractPlatformTransactionManager
tx.commit();
}
catch (RollbackException ex) {
if (ex.getCause() instanceof RuntimeException) {
DataAccessException dae = getJpaDialect().translateExceptionIfPossible((RuntimeException) ex.getCause());
if (ex.getCause() instanceof RuntimeException runtimeException) {
DataAccessException dae = getJpaDialect().translateExceptionIfPossible(runtimeException);
if (dae != null) {
throw dae;
}
@ -695,8 +695,8 @@ public class JpaTransactionManager extends AbstractPlatformTransactionManager @@ -695,8 +695,8 @@ public class JpaTransactionManager extends AbstractPlatformTransactionManager
public void setTransactionData(@Nullable Object transactionData) {
this.transactionData = transactionData;
getEntityManagerHolder().setTransactionActive(true);
if (transactionData instanceof SavepointManager) {
getEntityManagerHolder().setSavepointManager((SavepointManager) transactionData);
if (transactionData instanceof SavepointManager savepointManager) {
getEntityManagerHolder().setSavepointManager(savepointManager);
}
}

4
spring-orm/src/main/java/org/springframework/orm/jpa/LocalContainerEntityManagerFactoryBean.java

@ -342,10 +342,10 @@ public class LocalContainerEntityManagerFactoryBean extends AbstractEntityManage @@ -342,10 +342,10 @@ public class LocalContainerEntityManagerFactoryBean extends AbstractEntityManage
this.persistenceUnitInfo = determinePersistenceUnitInfo(managerToUse);
JpaVendorAdapter jpaVendorAdapter = getJpaVendorAdapter();
if (jpaVendorAdapter != null && this.persistenceUnitInfo instanceof SmartPersistenceUnitInfo) {
if (jpaVendorAdapter != null && this.persistenceUnitInfo instanceof SmartPersistenceUnitInfo smartInfo) {
String rootPackage = jpaVendorAdapter.getPersistenceProviderRootPackage();
if (rootPackage != null) {
((SmartPersistenceUnitInfo) this.persistenceUnitInfo).setPersistenceProviderPackageName(rootPackage);
smartInfo.setPersistenceProviderPackageName(rootPackage);
}
}

20
spring-orm/src/main/java/org/springframework/orm/jpa/SharedEntityManagerCreator.java

@ -127,8 +127,8 @@ public abstract class SharedEntityManagerCreator { @@ -127,8 +127,8 @@ public abstract class SharedEntityManagerCreator {
public static EntityManager createSharedEntityManager(
EntityManagerFactory emf, @Nullable Map<?, ?> properties, boolean synchronizedWithTransaction) {
Class<?> emIfc = (emf instanceof EntityManagerFactoryInfo ?
((EntityManagerFactoryInfo) emf).getEntityManagerInterface() : EntityManager.class);
Class<?> emIfc = (emf instanceof EntityManagerFactoryInfo emfInfo ?
emfInfo.getEntityManagerInterface() : EntityManager.class);
return createSharedEntityManager(emf, properties, synchronizedWithTransaction,
(emIfc == null ? NO_ENTITY_MANAGER_INTERFACES : new Class<?>[] {emIfc}));
}
@ -164,8 +164,8 @@ public abstract class SharedEntityManagerCreator { @@ -164,8 +164,8 @@ public abstract class SharedEntityManagerCreator {
boolean synchronizedWithTransaction, Class<?>... entityManagerInterfaces) {
ClassLoader cl = null;
if (emf instanceof EntityManagerFactoryInfo) {
cl = ((EntityManagerFactoryInfo) emf).getBeanClassLoader();
if (emf instanceof EntityManagerFactoryInfo emfInfo) {
cl = emfInfo.getBeanClassLoader();
}
Class<?>[] ifcs = new Class<?>[entityManagerInterfaces.length + 1];
System.arraycopy(entityManagerInterfaces, 0, ifcs, 0, entityManagerInterfaces.length);
@ -206,8 +206,8 @@ public abstract class SharedEntityManagerCreator { @@ -206,8 +206,8 @@ public abstract class SharedEntityManagerCreator {
}
private void initProxyClassLoader() {
if (this.targetFactory instanceof EntityManagerFactoryInfo) {
this.proxyClassLoader = ((EntityManagerFactoryInfo) this.targetFactory).getBeanClassLoader();
if (this.targetFactory instanceof EntityManagerFactoryInfo emfInfo) {
this.proxyClassLoader = emfInfo.getBeanClassLoader();
}
else {
this.proxyClassLoader = this.targetFactory.getClass().getClassLoader();
@ -392,8 +392,8 @@ public abstract class SharedEntityManagerCreator { @@ -392,8 +392,8 @@ public abstract class SharedEntityManagerCreator {
throw new IllegalArgumentException("OUT/INOUT parameter not available: " + key);
}
Object value = this.outputParameters.get(key);
if (value instanceof IllegalArgumentException) {
throw (IllegalArgumentException) value;
if (value instanceof IllegalArgumentException iae) {
throw iae;
}
return value;
}
@ -423,8 +423,8 @@ public abstract class SharedEntityManagerCreator { @@ -423,8 +423,8 @@ public abstract class SharedEntityManagerCreator {
for (Map.Entry<Object, Object> entry : this.outputParameters.entrySet()) {
try {
Object key = entry.getKey();
if (key instanceof Integer) {
entry.setValue(storedProc.getOutputParameterValue((Integer) key));
if (key instanceof Integer number) {
entry.setValue(storedProc.getOutputParameterValue(number));
}
else {
entry.setValue(storedProc.getOutputParameterValue(key.toString()));

4
spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/SpringPersistenceUnitInfo.java

@ -96,8 +96,8 @@ class SpringPersistenceUnitInfo extends MutablePersistenceUnitInfo { @@ -96,8 +96,8 @@ class SpringPersistenceUnitInfo extends MutablePersistenceUnitInfo {
ClassLoader tcl = (this.loadTimeWeaver != null ? this.loadTimeWeaver.getThrowawayClassLoader() :
new SimpleThrowawayClassLoader(this.classLoader));
String packageToExclude = getPersistenceProviderPackageName();
if (packageToExclude != null && tcl instanceof DecoratingClassLoader) {
((DecoratingClassLoader) tcl).excludePackage(packageToExclude);
if (packageToExclude != null && tcl instanceof DecoratingClassLoader dcl) {
dcl.excludePackage(packageToExclude);
}
return tcl;
}

43
spring-orm/src/main/java/org/springframework/orm/jpa/vendor/HibernateJpaDialect.java vendored

@ -1,5 +1,5 @@ @@ -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.
@ -165,8 +165,7 @@ public class HibernateJpaDialect extends DefaultJpaDialect { @@ -165,8 +165,7 @@ public class HibernateJpaDialect extends DefaultJpaDialect {
// Adapt flush mode and store previous isolation level, if any.
FlushMode previousFlushMode = prepareFlushMode(session, definition.isReadOnly());
if (definition instanceof ResourceTransactionDefinition &&
((ResourceTransactionDefinition) definition).isLocalResource()) {
if (definition instanceof ResourceTransactionDefinition rtd && rtd.isLocalResource()) {
// As of 5.1, we explicitly optimize for a transaction-local EntityManager,
// aligned with native HibernateTransactionManager behavior.
previousFlushMode = null;
@ -210,8 +209,8 @@ public class HibernateJpaDialect extends DefaultJpaDialect { @@ -210,8 +209,8 @@ public class HibernateJpaDialect extends DefaultJpaDialect {
@Override
public void cleanupTransaction(@Nullable Object transactionData) {
if (transactionData instanceof SessionTransactionData) {
((SessionTransactionData) transactionData).resetSessionState();
if (transactionData instanceof SessionTransactionData sessionTransactionData) {
sessionTransactionData.resetSessionState();
}
}
@ -226,11 +225,11 @@ public class HibernateJpaDialect extends DefaultJpaDialect { @@ -226,11 +225,11 @@ public class HibernateJpaDialect extends DefaultJpaDialect {
@Override
@Nullable
public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
if (ex instanceof HibernateException) {
return convertHibernateAccessException((HibernateException) ex);
if (ex instanceof HibernateException hibernateEx) {
return convertHibernateAccessException(hibernateEx);
}
if (ex instanceof PersistenceException && ex.getCause() instanceof HibernateException) {
return convertHibernateAccessException((HibernateException) ex.getCause());
if (ex instanceof PersistenceException && ex.getCause() instanceof HibernateException hibernateEx) {
return convertHibernateAccessException(hibernateEx);
}
return EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(ex);
}
@ -253,24 +252,24 @@ public class HibernateJpaDialect extends DefaultJpaDialect { @@ -253,24 +252,24 @@ public class HibernateJpaDialect extends DefaultJpaDialect {
if (ex instanceof JDBCConnectionException) {
return new DataAccessResourceFailureException(ex.getMessage(), ex);
}
if (ex instanceof SQLGrammarException jdbcEx) {
return new InvalidDataAccessResourceUsageException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex);
if (ex instanceof SQLGrammarException hibJdbcEx) {
return new InvalidDataAccessResourceUsageException(ex.getMessage() + "; SQL [" + hibJdbcEx.getSQL() + "]", ex);
}
if (ex instanceof QueryTimeoutException jdbcEx) {
return new org.springframework.dao.QueryTimeoutException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex);
if (ex instanceof QueryTimeoutException hibJdbcEx) {
return new org.springframework.dao.QueryTimeoutException(ex.getMessage() + "; SQL [" + hibJdbcEx.getSQL() + "]", ex);
}
if (ex instanceof LockAcquisitionException jdbcEx) {
return new CannotAcquireLockException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex);
if (ex instanceof LockAcquisitionException hibJdbcEx) {
return new CannotAcquireLockException(ex.getMessage() + "; SQL [" + hibJdbcEx.getSQL() + "]", ex);
}
if (ex instanceof PessimisticLockException jdbcEx) {
return new PessimisticLockingFailureException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex);
if (ex instanceof PessimisticLockException hibJdbcEx) {
return new PessimisticLockingFailureException(ex.getMessage() + "; SQL [" + hibJdbcEx.getSQL() + "]", ex);
}
if (ex instanceof ConstraintViolationException jdbcEx) {
return new DataIntegrityViolationException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() +
"]; constraint [" + jdbcEx.getConstraintName() + "]", ex);
if (ex instanceof ConstraintViolationException hibJdbcEx) {
return new DataIntegrityViolationException(ex.getMessage() + "; SQL [" + hibJdbcEx.getSQL() +
"]; constraint [" + hibJdbcEx.getConstraintName() + "]", ex);
}
if (ex instanceof DataException jdbcEx) {
return new DataIntegrityViolationException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex);
if (ex instanceof DataException hibJdbcEx) {
return new DataIntegrityViolationException(ex.getMessage() + "; SQL [" + hibJdbcEx.getSQL() + "]", ex);
}
// end of JDBCException subclass handling

6
spring-orm/src/main/java/org/springframework/orm/jpa/vendor/SpringHibernateJpaPersistenceProvider.java vendored

@ -1,5 +1,5 @@ @@ -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.
@ -54,8 +54,8 @@ class SpringHibernateJpaPersistenceProvider extends HibernatePersistenceProvider @@ -54,8 +54,8 @@ class SpringHibernateJpaPersistenceProvider extends HibernatePersistenceProvider
@SuppressWarnings("rawtypes")
public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map properties) {
final List<String> mergedClassesAndPackages = new ArrayList<>(info.getManagedClassNames());
if (info instanceof SmartPersistenceUnitInfo) {
mergedClassesAndPackages.addAll(((SmartPersistenceUnitInfo) info).getManagedPackages());
if (info instanceof SmartPersistenceUnitInfo smartInfo) {
mergedClassesAndPackages.addAll(smartInfo.getManagedPackages());
}
return new EntityManagerFactoryBuilderImpl(
new PersistenceUnitInfoDescriptor(info) {

Loading…
Cancel
Save