Skip to content

Fix AOT processing for lazy loading jdk proxies. #4352

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 3 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.0.5-SNAPSHOT</version>
<version>4.0.x-4351-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.0.5-SNAPSHOT</version>
<version>4.0.x-4351-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.0.5-SNAPSHOT</version>
<version>4.0.x-4351-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.0.5-SNAPSHOT</version>
<version>4.0.x-4351-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,21 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
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.ResolvableType;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.data.annotation.Reference;
import org.springframework.data.mongodb.core.convert.LazyLoadingProxyFactory;
import org.springframework.data.mongodb.core.convert.LazyLoadingProxyFactory.LazyLoadingInterceptor;
import org.springframework.data.mongodb.core.mapping.DBRef;
import org.springframework.data.mongodb.core.mapping.DocumentReference;
import org.springframework.data.util.TypeUtils;

/**
* @author Christoph Strobl
Expand Down Expand Up @@ -66,9 +65,7 @@ public void registerLazyLoadingProxyIfNeeded(Class<?> type, GenerationContext ge
if (field.getType().isInterface()) {

List<Class<?>> interfaces = new ArrayList<>(
TypeUtils.resolveTypesInSignature(ResolvableType.forField(field, type)));

interfaces.add(0, org.springframework.data.mongodb.core.convert.LazyLoadingProxy.class);
Arrays.asList(LazyLoadingProxyFactory.prepareFactory(field.getType()).getProxiedInterfaces()));
interfaces.add(org.springframework.aop.SpringProxy.class);
interfaces.add(org.springframework.aop.framework.Advised.class);
interfaces.add(org.springframework.core.DecoratingProxy.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,14 @@ public static Class<?> resolveProxyType(Class<?> propertyType, Supplier<LazyLoad
.getProxyClass(LazyLoadingProxy.class.getClassLoader());
}

private ProxyFactory prepareProxyFactory(Class<?> propertyType, Supplier<LazyLoadingInterceptor> interceptor) {
/**
* Create the {@link ProxyFactory} for the given type, already adding required additional interfaces.
*
* @param propertyType the type to proxy
* @return the proxy type.
* @since 4.0.5
*/
public static ProxyFactory prepareFactory(Class<?> propertyType) {

ProxyFactory proxyFactory = new ProxyFactory();

Expand All @@ -100,6 +107,13 @@ private ProxyFactory prepareProxyFactory(Class<?> propertyType, Supplier<LazyLoa

proxyFactory.addInterface(LazyLoadingProxy.class);
proxyFactory.addInterface(propertyType);

return proxyFactory;
}

private ProxyFactory prepareProxyFactory(Class<?> propertyType, Supplier<LazyLoadingInterceptor> interceptor) {

ProxyFactory proxyFactory = prepareFactory(propertyType);
proxyFactory.addAdvice(interceptor.get());

return proxyFactory;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2023 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 static org.assertj.core.api.Assertions.*;

import java.util.List;

import org.junit.jupiter.api.Test;
import org.springframework.aot.generate.ClassNameGenerator;
import org.springframework.aot.generate.DefaultGenerationContext;
import org.springframework.aot.generate.GenerationContext;
import org.springframework.aot.generate.InMemoryGeneratedFiles;
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
import org.springframework.data.mongodb.core.mapping.DBRef;
import org.springframework.javapoet.ClassName;

/**
* @author Christoph Strobl
*/
class LazyLoadingProxyAotProcessorUnitTests {

@Test // GH-4351
void registersProxyForLazyDbRefCorrectlyWhenTypeIsCollectionInterface() {

GenerationContext ctx = new DefaultGenerationContext(new ClassNameGenerator(ClassName.get(this.getClass())),
new InMemoryGeneratedFiles());

new LazyLoadingProxyAotProcessor().registerLazyLoadingProxyIfNeeded(A.class, ctx);

assertThat(ctx.getRuntimeHints())
.satisfies(RuntimeHintsPredicates.proxies().forInterfaces(java.util.Collection.class,
org.springframework.data.mongodb.core.convert.LazyLoadingProxy.class, java.util.List.class,
org.springframework.aop.SpringProxy.class, org.springframework.aop.framework.Advised.class,
org.springframework.core.DecoratingProxy.class)::test);
}

static class A {

String id;

@DBRef(lazy = true) //
List<B> listRef;
}

static class B {
String id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ void shouldAddDefaultCollationToFindWithSortForExampleIfPresent() {
void shouldAddDefaultCollationToFindOneForExampleIfPresent() {

when(mongoOperations.find(any(), any(), any())).thenReturn(flux);
when(flux.buffer(anyInt())).thenReturn(flux);
when(flux.map(any())).thenReturn(flux);
when(flux.next()).thenReturn(mono);

Collation collation = Collation.of("en_US");

Expand Down