extract two methods / code style

Signed-off-by: Gavin King <gavin@hibernate.org>
This commit is contained in:
Gavin King 2024-05-27 10:09:05 +02:00
parent 9d015ac7ea
commit 3ba2958e5d
3 changed files with 76 additions and 43 deletions

View File

@ -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<? extends Annotation> annotationType = annotation.annotationType();
final IdGeneratorType idGeneratorType = annotationType.getAnnotation( IdGeneratorType.class );
@ -383,19 +386,70 @@ public class GeneratorBinder {
return creationContext -> {
final Class<? extends Generator> generatorClass = idGeneratorType.value();
checkGeneratorClass( generatorClass );
Generator generator;
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<? extends Generator> generatorClass,
Member member,
Class<? extends Annotation> annotationType) {
if ( beanContainer != null ) {
generator = beanContainer.getBean( generatorClass, new BeanContainer.LifecycleOptions() {
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<? extends Generator> generatorClass,
Member member,
Class<? extends Annotation> 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> B produceBeanInstance(Class<B> beanType) {
@ -408,28 +462,12 @@ public class GeneratorBinder {
generatorClass
);
}
@Override
public <B> B produceBeanInstance(String name, Class<B> 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 <C, G extends Generator> G instantiateGenerator(

View File

@ -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<Annotation> generatorAnnotations, List<Annotation> idGeneratorAnnotations) {
for ( Annotation id : idGeneratorAnnotations ) {
Iterator<Annotation> 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() ) );
}
}
}

View File

@ -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")