make @IdGeneratorType accept Generator

This commit is contained in:
Gavin 2022-12-02 17:26:00 +01:00 committed by Gavin King
parent eb6860d9d2
commit 6596389e55
1 changed files with 11 additions and 4 deletions

View File

@ -448,12 +448,15 @@ public class PropertyBinder {
return creator; return creator;
} }
private static void checkGeneratorType(Class<? extends Generator> generatorClass) { private static void checkGeneratorClass(Class<? extends Generator> generatorClass) {
if ( !InMemoryGenerator.class.isAssignableFrom( generatorClass ) if ( !InMemoryGenerator.class.isAssignableFrom( generatorClass )
&& !InDatabaseGenerator.class.isAssignableFrom( generatorClass ) ) { && !InDatabaseGenerator.class.isAssignableFrom( generatorClass ) ) {
throw new MappingException("Generator class '" + generatorClass.getName() throw new MappingException("Generator class '" + generatorClass.getName()
+ "' must implement either 'InMemoryGenerator' or 'InDatabaseGenerator'"); + "' must implement either 'InMemoryGenerator' or 'InDatabaseGenerator'");
} }
}
private static void checkGeneratorInterfaces(Class<? extends Generator> generatorClass) {
// we don't yet support the additional "fancy" operations of // we don't yet support the additional "fancy" operations of
// IdentifierGenerator with regular generators, though this // IdentifierGenerator with regular generators, though this
// would be extremely easy to add if anyone asks for it // would be extremely easy to add if anyone asks for it
@ -480,7 +483,9 @@ public class PropertyBinder {
if ( generatorAnnotation == null ) { if ( generatorAnnotation == null ) {
return null; return null;
} }
checkGeneratorType( generatorAnnotation.generatedBy() ); final Class<? extends Generator> generatorClass = generatorAnnotation.generatedBy();
checkGeneratorClass( generatorClass );
checkGeneratorInterfaces( generatorClass );
return creationContext -> { return creationContext -> {
final Generator generator = final Generator generator =
instantiateGenerator( instantiateGenerator(
@ -489,7 +494,7 @@ public class PropertyBinder {
annotationType, annotationType,
creationContext, creationContext,
GeneratorCreationContext.class, GeneratorCreationContext.class,
generatorAnnotation.generatedBy() generatorClass
); );
callInitialize( annotation, member, creationContext, generator ); callInitialize( annotation, member, creationContext, generator );
checkVersionGenerationAlways( property, generator ); checkVersionGenerationAlways( property, generator );
@ -503,6 +508,8 @@ public class PropertyBinder {
final IdGeneratorType idGeneratorType = annotationType.getAnnotation( IdGeneratorType.class ); final IdGeneratorType idGeneratorType = annotationType.getAnnotation( IdGeneratorType.class );
assert idGeneratorType != null; assert idGeneratorType != null;
return creationContext -> { return creationContext -> {
final Class<? extends Generator> generatorClass = idGeneratorType.value();
checkGeneratorClass( generatorClass );
final Generator generator = final Generator generator =
instantiateGenerator( instantiateGenerator(
annotation, annotation,
@ -510,7 +517,7 @@ public class PropertyBinder {
annotationType, annotationType,
creationContext, creationContext,
CustomIdGeneratorCreationContext.class, CustomIdGeneratorCreationContext.class,
idGeneratorType.value() generatorClass
); );
callInitialize( annotation, member, creationContext, generator ); callInitialize( annotation, member, creationContext, generator );
checkIdGeneratorTiming( annotationType, generator ); checkIdGeneratorTiming( annotationType, generator );