Skip to content

Commit d00db4b

Browse files
christophstroblmp911de
authored andcommitted
Add missing hints for Querydsl integration.
This commit adds missing reflection configuration for Querydsl integration. We now also make sure to call the queryMixing getter instead of reading the field via reflection. Closes #4244 Original pull request: #4245
1 parent a5dcbf0 commit d00db4b

File tree

4 files changed

+214
-7
lines changed

4 files changed

+214
-7
lines changed

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

+30-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import org.springframework.data.mongodb.core.mapping.event.ReactiveAfterSaveCallback;
3232
import org.springframework.data.mongodb.core.mapping.event.ReactiveBeforeConvertCallback;
3333
import org.springframework.data.mongodb.core.mapping.event.ReactiveBeforeSaveCallback;
34+
import org.springframework.data.mongodb.repository.support.QuerydslMongoPredicateExecutor;
35+
import org.springframework.data.mongodb.repository.support.ReactiveQuerydslMongoPredicateExecutor;
3436
import org.springframework.lang.Nullable;
3537
import org.springframework.util.ClassUtils;
3638

@@ -62,13 +64,15 @@ public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader)
6264
TypeReference.of(ReactiveAfterSaveCallback.class)),
6365
builder -> builder.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS,
6466
MemberCategory.INVOKE_PUBLIC_METHODS));
65-
6667
}
68+
69+
registerQuerydslHints(hints, classLoader);
6770
}
6871

6972
private static void registerTransactionProxyHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
7073

71-
if (MongoAotPredicates.isSyncClientPresent(classLoader) && ClassUtils.isPresent("org.springframework.aop.SpringProxy", classLoader)) {
74+
if (MongoAotPredicates.isSyncClientPresent(classLoader)
75+
&& ClassUtils.isPresent("org.springframework.aop.SpringProxy", classLoader)) {
7276

7377
hints.proxies().registerJdkProxy(TypeReference.of("com.mongodb.client.MongoDatabase"),
7478
TypeReference.of("org.springframework.aop.SpringProxy"),
@@ -78,4 +82,28 @@ private static void registerTransactionProxyHints(RuntimeHints hints, @Nullable
7882
TypeReference.of("org.springframework.core.DecoratingProxy"));
7983
}
8084
}
85+
86+
/**
87+
* Register hints for Querydsl integration.
88+
*
89+
* @param hints must not be {@literal null}.
90+
* @param classLoader can be {@literal null}.
91+
* @since 4.0.1
92+
*/
93+
private static void registerQuerydslHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
94+
95+
if (ClassUtils.isPresent("com.querydsl.core.types.Predicate", classLoader)) {
96+
97+
if (isReactorPresent()) {
98+
hints.reflection().registerType(ReactiveQuerydslMongoPredicateExecutor.class,
99+
MemberCategory.INVOKE_PUBLIC_METHODS, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
100+
101+
}
102+
103+
if (MongoAotPredicates.isSyncClientPresent(classLoader)) {
104+
hints.reflection().registerType(QuerydslMongoPredicateExecutor.class, MemberCategory.INVOKE_PUBLIC_METHODS,
105+
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
106+
}
107+
}
108+
}
81109
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SpringDataMongodbQuerySupport.java

+2-5
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
import org.bson.json.JsonMode;
2323
import org.bson.json.JsonWriterSettings;
2424

25-
import org.springframework.beans.DirectFieldAccessor;
26-
2725
import com.mongodb.MongoClientSettings;
2826
import com.querydsl.core.support.QueryMixin;
2927
import com.querydsl.core.types.OrderSpecifier;
@@ -49,11 +47,10 @@ abstract class SpringDataMongodbQuerySupport<Q extends SpringDataMongodbQuerySup
4947

5048
@SuppressWarnings("unchecked")
5149
SpringDataMongodbQuerySupport(MongodbDocumentSerializer serializer) {
50+
5251
super(serializer);
5352
this.serializer = serializer;
54-
55-
DirectFieldAccessor fieldAccessor = new DirectFieldAccessor(this);
56-
this.superQueryMixin = (QueryMixin<Q>) fieldAccessor.getPropertyValue("queryMixin");
53+
this.superQueryMixin = super.getQueryMixin();
5754
}
5855

5956
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2022 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 org.junit.jupiter.api.Disabled;
21+
import org.junit.jupiter.api.Test;
22+
import org.springframework.aot.hint.RuntimeHints;
23+
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
24+
import org.springframework.data.mongodb.classloading.HidingClassLoader;
25+
import org.springframework.data.mongodb.repository.support.QuerydslMongoPredicateExecutor;
26+
import org.springframework.data.mongodb.repository.support.ReactiveQuerydslMongoPredicateExecutor;
27+
28+
import com.mongodb.client.MongoClient;
29+
30+
/**
31+
* @author Christoph Strobl
32+
*/
33+
class MongoRuntimeHintsUnitTests {
34+
35+
@Test // GH-4244
36+
void doesNotRegisterTypesForQuerydslIntegrationWhenQuerydslNotPresent() {
37+
38+
RuntimeHints runtimeHints = new RuntimeHints();
39+
new MongoRuntimeHints().registerHints(runtimeHints, new HidingClassLoader("com.querydsl"));
40+
41+
assertThat(runtimeHints)
42+
.matches(RuntimeHintsPredicates.reflection().onType(QuerydslMongoPredicateExecutor.class).negate()
43+
.and(RuntimeHintsPredicates.reflection().onType(ReactiveQuerydslMongoPredicateExecutor.class).negate()));
44+
45+
}
46+
47+
@Test // GH-4244
48+
void registersTypesForQuerydslIntegration() {
49+
50+
RuntimeHints runtimeHints = new RuntimeHints();
51+
new MongoRuntimeHints().registerHints(runtimeHints, null);
52+
53+
assertThat(runtimeHints).matches(RuntimeHintsPredicates.reflection().onType(QuerydslMongoPredicateExecutor.class)
54+
.and(RuntimeHintsPredicates.reflection().onType(ReactiveQuerydslMongoPredicateExecutor.class)));
55+
}
56+
57+
@Test // GH-4244
58+
void onlyRegistersReactiveTypesForQuerydslIntegrationWhenNoSyncClientPresent() {
59+
60+
RuntimeHints runtimeHints = new RuntimeHints();
61+
new MongoRuntimeHints().registerHints(runtimeHints, HidingClassLoader.hide(MongoClient.class));
62+
63+
assertThat(runtimeHints).matches(RuntimeHintsPredicates.reflection().onType(QuerydslMongoPredicateExecutor.class)
64+
.negate().and(RuntimeHintsPredicates.reflection().onType(ReactiveQuerydslMongoPredicateExecutor.class)));
65+
}
66+
67+
@Test // GH-4244
68+
@Disabled("TODO: ReactiveWrappers does not support ClassLoader")
69+
void doesNotRegistersReactiveTypesForQuerydslIntegrationWhenReactorNotPresent() {
70+
71+
RuntimeHints runtimeHints = new RuntimeHints();
72+
new MongoRuntimeHints().registerHints(runtimeHints, new HidingClassLoader("reactor.core"));
73+
74+
assertThat(runtimeHints).matches(RuntimeHintsPredicates.reflection().onType(QuerydslMongoPredicateExecutor.class)
75+
.and(RuntimeHintsPredicates.reflection().onType(ReactiveQuerydslMongoPredicateExecutor.class).negate()));
76+
}
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright 2022 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.classloading;
17+
18+
import java.net.URLClassLoader;
19+
import java.util.Arrays;
20+
import java.util.Collection;
21+
import java.util.stream.Collectors;
22+
23+
import org.springframework.instrument.classloading.ShadowingClassLoader;
24+
import org.springframework.util.Assert;
25+
26+
/**
27+
* is intended for testing code that depends on the presence/absence of certain classes. Classes can be:
28+
* <ul>
29+
* <li>shadowed: reloaded by this classloader no matter if they are loaded already by the SystemClassLoader</li>
30+
* <li>hidden: not loaded by this classloader no matter if they are loaded already by the SystemClassLoader. Trying to
31+
* load these classes results in a {@link ClassNotFoundException}</li>
32+
* <li>all other classes get loaded by the SystemClassLoader</li>
33+
* </ul>
34+
*
35+
* @author Jens Schauder
36+
* @author Oliver Gierke
37+
* @author Christoph Strobl
38+
*/
39+
public class HidingClassLoader extends ShadowingClassLoader {
40+
41+
private final Collection<String> hidden;
42+
43+
public HidingClassLoader(String... hidden) {
44+
this(Arrays.asList(hidden));
45+
}
46+
47+
public HidingClassLoader(Collection<String> hidden) {
48+
49+
super(URLClassLoader.getSystemClassLoader(), false);
50+
51+
this.hidden = hidden;
52+
}
53+
54+
/**
55+
* Creates a new {@link HidingClassLoader} with the packages of the given classes hidden.
56+
*
57+
* @param packages must not be {@literal null}.
58+
* @return
59+
*/
60+
public static HidingClassLoader hide(Class<?>... packages) {
61+
62+
Assert.notNull(packages, "Packages must not be null");
63+
64+
return new HidingClassLoader(Arrays.stream(packages)//
65+
.map(it -> it.getPackage().getName())//
66+
.collect(Collectors.toList()));
67+
}
68+
69+
public static HidingClassLoader hideTypes(Class<?>... types) {
70+
71+
Assert.notNull(types, "Types must not be null!");
72+
73+
return new HidingClassLoader(Arrays.stream(types)//
74+
.map(it -> it.getName())//
75+
.collect(Collectors.toList()));
76+
}
77+
78+
@Override
79+
public Class<?> loadClass(String name) throws ClassNotFoundException {
80+
81+
Class<?> loaded = super.loadClass(name);
82+
checkIfHidden(loaded);
83+
return loaded;
84+
}
85+
86+
@Override
87+
protected boolean isEligibleForShadowing(String className) {
88+
return isExcluded(className);
89+
}
90+
91+
@Override
92+
protected Class<?> findClass(String name) throws ClassNotFoundException {
93+
94+
Class<?> loaded = super.findClass(name);
95+
checkIfHidden(loaded);
96+
return loaded;
97+
}
98+
99+
private void checkIfHidden(Class<?> type) throws ClassNotFoundException {
100+
101+
if (hidden.stream().anyMatch(it -> type.getName().startsWith(it))) {
102+
throw new ClassNotFoundException();
103+
}
104+
}
105+
}

0 commit comments

Comments
 (0)