Skip to content

Commit b081089

Browse files
christophstroblmp911de
authored andcommitted
Fix AOT processing for lazy-loading Jdk proxies.
This commit makes sure to use the ProxyFactory for retrieving the proxied interfaces. This makes sure to capture the exact interface order required when finally loading the proxy at runtime. Original pull request: #4352 Closes #4351
1 parent 414cf51 commit b081089

File tree

3 files changed

+79
-6
lines changed

3 files changed

+79
-6
lines changed

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

+2-5
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,21 @@
1818
import java.lang.annotation.Annotation;
1919
import java.lang.reflect.Field;
2020
import java.util.ArrayList;
21+
import java.util.Arrays;
2122
import java.util.LinkedHashSet;
2223
import java.util.List;
2324
import java.util.Set;
2425

2526
import org.springframework.aot.generate.GenerationContext;
2627
import org.springframework.aot.hint.MemberCategory;
2728
import org.springframework.aot.hint.TypeReference;
28-
import org.springframework.core.ResolvableType;
2929
import org.springframework.core.annotation.AnnotatedElementUtils;
3030
import org.springframework.core.annotation.MergedAnnotations;
3131
import org.springframework.data.annotation.Reference;
3232
import org.springframework.data.mongodb.core.convert.LazyLoadingProxyFactory;
3333
import org.springframework.data.mongodb.core.convert.LazyLoadingProxyFactory.LazyLoadingInterceptor;
3434
import org.springframework.data.mongodb.core.mapping.DBRef;
3535
import org.springframework.data.mongodb.core.mapping.DocumentReference;
36-
import org.springframework.data.util.TypeUtils;
3736

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

6867
List<Class<?>> interfaces = new ArrayList<>(
69-
TypeUtils.resolveTypesInSignature(ResolvableType.forField(field, type)));
70-
71-
interfaces.add(0, org.springframework.data.mongodb.core.convert.LazyLoadingProxy.class);
68+
Arrays.asList(LazyLoadingProxyFactory.prepareFactory(field.getType()).getProxiedInterfaces()));
7269
interfaces.add(org.springframework.aop.SpringProxy.class);
7370
interfaces.add(org.springframework.aop.framework.Advised.class);
7471
interfaces.add(org.springframework.core.DecoratingProxy.class);

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/LazyLoadingProxyFactory.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,14 @@ public static Class<?> resolveProxyType(Class<?> propertyType, Supplier<LazyLoad
9090
.getProxyClass(LazyLoadingProxy.class.getClassLoader());
9191
}
9292

93-
private ProxyFactory prepareProxyFactory(Class<?> propertyType, Supplier<LazyLoadingInterceptor> interceptor) {
93+
/**
94+
* Create the {@link ProxyFactory} for the given type, already adding required additional interfaces.
95+
*
96+
* @param propertyType the type to proxy
97+
* @return the proxy type.
98+
* @since 4.0.5
99+
*/
100+
public static ProxyFactory prepareFactory(Class<?> propertyType) {
94101

95102
ProxyFactory proxyFactory = new ProxyFactory();
96103

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

101108
proxyFactory.addInterface(LazyLoadingProxy.class);
102109
proxyFactory.addInterface(propertyType);
110+
111+
return proxyFactory;
112+
}
113+
114+
private ProxyFactory prepareProxyFactory(Class<?> propertyType, Supplier<LazyLoadingInterceptor> interceptor) {
115+
116+
ProxyFactory proxyFactory = prepareFactory(propertyType);
103117
proxyFactory.addAdvice(interceptor.get());
104118

105119
return proxyFactory;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2023 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 static org.assertj.core.api.Assertions.*;
19+
20+
import java.util.List;
21+
22+
import org.junit.jupiter.api.Test;
23+
import org.springframework.aot.generate.ClassNameGenerator;
24+
import org.springframework.aot.generate.DefaultGenerationContext;
25+
import org.springframework.aot.generate.GenerationContext;
26+
import org.springframework.aot.generate.InMemoryGeneratedFiles;
27+
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
28+
import org.springframework.data.mongodb.core.mapping.DBRef;
29+
import org.springframework.javapoet.ClassName;
30+
31+
/**
32+
* @author Christoph Strobl
33+
*/
34+
class LazyLoadingProxyAotProcessorUnitTests {
35+
36+
@Test // GH-4351
37+
void registersProxyForLazyDbRefCorrectlyWhenTypeIsCollectionInterface() {
38+
39+
GenerationContext ctx = new DefaultGenerationContext(new ClassNameGenerator(ClassName.get(this.getClass())),
40+
new InMemoryGeneratedFiles());
41+
42+
new LazyLoadingProxyAotProcessor().registerLazyLoadingProxyIfNeeded(A.class, ctx);
43+
44+
assertThat(ctx.getRuntimeHints())
45+
.satisfies(RuntimeHintsPredicates.proxies().forInterfaces(java.util.Collection.class,
46+
org.springframework.data.mongodb.core.convert.LazyLoadingProxy.class, java.util.List.class,
47+
org.springframework.aop.SpringProxy.class, org.springframework.aop.framework.Advised.class,
48+
org.springframework.core.DecoratingProxy.class)::test);
49+
}
50+
51+
static class A {
52+
53+
String id;
54+
55+
@DBRef(lazy = true) //
56+
List<B> listRef;
57+
}
58+
59+
static class B {
60+
String id;
61+
}
62+
}

0 commit comments

Comments
 (0)