squash warnings in bean LifecycleStrategy stuff

Signed-off-by: Gavin King <gavin@hibernate.org>
This commit is contained in:
Gavin King 2024-08-30 20:58:06 +02:00
parent 7b8c403df7
commit 681bd09b9d
4 changed files with 52 additions and 60 deletions

View File

@ -45,7 +45,6 @@ import org.hibernate.resource.beans.container.spi.BeanContainer;
import org.hibernate.resource.beans.spi.BeanInstanceProducer; import org.hibernate.resource.beans.spi.BeanInstanceProducer;
import org.hibernate.resource.beans.spi.ManagedBeanRegistry; import org.hibernate.resource.beans.spi.ManagedBeanRegistry;
import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistry;
import org.hibernate.type.Type;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;

View File

@ -20,10 +20,10 @@ import org.jboss.logging.Logger;
/** /**
* A {@link BeanLifecycleStrategy} to use when CDI compliance is required * A {@link BeanLifecycleStrategy} to use when CDI compliance is required
* (i.e. when the bean lifecycle is to be managed by the CDI runtime, not the JPA runtime). * (i.e. when the bean lifecycle is to be managed by the CDI runtime, not the JPA runtime).
* * <p>
* The main characteristic of this strategy is that every create/destroy operation is delegated * The main characteristic of this strategy is that every create/destroy operation is delegated
* to the CDI runtime. * to the CDI runtime.
* * <p>
* In particular, @Singleton-scoped or @ApplicationScoped beans are retrieved from the CDI context, * In particular, @Singleton-scoped or @ApplicationScoped beans are retrieved from the CDI context,
* and are not duplicated, in contrast to {@link JpaCompliantLifecycleStrategy}. * and are not duplicated, in contrast to {@link JpaCompliantLifecycleStrategy}.
*/ */
@ -89,16 +89,16 @@ public class ContainerManagedLifecycleStrategy implements BeanLifecycleStrategy
} }
try { try {
this.instance = resolveContainerInstance(); instance = resolveContainerInstance();
this.beanInstance = this.instance.get(); beanInstance = instance.get();
} }
catch (NotYetReadyException e) { catch (NotYetReadyException e) {
throw e; throw e;
} }
catch (Exception e) { catch (Exception e) {
log.debug( "Error resolving CDI bean - using fallback" ); log.debug( "Error resolving CDI bean - using fallback" );
this.beanInstance = produceFallbackInstance(); beanInstance = produceFallbackInstance();
this.instance = null; instance = null;
} }
this.beanManager = null; this.beanManager = null;
@ -154,8 +154,8 @@ public class ContainerManagedLifecycleStrategy implements BeanLifecycleStrategy
root = beanManager.createInstance(); root = beanManager.createInstance();
} }
catch (Exception e) { catch (Exception e) {
// this indicates that the BeanManager is not yet ready to use, which // this indicates that the BeanManager is not yet ready to use,
// should be consider an error // which should be considered an error
throw new NotYetReadyException( e ); throw new NotYetReadyException( e );
} }
@ -186,15 +186,14 @@ public class ContainerManagedLifecycleStrategy implements BeanLifecycleStrategy
} }
@Override @Override
@SuppressWarnings("unchecked")
protected Instance<B> resolveContainerInstance() { protected Instance<B> resolveContainerInstance() {
final Instance root; final Instance<Object> root;
try { try {
root = beanManager.createInstance(); root = beanManager.createInstance();
} }
catch (Exception e) { catch (Exception e) {
// this indicates that the BeanManager is not yet ready to use, which // this indicates that the BeanManager is not yet ready to use,
// should be consider an error // which should be considered an error
throw new NotYetReadyException( e ); throw new NotYetReadyException( e );
} }

View File

@ -6,7 +6,6 @@
*/ */
package org.hibernate.resource.beans.container.internal; package org.hibernate.resource.beans.container.internal;
import java.util.Set;
import jakarta.enterprise.context.spi.CreationalContext; import jakarta.enterprise.context.spi.CreationalContext;
import jakarta.enterprise.inject.spi.AnnotatedType; import jakarta.enterprise.inject.spi.AnnotatedType;
import jakarta.enterprise.inject.spi.Bean; import jakarta.enterprise.inject.spi.Bean;
@ -23,12 +22,12 @@ import org.jboss.logging.Logger;
/** /**
* A {@link BeanLifecycleStrategy} to use when JPA compliance is required * A {@link BeanLifecycleStrategy} to use when JPA compliance is required
* (i.e. when the bean lifecycle is to be managed by the JPA runtime, not the CDI runtime). * (i.e. when the bean lifecycle is to be managed by the JPA runtime, not the CDI runtime).
* * <p>
* The main characteristic of this strategy is that each requested bean is instantiated directly * The main characteristic of this strategy is that each requested bean is instantiated directly
* and guaranteed to not be shared in the CDI context. * and guaranteed to not be shared in the CDI context.
* * <p>
* In particular, @Singleton-scoped or @ApplicationScoped beans are instantiated directly by this strategy, * In particular, {@code @Singleton}-scoped or {@code @ApplicationScoped} beans are instantiated
* even if there is already an instance in the CDI context. * directly by this strategy, even if there is already an instance in the CDI context.
* This means singletons are not really singletons, but this seems to be the behavior required by * This means singletons are not really singletons, but this seems to be the behavior required by
* the JPA 2.2 spec. * the JPA 2.2 spec.
*/ */
@ -128,10 +127,10 @@ public class JpaCompliantLifecycleStrategy implements BeanLifecycleStrategy {
} }
try { try {
this.injectionTarget = beanManager.getInjectionTargetFactory( annotatedType ).createInjectionTarget( (Bean) null ); injectionTarget = beanManager.getInjectionTargetFactory( annotatedType ).createInjectionTarget( null );
this.creationalContext = beanManager.createCreationalContext( null ); creationalContext = beanManager.createCreationalContext( null );
this.beanInstance = this.injectionTarget.produce( creationalContext ); beanInstance = injectionTarget.produce( creationalContext );
injectionTarget.inject( beanInstance, creationalContext ); injectionTarget.inject( beanInstance, creationalContext );
injectionTarget.postConstruct( beanInstance ); injectionTarget.postConstruct( beanInstance );
@ -141,7 +140,7 @@ public class JpaCompliantLifecycleStrategy implements BeanLifecycleStrategy {
} }
catch (Exception e) { catch (Exception e) {
log.debugf( "Error resolving CDI bean [%s] - using fallback", beanType.getName() ); log.debugf( "Error resolving CDI bean [%s] - using fallback", beanType.getName() );
this.beanInstance = fallbackProducer.produceBeanInstance( beanType ); beanInstance = fallbackProducer.produceBeanInstance( beanType );
try { try {
if ( this.creationalContext != null ) { if ( this.creationalContext != null ) {
@ -151,11 +150,11 @@ public class JpaCompliantLifecycleStrategy implements BeanLifecycleStrategy {
catch (Exception ignore) { catch (Exception ignore) {
} }
this.creationalContext = null; creationalContext = null;
this.injectionTarget = null; injectionTarget = null;
} }
this.beanManager = null; beanManager = null;
} }
@Override @Override
@ -171,15 +170,15 @@ public class JpaCompliantLifecycleStrategy implements BeanLifecycleStrategy {
} }
injectionTarget.preDestroy( beanInstance ); injectionTarget.preDestroy( beanInstance );
injectionTarget.dispose( beanInstance ); injectionTarget.dispose( beanInstance );
this.creationalContext.release(); creationalContext.release();
} }
catch (Exception ignore) { catch (Exception ignore) {
} }
finally { finally {
this.beanInstance = null; beanInstance = null;
this.creationalContext = null; creationalContext = null;
this.injectionTarget = null; injectionTarget = null;
} }
} }
} }
@ -239,31 +238,31 @@ public class JpaCompliantLifecycleStrategy implements BeanLifecycleStrategy {
} }
try { try {
this.creationalContext = beanManager.createCreationalContext( null ); creationalContext = beanManager.createCreationalContext( null );
} }
catch (Exception e) { catch (Exception e) {
throw new NotYetReadyException( e ); throw new NotYetReadyException( e );
} }
try { try {
Set<Bean<?>> beans = beanManager.getBeans( beanType, new NamedBeanQualifier( beanName ) ); bean = (Bean<B>) beanManager.resolve( beanManager.getBeans( beanType,
this.bean = (Bean<B>) beanManager.resolve( beans ); new NamedBeanQualifier( beanName ) ) );
this.beanInstance = bean.create( creationalContext ); beanInstance = bean.create( creationalContext );
} }
catch (Exception e) { catch (Exception e) {
log.debugf( "Error resolving CDI bean [%s] - using fallback", beanName ); log.debugf( "Error resolving CDI bean [%s] - using fallback", beanName );
this.beanInstance = fallbackProducer.produceBeanInstance( beanName, beanType ); beanInstance = fallbackProducer.produceBeanInstance( beanName, beanType );
try { try {
if ( this.creationalContext != null ) { if ( creationalContext != null ) {
this.creationalContext.release(); creationalContext.release();
} }
} }
catch (Exception ignore) { catch (Exception ignore) {
} }
this.creationalContext = null; creationalContext = null;
this.bean = null; bean = null;
} }
} }
@ -291,10 +290,10 @@ public class JpaCompliantLifecycleStrategy implements BeanLifecycleStrategy {
} }
} }
this.beanInstance = null; beanInstance = null;
this.creationalContext = null; creationalContext = null;
this.bean = null; bean = null;
this.beanManager = null; beanManager = null;
} }
} }

View File

@ -23,20 +23,17 @@ import org.hibernate.resource.beans.spi.BeanInstanceProducer;
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public abstract class AbstractCdiBeanContainer implements CdiBasedBeanContainer { public abstract class AbstractCdiBeanContainer implements CdiBasedBeanContainer {
private Map<String,ContainedBeanImplementor<?>> beanCache = new HashMap<>(); private final Map<String,ContainedBeanImplementor<?>> beanCache = new HashMap<>();
private List<ContainedBeanImplementor<?>> registeredBeans = new ArrayList<>(); private final List<ContainedBeanImplementor<?>> registeredBeans = new ArrayList<>();
@Override @Override
public <B> ContainedBean<B> getBean( public <B> ContainedBean<B> getBean(
Class<B> beanType, Class<B> beanType,
LifecycleOptions lifecycleOptions, LifecycleOptions lifecycleOptions,
BeanInstanceProducer fallbackProducer) { BeanInstanceProducer fallbackProducer) {
if ( lifecycleOptions.canUseCachedReferences() ) { return lifecycleOptions.canUseCachedReferences()
return getCacheableBean( beanType, lifecycleOptions, fallbackProducer ); ? getCacheableBean( beanType, lifecycleOptions, fallbackProducer )
} : createBean( beanType, lifecycleOptions, fallbackProducer );
else {
return createBean( beanType, lifecycleOptions, fallbackProducer );
}
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -46,22 +43,21 @@ public abstract class AbstractCdiBeanContainer implements CdiBasedBeanContainer
BeanInstanceProducer fallbackProducer) { BeanInstanceProducer fallbackProducer) {
final String beanCacheKey = Helper.determineBeanCacheKey( beanType ); final String beanCacheKey = Helper.determineBeanCacheKey( beanType );
final ContainedBeanImplementor existing = beanCache.get( beanCacheKey ); final ContainedBeanImplementor<?> existing = beanCache.get( beanCacheKey );
if ( existing != null ) { if ( existing != null ) {
return existing; return (ContainedBeanImplementor<B>) existing;
} }
final ContainedBeanImplementor bean = createBean( beanType, lifecycleOptions, fallbackProducer ); final ContainedBeanImplementor<B> bean = createBean( beanType, lifecycleOptions, fallbackProducer );
beanCache.put( beanCacheKey, bean ); beanCache.put( beanCacheKey, bean );
return bean; return bean;
} }
@SuppressWarnings("unchecked")
private <B> ContainedBeanImplementor<B> createBean( private <B> ContainedBeanImplementor<B> createBean(
Class<B> beanType, Class<B> beanType,
LifecycleOptions lifecycleOptions, LifecycleOptions lifecycleOptions,
BeanInstanceProducer fallbackProducer) { BeanInstanceProducer fallbackProducer) {
final ContainedBeanImplementor bean = createBean( final ContainedBeanImplementor<B> bean = createBean(
beanType, beanType,
lifecycleOptions.useJpaCompliantCreation() lifecycleOptions.useJpaCompliantCreation()
? JpaCompliantLifecycleStrategy.INSTANCE ? JpaCompliantLifecycleStrategy.INSTANCE
@ -99,23 +95,22 @@ public abstract class AbstractCdiBeanContainer implements CdiBasedBeanContainer
BeanInstanceProducer fallbackProducer) { BeanInstanceProducer fallbackProducer) {
final String beanCacheKey = Helper.determineBeanCacheKey( beanName, beanType ); final String beanCacheKey = Helper.determineBeanCacheKey( beanName, beanType );
final ContainedBeanImplementor existing = beanCache.get( beanCacheKey ); final ContainedBeanImplementor<?> existing = beanCache.get( beanCacheKey );
if ( existing != null ) { if ( existing != null ) {
return existing; return (ContainedBeanImplementor<B>) existing;
} }
final ContainedBeanImplementor bean = createBean( beanName, beanType, lifecycleOptions, fallbackProducer ); final ContainedBeanImplementor<B> bean = createBean( beanName, beanType, lifecycleOptions, fallbackProducer );
beanCache.put( beanCacheKey, bean ); beanCache.put( beanCacheKey, bean );
return bean; return bean;
} }
@SuppressWarnings("unchecked")
private <B> ContainedBeanImplementor<B> createBean( private <B> ContainedBeanImplementor<B> createBean(
String beanName, String beanName,
Class<B> beanType, Class<B> beanType,
LifecycleOptions lifecycleOptions, LifecycleOptions lifecycleOptions,
BeanInstanceProducer fallbackProducer) { BeanInstanceProducer fallbackProducer) {
final ContainedBeanImplementor bean = createBean( final ContainedBeanImplementor<B> bean = createBean(
beanName, beanName,
beanType, beanType,
lifecycleOptions.useJpaCompliantCreation() lifecycleOptions.useJpaCompliantCreation()