Skip to content

Commit 8eec269

Browse files
mp911dechristophstrobl
authored andcommitted
Polishing.
Move off TypeReference for known and loaded classes for easier handling. Introduce configuration for enabled and include/exclude filters. Refactor configuration to functional style. Original Pull Request: #3318
1 parent 54b8baa commit 8eec269

15 files changed

+319
-263
lines changed

src/main/java/org/springframework/data/aot/AotContext.java

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.util.Optional;
2323
import java.util.Set;
2424
import java.util.function.Consumer;
25-
import java.util.function.Predicate;
2625

2726
import org.jspecify.annotations.Nullable;
2827
import org.springframework.aot.hint.TypeReference;
@@ -230,18 +229,31 @@ default IntrospectedBeanDefinition introspectBeanDefinition(BeanReference refere
230229
*/
231230
IntrospectedBeanDefinition introspectBeanDefinition(String beanName);
232231

233-
InstantiationCreator instantiationCreator(TypeReference typeReference);
234-
235-
default AotTypeConfiguration typeConfiguration(ResolvableType resolvableType) {
236-
return typeConfiguration(resolvableType.toClass());
237-
}
238-
239-
default AotTypeConfiguration typeConfiguration(Class<?> type) {
240-
return typeConfiguration(TypeReference.of(type));
232+
/**
233+
* Obtain a {@link AotTypeConfiguration} for the given {@link ResolvableType} to customize the AOT processing for the
234+
* given type.
235+
*
236+
* @param resolvableType the resolvable type to configure.
237+
* @param configurationConsumer configuration consumer function.
238+
*/
239+
default void typeConfiguration(ResolvableType resolvableType, Consumer<AotTypeConfiguration> configurationConsumer) {
240+
typeConfiguration(resolvableType.toClass(), configurationConsumer);
241241
}
242242

243-
AotTypeConfiguration typeConfiguration(TypeReference typeReference);
243+
/**
244+
* Obtain a {@link AotTypeConfiguration} for the given {@link ResolvableType} to customize the AOT processing for the
245+
* given type.
246+
*
247+
* @param type the type to configure.
248+
* @param configurationConsumer configuration consumer function.
249+
*/
250+
void typeConfiguration(Class<?> type, Consumer<AotTypeConfiguration> configurationConsumer);
244251

252+
/**
253+
* Return all type configurations registered with this {@link AotContext}.
254+
*
255+
* @return all type configurations registered with this {@link AotContext}.
256+
*/
245257
Collection<AotTypeConfiguration> typeConfigurations();
246258

247259
/**
@@ -358,9 +370,4 @@ interface IntrospectedBeanDefinition {
358370
Class<?> resolveType();
359371
}
360372

361-
interface InstantiationCreator {
362-
boolean isAvailable();
363-
void create();
364-
}
365-
366373
}

src/main/java/org/springframework/data/aot/AotMappingContext.java

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
import org.springframework.data.mapping.model.BasicPersistentEntity;
2525
import org.springframework.data.mapping.model.ClassGeneratingPropertyAccessorFactory;
2626
import org.springframework.data.mapping.model.EntityInstantiator;
27+
import org.springframework.data.mapping.model.EntityInstantiatorSource;
2728
import org.springframework.data.mapping.model.EntityInstantiators;
28-
import org.springframework.data.mapping.model.PersistentEntityClassInitializer;
2929
import org.springframework.data.mapping.model.Property;
3030
import org.springframework.data.mapping.model.SimpleTypeHolder;
3131
import org.springframework.data.repository.aot.generate.RepositoryContributor;
@@ -37,48 +37,53 @@
3737
* @author Mark Paluch
3838
* @since 4.0
3939
*/
40-
class AotMappingContext extends // TODO: hide this one and delegate to other component - can we use the
41-
// AotContext for it?
42-
AbstractMappingContext<BasicPersistentEntity<?, AotMappingContext.BasicPersistentProperty>, AotMappingContext.BasicPersistentProperty> {
40+
class AotMappingContext extends
41+
AbstractMappingContext<BasicPersistentEntity<?, AotMappingContext.AotPersistentProperty>, AotMappingContext.AotPersistentProperty> {
4342

4443
private static final Log logger = LogFactory.getLog(AotMappingContext.class);
4544

4645
private final EntityInstantiators instantiators = new EntityInstantiators();
47-
private final ClassGeneratingPropertyAccessorFactory propertyAccessorFactory = new ClassGeneratingPropertyAccessorFactory();
46+
private final AotAccessorFactory propertyAccessorFactory = new AotAccessorFactory();
4847

4948
/**
5049
* Contribute entity instantiators and property accessors for the given {@link PersistentEntity} that are captured
5150
* through Spring's {@code CglibClassHandler}. Otherwise, this is a no-op if contributions are not ran through
5251
* {@code CglibClassHandler}.
5352
*
54-
* @param entity
53+
* @param entityType
5554
*/
56-
public void contribute(PersistentEntity<?, ?> entity) {
57-
EntityInstantiator instantiator = instantiators.getInstantiatorFor(entity);
58-
if (instantiator instanceof PersistentEntityClassInitializer pec) {
59-
pec.initialize(entity);
55+
public void contribute(Class<?> entityType) {
56+
57+
BasicPersistentEntity<?, AotPersistentProperty> entity = getPersistentEntity(entityType);
58+
59+
if (entity != null) {
60+
61+
EntityInstantiator instantiator = instantiators.getInstantiatorFor(entity);
62+
if (instantiator instanceof EntityInstantiatorSource source) {
63+
source.getInstantiatorFor(entity);
64+
}
65+
66+
propertyAccessorFactory.initialize(entity);
6067
}
61-
propertyAccessorFactory.initialize(entity);
6268
}
6369

64-
// TODO: can we extract some util for this using only type
6570
@Override
66-
protected <T> BasicPersistentEntity<?, BasicPersistentProperty> createPersistentEntity(
71+
protected <T> BasicPersistentEntity<?, AotPersistentProperty> createPersistentEntity(
6772
TypeInformation<T> typeInformation) {
6873
logger.debug("I hate gradle: create persistent entity for type: " + typeInformation);
6974
return new BasicPersistentEntity<>(typeInformation);
7075
}
7176

7277
@Override
73-
protected BasicPersistentProperty createPersistentProperty(Property property,
74-
BasicPersistentEntity<?, BasicPersistentProperty> owner, SimpleTypeHolder simpleTypeHolder) {
78+
protected AotPersistentProperty createPersistentProperty(Property property,
79+
BasicPersistentEntity<?, AotPersistentProperty> owner, SimpleTypeHolder simpleTypeHolder) {
7580
logger.info("creating property: " + property.getName());
76-
return new BasicPersistentProperty(property, owner, simpleTypeHolder);
81+
return new AotPersistentProperty(property, owner, simpleTypeHolder);
7782
}
7883

79-
static class BasicPersistentProperty extends AnnotationBasedPersistentProperty<BasicPersistentProperty> {
84+
static class AotPersistentProperty extends AnnotationBasedPersistentProperty<AotPersistentProperty> {
8085

81-
public BasicPersistentProperty(Property property, PersistentEntity<?, BasicPersistentProperty> owner,
86+
public AotPersistentProperty(Property property, PersistentEntity<?, AotPersistentProperty> owner,
8287
SimpleTypeHolder simpleTypeHolder) {
8388
super(property, owner, simpleTypeHolder);
8489
}
@@ -89,15 +94,22 @@ public boolean isAssociation() {
8994
}
9095

9196
@Override
92-
protected Association<BasicPersistentProperty> createAssociation() {
97+
protected Association<AotPersistentProperty> createAssociation() {
9398
return new Association<>(this, null);
9499
}
95100

96101
@Override
97-
public Association<BasicPersistentProperty> getRequiredAssociation() {
102+
public Association<AotPersistentProperty> getAssociation() {
98103
return new Association<>(this, null);
99104
}
100105

101106
}
102107

108+
static class AotAccessorFactory extends ClassGeneratingPropertyAccessorFactory {
109+
110+
public void initialize(PersistentEntity<?, ?> entity) {
111+
potentiallyCreateAndRegisterPersistentPropertyAccessorClass(entity);
112+
}
113+
}
114+
103115
}

src/main/java/org/springframework/data/aot/AotTypeConfiguration.java

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ public interface AotTypeConfiguration {
3939

4040
AotTypeConfiguration forReflectiveAccess(MemberCategory... categories);
4141

42-
AotTypeConfiguration generateEntityInstantiator();
42+
AotTypeConfiguration contributeAccessors();
4343

4444
// TODO: ? should this be a global condition for the entire configuration or do we need it for certain aspects ?
45-
AotTypeConfiguration conditional(Predicate<TypeReference> filter);
45+
AotTypeConfiguration filter(Predicate<Class<?>> filter);
4646

4747
default AotTypeConfiguration usedAsProjectionInterface() {
4848
return proxyInterface(TargetAware.class, SpringProxy.class, DecoratingProxy.class);
@@ -69,42 +69,12 @@ default AotTypeConfiguration repositoryProxy() {
6969

7070
AotTypeConfiguration proxyInterface(List<TypeReference> proxyInterfaces);
7171

72-
default AotTypeConfiguration proxyInterface(TypeReference... proxyInterfaces) {
73-
return proxyInterface(List.of(proxyInterfaces));
74-
}
75-
7672
default AotTypeConfiguration proxyInterface(Class<?>... proxyInterfaces) {
7773
return proxyInterface(Stream.of(proxyInterfaces).map(TypeReference::of).toList());
7874
}
7975

8076
AotTypeConfiguration forQuerydsl();
8177

82-
void contribute(GenerationContext generationContext);
83-
84-
static Predicate<TypeReference> userConfiguredCondition(Environment environment) {
85-
86-
return new Predicate<TypeReference>() {
87-
88-
private final List<String> allowedAccessorTypes = environment.getProperty("spring.data.aot.generate.accessor",
89-
List.class, List.of());
90-
91-
@Override
92-
@SuppressWarnings("unchecked")
93-
public boolean test(TypeReference typeReference) {
94-
95-
if (!allowedAccessorTypes.isEmpty()) {
96-
if (allowedAccessorTypes.contains("none") || allowedAccessorTypes.contains("false")
97-
|| allowedAccessorTypes.contains("off")) {
98-
return false;
99-
}
100-
if (!allowedAccessorTypes.contains(typeReference.getName())) {
101-
return false;
102-
}
103-
}
104-
105-
return true;
106-
}
107-
};
108-
}
78+
void contribute(Environment environment, GenerationContext generationContext);
10979

11080
}

0 commit comments

Comments
 (0)