From 3fc2cb916e1f2feed65f3945a78daee121fe3b2e Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Tue, 23 Sep 2025 15:59:11 +0200 Subject: [PATCH] Polishing. Refine naming. See: #3267 Original Pull Request: #3367 --- ...agedTypesBeanRegistrationAotProcessor.java | 12 ++-- ...nagedTypesRegistrationAotContribution.java | 6 +- .../RepositoryRegistrationAotProcessor.java | 69 ++++++++++--------- 3 files changed, 47 insertions(+), 40 deletions(-) diff --git a/src/main/java/org/springframework/data/aot/ManagedTypesBeanRegistrationAotProcessor.java b/src/main/java/org/springframework/data/aot/ManagedTypesBeanRegistrationAotProcessor.java index a6c836561..c5139d44d 100644 --- a/src/main/java/org/springframework/data/aot/ManagedTypesBeanRegistrationAotProcessor.java +++ b/src/main/java/org/springframework/data/aot/ManagedTypesBeanRegistrationAotProcessor.java @@ -119,7 +119,7 @@ public class ManagedTypesBeanRegistrationAotProcessor implements BeanRegistratio /** * Hook to provide a customized flavor of {@link BeanRegistrationAotContribution}. By overriding this method calls to - * {@link #contributeType(ResolvableType, GenerationContext, AotContext)} might no longer be issued. + * {@link #registerTypeHints(ResolvableType, AotContext, GenerationContext)} might no longer be issued. * * @param aotContext never {@literal null}. * @param managedTypes never {@literal null}. @@ -128,7 +128,7 @@ public class ManagedTypesBeanRegistrationAotProcessor implements BeanRegistratio protected BeanRegistrationAotContribution contribute(AotContext aotContext, ManagedTypes managedTypes, RegisteredBean registeredBean) { return new ManagedTypesRegistrationAotContribution(aotContext, managedTypes, registeredBean, - typeCollectorCustomizer(), this::contributeType); + typeCollectorCustomizer(), this::registerTypeHints); } /** @@ -140,13 +140,14 @@ public class ManagedTypesBeanRegistrationAotProcessor implements BeanRegistratio protected Consumer typeCollectorCustomizer() { return typeCollector -> {}; } + /** * Hook to contribute configuration for a given {@literal type}. * * @param type never {@literal null}. * @param generationContext never {@literal null}. */ - protected void contributeType(ResolvableType type, GenerationContext generationContext, AotContext aotContext) { + protected void registerTypeHints(ResolvableType type, AotContext aotContext, GenerationContext generationContext) { if (logger.isDebugEnabled()) { logger.debug(String.format("Contributing type information for [%s]", type.getType())); @@ -154,8 +155,7 @@ public class ManagedTypesBeanRegistrationAotProcessor implements BeanRegistratio Set annotationNamespaces = Collections.singleton(TypeContributor.DATA_NAMESPACE); - configureTypeContribution(type.toClass(), aotContext); - aotContext.contributeTypeConfigurations(generationContext); + configureTypeHints(type.toClass(), aotContext); TypeUtils.resolveUsedAnnotations(type.toClass()).forEach( annotation -> TypeContributor.contribute(annotation.getType(), annotationNamespaces, generationContext)); @@ -168,7 +168,7 @@ public class ManagedTypesBeanRegistrationAotProcessor implements BeanRegistratio * @param aotContext AOT context for type configuration. * @since 4.0 */ - protected void configureTypeContribution(Class type, AotContext aotContext) { + protected void configureTypeHints(Class type, AotContext aotContext) { aotContext.typeConfiguration(type, config -> config.forDataBinding().contributeAccessors().forQuerydsl()); } diff --git a/src/main/java/org/springframework/data/aot/ManagedTypesRegistrationAotContribution.java b/src/main/java/org/springframework/data/aot/ManagedTypesRegistrationAotContribution.java index 2463576a4..daeb21b58 100644 --- a/src/main/java/org/springframework/data/aot/ManagedTypesRegistrationAotContribution.java +++ b/src/main/java/org/springframework/data/aot/ManagedTypesRegistrationAotContribution.java @@ -98,8 +98,10 @@ class ManagedTypesRegistrationAotContribution implements RegisteredBeanAotContri if (!types.isEmpty()) { TypeCollector.inspect(typeCollectorCustomizer, types) - .forEach(type -> contributionAction.register(type, generationContext, aotContext)); + .forEach(type -> contributionAction.register(type, aotContext, generationContext)); } + + aotContext.contributeTypeConfigurations(generationContext); } @Override @@ -117,7 +119,7 @@ class ManagedTypesRegistrationAotContribution implements RegisteredBeanAotContri } interface TypeRegistration { - void register(ResolvableType type, GenerationContext generationContext, AotContext aotContext); + void register(ResolvableType type, AotContext aotContext, GenerationContext generationContext); } /** diff --git a/src/main/java/org/springframework/data/repository/config/RepositoryRegistrationAotProcessor.java b/src/main/java/org/springframework/data/repository/config/RepositoryRegistrationAotProcessor.java index 99c54f865..e9f1b0a74 100644 --- a/src/main/java/org/springframework/data/repository/config/RepositoryRegistrationAotProcessor.java +++ b/src/main/java/org/springframework/data/repository/config/RepositoryRegistrationAotProcessor.java @@ -67,11 +67,11 @@ import org.springframework.util.ClassUtils; * not match due to customization of the factory bean by the user, at least the target repository type is provided via * the {@link FactoryBean#OBJECT_TYPE_ATTRIBUTE}. *

- * With {@link RepositoryRegistrationAotProcessor#contributeRepositoryHints(AotRepositoryContext, GenerationContext)} - * and {@link RepositoryRegistrationAotProcessor#contributeAotRepository(AotRepositoryContext)}, stores can provide - * custom logic for contributing additional (e.g. reflection) configuration. By default, reflection configuration will - * be added for types reachable from the repository declaration and query methods as well as all used {@link Annotation - * annotations} from the {@literal org.springframework.data} namespace. + * With {@link #registerRepositoryCompositionHints(AotRepositoryContext, GenerationContext)} (specifically + * {@link #configureTypeContribution(Class, AotContext)} and {@link #contributeAotRepository(AotRepositoryContext)}, + * stores can provide custom logic for contributing additional (e.g. reflection) configuration. By default, reflection + * configuration will be added for types reachable from the repository declaration and query methods as well as all used + * {@link Annotation annotations} from the {@literal org.springframework.data} namespace. *

* The processor is typically configured via {@link RepositoryConfigurationExtension#getRepositoryAotProcessor()} and * gets added by the {@link org.springframework.data.repository.config.RepositoryConfigurationDelegate}. @@ -163,8 +163,8 @@ public class RepositoryRegistrationAotProcessor BeanRegistrationAotContribution contribution = (generationContext, beanRegistrationCode) -> { - contributeRepositoryHints(repositoryContext, generationContext); - contributeTypes(repositoryContext, generationContext); + registerRepositoryCompositionHints(repositoryContext, generationContext); + configureTypeContributions(repositoryContext, generationContext); repositoryContext.contributeTypeConfigurations(generationContext); }; @@ -181,7 +181,7 @@ public class RepositoryRegistrationAotProcessor * @param generationContext the generation context. * @since 4.0 */ - protected void contributeRepositoryHints(AotRepositoryContext repositoryContext, + protected void registerRepositoryCompositionHints(AotRepositoryContext repositoryContext, GenerationContext generationContext) { RepositoryInformation repositoryInformation = repositoryContext.getRepositoryInformation(); @@ -200,7 +200,7 @@ public class RepositoryRegistrationAotProcessor .forReflectiveAccess(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, MemberCategory.INVOKE_PUBLIC_METHODS)); // Repository Fragments - contributeFragments(repositoryInformation.getFragments(), generationContext); + registerFragmentsHints(repositoryInformation.getFragments(), generationContext); // Kotlin if (isKotlinCoroutineRepository(repositoryInformation)) { @@ -209,20 +209,19 @@ public class RepositoryRegistrationAotProcessor } /** - * Contribute types for reflection, proxies, etc. Customization hook for subclasses that wish to customize type - * contribution hints. + * Register type-specific hints and AOT artifacts for domain types, reachable types, projection interfaces derived + * from query method return types, and annotations from {@literal org.springframework.data} packages. * * @param repositoryContext the repository context. * @param generationContext the generation context. * @since 4.0 */ - protected void contributeTypes(AotRepositoryContext repositoryContext, GenerationContext generationContext) { - - contributeDomainTypes(repositoryContext, generationContext); - contributeResolvedTypes(repositoryContext, generationContext); + private void configureTypeContributions(AotRepositoryContext repositoryContext, GenerationContext generationContext) { RepositoryInformation information = repositoryContext.getRepositoryInformation(); + configureDomainTypeContributions(repositoryContext, generationContext); + // Repository query methods information.getQueryMethods().stream().map(information::getReturnedDomainClass).filter(Class::isInterface) .forEach(type -> { @@ -238,44 +237,50 @@ public class RepositoryRegistrationAotProcessor } /** - * Customization hook for subclasses that wish to customize domain type contribution hints. + * Customization hook for subclasses that wish to customize domain type hint contributions. + *

+ * Type hints are registered for the domain, alternative domain types, and types reachable from there + * ({@link AotRepositoryContext#getResolvedTypes()}) * * @param repositoryContext the repository context. * @param generationContext the generation context. * @since 4.0 */ - protected void contributeDomainTypes(AotRepositoryContext repositoryContext, GenerationContext generationContext) { + protected void configureDomainTypeContributions(AotRepositoryContext repositoryContext, + GenerationContext generationContext) { RepositoryInformation information = repositoryContext.getRepositoryInformation(); - RuntimeHints hints = generationContext.getRuntimeHints(); // Domain types, related types, projections - repositoryContext.typeConfiguration(information.getDomainType(), config -> config.forDataBinding().forQuerydsl()); - ReflectiveRuntimeHintsRegistrar registrar = new ReflectiveRuntimeHintsRegistrar(); Stream.concat(Stream.of(information.getDomainType()), information.getAlternativeDomainTypes().stream()) .forEach(it -> { - // TODO cross check with #contributeResolvedTypes registrar.registerRuntimeHints(hints, it); - repositoryContext.typeConfiguration(it, AotTypeConfiguration::contributeAccessors); + configureTypeContribution(it, repositoryContext); }); - } - private void contributeResolvedTypes(AotRepositoryContext repositoryContext, GenerationContext generationContext) { - - RepositoryInformation information = repositoryContext.getRepositoryInformation(); - - // TODO: These are twice. + // TODO: Looks like a duplicate repositoryContext.getResolvedTypes().stream() .filter(it -> TypeContributor.isPartOf(it, Set.of(information.getDomainType().getPackageName()))) - .forEach(it -> repositoryContext.typeConfiguration(it, AotTypeConfiguration::contributeAccessors)); + .forEach(it -> configureTypeContribution(it, repositoryContext)); repositoryContext.getResolvedTypes().stream().filter(it -> !isJavaOrPrimitiveType(it)) .forEach(it -> contributeType(it, generationContext)); } + /** + * Customization hook to configure the {@link TypeContributor} used to register the given {@literal type}. + * + * @param type the class to configure the contribution for. + * @param aotContext AOT context for type configuration. + * @since 4.0 + */ + protected void configureTypeContribution(Class type, AotContext aotContext) { + aotContext.typeConfiguration(type, config -> config.forDataBinding().contributeAccessors().forQuerydsl()); + } + /** * This method allows for the creation to be overridden by subclasses. * @@ -308,11 +313,11 @@ public class RepositoryRegistrationAotProcessor TypeContributor.contribute(type, it -> true, context); } - private void contributeFragments(Iterable> fragments, GenerationContext contribution) { - fragments.forEach(it -> contributeFragment(it, contribution)); + private void registerFragmentsHints(Iterable> fragments, GenerationContext contribution) { + fragments.forEach(it -> registerFragmentHints(it, contribution)); } - private static void contributeFragment(RepositoryFragment fragment, GenerationContext context) { + private static void registerFragmentHints(RepositoryFragment fragment, GenerationContext context) { Class repositoryFragmentType = fragment.getSignatureContributor(); Optional> implementation = fragment.getImplementationClass();