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,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 ());
0 commit comments