Skip to content

Generate proxy and register reflection for default sorting during AOT. #4747

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.4.0-SNAPSHOT</version>
<version>4.4.x-GH-4744-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-benchmarks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.4.0-SNAPSHOT</version>
<version>4.4.x-GH-4744-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.4.0-SNAPSHOT</version>
<version>4.4.x-GH-4744-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.4.0-SNAPSHOT</version>
<version>4.4.x-GH-4744-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.util.Set;

import org.springframework.aot.generate.GenerationContext;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.TypeReference;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.MergedAnnotations;
Expand Down Expand Up @@ -77,8 +76,7 @@ public void registerLazyLoadingProxyIfNeeded(Class<?> type, GenerationContext ge
LazyLoadingInterceptor::none);

// see: spring-projects/spring-framework/issues/29309
generationContext.getRuntimeHints().reflection().registerType(proxyClass,
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, MemberCategory.INVOKE_DECLARED_METHODS, MemberCategory.DECLARED_FIELDS);
generationContext.getRuntimeHints().reflection().registerType(proxyClass, MongoAotReflectionHelper::cglibProxyReflectionMemberAccess);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mongodb.aot;

import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.TypeHint.Builder;

/**
* @author Christoph Strobl
*/
public final class MongoAotReflectionHelper {

public static void cglibProxyReflectionMemberAccess(Builder builder) {

builder.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, MemberCategory.INVOKE_DECLARED_METHODS,
MemberCategory.DECLARED_FIELDS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,20 @@ public BasicQuery(Document queryObject, Document fieldsObject) {
this.sortObject = new Document();
}

/**
* Create a BasicQuery given a {@link Query}. The resulting query is a copy of {@link Query}.
*
* @param query
*/
public BasicQuery(Query query) {

super(query);
this.queryObject = query.getQueryObject();
this.setFieldsObject(query.getFieldsObject());
this.setSortObject(query.getSortObject());
this.setMeta(query.getMeta());
}

@Override
public Query addCriteria(CriteriaDefinition criteria) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,20 @@ public class Query implements ReadConcernAware, ReadPreferenceAware {

private Optional<Collation> collation = Optional.empty();

Query(Query query) {
this.restrictedTypes = query.restrictedTypes;
this.fieldSpec = query.fieldSpec;
this.sort = query.sort;
this.limit = query.limit;
this.skip = query.skip;
this.keysetScrollPosition = query.keysetScrollPosition;
this.readConcern = query.readConcern;
this.readPreference = query.readPreference;
this.hint = query.hint;
this.meta = query.meta;
this.collation = query.collation;
}

/**
* Static factory method to create a {@link Query} using the provided {@link CriteriaDefinition}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,5 @@ private static void registerQuerydslHints(RuntimeHints hints, @Nullable ClassLoa
private static boolean isAopPresent(@Nullable ClassLoader classLoader) {
return ClassUtils.isPresent("org.springframework.aop.Pointcut", classLoader);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.bson.Document;
import org.bson.codecs.configuration.CodecRegistry;

import org.springframework.data.mapping.model.SpELExpressionEvaluator;
import org.springframework.data.mongodb.core.ExecutableFindOperation.ExecutableFind;
import org.springframework.data.mongodb.core.ExecutableFindOperation.FindWithQuery;
Expand Down Expand Up @@ -155,7 +156,7 @@ protected Object doExecute(MongoQueryMethod method, ResultProcessor processor, C
* @since 4.2
*/
private Query applyAnnotatedReadPreferenceIfPresent(Query query) {

if (!method.hasAnnotatedReadPreference()) {
return query;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
import java.util.Arrays;
import java.util.List;

import org.aopalliance.intercept.MethodInterceptor;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.bson.Document;
import org.springframework.aop.framework.ProxyFactory;

import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.core.query.Collation;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.repository.query.QueryMethodEvaluationContextProvider;
Expand All @@ -39,6 +41,8 @@
*/
class QueryUtils {

protected static final Log LOGGER = LogFactory.getLog(QueryUtils.class);

/**
* Decorate {@link Query} and add a default sort expression to the given {@link Query}. Attributes of the given
* {@code sort} may be overwritten by the sort explicitly defined by the {@link Query} itself.
Expand All @@ -53,20 +57,13 @@ static Query decorateSort(Query query, Document defaultSort) {
return query;
}

ProxyFactory factory = new ProxyFactory(query);
factory.addAdvice((MethodInterceptor) invocation -> {
BasicQuery defaultSortQuery = query instanceof BasicQuery bq ? bq : new BasicQuery(query);

if (!invocation.getMethod().getName().equals("getSortObject")) {
return invocation.proceed();
}
Document combinedSort = new Document(defaultSort);
combinedSort.putAll(defaultSortQuery.getSortObject());
defaultSortQuery.setSortObject(combinedSort);

Document combinedSort = new Document(defaultSort);
combinedSort.putAll((Document) invocation.proceed());
return combinedSort;
});
factory.setInterfaces(new Class[0]);

return (Query) factory.getProxy(query.getClass().getClassLoader());
return defaultSortQuery;
}

/**
Expand Down Expand Up @@ -111,17 +108,18 @@ static int indexOfAssignableParameter(Class<?> type, Class<?>[] parameters) {
*/
static int indexOfAssignableParameter(Class<?> type, List<Class<?>> parameters) {

if(parameters.isEmpty()) {
if (parameters.isEmpty()) {
return -1;
}

int i = 0;
for(Class<?> parameterType : parameters) {
if(ClassUtils.isAssignable(type, parameterType)) {
for (Class<?> parameterType : parameters) {
if (ClassUtils.isAssignable(type, parameterType)) {
return i;
}
i++;
}
return -1;
}

}