Skip to content

Commit 8ffc585

Browse files
committed
Polishing.
Use refined convenience methods from AotQueryMethodGenerationContext. See #3991
1 parent 48bc6aa commit 8ffc585

File tree

1 file changed

+10
-12
lines changed
  • spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/aot

1 file changed

+10
-12
lines changed

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/aot/JpaCodeBlocks.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import jakarta.persistence.QueryHint;
2121
import jakarta.persistence.Tuple;
2222

23-
import java.lang.reflect.Type;
2423
import java.util.Arrays;
2524
import java.util.Collection;
2625
import java.util.List;
@@ -160,8 +159,6 @@ public CodeBlock build() {
160159
Assert.notNull(queries, "Queries must not be null");
161160

162161
boolean isProjecting = context.getReturnedType().isProjecting();
163-
Class<?> actualReturnType = isProjecting ? context.getActualReturnType().toClass()
164-
: context.getRepositoryInformation().getDomainType();
165162

166163
String dynamicReturnType = null;
167164
if (queryMethod.getParameters().hasDynamicProjection()) {
@@ -212,7 +209,7 @@ public CodeBlock build() {
212209
if ((StringUtils.hasText(sortParameterName) || StringUtils.hasText(dynamicReturnType))
213210
&& queries != null && queries.result() instanceof StringAotQuery
214211
&& StringUtils.hasText(queryStringVariableName)) {
215-
builder.add(applyRewrite(sortParameterName, dynamicReturnType, queryStringVariableName, actualReturnType));
212+
builder.add(applyRewrite(sortParameterName, dynamicReturnType, isProjecting, queryStringVariableName));
216213
}
217214

218215
builder.add(createQuery(false, queryVariableName, queryStringVariableName, queryRewriterName, queries.result(),
@@ -246,8 +243,8 @@ private CodeBlock buildQueryString(StringAotQuery sq, String queryStringVariable
246243
return builder.build();
247244
}
248245

249-
private CodeBlock applyRewrite(@Nullable String sort, @Nullable String dynamicReturnType, String queryString,
250-
Class<?> actualReturnType) {
246+
private CodeBlock applyRewrite(@Nullable String sort, @Nullable String dynamicReturnType, boolean isProjecting,
247+
String queryString) {
251248

252249
Builder builder = CodeBlock.builder();
253250

@@ -266,6 +263,9 @@ private CodeBlock applyRewrite(@Nullable String sort, @Nullable String dynamicRe
266263
builder.addStatement("$L = rewriteQuery($L, $L, $L)", queryString, context.localVariable("declaredQuery"), sort,
267264
dynamicReturnType);
268265
} else if (hasSort) {
266+
267+
Object actualReturnType = isProjecting ? context.getActualReturnTypeName() : context.getDomainType();
268+
269269
builder.addStatement("$L = rewriteQuery($L, $L, $T.class)", queryString, context.localVariable("declaredQuery"),
270270
sort, actualReturnType);
271271
} else if (hasDynamicReturnType) {
@@ -657,11 +657,9 @@ public CodeBlock build() {
657657

658658
Builder builder = CodeBlock.builder();
659659

660-
boolean isProjecting = context.getActualReturnType() != null
661-
&& !ObjectUtils.nullSafeEquals(TypeName.get(context.getRepositoryInformation().getDomainType()),
662-
context.getActualReturnType());
663-
Type actualReturnType = isProjecting ? context.getActualReturnType().getType()
664-
: context.getRepositoryInformation().getDomainType();
660+
boolean isProjecting = !ObjectUtils.nullSafeEquals(context.getDomainType(), context.getActualReturnTypeName());
661+
TypeName actualReturnType = isProjecting ? context.getActualReturnTypeName()
662+
: TypeName.get(context.getDomainType());
665663
builder.add("\n");
666664

667665
if (modifying.isPresent()) {
@@ -775,7 +773,7 @@ public CodeBlock build() {
775773
if (Optional.class.isAssignableFrom(context.getReturnType().toClass())) {
776774
builder.addStatement("return $T.ofNullable(($T) convertOne($L.getSingleResultOrNull(), $L, $T.class))",
777775
Optional.class, actualReturnType, queryVariableName, aotQuery.isNative(),
778-
context.getActualReturnType().toClass());
776+
queryResultType);
779777
} else {
780778
builder.addStatement("return ($T) convertOne($L.getSingleResultOrNull(), $L, $T.class)",
781779
context.getReturnTypeName(), queryVariableName, aotQuery.isNative(),

0 commit comments

Comments
 (0)