Polishing.

Move QuerydslPredicateExecutor hints to RepositoryRuntimeHints.

See #4244
Original pull request: #4245
This commit is contained in:
Mark Paluch
2023-01-23 14:08:10 +01:00
parent d00db4bd40
commit 33902b5061
3 changed files with 35 additions and 52 deletions

View File

@@ -31,8 +31,6 @@ import org.springframework.data.mongodb.core.mapping.event.ReactiveAfterConvertC
import org.springframework.data.mongodb.core.mapping.event.ReactiveAfterSaveCallback;
import org.springframework.data.mongodb.core.mapping.event.ReactiveBeforeConvertCallback;
import org.springframework.data.mongodb.core.mapping.event.ReactiveBeforeSaveCallback;
import org.springframework.data.mongodb.repository.support.QuerydslMongoPredicateExecutor;
import org.springframework.data.mongodb.repository.support.ReactiveQuerydslMongoPredicateExecutor;
import org.springframework.lang.Nullable;
import org.springframework.util.ClassUtils;
@@ -66,7 +64,6 @@ class MongoRuntimeHints implements RuntimeHintsRegistrar {
MemberCategory.INVOKE_PUBLIC_METHODS));
}
registerQuerydslHints(hints, classLoader);
}
private static void registerTransactionProxyHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
@@ -83,27 +80,4 @@ class MongoRuntimeHints implements RuntimeHintsRegistrar {
}
}
/**
* Register hints for Querydsl integration.
*
* @param hints must not be {@literal null}.
* @param classLoader can be {@literal null}.
* @since 4.0.1
*/
private static void registerQuerydslHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
if (ClassUtils.isPresent("com.querydsl.core.types.Predicate", classLoader)) {
if (isReactorPresent()) {
hints.reflection().registerType(ReactiveQuerydslMongoPredicateExecutor.class,
MemberCategory.INVOKE_PUBLIC_METHODS, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
}
if (MongoAotPredicates.isSyncClientPresent(classLoader)) {
hints.reflection().registerType(QuerydslMongoPredicateExecutor.class, MemberCategory.INVOKE_PUBLIC_METHODS,
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
}
}
}
}

View File

@@ -17,13 +17,15 @@ package org.springframework.data.mongodb.repository.aot;
import static org.springframework.data.mongodb.aot.MongoAotPredicates.*;
import java.util.Arrays;
import java.util.List;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.aot.hint.TypeReference;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.data.mongodb.aot.MongoAotPredicates;
import org.springframework.data.mongodb.repository.support.QuerydslMongoPredicateExecutor;
import org.springframework.data.mongodb.repository.support.ReactiveQuerydslMongoPredicateExecutor;
import org.springframework.data.querydsl.QuerydslUtils;
import org.springframework.lang.Nullable;
@@ -37,25 +39,42 @@ class RepositoryRuntimeHints implements RuntimeHintsRegistrar {
public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
hints.reflection().registerTypes(
Arrays.asList(TypeReference.of("org.springframework.data.mongodb.repository.support.SimpleMongoRepository")),
List.of(TypeReference.of("org.springframework.data.mongodb.repository.support.SimpleMongoRepository")),
builder -> builder.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS,
MemberCategory.INVOKE_PUBLIC_METHODS));
if (isReactorPresent()) {
hints.reflection().registerTypes(
Arrays.asList(
List.of(
TypeReference.of("org.springframework.data.mongodb.repository.support.SimpleReactiveMongoRepository")),
builder -> builder.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS,
MemberCategory.INVOKE_PUBLIC_METHODS));
}
if (QuerydslUtils.QUERY_DSL_PRESENT) {
registerQuerydslHints(hints, classLoader);
}
}
hints.reflection().registerType(
TypeReference.of("org.springframework.data.mongodb.repository.support.QuerydslMongoPredicateExecutor"),
hint -> hint.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS, MemberCategory.INVOKE_PUBLIC_METHODS)
.onReachableType(QuerydslPredicateExecutor.class));
/**
* Register hints for Querydsl integration.
*
* @param hints must not be {@literal null}.
* @param classLoader can be {@literal null}.
* @since 4.0.2
*/
private static void registerQuerydslHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
if (isReactorPresent()) {
hints.reflection().registerType(ReactiveQuerydslMongoPredicateExecutor.class,
MemberCategory.INVOKE_PUBLIC_METHODS, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
}
if (MongoAotPredicates.isSyncClientPresent(classLoader)) {
hints.reflection().registerType(QuerydslMongoPredicateExecutor.class, MemberCategory.INVOKE_PUBLIC_METHODS,
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2022 the original author or authors.
* 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.
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mongodb.aot;
package org.springframework.data.mongodb.repository.aot;
import static org.assertj.core.api.Assertions.*;
@@ -28,27 +28,17 @@ import org.springframework.data.mongodb.repository.support.ReactiveQuerydslMongo
import com.mongodb.client.MongoClient;
/**
* Unit tests for {@link RepositoryRuntimeHints}.
*
* @author Christoph Strobl
*/
class MongoRuntimeHintsUnitTests {
@Test // GH-4244
void doesNotRegisterTypesForQuerydslIntegrationWhenQuerydslNotPresent() {
RuntimeHints runtimeHints = new RuntimeHints();
new MongoRuntimeHints().registerHints(runtimeHints, new HidingClassLoader("com.querydsl"));
assertThat(runtimeHints)
.matches(RuntimeHintsPredicates.reflection().onType(QuerydslMongoPredicateExecutor.class).negate()
.and(RuntimeHintsPredicates.reflection().onType(ReactiveQuerydslMongoPredicateExecutor.class).negate()));
}
class RepositoryRuntimeHintsUnitTests {
@Test // GH-4244
void registersTypesForQuerydslIntegration() {
RuntimeHints runtimeHints = new RuntimeHints();
new MongoRuntimeHints().registerHints(runtimeHints, null);
new RepositoryRuntimeHints().registerHints(runtimeHints, null);
assertThat(runtimeHints).matches(RuntimeHintsPredicates.reflection().onType(QuerydslMongoPredicateExecutor.class)
.and(RuntimeHintsPredicates.reflection().onType(ReactiveQuerydslMongoPredicateExecutor.class)));
@@ -58,7 +48,7 @@ class MongoRuntimeHintsUnitTests {
void onlyRegistersReactiveTypesForQuerydslIntegrationWhenNoSyncClientPresent() {
RuntimeHints runtimeHints = new RuntimeHints();
new MongoRuntimeHints().registerHints(runtimeHints, HidingClassLoader.hide(MongoClient.class));
new RepositoryRuntimeHints().registerHints(runtimeHints, HidingClassLoader.hide(MongoClient.class));
assertThat(runtimeHints).matches(RuntimeHintsPredicates.reflection().onType(QuerydslMongoPredicateExecutor.class)
.negate().and(RuntimeHintsPredicates.reflection().onType(ReactiveQuerydslMongoPredicateExecutor.class)));
@@ -69,7 +59,7 @@ class MongoRuntimeHintsUnitTests {
void doesNotRegistersReactiveTypesForQuerydslIntegrationWhenReactorNotPresent() {
RuntimeHints runtimeHints = new RuntimeHints();
new MongoRuntimeHints().registerHints(runtimeHints, new HidingClassLoader("reactor.core"));
new RepositoryRuntimeHints().registerHints(runtimeHints, new HidingClassLoader("reactor.core"));
assertThat(runtimeHints).matches(RuntimeHintsPredicates.reflection().onType(QuerydslMongoPredicateExecutor.class)
.and(RuntimeHintsPredicates.reflection().onType(ReactiveQuerydslMongoPredicateExecutor.class).negate()));