Skip to content

Commit 2c4e594

Browse files
committed
Polish changes in Spring Data adapting to the core Spring Framework AOT API changes.
* Reorganize the Spring Data AOT infrastructure classes; simplify logic. * Use AotSpringFactoriesLoader. * Fix failing tests.
1 parent b8c4702 commit 2c4e594

20 files changed

+1003
-621
lines changed

src/main/java/org/springframework/data/ManagedTypes.java

+47-12
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,64 @@
1616
package org.springframework.data;
1717

1818
import java.util.ArrayList;
19+
import java.util.Collections;
1920
import java.util.List;
2021
import java.util.function.Consumer;
2122
import java.util.function.Supplier;
2223
import java.util.stream.Stream;
2324

2425
import org.springframework.data.util.Lazy;
2526
import org.springframework.lang.NonNull;
27+
import org.springframework.lang.Nullable;
2628

2729
/**
28-
* Types managed by a Spring Data implementation. Used to predefine a set of know entities that might need processing
29-
* during the Spring container, Spring Data Repository initialization phase.
30+
* Types managed by a Spring Data implementation.
31+
*
32+
* Used to predefine a set of know entities that might need processing during the Spring container,
33+
* Spring Data Repository initialization phase.
3034
*
3135
* @author Christoph Strobl
36+
* @author John Blum
3237
* @see java.lang.FunctionalInterface
3338
* @since 3.0
3439
*/
40+
// TODO: Does this need to be in org.springframework.data? Maybe move to org.springframework.data.domain?
3541
@FunctionalInterface
3642
public interface ManagedTypes {
3743

3844
/**
39-
* Factory method used to construct {@link ManagedTypes} from the given {@link Iterable} of {@link Class types}.
45+
* Factory method used to construct a new instance of {@link ManagedTypes} containing no {@link Class types}.
46+
*
47+
* @return an empty {@link ManagedTypes} instance.
48+
* @see java.util.Collections#emptySet()
49+
* @see #of(Iterable)
50+
*/
51+
@NonNull
52+
static ManagedTypes empty() {
53+
return of(Collections.emptySet());
54+
}
55+
56+
/**
57+
* Factory method used to return a {@literal null-safe} instance of {@link ManagedTypes}.
58+
*
59+
* @param types {@link ManagedTypes} to evaluate.
60+
* @return the given {@link ManagedTypes} if not {@literal null}
61+
* or an {@link #empty()} {@link ManagedTypes} instance.
62+
* @see #empty()
63+
*/
64+
@NonNull
65+
static ManagedTypes nullSafeManagedTypes(@Nullable ManagedTypes types) {
66+
return types != null ? types : empty();
67+
}
68+
69+
/**
70+
* Factory method used to construct {@link ManagedTypes} from the given, required {@link Iterable}
71+
* of {@link Class types}.
4072
*
41-
* @param types {@link Iterable} of {@link Class types} used to initialize the new {@link ManagedTypes} instance;
73+
* @param types {@link Iterable} of {@link Class types} used to initialize the {@link ManagedTypes};
4274
* must not be {@literal null}.
43-
* @return new instance of {@link ManagedTypes} initialized the given {@link Iterable} of {@link Class types}.
75+
* @return new instance of {@link ManagedTypes} initialized the given, required {@link Iterable}
76+
* of {@link Class types}.
4477
* @see java.lang.Iterable
4578
* @see #of(Stream)
4679
* @see #of(Supplier)
@@ -51,11 +84,13 @@ static ManagedTypes of(@NonNull Iterable<? extends Class<?>> types) {
5184
}
5285

5386
/**
54-
* Factory method used to construct {@link ManagedTypes} from the given {@link Stream} of {@link Class types}.
87+
* Factory method used to construct {@link ManagedTypes} from the given, required {@link Stream}
88+
* of {@link Class types}.
5589
*
56-
* @param types {@link Stream} of {@link Class types} used to initialize the new {@link ManagedTypes} instance;
90+
* @param types {@link Stream} of {@link Class types} used to initialize the {@link ManagedTypes};
5791
* must not be {@literal null}.
58-
* @return new instance of {@link ManagedTypes} initialized the given {@link Stream} of {@link Class types}.
92+
* @return new instance of {@link ManagedTypes} initialized the given, required {@link Stream}
93+
* of {@link Class types}.
5994
* @see java.util.stream.Stream
6095
* @see #of(Iterable)
6196
* @see #of(Supplier)
@@ -66,12 +101,12 @@ static ManagedTypes of(@NonNull Stream<? extends Class<?>> types) {
66101
}
67102

68103
/**
69-
* Factory method used to construct {@link ManagedTypes} from the given {@link Supplier} of
104+
* Factory method used to construct {@link ManagedTypes} from the given, required {@link Supplier} of
70105
* an {@link Iterable} of {@link Class types}.
71106
*
72107
* @param dataProvider {@link Supplier} of an {@link Iterable} of {@link Class types} used to lazily initialize
73-
* the new {@link ManagedTypes} instance; must not be {@literal null}.
74-
* @return new instance of {@link ManagedTypes} initialized the given {@link Supplier} of
108+
* the {@link ManagedTypes}; must not be {@literal null}.
109+
* @return new instance of {@link ManagedTypes} initialized the given, required {@link Supplier} of
75110
* an {@link Iterable} of {@link Class types}.
76111
* @see java.util.function.Supplier
77112
* @see java.lang.Iterable
@@ -93,7 +128,7 @@ public void forEach(@NonNull Consumer<Class<?>> action) {
93128
}
94129

95130
/**
96-
* Applies the given {@link Consumer action} to each of the {@link Class type} contained in
131+
* Applies the given {@link Consumer action} to each of the {@link Class types} contained in
97132
* this {@link ManagedTypes} instance.
98133
*
99134
* @param action {@link Consumer} defining the action to perform on the {@link Class types}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ default Class<?> resolveRequiredType(@NonNull String typeName) throws TypeNotPre
171171
default Optional<Class<?>> resolveType(@NonNull String typeName) {
172172

173173
return isTypePresent(typeName)
174-
? Optional.of(resolveRequiredType(typeName))
175-
: Optional.empty();
174+
? Optional.of(resolveRequiredType(typeName))
175+
: Optional.empty();
176176
}
177177

178178
/**

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

+10-10
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,21 @@ public ConfigurableListableBeanFactory getBeanFactory() {
6262
}
6363

6464
@Override
65-
public String getBeanName() {
66-
return beanName;
65+
public Set<String> getBasePackages() {
66+
return basePackages;
6767
}
6868

69-
public void setBeanName(String beanName) {
70-
this.beanName = beanName;
69+
public void setBasePackages(Set<String> basePackages) {
70+
this.basePackages = basePackages;
7171
}
7272

7373
@Override
74-
public Set<String> getBasePackages() {
75-
return basePackages;
74+
public String getBeanName() {
75+
return beanName;
7676
}
7777

78-
public void setBasePackages(Set<String> basePackages) {
79-
this.basePackages = basePackages;
78+
public void setBeanName(String beanName) {
79+
this.beanName = beanName;
8080
}
8181

8282
@Override
@@ -110,8 +110,8 @@ public Set<Class<?>> getResolvedTypes() {
110110
protected Set<MergedAnnotation<Annotation>> discoverAnnotations() {
111111

112112
Set<MergedAnnotation<Annotation>> annotations = getResolvedTypes().stream()
113-
.flatMap(type -> TypeUtils.resolveUsedAnnotations(type).stream())
114-
.collect(Collectors.toCollection(LinkedHashSet::new));
113+
.flatMap(type -> TypeUtils.resolveUsedAnnotations(type).stream())
114+
.collect(Collectors.toCollection(LinkedHashSet::new));
115115

116116
annotations.addAll(TypeUtils.resolveUsedAnnotations(repositoryInformation.getRepositoryInterface()));
117117

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2012-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.aot;
17+
18+
import java.util.List;
19+
import java.util.function.BiConsumer;
20+
21+
import org.springframework.aot.generate.GenerationContext;
22+
import org.springframework.beans.factory.aot.BeanRegistrationAotContribution;
23+
import org.springframework.beans.factory.aot.BeanRegistrationCode;
24+
import org.springframework.core.ResolvableType;
25+
import org.springframework.data.ManagedTypes;
26+
import org.springframework.lang.NonNull;
27+
28+
/**
29+
* {@link BeanRegistrationAotContribution} used to contribute a {@link ManagedTypes} registration.
30+
*
31+
* @author John Blum
32+
* @see org.springframework.beans.factory.aot.BeanRegistrationAotContribution
33+
* @since 3.0.0
34+
*/
35+
public class ManagedTypesRegistrationAotContribution implements BeanRegistrationAotContribution {
36+
37+
private final AotContext aotContext;
38+
private final ManagedTypes managedTypes;
39+
private final BiConsumer<ResolvableType, GenerationContext> contributionAction;
40+
41+
public ManagedTypesRegistrationAotContribution(AotContext aotContext, ManagedTypes managedTypes,
42+
@NonNull BiConsumer<ResolvableType, GenerationContext> contributionAction) {
43+
44+
this.aotContext = aotContext;
45+
this.managedTypes = managedTypes;
46+
this.contributionAction = contributionAction;
47+
}
48+
49+
protected AotContext getAotContext() {
50+
return this.aotContext;
51+
}
52+
53+
@NonNull
54+
protected ManagedTypes getManagedTypes() {
55+
return ManagedTypes.nullSafeManagedTypes(this.managedTypes);
56+
}
57+
58+
@Override
59+
public void applyTo(@NonNull GenerationContext generationContext,
60+
@NonNull BeanRegistrationCode beanRegistrationCode) {
61+
62+
List<Class<?>> types = getManagedTypes().toList();
63+
64+
if (!types.isEmpty()) {
65+
TypeCollector.inspect(types).forEach(type -> contributionAction.accept(type, generationContext));
66+
}
67+
}
68+
}

src/main/java/org/springframework/data/aot/AotManagedTypesPostProcessor.java renamed to src/main/java/org/springframework/data/aot/ManagedTypesRegistrationAotProcessor.java

+20-51
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,17 @@
1616
package org.springframework.data.aot;
1717

1818
import java.util.Collections;
19-
import java.util.List;
20-
import java.util.function.BiConsumer;
19+
import java.util.Set;
2120

2221
import org.apache.commons.logging.Log;
2322
import org.apache.commons.logging.LogFactory;
23+
2424
import org.springframework.aot.generate.GenerationContext;
2525
import org.springframework.beans.BeansException;
2626
import org.springframework.beans.factory.BeanFactory;
2727
import org.springframework.beans.factory.BeanFactoryAware;
2828
import org.springframework.beans.factory.aot.BeanRegistrationAotContribution;
2929
import org.springframework.beans.factory.aot.BeanRegistrationAotProcessor;
30-
import org.springframework.beans.factory.aot.BeanRegistrationCode;
3130
import org.springframework.beans.factory.support.RegisteredBean;
3231
import org.springframework.beans.factory.support.RootBeanDefinition;
3332
import org.springframework.core.ResolvableType;
@@ -38,18 +37,18 @@
3837
import org.springframework.util.StringUtils;
3938

4039
/**
41-
* {@link BeanRegistrationAotProcessor} handling {@link #getModulePrefix() prefixed} {@link ManagedTypes} instances.
42-
* This allows to register store specific handling of discovered types.
40+
* {@link BeanRegistrationAotProcessor} handling {@link #getModulePrefix() module prefixed} {@link ManagedTypes}
41+
* instances. This AOT processor allows store specific handling of discovered types to be registered.
4342
*
4443
* @author Christoph Strobl
4544
* @author John Blum
4645
* @see org.springframework.beans.factory.BeanFactoryAware
4746
* @see org.springframework.beans.factory.aot.BeanRegistrationAotProcessor
4847
* @since 3.0
4948
*/
50-
public class AotManagedTypesPostProcessor implements BeanRegistrationAotProcessor, BeanFactoryAware {
49+
public class ManagedTypesRegistrationAotProcessor implements BeanRegistrationAotProcessor, BeanFactoryAware {
5150

52-
private static final Log logger = LogFactory.getLog(AotManagedTypesPostProcessor.class);
51+
private final Log logger = LogFactory.getLog(getClass());
5352

5453
private BeanFactory beanFactory;
5554

@@ -62,6 +61,7 @@ public BeanRegistrationAotContribution processAheadOfTime(@NonNull RegisteredBea
6261
}
6362

6463
@Nullable
64+
@SuppressWarnings("unused")
6565
protected BeanRegistrationAotContribution contribute(@NonNull RootBeanDefinition beanDefinition,
6666
@NonNull Class<?> beanType, @NonNull String beanName) {
6767

@@ -88,11 +88,11 @@ protected boolean matchesPrefix(@Nullable String beanName) {
8888
*
8989
* @param aotContext never {@literal null}.
9090
* @param managedTypes never {@literal null}.
91-
* @return new instance of {@link AotManagedTypesPostProcessor} or {@literal null} if nothing to do.
91+
* @return new instance of {@link ManagedTypesRegistrationAotProcessor} or {@literal null} if nothing to do.
9292
*/
9393
@Nullable
9494
protected BeanRegistrationAotContribution contribute(AotContext aotContext, ManagedTypes managedTypes) {
95-
return new ManagedTypesContribution(aotContext, managedTypes, this::contributeType);
95+
return new ManagedTypesRegistrationAotContribution(aotContext, managedTypes, this::contributeType);
9696
}
9797

9898
/**
@@ -104,59 +104,28 @@ protected BeanRegistrationAotContribution contribute(AotContext aotContext, Mana
104104
protected void contributeType(ResolvableType type, GenerationContext generationContext) {
105105

106106
if (logger.isDebugEnabled()) {
107-
logger.debug(String.format("Contributing type information for [%s].", type.getType()));
107+
logger.debug(String.format("Contributing type information for [%s]", type.getType()));
108108
}
109109

110-
TypeContributor.contribute(type.toClass(), Collections.singleton(TypeContributor.DATA_NAMESPACE), generationContext);
111-
TypeUtils.resolveUsedAnnotations(type.toClass()).forEach(annotation -> TypeContributor
112-
.contribute(annotation.getType(), Collections.singleton(TypeContributor.DATA_NAMESPACE), generationContext));
113-
}
110+
Set<String> annotationNamespaces = Collections.singleton(TypeContributor.DATA_NAMESPACE);
114111

115-
@Nullable
116-
public String getModulePrefix() {
117-
return modulePrefix;
118-
}
112+
TypeContributor.contribute(type.toClass(), annotationNamespaces, generationContext);
119113

120-
public void setModulePrefix(@Nullable String modulePrefix) {
121-
this.modulePrefix = modulePrefix;
114+
TypeUtils.resolveUsedAnnotations(type.toClass()).forEach(annotation ->
115+
TypeContributor.contribute(annotation.getType(), annotationNamespaces, generationContext));
122116
}
123117

124118
@Override
125119
public void setBeanFactory(@NonNull BeanFactory beanFactory) throws BeansException {
126120
this.beanFactory = beanFactory;
127121
}
128122

129-
static class ManagedTypesContribution implements BeanRegistrationAotContribution {
130-
131-
private final AotContext aotContext;
132-
private final ManagedTypes managedTypes;
133-
private final BiConsumer<ResolvableType, GenerationContext> contributionAction;
134-
135-
public ManagedTypesContribution(AotContext aotContext, ManagedTypes managedTypes,
136-
BiConsumer<ResolvableType, GenerationContext> contributionAction) {
137-
138-
this.aotContext = aotContext;
139-
this.managedTypes = managedTypes;
140-
this.contributionAction = contributionAction;
141-
}
142-
143-
public AotContext getAotContext() {
144-
return aotContext;
145-
}
146-
147-
public ManagedTypes getManagedTypes() {
148-
return managedTypes;
149-
}
150-
151-
@Override
152-
public void applyTo(@NonNull GenerationContext generationContext,
153-
@NonNull BeanRegistrationCode beanRegistrationCode) {
154-
155-
List<Class<?>> types = getManagedTypes().toList();
123+
public void setModulePrefix(@Nullable String modulePrefix) {
124+
this.modulePrefix = modulePrefix;
125+
}
156126

157-
if (!types.isEmpty()) {
158-
TypeCollector.inspect(types).forEach(type -> contributionAction.accept(type, generationContext));
159-
}
160-
}
127+
@Nullable
128+
public String getModulePrefix() {
129+
return this.modulePrefix;
161130
}
162131
}

0 commit comments

Comments
 (0)