Skip to content

Commit 9c993e2

Browse files
committed
Polishing.
Reformat code and reorder methods according to visibility. Reduce type and method visibility where applicable. See #2680 Original pull request: #2682.
1 parent e7cc9a6 commit 9c993e2

8 files changed

+124
-100
lines changed

pom.xml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
35

46
<modelVersion>4.0.0</modelVersion>
57

@@ -11,8 +13,11 @@
1113
<description>Core Spring concepts underpinning every Spring Data module.</description>
1214
<url>https://spring.io/projects/spring-data</url>
1315
<scm>
14-
<connection>scm:git:https://github.com/spring-projects/spring-data-commons.git</connection>
15-
<developerConnection>scm:git:[email protected]:spring-projects/spring-data-commons.git</developerConnection>
16+
<connection>scm:git:https://github.com/spring-projects/spring-data-commons.git
17+
</connection>
18+
<developerConnection>
19+
scm:git:[email protected]:spring-projects/spring-data-commons.git
20+
</developerConnection>
1621
<url>https://github.com/spring-projects/spring-data-commons</url>
1722
</scm>
1823
<issueManagement>
@@ -77,6 +82,7 @@
7782
<artifactId>spring-webflux</artifactId>
7883
<optional>true</optional>
7984
</dependency>
85+
8086
<dependency>
8187
<groupId>org.springframework</groupId>
8288
<artifactId>spring-core-test</artifactId>
@@ -121,11 +127,6 @@
121127
<artifactId>reactor-test</artifactId>
122128
<scope>test</scope>
123129
</dependency>
124-
<dependency>
125-
<groupId>org.springframework</groupId>
126-
<artifactId>spring-core-test</artifactId>
127-
<scope>test</scope>
128-
</dependency>
129130

130131
<!-- RxJava -->
131132

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

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ public class ManagedTypesBeanRegistrationAotProcessor implements BeanRegistratio
4848
private final Log logger = LogFactory.getLog(getClass());
4949
private @Nullable String moduleIdentifier;
5050

51+
public void setModuleIdentifier(@Nullable String moduleIdentifier) {
52+
this.moduleIdentifier = moduleIdentifier;
53+
}
54+
55+
@Nullable
56+
public String getModuleIdentifier() {
57+
return this.moduleIdentifier;
58+
}
59+
5160
@Override
5261
public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) {
5362

@@ -59,18 +68,20 @@ public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registe
5968
return contribute(AotContext.from(beanFactory), resolveManagedTypes(registeredBean), registeredBean);
6069
}
6170

62-
ManagedTypes resolveManagedTypes(RegisteredBean registeredBean) {
71+
private ManagedTypes resolveManagedTypes(RegisteredBean registeredBean) {
6372

6473
RootBeanDefinition beanDefinition = registeredBean.getMergedBeanDefinition();
74+
6575
if (beanDefinition.hasConstructorArgumentValues()) {
76+
6677
ValueHolder indexedArgumentValue = beanDefinition.getConstructorArgumentValues().getIndexedArgumentValue(0, null);
6778
Object value = indexedArgumentValue.getValue();
68-
if (value instanceof Collection<?> values) {
69-
if (values.stream().allMatch(it -> it instanceof Class)) {
70-
return ManagedTypes.fromIterable((Collection<Class<?>>) values);
71-
}
79+
80+
if (value instanceof Collection<?> values && values.stream().allMatch(it -> it instanceof Class)) {
81+
return ManagedTypes.fromIterable((Collection<Class<?>>) values);
7282
}
7383
}
84+
7485
if (logger.isDebugEnabled()) {
7586
logger.debug(
7687
String.format("ManagedTypes BeanDefinition '%s' does serve arguments. Trying to resolve bean instance.",
@@ -93,29 +104,18 @@ ManagedTypes resolveManagedTypes(RegisteredBean registeredBean) {
93104
return ManagedTypes.empty();
94105
}
95106

96-
protected boolean isMatch(@Nullable Class<?> beanType, @Nullable String beanName) {
97-
return matchesByType(beanType) && matchesPrefix(beanName);
98-
}
99-
100-
protected boolean matchesByType(@Nullable Class<?> beanType) {
101-
return beanType != null && ClassUtils.isAssignable(ManagedTypes.class, beanType);
102-
}
103-
104-
protected boolean matchesPrefix(@Nullable String beanName) {
105-
return StringUtils.startsWithIgnoreCase(beanName, getModuleIdentifier());
106-
}
107-
108107
/**
109108
* Hook to provide a customized flavor of {@link BeanRegistrationAotContribution}. By overriding this method calls to
110109
* {@link #contributeType(ResolvableType, GenerationContext)} might no longer be issued.
111110
*
112111
* @param aotContext never {@literal null}.
113112
* @param managedTypes never {@literal null}.
114-
* @return new instance of {@link ManagedTypesBeanRegistrationAotProcessor} or {@literal null} if nothing to do.
113+
* @return new instance of {@link BeanRegistrationAotContribution} or {@literal null} if nothing to do.
115114
*/
116115
@Nullable
117-
protected BeanRegistrationAotContribution contribute(AotContext aotContext, ManagedTypes managedTypes, RegisteredBean registeredBean) {
118-
return new ManagedTypesRegistrationAotContribution(aotContext, managedTypes, registeredBean, this::contributeType);
116+
protected BeanRegistrationAotContribution contribute(AotContext aotContext, ManagedTypes managedTypes,
117+
RegisteredBean registeredBean) {
118+
return new ManagedTypesRegistrationAotContribution(managedTypes, registeredBean, this::contributeType);
119119
}
120120

121121
/**
@@ -138,12 +138,15 @@ protected void contributeType(ResolvableType type, GenerationContext generationC
138138
annotation -> TypeContributor.contribute(annotation.getType(), annotationNamespaces, generationContext));
139139
}
140140

141-
public void setModuleIdentifier(@Nullable String moduleIdentifier) {
142-
this.moduleIdentifier = moduleIdentifier;
141+
protected boolean isMatch(@Nullable Class<?> beanType, @Nullable String beanName) {
142+
return matchesByType(beanType) && matchesPrefix(beanName);
143143
}
144144

145-
@Nullable
146-
public String getModuleIdentifier() {
147-
return this.moduleIdentifier;
145+
protected boolean matchesByType(@Nullable Class<?> beanType) {
146+
return beanType != null && ClassUtils.isAssignable(ManagedTypes.class, beanType);
147+
}
148+
149+
protected boolean matchesPrefix(@Nullable String beanName) {
150+
return StringUtils.startsWithIgnoreCase(beanName, getModuleIdentifier());
148151
}
149152
}

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

Lines changed: 64 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -50,51 +50,42 @@
5050
* {@link BeanRegistrationAotContribution#customizeBeanRegistrationCodeFragments(GenerationContext, BeanRegistrationCodeFragments)}.
5151
* The generated code resolves potential factory methods accepting either a {@link ManagedTypes} instance, or a
5252
* {@link List} of either {@link Class} or {@link String} (classname) values.
53-
*
54-
* <pre>
55-
* <code>
53+
*
54+
* <pre class="code">
5655
* public static InstanceSupplier&lt;ManagedTypes&gt; instance() {
5756
* return (registeredBean) -> {
5857
* var types = List.of("com.example.A", "com.example.B");
5958
* return ManagedTypes.ofStream(types.stream().map(it -> ClassUtils.forName(it, registeredBean.getBeanFactory().getBeanClassLoader())));
6059
* }
6160
* }
62-
* </code>
6361
* </pre>
6462
*
6563
* @author John Blum
6664
* @author Christoph Strobl
65+
* @author Mark Paluch
6766
* @see org.springframework.beans.factory.aot.BeanRegistrationAotContribution
68-
* @since 3.0.0
67+
* @since 3.0
6968
*/
70-
public class ManagedTypesRegistrationAotContribution implements RegisteredBeanAotContribution {
69+
class ManagedTypesRegistrationAotContribution implements RegisteredBeanAotContribution {
7170

72-
private final AotContext aotContext;
7371
private final ManagedTypes managedTypes;
72+
private final Lazy<List<Class<?>>> sourceTypes;
7473
private final BiConsumer<ResolvableType, GenerationContext> contributionAction;
7574
private final RegisteredBean source;
7675

77-
public ManagedTypesRegistrationAotContribution(AotContext aotContext, @Nullable ManagedTypes managedTypes,
78-
RegisteredBean registeredBean, BiConsumer<ResolvableType, GenerationContext> contributionAction) {
76+
public ManagedTypesRegistrationAotContribution(ManagedTypes managedTypes, RegisteredBean registeredBean,
77+
BiConsumer<ResolvableType, GenerationContext> contributionAction) {
7978

80-
this.aotContext = aotContext;
8179
this.managedTypes = managedTypes;
80+
this.sourceTypes = Lazy.of(managedTypes::toList);
8281
this.contributionAction = contributionAction;
8382
this.source = registeredBean;
8483
}
8584

86-
protected AotContext getAotContext() {
87-
return this.aotContext;
88-
}
89-
90-
protected ManagedTypes getManagedTypes() {
91-
return managedTypes == null ? ManagedTypes.empty() : managedTypes;
92-
}
93-
9485
@Override
9586
public void applyTo(GenerationContext generationContext, BeanRegistrationCode beanRegistrationCode) {
9687

97-
List<Class<?>> types = getManagedTypes().toList();
88+
List<Class<?>> types = sourceTypes.get();
9889

9990
if (!types.isEmpty()) {
10091
TypeCollector.inspect(types).forEach(type -> contributionAction.accept(type, generationContext));
@@ -109,7 +100,7 @@ public BeanRegistrationCodeFragments customizeBeanRegistrationCodeFragments(Gene
109100
return codeFragments;
110101
}
111102

112-
ManagedTypesInstanceCodeFragment fragment = new ManagedTypesInstanceCodeFragment(getManagedTypes(), source,
103+
ManagedTypesInstanceCodeFragment fragment = new ManagedTypesInstanceCodeFragment(sourceTypes.get(), source,
113104
codeFragments);
114105
return fragment.canGenerateCode() ? fragment : codeFragments;
115106
}
@@ -119,32 +110,27 @@ public RegisteredBean getSource() {
119110
return source;
120111
}
121112

113+
/**
114+
* Class used to generate the fragment of code needed to define a {@link ManagedTypes} bean from previously discovered
115+
* managed types.
116+
*/
122117
static class ManagedTypesInstanceCodeFragment extends BeanRegistrationCodeFragments {
123118

124-
private ManagedTypes sourceTypes;
125-
private RegisteredBean source;
126-
private Lazy<Method> instanceMethod = Lazy.of(this::findInstanceFactory);
119+
public static final ResolvableType LIST_TYPE = ResolvableType.forType(List.class);
120+
public static final ResolvableType MANAGED_TYPES_TYPE = ResolvableType.forType(ManagedTypes.class);
121+
private final List<Class<?>> sourceTypes;
122+
private final RegisteredBean source;
123+
private final Lazy<Method> instanceMethod = Lazy.of(this::findInstanceFactory);
127124

128-
protected ManagedTypesInstanceCodeFragment(ManagedTypes managedTypes, RegisteredBean source,
125+
protected ManagedTypesInstanceCodeFragment(List<Class<?>> sourceTypes, RegisteredBean source,
129126
BeanRegistrationCodeFragments codeFragments) {
130127

131128
super(codeFragments);
132129

133-
this.sourceTypes = managedTypes;
130+
this.sourceTypes = sourceTypes;
134131
this.source = source;
135132
}
136133

137-
/**
138-
* @return {@literal true} if the instance method code can be generated. {@literal false} otherwise.
139-
*/
140-
boolean canGenerateCode() {
141-
142-
if (ObjectUtils.nullSafeEquals(source.getBeanClass(), ManagedTypes.class)) {
143-
return true;
144-
}
145-
return instanceMethod.getNullable() != null;
146-
}
147-
148134
@Override
149135
public CodeBlock generateInstanceSupplierCode(GenerationContext generationContext,
150136
BeanRegistrationCode beanRegistrationCode, Executable constructorOrFactoryMethod,
@@ -156,33 +142,20 @@ public CodeBlock generateInstanceSupplierCode(GenerationContext generationContex
156142
return CodeBlock.of("$T.$L()", beanRegistrationCode.getClassName(), generatedMethod.getName());
157143
}
158144

159-
private CodeBlock toCodeBlock(List<Class<?>> values, boolean allPublic) {
145+
/**
146+
* @return {@literal true} if the instance method code can be generated. {@literal false} otherwise.
147+
*/
148+
boolean canGenerateCode() {
160149

161-
if (allPublic) {
162-
return CodeBlock.join(values.stream().map(value -> CodeBlock.of("$T.class", value)).toList(), ", ");
150+
if (ObjectUtils.nullSafeEquals(source.getBeanClass(), ManagedTypes.class)) {
151+
return true;
163152
}
164-
return CodeBlock.join(values.stream().map(value -> CodeBlock.of("$S", value.getName())).toList(), ", ");
165-
}
166-
167-
private Method findInstanceFactory() {
168-
169-
for (Method beanMethod : ReflectionUtils.getDeclaredMethods(source.getBeanClass())) {
170153

171-
if (beanMethod.getParameterCount() == 1 && java.lang.reflect.Modifier.isPublic(beanMethod.getModifiers())
172-
&& java.lang.reflect.Modifier.isStatic(beanMethod.getModifiers())) {
173-
ResolvableType parameterType = ResolvableType.forMethodParameter(beanMethod, 0, source.getBeanClass());
174-
if (parameterType.isAssignableFrom(ResolvableType.forType(List.class))
175-
|| parameterType.isAssignableFrom(ResolvableType.forType(ManagedTypes.class))) {
176-
return beanMethod;
177-
}
178-
}
179-
}
180-
return null;
154+
return instanceMethod.getNullable() != null;
181155
}
182156

183157
void generateInstanceFactory(Builder method) {
184158

185-
List<Class<?>> sourceTypes = this.sourceTypes.toList();
186159
boolean allSourceTypesVisible = sourceTypes.stream()
187160
.allMatch(it -> AccessVisibility.PUBLIC.equals(AccessVisibility.forClass(it)));
188161

@@ -207,6 +180,7 @@ void generateInstanceFactory(Builder method) {
207180
.addStatement("throw new $T($S, e)", IllegalArgumentException.class, "Cannot to load type").endControlFlow()
208181
.endControlFlow("))").build());
209182
}
183+
210184
if (ObjectUtils.nullSafeEquals(source.getBeanClass(), ManagedTypes.class)) {
211185
builder.add("return managedTypes");
212186
} else {
@@ -221,8 +195,42 @@ void generateInstanceFactory(Builder method) {
221195
instanceFactoryMethod.getName(), "managedTypes");
222196
}
223197
}
198+
224199
builder.endControlFlow(")");
225200
method.addCode(builder.build());
226201
}
202+
203+
private CodeBlock toCodeBlock(List<Class<?>> values, boolean allPublic) {
204+
205+
if (allPublic) {
206+
return CodeBlock.join(values.stream().map(value -> CodeBlock.of("$T.class", value)).toList(), ", ");
207+
}
208+
return CodeBlock.join(values.stream().map(value -> CodeBlock.of("$S", value.getName())).toList(), ", ");
209+
}
210+
211+
@Nullable
212+
private Method findInstanceFactory() {
213+
214+
for (Method beanMethod : ReflectionUtils.getDeclaredMethods(source.getBeanClass())) {
215+
216+
if (!isInstanceFactory(beanMethod)) {
217+
continue;
218+
}
219+
220+
ResolvableType parameterType = ResolvableType.forMethodParameter(beanMethod, 0, source.getBeanClass());
221+
222+
if (parameterType.isAssignableFrom(LIST_TYPE) || parameterType.isAssignableFrom(MANAGED_TYPES_TYPE)) {
223+
return beanMethod;
224+
}
225+
}
226+
227+
return null;
228+
}
229+
230+
private static boolean isInstanceFactory(Method beanMethod) {
231+
return beanMethod.getParameterCount() == 1 //
232+
&& java.lang.reflect.Modifier.isPublic(beanMethod.getModifiers()) //
233+
&& java.lang.reflect.Modifier.isStatic(beanMethod.getModifiers());
234+
}
227235
}
228236
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,16 @@
1919
import org.springframework.beans.factory.support.RegisteredBean;
2020

2121
/**
22+
* Extension to {@link BeanRegistrationAotContribution} that bases its contribution on a {@link RegisteredBean}. This
23+
* interface exposes its {@link #getSource() source}.
24+
*
2225
* @author Christoph Strobl
26+
* @since 3.0
2327
*/
2428
public interface RegisteredBeanAotContribution extends BeanRegistrationAotContribution {
2529

30+
/**
31+
* @return the source {@link RegisteredBean}.
32+
*/
2633
RegisteredBean getSource();
2734
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
*
3737
* @author Christoph Strobl
3838
* @author John Blum
39-
* @since 3.0.0
39+
* @since 3.0
4040
*/
4141
class RepositoryBeanDefinitionReader {
4242

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
*
6262
* @author Christoph Strobl
6363
* @author John Blum
64-
* @since 3.0.0
64+
* @since 3.0
6565
*/
6666
@SuppressWarnings("unused")
6767
public class RepositoryRegistrationAotProcessor implements BeanRegistrationAotProcessor, BeanFactoryAware {

src/test/java/org/springframework/data/aot/AotTestCodeContributionBuilder.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import org.springframework.javapoet.ParameterizedTypeName;
3232

3333
/**
34+
* Test utility to build and compile AOT code contributions.
35+
*
3436
* @author Christoph Strobl
3537
*/
3638
public class AotTestCodeContributionBuilder {

0 commit comments

Comments
 (0)