Skip to content

Commit da9e087

Browse files
committed
Reduce API surface, expose interfaces only.
1 parent b219871 commit da9e087

File tree

8 files changed

+216
-107
lines changed

8 files changed

+216
-107
lines changed

src/main/java/org/springframework/data/repository/aot/generate/AotRepositoryBuilder.java

+20-28
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.LinkedHashMap;
2323
import java.util.List;
2424
import java.util.Map;
25+
import java.util.function.Consumer;
2526

2627
import javax.lang.model.element.Modifier;
2728

@@ -43,6 +44,7 @@
4344
import org.springframework.javapoet.MethodSpec;
4445
import org.springframework.javapoet.TypeName;
4546
import org.springframework.javapoet.TypeSpec;
47+
import org.springframework.util.Assert;
4648

4749
/**
4850
* Builder for AOT repository fragments.
@@ -59,9 +61,9 @@ class AotRepositoryBuilder {
5961
private final ProjectionFactory projectionFactory;
6062
private final AotRepositoryFragmentMetadata generationMetadata;
6163

62-
private @Nullable ConstructorCustomizer constructorCustomizer;
64+
private @Nullable Consumer<AotRepositoryConstructorBuilder> constructorCustomizer;
6365
private @Nullable MethodContributorFactory methodContributorFactory;
64-
private ClassCustomizer customizer;
66+
private Consumer<AotRepositoryClassBuilder> classCustomizer;
6567

6668
private AotRepositoryBuilder(RepositoryInformation repositoryInformation, String moduleName,
6769
ProjectionFactory projectionFactory) {
@@ -76,7 +78,7 @@ private AotRepositoryBuilder(RepositoryInformation repositoryInformation, String
7678
.initializer("$T.getLog($T.class)", TypeName.get(LogFactory.class), this.generationMetadata.getTargetTypeName())
7779
.build());
7880

79-
this.customizer = (info, builder) -> {};
81+
this.classCustomizer = (builder) -> {};
8082
}
8183

8284
/**
@@ -93,14 +95,14 @@ public static AotRepositoryBuilder forRepository(RepositoryInformation informati
9395
}
9496

9597
/**
96-
* Configure a {@link ClassCustomizer} customizer.
98+
* Configure a {@link AotRepositoryConstructorBuilder} customizer.
9799
*
98100
* @param classCustomizer must not be {@literal null}.
99101
* @return {@code this}.
100102
*/
101-
public AotRepositoryBuilder withClassCustomizer(ClassCustomizer classCustomizer) {
103+
public AotRepositoryBuilder withClassCustomizer(Consumer<AotRepositoryClassBuilder> classCustomizer) {
102104

103-
this.customizer = classCustomizer;
105+
this.classCustomizer = classCustomizer;
104106
return this;
105107
}
106108

@@ -110,7 +112,8 @@ public AotRepositoryBuilder withClassCustomizer(ClassCustomizer classCustomizer)
110112
* @param constructorCustomizer must not be {@literal null}.
111113
* @return {@code this}.
112114
*/
113-
public AotRepositoryBuilder withConstructorCustomizer(ConstructorCustomizer constructorCustomizer) {
115+
public AotRepositoryBuilder withConstructorCustomizer(
116+
Consumer<AotRepositoryConstructorBuilder> constructorCustomizer) {
114117

115118
this.constructorCustomizer = constructorCustomizer;
116119
return this;
@@ -162,7 +165,12 @@ public AotBundle build() {
162165
generationMetadata.getFields().values().forEach(builder::addField);
163166

164167
// finally customize the file itself
165-
this.customizer.customize(repositoryInformation, builder);
168+
this.classCustomizer.accept(customizer -> {
169+
170+
Assert.notNull(customizer, "ClassCustomizer must not be null");
171+
customizer.customize(builder);
172+
});
173+
166174
JavaFile javaFile = JavaFile.builder(packageName(), builder.build()).build();
167175
AotRepositoryMetadata metadata = getAotRepositoryMetadata(methodMetadata);
168176

@@ -171,11 +179,11 @@ public AotBundle build() {
171179

172180
private MethodSpec buildConstructor() {
173181

174-
AotRepositoryConstructorBuilder constructorBuilder = new AotRepositoryConstructorBuilder(repositoryInformation,
182+
RepositoryConstructorBuilder constructorBuilder = new RepositoryConstructorBuilder(
175183
generationMetadata);
176184

177185
if (constructorCustomizer != null) {
178-
constructorCustomizer.customize(constructorBuilder);
186+
constructorCustomizer.accept(constructorBuilder);
179187
}
180188

181189
return constructorBuilder.buildConstructor();
@@ -213,8 +221,7 @@ private void contributeMethod(Method method, RepositoryComposition repositoryCom
213221

214222
if (repositoryInformation.isQueryMethod(method) && methodContributorFactory != null) {
215223

216-
MethodContributor<? extends QueryMethod> contributor = methodContributorFactory.create(method,
217-
repositoryInformation);
224+
MethodContributor<? extends QueryMethod> contributor = methodContributorFactory.create(method);
218225

219226
if (contributor != null) {
220227

@@ -273,20 +280,6 @@ public ProjectionFactory getProjectionFactory() {
273280
return projectionFactory;
274281
}
275282

276-
/**
277-
* Customizer interface to customize the AOT repository fragment class after it has been defined.
278-
*/
279-
public interface ClassCustomizer {
280-
281-
/**
282-
* Apply customization ot the AOT repository fragment class after it has been defined.
283-
*
284-
* @param information the repository information that is used for the AOT fragment.
285-
* @param builder the class builder to be customized.
286-
*/
287-
void customize(RepositoryInformation information, TypeSpec.Builder builder);
288-
289-
}
290283

291284
/**
292285
* Customizer interface to customize the AOT repository fragment constructor through
@@ -313,12 +306,11 @@ public interface MethodContributorFactory {
313306
* Apply customization ot the AOT repository fragment constructor.
314307
*
315308
* @param method the method to be contributed.
316-
* @param information repository information.
317309
* @return the {@link MethodContributor} to be used. Can be {@literal null} if the method and method metadata should
318310
* not be contributed.
319311
*/
320312
@Nullable
321-
MethodContributor<? extends QueryMethod> create(Method method, RepositoryInformation information);
313+
MethodContributor<? extends QueryMethod> create(Method method);
322314

323315
}
324316

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2025 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+
* https://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.repository.aot.generate;
17+
18+
import org.springframework.javapoet.TypeSpec;
19+
20+
/**
21+
* Builder for AOT repository fragment classes.
22+
*
23+
* @author Mark Paluch
24+
* @since 4.0
25+
*/
26+
public interface AotRepositoryClassBuilder {
27+
28+
/**
29+
* Add a class customizer. Customizer is invoked after building the class.
30+
*
31+
* @param customizer the customizer with direct access to the {@link TypeSpec.Builder type builder}.
32+
*/
33+
void customize(ClassCustomizer customizer);
34+
35+
/**
36+
* Customizer interface to customize the AOT repository fragment class after it has been defined.
37+
*/
38+
interface ClassCustomizer {
39+
40+
/**
41+
* Apply customization ot the AOT repository fragment class after it has been defined.
42+
*
43+
* @param builder the class builder to be customized.
44+
*/
45+
void customize(TypeSpec.Builder builder);
46+
47+
}
48+
49+
}

src/main/java/org/springframework/data/repository/aot/generate/AotRepositoryConstructorBuilder.java

+7-64
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,7 @@
1515
*/
1616
package org.springframework.data.repository.aot.generate;
1717

18-
import java.util.Map.Entry;
19-
20-
import javax.lang.model.element.Modifier;
21-
22-
import org.springframework.core.ResolvableType;
23-
import org.springframework.data.repository.aot.generate.AotRepositoryFragmentMetadata.ConstructorArgument;
24-
import org.springframework.data.repository.core.RepositoryInformation;
2518
import org.springframework.javapoet.MethodSpec;
26-
import org.springframework.javapoet.ParameterizedTypeName;
2719
import org.springframework.javapoet.TypeName;
2820

2921
/**
@@ -33,43 +25,23 @@
3325
* @author Mark Paluch
3426
* @since 4.0
3527
*/
36-
public class AotRepositoryConstructorBuilder {
37-
38-
private final RepositoryInformation repositoryInformation;
39-
private final AotRepositoryFragmentMetadata metadata;
40-
41-
private ConstructorCustomizer customizer = (info, builder) -> {};
42-
43-
AotRepositoryConstructorBuilder(RepositoryInformation repositoryInformation, AotRepositoryFragmentMetadata metadata) {
44-
45-
this.repositoryInformation = repositoryInformation;
46-
this.metadata = metadata;
47-
}
28+
public interface AotRepositoryConstructorBuilder {
4829

4930
/**
5031
* Add constructor parameter and create a field storing its value.
5132
*
5233
* @param parameterName name of the parameter.
5334
* @param type parameter type.
5435
*/
55-
public void addParameter(String parameterName, Class<?> type) {
56-
57-
ResolvableType resolvableType = ResolvableType.forClass(type);
58-
if (!resolvableType.hasGenerics() || !resolvableType.hasResolvableGenerics()) {
59-
addParameter(parameterName, TypeName.get(type));
60-
return;
61-
}
62-
63-
addParameter(parameterName, ParameterizedTypeName.get(type, resolvableType.resolveGenerics()));
64-
}
36+
void addParameter(String parameterName, Class<?> type);
6537

6638
/**
6739
* Add constructor parameter and create a field storing its value.
6840
*
6941
* @param parameterName name of the parameter.
7042
* @param type parameter type.
7143
*/
72-
public void addParameter(String parameterName, TypeName type) {
44+
default void addParameter(String parameterName, TypeName type) {
7345
addParameter(parameterName, type, true);
7446
}
7547

@@ -80,56 +52,27 @@ public void addParameter(String parameterName, TypeName type) {
8052
* @param type parameter type.
8153
* @param createField whether to create a field for the parameter and assign its value to the field.
8254
*/
83-
public void addParameter(String parameterName, TypeName type, boolean createField) {
84-
85-
this.metadata.addConstructorArgument(parameterName, type, createField ? parameterName : null);
86-
87-
if (createField) {
88-
this.metadata.addField(parameterName, type, Modifier.PRIVATE, Modifier.FINAL);
89-
}
90-
}
55+
void addParameter(String parameterName, TypeName type, boolean createField);
9156

9257
/**
9358
* Add constructor customizer. Customizer is invoked after adding constructor arguments and before assigning
9459
* constructor arguments to fields.
9560
*
9661
* @param customizer the customizer with direct access to the {@link MethodSpec.Builder constructor builder}.
9762
*/
98-
public void customize(ConstructorCustomizer customizer) {
99-
this.customizer = customizer;
100-
}
101-
102-
MethodSpec buildConstructor() {
103-
104-
MethodSpec.Builder builder = MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC);
105-
106-
for (Entry<String, ConstructorArgument> parameter : this.metadata.getConstructorArguments().entrySet()) {
107-
builder.addParameter(parameter.getValue().typeName(), parameter.getKey());
108-
}
109-
110-
customizer.customize(repositoryInformation, builder);
111-
112-
for (Entry<String, ConstructorArgument> parameter : this.metadata.getConstructorArguments().entrySet()) {
113-
if (parameter.getValue().isForLocalField()) {
114-
builder.addStatement("this.$N = $N", parameter.getKey(), parameter.getKey());
115-
}
116-
}
117-
118-
return builder.build();
119-
}
63+
void customize(ConstructorCustomizer customizer);
12064

12165
/**
12266
* Customizer for the AOT repository constructor.
12367
*/
124-
public interface ConstructorCustomizer {
68+
interface ConstructorCustomizer {
12569

12670
/**
12771
* Customize the constructor.
12872
*
129-
* @param information the repository information that is used for the AOT fragment.
13073
* @param builder the constructor builder to be customized.
13174
*/
132-
void customize(RepositoryInformation information, MethodSpec.Builder builder);
75+
void customize(MethodSpec.Builder builder);
13376

13477
}
13578

0 commit comments

Comments
 (0)