Skip to content

Commit 24ea7ac

Browse files
christophstroblmp911de
authored andcommitted
Generate proxy and register reflection for default sorting during AOT.
This commit makes sure to generate required cglib proxies during AOT phase so they are ready to use within a native image. Prior to this change default sorting raised an error as class generation is not allowed in a GraalVM native image. Original pull request: #4747 Closes #4744
1 parent 946492a commit 24ea7ac

File tree

4 files changed

+99
-18
lines changed

4 files changed

+99
-18
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/aot/LazyLoadingProxyAotProcessor.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.Set;
2525

2626
import org.springframework.aot.generate.GenerationContext;
27-
import org.springframework.aot.hint.MemberCategory;
2827
import org.springframework.aot.hint.TypeReference;
2928
import org.springframework.core.annotation.AnnotatedElementUtils;
3029
import org.springframework.core.annotation.MergedAnnotations;
@@ -77,8 +76,7 @@ public void registerLazyLoadingProxyIfNeeded(Class<?> type, GenerationContext ge
7776
LazyLoadingInterceptor::none);
7877

7978
// see: spring-projects/spring-framework/issues/29309
80-
generationContext.getRuntimeHints().reflection().registerType(proxyClass,
81-
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, MemberCategory.INVOKE_DECLARED_METHODS, MemberCategory.DECLARED_FIELDS);
79+
generationContext.getRuntimeHints().reflection().registerType(proxyClass, MongoAotReflectionHelper::cglibProxyReflectionMemberAccess);
8280
}
8381
});
8482
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.aot;
17+
18+
import org.springframework.aot.hint.MemberCategory;
19+
import org.springframework.aot.hint.TypeHint.Builder;
20+
21+
/**
22+
* @author Christoph Strobl
23+
*/
24+
public final class MongoAotReflectionHelper {
25+
26+
public static void cglibProxyReflectionMemberAccess(Builder builder) {
27+
28+
builder.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, MemberCategory.INVOKE_DECLARED_METHODS,
29+
MemberCategory.DECLARED_FIELDS);
30+
}
31+
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/aot/RepositoryRuntimeHints.java

+16
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
import org.springframework.aot.hint.RuntimeHintsRegistrar;
2525
import org.springframework.aot.hint.TypeReference;
2626
import org.springframework.data.mongodb.aot.MongoAotPredicates;
27+
import org.springframework.data.mongodb.aot.MongoAotReflectionHelper;
28+
import org.springframework.data.mongodb.core.query.BasicQuery;
29+
import org.springframework.data.mongodb.core.query.Query;
30+
import org.springframework.data.mongodb.repository.query.QueryUtils;
2731
import org.springframework.data.mongodb.repository.support.CrudMethodMetadata;
2832
import org.springframework.data.mongodb.repository.support.QuerydslMongoPredicateExecutor;
2933
import org.springframework.data.mongodb.repository.support.ReactiveQuerydslMongoPredicateExecutor;
@@ -45,6 +49,8 @@ public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader)
4549
builder -> builder.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS,
4650
MemberCategory.INVOKE_PUBLIC_METHODS));
4751

52+
registerHintsForDefaultSorting(hints, classLoader);
53+
4854
if (isAopPresent(classLoader)) {
4955

5056
// required for pushing ReadPreference,... into the default repository implementation
@@ -92,4 +98,14 @@ private static void registerQuerydslHints(RuntimeHints hints, @Nullable ClassLoa
9298
private static boolean isAopPresent(@Nullable ClassLoader classLoader) {
9399
return ClassUtils.isPresent("org.springframework.aop.Pointcut", classLoader);
94100
}
101+
102+
private static void registerHintsForDefaultSorting(RuntimeHints hints, @Nullable ClassLoader classLoader) {
103+
104+
List<TypeReference> types = List.of(TypeReference.of(Query.class), //
105+
TypeReference.of(QueryUtils.queryProxyType(Query.class, classLoader)), //
106+
TypeReference.of(BasicQuery.class), //
107+
TypeReference.of(QueryUtils.queryProxyType(BasicQuery.class, classLoader)));
108+
109+
hints.reflection().registerTypes(types, MongoAotReflectionHelper::cglibProxyReflectionMemberAccess);
110+
}
95111
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/QueryUtils.java

+51-15
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,16 @@
1919
import java.util.List;
2020

2121
import org.aopalliance.intercept.MethodInterceptor;
22+
import org.aopalliance.intercept.MethodInvocation;
23+
import org.apache.commons.logging.Log;
24+
import org.apache.commons.logging.LogFactory;
2225
import org.bson.Document;
2326
import org.springframework.aop.framework.ProxyFactory;
2427
import org.springframework.data.mongodb.core.query.Collation;
2528
import org.springframework.data.mongodb.core.query.Query;
2629
import org.springframework.data.repository.query.QueryMethodEvaluationContextProvider;
2730
import org.springframework.expression.ExpressionParser;
31+
import org.springframework.lang.NonNull;
2832
import org.springframework.lang.Nullable;
2933
import org.springframework.util.ClassUtils;
3034

@@ -37,7 +41,9 @@
3741
* @since 2.1
3842
* @currentRead Assassin's Apprentice - Robin Hobb
3943
*/
40-
class QueryUtils {
44+
public class QueryUtils {
45+
46+
protected static final Log LOGGER = LogFactory.getLog(QueryUtils.class);
4147

4248
/**
4349
* Decorate {@link Query} and add a default sort expression to the given {@link Query}. Attributes of the given
@@ -47,28 +53,27 @@ class QueryUtils {
4753
* @param defaultSort the default sort expression to apply to the query.
4854
* @return the query having the given {@code sort} applied.
4955
*/
50-
static Query decorateSort(Query query, Document defaultSort) {
56+
public static Query decorateSort(Query query, Document defaultSort) {
5157

5258
if (defaultSort.isEmpty()) {
5359
return query;
5460
}
5561

56-
ProxyFactory factory = new ProxyFactory(query);
57-
factory.addAdvice((MethodInterceptor) invocation -> {
58-
59-
if (!invocation.getMethod().getName().equals("getSortObject")) {
60-
return invocation.proceed();
61-
}
62-
63-
Document combinedSort = new Document(defaultSort);
64-
combinedSort.putAll((Document) invocation.proceed());
65-
return combinedSort;
66-
});
67-
factory.setInterfaces(new Class[0]);
68-
62+
ProxyFactory factory = prepareQueryProxy(query.getClass(), defaultSort);
63+
factory.setTarget(query);
6964
return (Query) factory.getProxy(query.getClass().getClassLoader());
7065
}
7166

67+
/**
68+
* Decorate {@link Query} and add a default sort expression to the given {@link Query}. Attributes of the given
69+
* {@code sort} may be overwritten by the sort explicitly defined by the {@link Query} itself.
70+
*
71+
* @param classLoader the {@link ClassLoader} to use for generating the proxy type with.
72+
*/
73+
public static Class<?> queryProxyType(Class<? extends Query> baseType, ClassLoader classLoader) {
74+
return prepareQueryProxy(baseType, new Document()).getProxyClass(classLoader);
75+
}
76+
7277
/**
7378
* Apply a collation extracted from the given {@literal collationExpression} to the given {@link Query}. Potentially
7479
* replace parameter placeholders with values from the {@link ConvertingParameterAccessor accessor}.
@@ -124,4 +129,35 @@ static int indexOfAssignableParameter(Class<?> type, List<Class<?>> parameters)
124129
}
125130
return -1;
126131
}
132+
133+
private static ProxyFactory prepareQueryProxy(Class<? extends Query> query, Document defaultSort) {
134+
135+
ProxyFactory factory = new ProxyFactory();
136+
factory.setTargetClass(query);
137+
factory.addAdvice(new DefaultSortingInterceptor(defaultSort));
138+
factory.setInterfaces(new Class[0]);
139+
return factory;
140+
}
141+
142+
static class DefaultSortingInterceptor implements MethodInterceptor {
143+
144+
private final Document defaultSort;
145+
146+
public DefaultSortingInterceptor(Document defaultSort) {
147+
this.defaultSort = defaultSort;
148+
}
149+
150+
@Nullable
151+
@Override
152+
public Object invoke(@NonNull MethodInvocation invocation) throws Throwable {
153+
154+
if (!invocation.getMethod().getName().equals("getSortObject")) {
155+
return invocation.proceed();
156+
}
157+
158+
Document combinedSort = new Document(defaultSort);
159+
combinedSort.putAll((Document) invocation.proceed());
160+
return combinedSort;
161+
}
162+
}
127163
}

0 commit comments

Comments
 (0)