2020import java .util .ArrayList ;
2121import java .util .List ;
2222
23- import javax .lang .model .element .Modifier ;
24-
2523import org .jspecify .annotations .Nullable ;
24+
2625import org .springframework .core .ResolvableType ;
2726import org .springframework .core .annotation .MergedAnnotation ;
2827import org .springframework .core .annotation .MergedAnnotationSelectors ;
3130import org .springframework .data .repository .query .Parameter ;
3231import org .springframework .data .repository .query .QueryMethod ;
3332import org .springframework .data .repository .query .ReturnedType ;
34- import org .springframework .javapoet .FieldSpec ;
35- import org .springframework .javapoet .ParameterSpec ;
3633import org .springframework .javapoet .TypeName ;
3734import 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,23 +132,13 @@ public TypeName getReturnTypeName() {
127132 return TypeName .get (getReturnType ().getType ());
128133 }
129134
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-
140135 /**
141136 * Returns the required parameter name for the {@link Parameter#isBindable() bindable parameter} at the given
142137 * {@code parameterIndex} or throws {@link IllegalArgumentException} if the parameter cannot be determined by its
143138 * index.
144139 *
145140 * @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.
147142 */
148143 public String getRequiredBindableParameterName (int parameterIndex ) {
149144
@@ -161,9 +156,8 @@ public String getRequiredBindableParameterName(int parameterIndex) {
161156 * {@code parameterIndex} or {@code null} if the parameter cannot be determined by its index.
162157 *
163158 * @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.
165160 */
166- // TODO: Simplify?!
167161 public @ Nullable String getBindableParameterName (int parameterIndex ) {
168162
169163 int bindable = 0 ;
@@ -185,12 +179,12 @@ public String getRequiredBindableParameterName(int parameterIndex) {
185179 }
186180
187181 /**
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.
191184 *
192185 * @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
194188 */
195189 public String getRequiredBindableParameterName (String parameterName ) {
196190
@@ -204,13 +198,13 @@ public String getRequiredBindableParameterName(String parameterName) {
204198 }
205199
206200 /**
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 .
209203 *
210204 * @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
212207 */
213- // TODO: Simplify?!
214208 public @ Nullable String getBindableParameterName (String parameterName ) {
215209
216210 int totalIndex = 0 ;
@@ -237,7 +231,7 @@ public List<String> getBindableParameterNames() {
237231 List <String > result = new ArrayList <>();
238232
239233 for (Parameter parameter : queryMethod .getParameters ().getBindableParameters ()) {
240- getParameterName (parameter .getIndex ());
234+ result . add ( getParameterName (parameter .getIndex () ));
241235 }
242236
243237 return result ;
@@ -250,45 +244,50 @@ public List<String> getAllParameterNames() {
250244 return targetMethodMetadata .getMethodArguments ().keySet ().stream ().toList ();
251245 }
252246
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 );
272256 }
273257
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+ */
274265 public @ Nullable String getParameterName (int position ) {
275266 return targetMethodMetadata .getParameterName (position );
276267 }
277268
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+ */
282273 @ Nullable
283274 public String getSortParameterName () {
284275 return getParameterName (queryMethod .getParameters ().getSortIndex ());
285276 }
286277
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+ */
287282 @ Nullable
288283 public String getPageableParameterName () {
289284 return getParameterName (queryMethod .getParameters ().getPageableIndex ());
290285 }
291286
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+ */
292291 @ Nullable
293292 public String getLimitParameterName () {
294293 return getParameterName (queryMethod .getParameters ().getLimitIndex ());
0 commit comments