20
20
import java .util .ArrayList ;
21
21
import java .util .List ;
22
22
23
- import javax .lang .model .element .Modifier ;
24
-
25
23
import org .jspecify .annotations .Nullable ;
24
+
26
25
import org .springframework .core .ResolvableType ;
27
26
import org .springframework .core .annotation .MergedAnnotation ;
28
27
import org .springframework .core .annotation .MergedAnnotationSelectors ;
31
30
import org .springframework .data .repository .query .Parameter ;
32
31
import org .springframework .data .repository .query .QueryMethod ;
33
32
import org .springframework .data .repository .query .ReturnedType ;
34
- import org .springframework .javapoet .FieldSpec ;
35
- import org .springframework .javapoet .ParameterSpec ;
36
33
import org .springframework .javapoet .TypeName ;
37
34
import org .springframework .util .ObjectUtils ;
38
35
@@ -51,7 +48,6 @@ public class AotQueryMethodGenerationContext {
51
48
private final RepositoryInformation repositoryInformation ;
52
49
private final AotRepositoryFragmentMetadata targetTypeMetadata ;
53
50
private final MethodMetadata targetMethodMetadata ;
54
- private final CodeBlocks codeBlocks ;
55
51
private final VariableNameFactory variableNameFactory ;
56
52
57
53
AotQueryMethodGenerationContext (RepositoryInformation repositoryInformation , Method method , QueryMethod queryMethod ,
@@ -64,11 +60,6 @@ public class AotQueryMethodGenerationContext {
64
60
this .targetTypeMetadata = targetTypeMetadata ;
65
61
this .targetMethodMetadata = new MethodMetadata (repositoryInformation , method );
66
62
this .variableNameFactory = LocalVariableNameFactory .forMethod (targetMethodMetadata );
67
- this .codeBlocks = new CodeBlocks (targetTypeMetadata );
68
- }
69
-
70
- AotRepositoryFragmentMetadata getTargetTypeMetadata () {
71
- return targetTypeMetadata ;
72
63
}
73
64
74
65
MethodMetadata getTargetMethodMetadata () {
@@ -79,12 +70,18 @@ public RepositoryInformation getRepositoryInformation() {
79
70
return repositoryInformation ;
80
71
}
81
72
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 );
84
81
}
85
82
86
- public CodeBlocks codeBlocks () {
87
- return codeBlocks ;
83
+ public Method getMethod () {
84
+ return method ;
88
85
}
89
86
90
87
/**
@@ -112,10 +109,18 @@ public ReturnedType getReturnedType() {
112
109
return queryMethod .getResultProcessor ().getReturnedType ();
113
110
}
114
111
112
+ /**
113
+ * @return the actual returned domain type.
114
+ * @see org.springframework.data.repository.core.RepositoryMetadata#getReturnedDomainClass(Method)
115
+ */
115
116
public ResolvableType getActualReturnType () {
116
117
return targetMethodMetadata .getActualReturnType ();
117
118
}
118
119
120
+ /**
121
+ * @return the query method return type.
122
+ * @see org.springframework.data.repository.core.RepositoryMetadata#getReturnType(Method)
123
+ */
119
124
public ResolvableType getReturnType () {
120
125
return targetMethodMetadata .getReturnType ();
121
126
}
@@ -127,23 +132,13 @@ public TypeName getReturnTypeName() {
127
132
return TypeName .get (getReturnType ().getType ());
128
133
}
129
134
130
- /**
131
- * Suggest naming clash free variant for the given intended variable name within the local method context.
132
- *
133
- * @param variableName the intended variable name.
134
- * @return the suggested VariableName
135
- */
136
- public String suggestLocalVariableName (String variableName ) {
137
- return variableNameFactory .generateName (variableName );
138
- }
139
-
140
135
/**
141
136
* Returns the required parameter name for the {@link Parameter#isBindable() bindable parameter} at the given
142
137
* {@code parameterIndex} or throws {@link IllegalArgumentException} if the parameter cannot be determined by its
143
138
* index.
144
139
*
145
140
* @param parameterIndex the zero-based parameter index as used in the query to reference bindable parameters.
146
- * @return the parameter name.
141
+ * @return the method parameter name.
147
142
*/
148
143
public String getRequiredBindableParameterName (int parameterIndex ) {
149
144
@@ -161,9 +156,8 @@ public String getRequiredBindableParameterName(int parameterIndex) {
161
156
* {@code parameterIndex} or {@code null} if the parameter cannot be determined by its index.
162
157
*
163
158
* @param parameterIndex the zero-based parameter index as used in the query to reference bindable parameters.
164
- * @return the parameter name.
159
+ * @return the method parameter name.
165
160
*/
166
- // TODO: Simplify?!
167
161
public @ Nullable String getBindableParameterName (int parameterIndex ) {
168
162
169
163
int bindable = 0 ;
@@ -185,12 +179,12 @@ public String getRequiredBindableParameterName(int parameterIndex) {
185
179
}
186
180
187
181
/**
188
- * Returns the required parameter name for the {@link Parameter#isBindable() bindable parameter} at the given
189
- * {@code parameterName} or throws {@link IllegalArgumentException} if the parameter cannot be determined by its
190
- * 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.
191
184
*
192
185
* @param parameterName the parameter name as used in the query to reference bindable parameters.
193
- * @return the parameter name.
186
+ * @return the method parameter name.
187
+ * @see org.springframework.data.repository.query.Param
194
188
*/
195
189
public String getRequiredBindableParameterName (String parameterName ) {
196
190
@@ -204,13 +198,13 @@ public String getRequiredBindableParameterName(String parameterName) {
204
198
}
205
199
206
200
/**
207
- * Returns the required parameter name for the {@link Parameter#isBindable() bindable parameter} at the given
208
- * {@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 .
209
203
*
210
204
* @param parameterName the parameter name as used in the query to reference bindable parameters.
211
- * @return the parameter name.
205
+ * @return the method parameter name.
206
+ * @see org.springframework.data.repository.query.Param
212
207
*/
213
- // TODO: Simplify?!
214
208
public @ Nullable String getBindableParameterName (String parameterName ) {
215
209
216
210
int totalIndex = 0 ;
@@ -237,7 +231,7 @@ public List<String> getBindableParameterNames() {
237
231
List <String > result = new ArrayList <>();
238
232
239
233
for (Parameter parameter : queryMethod .getParameters ().getBindableParameters ()) {
240
- getParameterName (parameter .getIndex ());
234
+ result . add ( getParameterName (parameter .getIndex () ));
241
235
}
242
236
243
237
return result ;
@@ -250,45 +244,50 @@ public List<String> getAllParameterNames() {
250
244
return targetMethodMetadata .getMethodArguments ().keySet ().stream ().toList ();
251
245
}
252
246
253
- public boolean hasField (String fieldName ) {
254
- return targetTypeMetadata .hasField (fieldName );
255
- }
256
-
257
- public void addField (String fieldName , TypeName type , Modifier ... modifiers ) {
258
- targetTypeMetadata .addField (fieldName , type , modifiers );
259
- }
260
-
261
- public void addField (FieldSpec fieldSpec ) {
262
- targetTypeMetadata .addField (fieldSpec );
263
- }
264
-
265
- public @ Nullable String fieldNameOf (Class <?> type ) {
266
- return targetTypeMetadata .fieldNameOf (type );
267
- }
268
-
269
- @ Nullable
270
- public String getParameterNameOf (Class <?> type ) {
271
- 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 );
272
256
}
273
257
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
+ */
274
265
public @ Nullable String getParameterName (int position ) {
275
266
return targetMethodMetadata .getParameterName (position );
276
267
}
277
268
278
- public void addParameter ( ParameterSpec parameter ) {
279
- this . targetMethodMetadata . addParameter ( parameter );
280
- }
281
-
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
+ */
282
273
@ Nullable
283
274
public String getSortParameterName () {
284
275
return getParameterName (queryMethod .getParameters ().getSortIndex ());
285
276
}
286
277
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
+ */
287
282
@ Nullable
288
283
public String getPageableParameterName () {
289
284
return getParameterName (queryMethod .getParameters ().getPageableIndex ());
290
285
}
291
286
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
+ */
292
291
@ Nullable
293
292
public String getLimitParameterName () {
294
293
return getParameterName (queryMethod .getParameters ().getLimitIndex ());
0 commit comments