From 3ba2958e5d61fece55128a1021d86a098996fd70 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Mon, 27 May 2024 10:09:05 +0200 Subject: [PATCH] extract two methods / code style Signed-off-by: Gavin King --- .../boot/model/internal/GeneratorBinder.java | 88 +++++++++++++------ .../boot/model/internal/PropertyBinder.java | 19 ++-- .../resource/beans/internal/Helper.java | 12 ++- 3 files changed, 76 insertions(+), 43 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/GeneratorBinder.java b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/GeneratorBinder.java index 02d689570c..8f6707d831 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/GeneratorBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/GeneratorBinder.java @@ -375,7 +375,10 @@ public class GeneratorBinder { }; } - static IdentifierGeneratorCreator identifierGeneratorCreator(XProperty idProperty, Annotation annotation, BeanContainer beanContainer) { + static IdentifierGeneratorCreator identifierGeneratorCreator( + XProperty idProperty, + Annotation annotation, + BeanContainer beanContainer) { final Member member = HCANNHelper.getUnderlyingMember( idProperty ); final Class annotationType = annotation.annotationType(); final IdGeneratorType idGeneratorType = annotationType.getAnnotation( IdGeneratorType.class ); @@ -383,19 +386,70 @@ public class GeneratorBinder { return creationContext -> { final Class generatorClass = idGeneratorType.value(); checkGeneratorClass( generatorClass ); - Generator generator; - if ( beanContainer != null ) { - generator = beanContainer.getBean( generatorClass, new BeanContainer.LifecycleOptions() { + final Generator generator = + instantiateGenerator( + annotation, + beanContainer, + creationContext, + generatorClass, + member, + annotationType + ); + callInitialize( annotation, member, creationContext, generator ); + callConfigure( creationContext, generator ); + checkIdGeneratorTiming( annotationType, generator ); + return generator; + }; + } + + private static Generator instantiateGenerator( + Annotation annotation, + BeanContainer beanContainer, + CustomIdGeneratorCreationContext creationContext, + Class generatorClass, + Member member, + Class annotationType) { + if ( beanContainer != null ) { + return instantiateGeneratorAsBean( + annotation, + beanContainer, + creationContext, + generatorClass, + member, + annotationType + ); + } + else { + return instantiateGenerator( + annotation, + member, + annotationType, + creationContext, + CustomIdGeneratorCreationContext.class, + generatorClass + ); + } + } + + private static Generator instantiateGeneratorAsBean( + Annotation annotation, + BeanContainer beanContainer, + CustomIdGeneratorCreationContext creationContext, + Class generatorClass, + Member member, + Class annotationType) { + return beanContainer.getBean( generatorClass, + new BeanContainer.LifecycleOptions() { @Override public boolean canUseCachedReferences() { return false; } - @Override public boolean useJpaCompliantCreation() { return true; } - }, new BeanInstanceProducer() { + }, + new BeanInstanceProducer() { @SuppressWarnings( "unchecked" ) @Override public B produceBeanInstance(Class beanType) { @@ -408,28 +462,12 @@ public class GeneratorBinder { generatorClass ); } - @Override public B produceBeanInstance(String name, Class beanType) { - return produceBeanInstance(beanType); + return produceBeanInstance( beanType ); } - } ).getBeanInstance(); - } - else { - generator = instantiateGenerator( - annotation, - member, - annotationType, - creationContext, - CustomIdGeneratorCreationContext.class, - generatorClass - ); - } - callInitialize( annotation, member, creationContext, generator ); - callConfigure( creationContext, generator ); - checkIdGeneratorTiming( annotationType, generator ); - return generator; - }; + } ) + .getBeanInstance(); } private static G instantiateGenerator( diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/PropertyBinder.java b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/PropertyBinder.java index 4815fbbff8..6cea97ba35 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/PropertyBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/PropertyBinder.java @@ -8,7 +8,6 @@ package org.hibernate.boot.model.internal; import java.lang.annotation.Annotation; import java.util.HashMap; -import java.util.Iterator; import java.util.List; import java.util.Map; @@ -70,7 +69,6 @@ import org.hibernate.mapping.Value; import org.hibernate.metamodel.spi.EmbeddableInstantiator; import org.hibernate.resource.beans.container.spi.BeanContainer; -import org.hibernate.resource.beans.internal.Helper; import org.hibernate.resource.beans.spi.ManagedBeanRegistry; import org.hibernate.service.ServiceRegistry; import org.hibernate.usertype.CompositeUserType; @@ -104,6 +102,7 @@ import static org.hibernate.boot.model.internal.ToOneBinder.bindOneToOne; import static org.hibernate.boot.model.naming.Identifier.toIdentifier; import static org.hibernate.internal.util.StringHelper.qualify; import static org.hibernate.internal.util.collections.CollectionHelper.combine; +import static org.hibernate.resource.beans.internal.Helper.allowExtensionsInCdi; /** * A stateful binder responsible for creating {@link Property} objects. @@ -1434,9 +1433,13 @@ public class PropertyBinder { + "' has too many generator annotations " + combine( idGeneratorAnnotations, generatorAnnotations ) ); } if ( !idGeneratorAnnotations.isEmpty() ) { + final Annotation annotation = idGeneratorAnnotations.get(0); final ServiceRegistry serviceRegistry = context.getBootstrapContext().getServiceRegistry(); - final BeanContainer beanContainer = Helper.allowExtensionsInCdi( serviceRegistry ) ? serviceRegistry.requireService( ManagedBeanRegistry.class ).getBeanContainer() : null; - idValue.setCustomIdGeneratorCreator( identifierGeneratorCreator( idProperty, idGeneratorAnnotations.get(0), beanContainer ) ); + final BeanContainer beanContainer = + allowExtensionsInCdi( serviceRegistry ) + ? serviceRegistry.requireService( ManagedBeanRegistry.class ).getBeanContainer() + : null; + idValue.setCustomIdGeneratorCreator( identifierGeneratorCreator( idProperty, annotation, beanContainer ) ); } else if ( !generatorAnnotations.isEmpty() ) { // idValue.setCustomGeneratorCreator( generatorCreator( idProperty, generatorAnnotation ) ); @@ -1463,13 +1466,7 @@ public class PropertyBinder { // at most one annotation for a generator: private static void removeIdGenerators(List generatorAnnotations, List idGeneratorAnnotations) { for ( Annotation id : idGeneratorAnnotations ) { - Iterator iterator = generatorAnnotations.iterator(); - while ( iterator.hasNext() ) { - Annotation gen = iterator.next(); - if ( gen.annotationType().equals( id.annotationType() ) ) { - iterator.remove(); - } - } + generatorAnnotations.removeIf( gen -> gen.annotationType().equals( id.annotationType() ) ); } } } diff --git a/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/Helper.java b/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/Helper.java index f36ac6c987..e59e93cf07 100644 --- a/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/Helper.java +++ b/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/Helper.java @@ -6,14 +6,15 @@ */ package org.hibernate.resource.beans.internal; -import org.hibernate.cfg.AvailableSettings; import org.hibernate.engine.config.spi.ConfigurationService; -import org.hibernate.engine.config.spi.StandardConverters; import org.hibernate.resource.beans.container.internal.ContainerManagedLifecycleStrategy; import org.hibernate.resource.beans.container.internal.JpaCompliantLifecycleStrategy; import org.hibernate.resource.beans.container.spi.BeanLifecycleStrategy; import org.hibernate.service.ServiceRegistry; +import static org.hibernate.cfg.ManagedBeanSettings.ALLOW_EXTENSIONS_IN_CDI; +import static org.hibernate.engine.config.spi.StandardConverters.BOOLEAN; + /** * @author Steve Ebersole */ @@ -31,11 +32,8 @@ public final class Helper { } public static boolean allowExtensionsInCdi(ServiceRegistry serviceRegistry) { - return serviceRegistry.requireService( ConfigurationService.class ).getSetting( - AvailableSettings.ALLOW_EXTENSIONS_IN_CDI, - StandardConverters.BOOLEAN, - false - ); + return serviceRegistry.requireService( ConfigurationService.class ) + .getSetting( ALLOW_EXTENSIONS_IN_CDI, BOOLEAN, false ); } @SuppressWarnings("unused")