Skip to content

Commit 33902b5

Browse files
committed
Polishing.
Move QuerydslPredicateExecutor hints to RepositoryRuntimeHints. See #4244 Original pull request: #4245
1 parent d00db4b commit 33902b5

File tree

3 files changed

+35
-52
lines changed

3 files changed

+35
-52
lines changed

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

-26
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
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;
3634
import org.springframework.lang.Nullable;
3735
import org.springframework.util.ClassUtils;
3836

@@ -66,7 +64,6 @@ public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader)
6664
MemberCategory.INVOKE_PUBLIC_METHODS));
6765
}
6866

69-
registerQuerydslHints(hints, classLoader);
7067
}
7168

7269
private static void registerTransactionProxyHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
@@ -83,27 +80,4 @@ private static void registerTransactionProxyHints(RuntimeHints hints, @Nullable
8380
}
8481
}
8582

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-
}
10983
}

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

+27-8
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717

1818
import static org.springframework.data.mongodb.aot.MongoAotPredicates.*;
1919

20-
import java.util.Arrays;
20+
import java.util.List;
2121

2222
import org.springframework.aot.hint.MemberCategory;
2323
import org.springframework.aot.hint.RuntimeHints;
2424
import org.springframework.aot.hint.RuntimeHintsRegistrar;
2525
import org.springframework.aot.hint.TypeReference;
26-
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
26+
import org.springframework.data.mongodb.aot.MongoAotPredicates;
27+
import org.springframework.data.mongodb.repository.support.QuerydslMongoPredicateExecutor;
28+
import org.springframework.data.mongodb.repository.support.ReactiveQuerydslMongoPredicateExecutor;
2729
import org.springframework.data.querydsl.QuerydslUtils;
2830
import org.springframework.lang.Nullable;
2931

@@ -37,25 +39,42 @@ class RepositoryRuntimeHints implements RuntimeHintsRegistrar {
3739
public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
3840

3941
hints.reflection().registerTypes(
40-
Arrays.asList(TypeReference.of("org.springframework.data.mongodb.repository.support.SimpleMongoRepository")),
42+
List.of(TypeReference.of("org.springframework.data.mongodb.repository.support.SimpleMongoRepository")),
4143
builder -> builder.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS,
4244
MemberCategory.INVOKE_PUBLIC_METHODS));
4345

4446
if (isReactorPresent()) {
4547

4648
hints.reflection().registerTypes(
47-
Arrays.asList(
49+
List.of(
4850
TypeReference.of("org.springframework.data.mongodb.repository.support.SimpleReactiveMongoRepository")),
4951
builder -> builder.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS,
5052
MemberCategory.INVOKE_PUBLIC_METHODS));
5153
}
5254

5355
if (QuerydslUtils.QUERY_DSL_PRESENT) {
56+
registerQuerydslHints(hints, classLoader);
57+
}
58+
}
59+
60+
/**
61+
* Register hints for Querydsl integration.
62+
*
63+
* @param hints must not be {@literal null}.
64+
* @param classLoader can be {@literal null}.
65+
* @since 4.0.2
66+
*/
67+
private static void registerQuerydslHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
68+
69+
if (isReactorPresent()) {
70+
hints.reflection().registerType(ReactiveQuerydslMongoPredicateExecutor.class,
71+
MemberCategory.INVOKE_PUBLIC_METHODS, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
72+
73+
}
5474

55-
hints.reflection().registerType(
56-
TypeReference.of("org.springframework.data.mongodb.repository.support.QuerydslMongoPredicateExecutor"),
57-
hint -> hint.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS, MemberCategory.INVOKE_PUBLIC_METHODS)
58-
.onReachableType(QuerydslPredicateExecutor.class));
75+
if (MongoAotPredicates.isSyncClientPresent(classLoader)) {
76+
hints.reflection().registerType(QuerydslMongoPredicateExecutor.class, MemberCategory.INVOKE_PUBLIC_METHODS,
77+
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
5978
}
6079
}
6180
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/aot/MongoRuntimeHintsUnitTests.java renamed to spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/aot/RepositoryRuntimeHintsUnitTests.java

+8-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 the original author or authors.
2+
* Copyright 2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.data.mongodb.aot;
16+
package org.springframework.data.mongodb.repository.aot;
1717

1818
import static org.assertj.core.api.Assertions.*;
1919

@@ -28,27 +28,17 @@
2828
import com.mongodb.client.MongoClient;
2929

3030
/**
31+
* Unit tests for {@link RepositoryRuntimeHints}.
32+
*
3133
* @author Christoph Strobl
3234
*/
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-
}
35+
class RepositoryRuntimeHintsUnitTests {
4636

4737
@Test // GH-4244
4838
void registersTypesForQuerydslIntegration() {
4939

5040
RuntimeHints runtimeHints = new RuntimeHints();
51-
new MongoRuntimeHints().registerHints(runtimeHints, null);
41+
new RepositoryRuntimeHints().registerHints(runtimeHints, null);
5242

5343
assertThat(runtimeHints).matches(RuntimeHintsPredicates.reflection().onType(QuerydslMongoPredicateExecutor.class)
5444
.and(RuntimeHintsPredicates.reflection().onType(ReactiveQuerydslMongoPredicateExecutor.class)));
@@ -58,7 +48,7 @@ void registersTypesForQuerydslIntegration() {
5848
void onlyRegistersReactiveTypesForQuerydslIntegrationWhenNoSyncClientPresent() {
5949

6050
RuntimeHints runtimeHints = new RuntimeHints();
61-
new MongoRuntimeHints().registerHints(runtimeHints, HidingClassLoader.hide(MongoClient.class));
51+
new RepositoryRuntimeHints().registerHints(runtimeHints, HidingClassLoader.hide(MongoClient.class));
6252

6353
assertThat(runtimeHints).matches(RuntimeHintsPredicates.reflection().onType(QuerydslMongoPredicateExecutor.class)
6454
.negate().and(RuntimeHintsPredicates.reflection().onType(ReactiveQuerydslMongoPredicateExecutor.class)));
@@ -69,7 +59,7 @@ void onlyRegistersReactiveTypesForQuerydslIntegrationWhenNoSyncClientPresent() {
6959
void doesNotRegistersReactiveTypesForQuerydslIntegrationWhenReactorNotPresent() {
7060

7161
RuntimeHints runtimeHints = new RuntimeHints();
72-
new MongoRuntimeHints().registerHints(runtimeHints, new HidingClassLoader("reactor.core"));
62+
new RepositoryRuntimeHints().registerHints(runtimeHints, new HidingClassLoader("reactor.core"));
7363

7464
assertThat(runtimeHints).matches(RuntimeHintsPredicates.reflection().onType(QuerydslMongoPredicateExecutor.class)
7565
.and(RuntimeHintsPredicates.reflection().onType(ReactiveQuerydslMongoPredicateExecutor.class).negate()));

0 commit comments

Comments
 (0)