19
19
import java .lang .reflect .Method ;
20
20
import java .util .ArrayList ;
21
21
import java .util .List ;
22
- import java .util .Map .Entry ;
23
22
24
23
import javax .lang .model .element .Modifier ;
25
24
26
25
import org .jspecify .annotations .Nullable ;
27
-
28
26
import org .springframework .core .ResolvableType ;
29
27
import org .springframework .core .annotation .MergedAnnotation ;
30
28
import org .springframework .core .annotation .MergedAnnotationSelectors ;
@@ -54,6 +52,7 @@ public class AotQueryMethodGenerationContext {
54
52
private final AotRepositoryFragmentMetadata targetTypeMetadata ;
55
53
private final MethodMetadata targetMethodMetadata ;
56
54
private final CodeBlocks codeBlocks ;
55
+ private final VariableNameFactory variableNameFactory ;
57
56
58
57
AotQueryMethodGenerationContext (RepositoryInformation repositoryInformation , Method method , QueryMethod queryMethod ,
59
58
AotRepositoryFragmentMetadata targetTypeMetadata ) {
@@ -64,6 +63,7 @@ public class AotQueryMethodGenerationContext {
64
63
this .repositoryInformation = repositoryInformation ;
65
64
this .targetTypeMetadata = targetTypeMetadata ;
66
65
this .targetMethodMetadata = new MethodMetadata (repositoryInformation , method );
66
+ this .variableNameFactory = LocalVariableNameFactory .forMethod (targetMethodMetadata );
67
67
this .codeBlocks = new CodeBlocks (targetTypeMetadata );
68
68
}
69
69
@@ -127,6 +127,16 @@ public TypeName getReturnTypeName() {
127
127
return TypeName .get (getReturnType ().getType ());
128
128
}
129
129
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
+
130
140
/**
131
141
* Returns the required parameter name for the {@link Parameter#isBindable() bindable parameter} at the given
132
142
* {@code parameterIndex} or throws {@link IllegalArgumentException} if the parameter cannot be determined by its
@@ -227,7 +237,7 @@ public List<String> getBindableParameterNames() {
227
237
List <String > result = new ArrayList <>();
228
238
229
239
for (Parameter parameter : queryMethod .getParameters ().getBindableParameters ()) {
230
- parameter .getName (). map ( result :: add );
240
+ getParameterName ( parameter .getIndex () );
231
241
}
232
242
233
243
return result ;
@@ -237,14 +247,7 @@ public List<String> getBindableParameterNames() {
237
247
* @return list of all parameter names (including non-bindable special parameters).
238
248
*/
239
249
public List <String > getAllParameterNames () {
240
-
241
- List <String > result = new ArrayList <>();
242
-
243
- for (Parameter parameter : queryMethod .getParameters ()) {
244
- parameter .getName ().map (result ::add );
245
- }
246
-
247
- return result ;
250
+ return targetMethodMetadata .getMethodArguments ().keySet ().stream ().toList ();
248
251
}
249
252
250
253
public boolean hasField (String fieldName ) {
@@ -269,17 +272,7 @@ public String getParameterNameOf(Class<?> type) {
269
272
}
270
273
271
274
public @ Nullable String getParameterName (int position ) {
272
-
273
- if (0 > position ) {
274
- return null ;
275
- }
276
-
277
- List <Entry <String , ParameterSpec >> entries = new ArrayList <>(
278
- targetMethodMetadata .getMethodArguments ().entrySet ());
279
- if (position < entries .size ()) {
280
- return entries .get (position ).getKey ();
281
- }
282
- return null ;
275
+ return targetMethodMetadata .getParameterName (position );
283
276
}
284
277
285
278
public void addParameter (ParameterSpec parameter ) {
0 commit comments