Resolve package cycle between repository.config and repository.aot.

Closes #2708
This commit is contained in:
Christoph Strobl
2022-10-25 15:53:56 +02:00
committed by Mark Paluch
parent 0e00d5fd3f
commit 8d424855da
20 changed files with 209 additions and 109 deletions

View File

@@ -31,6 +31,8 @@ import org.springframework.beans.factory.support.RegisteredBean;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.core.ResolvableType;
import org.springframework.data.domain.ManagedTypes;
import org.springframework.data.util.TypeContributor;
import org.springframework.data.util.TypeUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;

View File

@@ -35,6 +35,7 @@ import org.springframework.beans.factory.support.RegisteredBean;
import org.springframework.core.ResolvableType;
import org.springframework.data.domain.ManagedTypes;
import org.springframework.data.util.Lazy;
import org.springframework.data.util.TypeCollector;
import org.springframework.javapoet.ClassName;
import org.springframework.javapoet.CodeBlock;
import org.springframework.javapoet.MethodSpec.Builder;

View File

@@ -1,52 +0,0 @@
/*
* Copyright 2022 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.aot;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.function.Predicate;
/**
* Abstract utility class containing common, reusable {@link Predicate Predicates}.
*
* @author John Blum
* @see java.util.function.Predicate
* @since 3.0
*/
// TODO: Consider moving to the org.springframework.data.util package.
@SuppressWarnings("unused")
public abstract class Predicates {
public static final Predicate<Member> IS_ENUM_MEMBER = member -> member.getDeclaringClass().isEnum();
public static final Predicate<Member> IS_HIBERNATE_MEMBER = member -> member.getName().startsWith("$$_hibernate"); // this
// should
// go
// into
// JPA
public static final Predicate<Member> IS_OBJECT_MEMBER = member -> Object.class.equals(member.getDeclaringClass());
public static final Predicate<Member> IS_JAVA = member -> member.getDeclaringClass().getPackageName().startsWith("java.");
public static final Predicate<Member> IS_NATIVE = member -> Modifier.isNative(member.getModifiers());
public static final Predicate<Member> IS_PRIVATE = member -> Modifier.isPrivate(member.getModifiers());
public static final Predicate<Member> IS_PROTECTED = member -> Modifier.isProtected(member.getModifiers());
public static final Predicate<Member> IS_PUBLIC = member -> Modifier.isPublic(member.getModifiers());
public static final Predicate<Member> IS_SYNTHETIC = Member::isSynthetic;
public static final Predicate<Member> IS_STATIC = member -> Modifier.isStatic(member.getModifiers());
public static final Predicate<Method> IS_BRIDGE_METHOD = Method::isBridge;
}

View File

@@ -0,0 +1,58 @@
/*
* Copyright 2022 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.querydsl.aot;
import java.util.Arrays;
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.querydsl.ReactiveQuerydslPredicateExecutor;
import org.springframework.lang.Nullable;
import org.springframework.util.ClassUtils;
import com.querydsl.core.types.Predicate;
/**
* @author Christoph Strobl
* @since 4.0
*/
public class QuerydslHints implements RuntimeHintsRegistrar {
@Override
public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
if (ClassUtils.isPresent("com.querydsl.core.types.Predicate", classLoader)) {
// repository infrastructure
hints.reflection().registerTypes(Arrays.asList( //
TypeReference.of(Predicate.class), //
TypeReference.of(QuerydslPredicateExecutor.class)), builder -> {
builder.withMembers(MemberCategory.INVOKE_PUBLIC_METHODS);
});
if (ClassUtils.isPresent("reactor.core.publisher.Flux", classLoader)) {
// repository infrastructure
hints.reflection().registerTypes(Arrays.asList( //
TypeReference.of(ReactiveQuerydslPredicateExecutor.class)), builder -> {
builder.withMembers(MemberCategory.INVOKE_PUBLIC_METHODS);
});
}
}
}
}

View File

@@ -26,9 +26,6 @@ import org.springframework.beans.factory.BeanFactory;
import org.springframework.core.io.InputStreamSource;
import org.springframework.data.domain.Example;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.data.querydsl.QuerydslUtils;
import org.springframework.data.querydsl.ReactiveQuerydslPredicateExecutor;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport;
import org.springframework.data.repository.core.support.RepositoryFragment;
@@ -42,8 +39,6 @@ import org.springframework.data.repository.query.ReactiveQueryByExampleExecutor;
import org.springframework.lang.Nullable;
import org.springframework.util.ClassUtils;
import com.querydsl.core.types.Predicate;
/**
* {@link RuntimeHintsRegistrar} holding required hints to bootstrap data repositories. <br />
* Already registered via {@literal aot.factories}.
@@ -87,24 +82,6 @@ class RepositoryRuntimeHints implements RuntimeHintsRegistrar {
});
}
if (QuerydslUtils.QUERY_DSL_PRESENT) {
// repository infrastructure
hints.reflection().registerTypes(Arrays.asList( //
TypeReference.of(Predicate.class), //
TypeReference.of(QuerydslPredicateExecutor.class)), builder -> {
builder.withMembers(MemberCategory.INVOKE_PUBLIC_METHODS);
});
if (PROJECT_REACTOR_PRESENT) {
// repository infrastructure
hints.reflection().registerTypes(Arrays.asList( //
TypeReference.of(ReactiveQuerydslPredicateExecutor.class)), builder -> {
builder.withMembers(MemberCategory.INVOKE_PUBLIC_METHODS);
});
}
}
// named queries
hints.reflection().registerTypes(Arrays.asList( //
TypeReference.of(Properties.class), //

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.repository.aot;
package org.springframework.data.repository.config;
import java.lang.annotation.Annotation;
import java.util.Set;

View File

@@ -1,3 +1,35 @@
/*
* Copyright 2022. 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
*
* http://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.
*/
/*
* Copyright 2022. 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
*
* http://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.
*/
/*
* Copyright 2022 the original author or authors.
*
@@ -13,7 +45,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.repository.aot;
package org.springframework.data.repository.config;
import java.lang.reflect.Method;
import java.util.Collection;

View File

@@ -1,3 +1,35 @@
/*
* Copyright 2022. 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
*
* http://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.
*/
/*
* Copyright 2022. 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
*
* http://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.
*/
/*
* Copyright 2022 the original author or authors.
*
@@ -13,7 +45,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.repository.aot;
package org.springframework.data.repository.config;
import java.lang.annotation.Annotation;
import java.util.LinkedHashSet;
@@ -23,8 +55,9 @@ import java.util.stream.Collectors;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.data.aot.AotContext;
import org.springframework.data.aot.TypeCollector;
import org.springframework.data.aot.TypeUtils;
import org.springframework.data.repository.config.AotRepositoryContext;
import org.springframework.data.util.TypeCollector;
import org.springframework.data.util.TypeUtils;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.util.Lazy;

View File

@@ -1,3 +1,35 @@
/*
* Copyright 2022. 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
*
* http://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.
*/
/*
* Copyright 2022. 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
*
* http://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.
*/
/*
* Copyright 2022 the original author or authors.
*
@@ -13,7 +45,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.repository.aot;
package org.springframework.data.repository.config;
import java.util.ArrayList;
import java.util.Collection;
@@ -23,8 +55,6 @@ import java.util.function.Supplier;
import java.util.stream.Collectors;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.data.repository.config.RepositoryConfiguration;
import org.springframework.data.repository.config.RepositoryFragmentConfigurationProvider;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata;
import org.springframework.data.repository.core.support.RepositoryFragment;

View File

@@ -23,7 +23,6 @@ import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.core.io.ResourceLoader;
import org.springframework.data.repository.aot.RepositoryRegistrationAotProcessor;
import org.springframework.lang.NonNull;
/**

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.repository.aot;
package org.springframework.data.repository.config;
import java.io.Serializable;
import java.lang.annotation.Annotation;
@@ -42,13 +42,11 @@ import org.springframework.core.DecoratingProxy;
import org.springframework.core.ResolvableType;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.data.aot.AotContext;
import org.springframework.data.aot.TypeContributor;
import org.springframework.data.aot.TypeUtils;
import org.springframework.data.util.TypeContributor;
import org.springframework.data.util.TypeUtils;
import org.springframework.data.projection.EntityProjectionIntrospector;
import org.springframework.data.projection.TargetAware;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.config.RepositoryConfiguration;
import org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport;
import org.springframework.data.repository.core.support.RepositoryFragment;

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.repository.aot;
package org.springframework.data.repository.config;
import java.lang.annotation.Annotation;
import java.util.Collections;
@@ -35,9 +35,7 @@ import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.RegisteredBean;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.data.aot.TypeContributor;
import org.springframework.data.repository.config.RepositoryConfiguration;
import org.springframework.data.repository.config.RepositoryConfigurationExtension;
import org.springframework.data.util.TypeContributor;
import org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

View File

@@ -15,6 +15,9 @@
*/
package org.springframework.data.util;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.function.Predicate;
import org.springframework.util.Assert;
@@ -27,6 +30,23 @@ import org.springframework.util.Assert;
*/
public interface Predicates {
public static final Predicate<Member> IS_ENUM_MEMBER = member -> member.getDeclaringClass().isEnum();
public static final Predicate<Member> IS_HIBERNATE_MEMBER = member -> member.getName().startsWith("$$_hibernate"); // this
// should
// go
// into
// JPA
public static final Predicate<Member> IS_OBJECT_MEMBER = member -> Object.class.equals(member.getDeclaringClass());
public static final Predicate<Member> IS_JAVA = member -> member.getDeclaringClass().getPackageName().startsWith("java.");
public static final Predicate<Member> IS_NATIVE = member -> Modifier.isNative(member.getModifiers());
public static final Predicate<Member> IS_PRIVATE = member -> Modifier.isPrivate(member.getModifiers());
public static final Predicate<Member> IS_PROTECTED = member -> Modifier.isProtected(member.getModifiers());
public static final Predicate<Member> IS_PUBLIC = member -> Modifier.isPublic(member.getModifiers());
public static final Predicate<Member> IS_SYNTHETIC = Member::isSynthetic;
public static final Predicate<Member> IS_STATIC = member -> Modifier.isStatic(member.getModifiers());
public static final Predicate<Method> IS_BRIDGE_METHOD = Method::isBridge;
/**
* A {@link Predicate} that yields always {@code true}.
*

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.aot;
package org.springframework.data.util;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
@@ -37,7 +37,6 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.ResolvableType;
import org.springframework.data.util.Lazy;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ReflectionUtils;
@@ -179,15 +178,15 @@ public class TypeCollector {
Predicate<Method> excludedDomainsPredicate = methodToTest -> excludedDomainsFilter
.test(methodToTest.getDeclaringClass());
Predicate<Method> excludedMethodsPredicate = Predicates.IS_BRIDGE_METHOD //
.or(Predicates.IS_STATIC) //
.or(Predicates.IS_SYNTHETIC) //
.or(Predicates.IS_NATIVE) //
.or(Predicates.IS_PRIVATE) //
.or(Predicates.IS_PROTECTED) //
.or(Predicates.IS_OBJECT_MEMBER) //
.or(Predicates.IS_HIBERNATE_MEMBER) //
.or(Predicates.IS_ENUM_MEMBER) //
Predicate<Method> excludedMethodsPredicate = org.springframework.data.util.Predicates.IS_BRIDGE_METHOD //
.or(org.springframework.data.util.Predicates.IS_STATIC) //
.or(org.springframework.data.util.Predicates.IS_SYNTHETIC) //
.or(org.springframework.data.util.Predicates.IS_NATIVE) //
.or(org.springframework.data.util.Predicates.IS_PRIVATE) //
.or(org.springframework.data.util.Predicates.IS_PROTECTED) //
.or(org.springframework.data.util.Predicates.IS_OBJECT_MEMBER) //
.or(org.springframework.data.util.Predicates.IS_HIBERNATE_MEMBER) //
.or(org.springframework.data.util.Predicates.IS_ENUM_MEMBER) //
.or(excludedDomainsPredicate.negate()); //
return excludedMethodsPredicate.negate();
@@ -196,8 +195,8 @@ public class TypeCollector {
@SuppressWarnings("rawtypes")
private Predicate<Field> createFieldFilter() {
Predicate<Member> excludedFieldPredicate = Predicates.IS_HIBERNATE_MEMBER //
.or(Predicates.IS_SYNTHETIC) //
Predicate<Member> excludedFieldPredicate = org.springframework.data.util.Predicates.IS_HIBERNATE_MEMBER //
.or(org.springframework.data.util.Predicates.IS_SYNTHETIC) //
.or(Predicates.IS_JAVA);
return (Predicate) excludedFieldPredicate.negate();

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.aot;
package org.springframework.data.util;
import java.lang.annotation.Annotation;
import java.util.Collections;

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.aot;
package org.springframework.data.util;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;