Skip to content

Commit db69d21

Browse files
committed
Polishing.
Remove CodeBlocks, reduce visibility where possible.
1 parent ee868ae commit db69d21

File tree

6 files changed

+93
-168
lines changed

6 files changed

+93
-168
lines changed

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

Lines changed: 58 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@
2020
import java.util.ArrayList;
2121
import java.util.List;
2222

23-
import javax.lang.model.element.Modifier;
24-
2523
import org.jspecify.annotations.Nullable;
24+
2625
import org.springframework.core.ResolvableType;
2726
import org.springframework.core.annotation.MergedAnnotation;
2827
import org.springframework.core.annotation.MergedAnnotationSelectors;
@@ -31,8 +30,6 @@
3130
import org.springframework.data.repository.query.Parameter;
3231
import org.springframework.data.repository.query.QueryMethod;
3332
import org.springframework.data.repository.query.ReturnedType;
34-
import org.springframework.javapoet.FieldSpec;
35-
import org.springframework.javapoet.ParameterSpec;
3633
import org.springframework.javapoet.TypeName;
3734
import org.springframework.util.ObjectUtils;
3835

@@ -51,7 +48,6 @@ public class AotQueryMethodGenerationContext {
5148
private final RepositoryInformation repositoryInformation;
5249
private final AotRepositoryFragmentMetadata targetTypeMetadata;
5350
private final MethodMetadata targetMethodMetadata;
54-
private final CodeBlocks codeBlocks;
5551
private final VariableNameFactory variableNameFactory;
5652

5753
AotQueryMethodGenerationContext(RepositoryInformation repositoryInformation, Method method, QueryMethod queryMethod,
@@ -64,11 +60,6 @@ public class AotQueryMethodGenerationContext {
6460
this.targetTypeMetadata = targetTypeMetadata;
6561
this.targetMethodMetadata = new MethodMetadata(repositoryInformation, method);
6662
this.variableNameFactory = LocalVariableNameFactory.forMethod(targetMethodMetadata);
67-
this.codeBlocks = new CodeBlocks(targetTypeMetadata);
68-
}
69-
70-
AotRepositoryFragmentMetadata getTargetTypeMetadata() {
71-
return targetTypeMetadata;
7263
}
7364

7465
MethodMetadata getTargetMethodMetadata() {
@@ -79,12 +70,18 @@ public RepositoryInformation getRepositoryInformation() {
7970
return repositoryInformation;
8071
}
8172

82-
public Method getMethod() {
83-
return method;
73+
/**
74+
* Obtain the field name by type.
75+
*
76+
* @param type
77+
* @return
78+
*/
79+
public @Nullable String fieldNameOf(Class<?> type) {
80+
return targetTypeMetadata.fieldNameOf(type);
8481
}
8582

86-
public CodeBlocks codeBlocks() {
87-
return codeBlocks;
83+
public Method getMethod() {
84+
return method;
8885
}
8986

9087
/**
@@ -112,10 +109,18 @@ public ReturnedType getReturnedType() {
112109
return queryMethod.getResultProcessor().getReturnedType();
113110
}
114111

112+
/**
113+
* @return the actual returned domain type.
114+
* @see org.springframework.data.repository.core.RepositoryMetadata#getReturnedDomainClass(Method)
115+
*/
115116
public ResolvableType getActualReturnType() {
116117
return targetMethodMetadata.getActualReturnType();
117118
}
118119

120+
/**
121+
* @return the query method return type.
122+
* @see org.springframework.data.repository.core.RepositoryMetadata#getReturnType(Method)
123+
*/
119124
public ResolvableType getReturnType() {
120125
return targetMethodMetadata.getReturnType();
121126
}
@@ -127,24 +132,13 @@ public TypeName getReturnTypeName() {
127132
return TypeName.get(getReturnType().getType());
128133
}
129134

130-
/**
131-
* Obtain a naming-clash free variant for the given logical variable name within the local method context. Returns the
132-
* target variable name when called multiple times with the same {@code variableName}.
133-
*
134-
* @param variableName the logical variable name.
135-
* @return the variable name used in the generated code.
136-
*/
137-
public String localVariable(String variableName) {
138-
return targetMethodMetadata.getLocalVariables().computeIfAbsent(variableName, variableNameFactory::generateName);
139-
}
140-
141135
/**
142136
* Returns the required parameter name for the {@link Parameter#isBindable() bindable parameter} at the given
143137
* {@code parameterIndex} or throws {@link IllegalArgumentException} if the parameter cannot be determined by its
144138
* index.
145139
*
146140
* @param parameterIndex the zero-based parameter index as used in the query to reference bindable parameters.
147-
* @return the parameter name.
141+
* @return the method parameter name.
148142
*/
149143
public String getRequiredBindableParameterName(int parameterIndex) {
150144

@@ -162,9 +156,8 @@ public String getRequiredBindableParameterName(int parameterIndex) {
162156
* {@code parameterIndex} or {@code null} if the parameter cannot be determined by its index.
163157
*
164158
* @param parameterIndex the zero-based parameter index as used in the query to reference bindable parameters.
165-
* @return the parameter name.
159+
* @return the method parameter name.
166160
*/
167-
// TODO: Simplify?!
168161
public @Nullable String getBindableParameterName(int parameterIndex) {
169162

170163
int bindable = 0;
@@ -186,12 +179,12 @@ public String getRequiredBindableParameterName(int parameterIndex) {
186179
}
187180

188181
/**
189-
* Returns the required parameter name for the {@link Parameter#isBindable() bindable parameter} at the given
190-
* {@code parameterName} or throws {@link IllegalArgumentException} if the parameter cannot be determined by its
191-
* index.
182+
* Returns the required parameter name for the {@link Parameter#isBindable() bindable parameter} at the given logical
183+
* {@code parameterName} or throws {@link IllegalArgumentException} if the parameter cannot be determined by its name.
192184
*
193185
* @param parameterName the parameter name as used in the query to reference bindable parameters.
194-
* @return the parameter name.
186+
* @return the method parameter name.
187+
* @see org.springframework.data.repository.query.Param
195188
*/
196189
public String getRequiredBindableParameterName(String parameterName) {
197190

@@ -205,13 +198,13 @@ public String getRequiredBindableParameterName(String parameterName) {
205198
}
206199

207200
/**
208-
* Returns the required parameter name for the {@link Parameter#isBindable() bindable parameter} at the given
209-
* {@code parameterName} or {@code null} if the parameter cannot be determined by its index.
201+
* Returns the required parameter name for the {@link Parameter#isBindable() bindable parameter} at the given logical
202+
* {@code parameterName} or {@code null} if the parameter cannot be determined by its name.
210203
*
211204
* @param parameterName the parameter name as used in the query to reference bindable parameters.
212-
* @return the parameter name.
205+
* @return the method parameter name.
206+
* @see org.springframework.data.repository.query.Param
213207
*/
214-
// TODO: Simplify?!
215208
public @Nullable String getBindableParameterName(String parameterName) {
216209

217210
int totalIndex = 0;
@@ -238,7 +231,7 @@ public List<String> getBindableParameterNames() {
238231
List<String> result = new ArrayList<>();
239232

240233
for (Parameter parameter : queryMethod.getParameters().getBindableParameters()) {
241-
getParameterName(parameter.getIndex());
234+
result.add(getParameterName(parameter.getIndex()));
242235
}
243236

244237
return result;
@@ -251,45 +244,50 @@ public List<String> getAllParameterNames() {
251244
return targetMethodMetadata.getMethodArguments().keySet().stream().toList();
252245
}
253246

254-
public boolean hasField(String fieldName) {
255-
return targetTypeMetadata.hasField(fieldName);
256-
}
257-
258-
public void addField(String fieldName, TypeName type, Modifier... modifiers) {
259-
targetTypeMetadata.addField(fieldName, type, modifiers);
260-
}
261-
262-
public void addField(FieldSpec fieldSpec) {
263-
targetTypeMetadata.addField(fieldSpec);
264-
}
265-
266-
public @Nullable String fieldNameOf(Class<?> type) {
267-
return targetTypeMetadata.fieldNameOf(type);
268-
}
269-
270-
@Nullable
271-
public String getParameterNameOf(Class<?> type) {
272-
return targetMethodMetadata.getParameterNameOf(type);
247+
/**
248+
* Obtain a naming-clash free variant for the given logical variable name within the local method context. Returns the
249+
* target variable name when called multiple times with the same {@code variableName}.
250+
*
251+
* @param variableName the logical variable name.
252+
* @return the variable name used in the generated code.
253+
*/
254+
public String localVariable(String variableName) {
255+
return targetMethodMetadata.getLocalVariables().computeIfAbsent(variableName, variableNameFactory::generateName);
273256
}
274257

258+
/**
259+
* Returns the parameter name for the method parameter at {@code position}.
260+
*
261+
* @param position zero-indexed parameter position.
262+
* @return
263+
* @see Method#getParameters()
264+
*/
275265
public @Nullable String getParameterName(int position) {
276266
return targetMethodMetadata.getParameterName(position);
277267
}
278268

279-
public void addParameter(ParameterSpec parameter) {
280-
this.targetMethodMetadata.addParameter(parameter);
281-
}
282-
269+
/**
270+
* @return the parameter name for the {@link org.springframework.data.domain.Sort sort parameter} or {@code null} if
271+
* the method does not declare a sort parameter.
272+
*/
283273
@Nullable
284274
public String getSortParameterName() {
285275
return getParameterName(queryMethod.getParameters().getSortIndex());
286276
}
287277

278+
/**
279+
* @return the parameter name for the {@link org.springframework.data.domain.Pageable pageable parameter} or
280+
* {@code null} if the method does not declare a pageable parameter.
281+
*/
288282
@Nullable
289283
public String getPageableParameterName() {
290284
return getParameterName(queryMethod.getParameters().getPageableIndex());
291285
}
292286

287+
/**
288+
* @return the parameter name for the {@link org.springframework.data.domain.Limit limit parameter} or {@code null} if
289+
* the method does not declare a limit parameter.
290+
*/
293291
@Nullable
294292
public String getLimitParameterName() {
295293
return getParameterName(queryMethod.getParameters().getLimitIndex());

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ private AotRepositoryBuilder(RepositoryInformation repositoryInformation, Projec
7373
.initializer("$T.getLog($T.class)", TypeName.get(LogFactory.class), this.generationMetadata.getTargetTypeName())
7474
.build());
7575

76-
this.customizer = (info, metadata, builder) -> {};
76+
this.customizer = (info, builder) -> {};
7777
}
7878

7979
public static <M extends QueryMethod> AotRepositoryBuilder forRepository(RepositoryInformation repositoryInformation,
@@ -144,7 +144,7 @@ public AotBundle build() {
144144
generationMetadata.getFields().values().forEach(builder::addField);
145145

146146
// finally customize the file itself
147-
this.customizer.customize(repositoryInformation, generationMetadata, builder);
147+
this.customizer.customize(repositoryInformation, builder);
148148
JavaFile javaFile = JavaFile.builder(packageName(), builder.build()).build();
149149

150150
// TODO: module identifier
@@ -242,11 +242,10 @@ public interface ClassCustomizer {
242242
/**
243243
* Apply customization ot the AOT repository fragment class after it has been defined..
244244
*
245-
* @param information
246-
* @param metadata
247-
* @param builder
245+
* @param information the repository information that is used for the AOT fragment.
246+
* @param builder the class builder to be customized.
248247
*/
249-
void customize(RepositoryInformation information, AotRepositoryFragmentMetadata metadata, TypeSpec.Builder builder);
248+
void customize(RepositoryInformation information, TypeSpec.Builder builder);
250249

251250
}
252251

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

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,24 @@
3333
* @author Mark Paluch
3434
* @since 4.0
3535
*/
36-
// TODO: extract constructor contributor in a similar way to MethodContributor.
3736
public class AotRepositoryConstructorBuilder {
3837

3938
private final RepositoryInformation repositoryInformation;
4039
private final AotRepositoryFragmentMetadata metadata;
4140

4241
private ConstructorCustomizer customizer = (info, builder) -> {};
4342

44-
AotRepositoryConstructorBuilder(RepositoryInformation repositoryInformation,
45-
AotRepositoryFragmentMetadata metadata) {
43+
AotRepositoryConstructorBuilder(RepositoryInformation repositoryInformation, AotRepositoryFragmentMetadata metadata) {
4644

4745
this.repositoryInformation = repositoryInformation;
4846
this.metadata = metadata;
4947
}
5048

5149
/**
52-
* Add constructor parameter.
50+
* Add constructor parameter and create a field storing its value.
5351
*
54-
* @param parameterName
55-
* @param type
52+
* @param parameterName name of the parameter.
53+
* @param type parameter type.
5654
*/
5755
public void addParameter(String parameterName, Class<?> type) {
5856

@@ -61,14 +59,15 @@ public void addParameter(String parameterName, Class<?> type) {
6159
addParameter(parameterName, TypeName.get(type));
6260
return;
6361
}
62+
6463
addParameter(parameterName, ParameterizedTypeName.get(type, resolvableType.resolveGenerics()));
6564
}
6665

6766
/**
68-
* Add constructor parameter and create a field for it.
67+
* Add constructor parameter and create a field storing its value.
6968
*
70-
* @param parameterName
71-
* @param type
69+
* @param parameterName name of the parameter.
70+
* @param type parameter type.
7271
*/
7372
public void addParameter(String parameterName, TypeName type) {
7473
addParameter(parameterName, type, true);
@@ -77,13 +76,15 @@ public void addParameter(String parameterName, TypeName type) {
7776
/**
7877
* Add constructor parameter.
7978
*
80-
* @param parameterName
81-
* @param type
79+
* @param parameterName name of the parameter.
80+
* @param type parameter type.
81+
* @param createField whether to create a field for the parameter and assign its value to the field.
8282
*/
8383
public void addParameter(String parameterName, TypeName type, boolean createField) {
8484

8585
this.metadata.addConstructorArgument(parameterName, type, createField ? parameterName : null);
86-
if(createField) {
86+
87+
if (createField) {
8788
this.metadata.addField(parameterName, type, Modifier.PRIVATE, Modifier.FINAL);
8889
}
8990
}
@@ -92,7 +93,7 @@ public void addParameter(String parameterName, TypeName type, boolean createFiel
9293
* Add constructor customizer. Customizer is invoked after adding constructor arguments and before assigning
9394
* constructor arguments to fields.
9495
*
95-
* @param customizer
96+
* @param customizer the customizer with direct access to the {@link MethodSpec.Builder constructor builder}.
9697
*/
9798
public void customize(ConstructorCustomizer customizer) {
9899
this.customizer = customizer;
@@ -109,9 +110,8 @@ MethodSpec buildConstructor() {
109110
customizer.customize(repositoryInformation, builder);
110111

111112
for (Entry<String, ConstructorArgument> parameter : this.metadata.getConstructorArguments().entrySet()) {
112-
if(parameter.getValue().isForLocalField()) {
113-
builder.addStatement("this.$N = $N", parameter.getKey(),
114-
parameter.getKey());
113+
if (parameter.getValue().isForLocalField()) {
114+
builder.addStatement("this.$N = $N", parameter.getKey(), parameter.getKey());
115115
}
116116
}
117117

@@ -123,7 +123,14 @@ MethodSpec buildConstructor() {
123123
*/
124124
public interface ConstructorCustomizer {
125125

126+
/**
127+
* Customize the constructor.
128+
*
129+
* @param information the repository information that is used for the AOT fragment.
130+
* @param builder the constructor builder to be customized.
131+
*/
126132
void customize(RepositoryInformation information, MethodSpec.Builder builder);
133+
127134
}
128135

129136
}

0 commit comments

Comments
 (0)