From c41cfe1a0f364d530447fca2eceeb9f5d38ab1fe Mon Sep 17 00:00:00 2001 From: Gavin King Date: Thu, 20 Feb 2025 11:13:34 +0100 Subject: [PATCH] use safe casts --- .../java/org/hibernate/reactive/mutiny/Mutiny.java | 13 ++++++------- .../java/org/hibernate/reactive/stage/Stage.java | 13 ++++++------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/hibernate-reactive-core/src/main/java/org/hibernate/reactive/mutiny/Mutiny.java b/hibernate-reactive-core/src/main/java/org/hibernate/reactive/mutiny/Mutiny.java index e6cdab9d5..a8faefbbd 100644 --- a/hibernate-reactive-core/src/main/java/org/hibernate/reactive/mutiny/Mutiny.java +++ b/hibernate-reactive-core/src/main/java/org/hibernate/reactive/mutiny/Mutiny.java @@ -18,7 +18,6 @@ import org.hibernate.LockMode; import org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor; import org.hibernate.collection.spi.AbstractPersistentCollection; -import org.hibernate.collection.spi.PersistentCollection; import org.hibernate.engine.spi.PersistentAttributeInterceptable; import org.hibernate.engine.spi.PersistentAttributeInterceptor; import org.hibernate.engine.spi.SharedSessionContractImplementor; @@ -2452,18 +2451,18 @@ static Uni fetch(T association) { } final SharedSessionContractImplementor session; - if ( association instanceof HibernateProxy ) { - session = ( (HibernateProxy) association ).getHibernateLazyInitializer().getSession(); + if ( association instanceof HibernateProxy proxy ) { + session = proxy.getHibernateLazyInitializer().getSession(); } - else if ( association instanceof PersistentCollection ) { + else if ( association instanceof AbstractPersistentCollection collection ) { //this unfortunately doesn't work for stateless session because the session ref gets set to null - session = ( (AbstractPersistentCollection) association ).getSession(); + session = collection.getSession(); } else if ( isPersistentAttributeInterceptable( association ) ) { final PersistentAttributeInterceptable interceptable = asPersistentAttributeInterceptable( association ); final PersistentAttributeInterceptor interceptor = interceptable.$$_hibernate_getInterceptor(); - if ( interceptor instanceof EnhancementAsProxyLazinessInterceptor ) { - session = ( (EnhancementAsProxyLazinessInterceptor) interceptor ).getLinkedSession(); + if ( interceptor instanceof EnhancementAsProxyLazinessInterceptor lazinessInterceptor ) { + session = lazinessInterceptor.getLinkedSession(); } else { return Uni.createFrom().item( association ); diff --git a/hibernate-reactive-core/src/main/java/org/hibernate/reactive/stage/Stage.java b/hibernate-reactive-core/src/main/java/org/hibernate/reactive/stage/Stage.java index 1cf8f4fed..711a91ab9 100644 --- a/hibernate-reactive-core/src/main/java/org/hibernate/reactive/stage/Stage.java +++ b/hibernate-reactive-core/src/main/java/org/hibernate/reactive/stage/Stage.java @@ -19,7 +19,6 @@ import org.hibernate.LockMode; import org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor; import org.hibernate.collection.spi.AbstractPersistentCollection; -import org.hibernate.collection.spi.PersistentCollection; import org.hibernate.engine.spi.PersistentAttributeInterceptable; import org.hibernate.engine.spi.PersistentAttributeInterceptor; import org.hibernate.engine.spi.SharedSessionContractImplementor; @@ -2499,17 +2498,17 @@ static CompletionStage fetch(T association) { } final SharedSessionContractImplementor session; - if ( association instanceof HibernateProxy) { - session = ( (HibernateProxy) association ).getHibernateLazyInitializer().getSession(); + if ( association instanceof HibernateProxy proxy ) { + session = proxy.getHibernateLazyInitializer().getSession(); } - else if ( association instanceof PersistentCollection) { - session = ( (AbstractPersistentCollection) association ).getSession(); + else if ( association instanceof AbstractPersistentCollection collection ) { + session = collection.getSession(); } else if ( isPersistentAttributeInterceptable( association ) ) { final PersistentAttributeInterceptable interceptable = asPersistentAttributeInterceptable( association ); final PersistentAttributeInterceptor interceptor = interceptable.$$_hibernate_getInterceptor(); - if ( interceptor instanceof EnhancementAsProxyLazinessInterceptor) { - session = ( (EnhancementAsProxyLazinessInterceptor) interceptor ).getLinkedSession(); + if ( interceptor instanceof EnhancementAsProxyLazinessInterceptor lazinessInterceptor) { + session = lazinessInterceptor.getLinkedSession(); } else { return CompletionStages.completedFuture( association );