Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>4.0.0-SNAPSHOT</version>
<version>4.0.0-GH-3362-SNAPSHOT</version>

<name>Spring Data Core</name>
<description>Core Spring concepts underpinning every Spring Data module.</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.Set;
import java.util.function.Consumer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand All @@ -35,6 +36,7 @@
import org.springframework.core.env.StandardEnvironment;
import org.springframework.data.domain.ManagedTypes;
import org.springframework.data.util.Lazy;
import org.springframework.data.util.TypeCollector;
import org.springframework.data.util.TypeContributor;
import org.springframework.data.util.TypeUtils;
import org.springframework.util.ClassUtils;
Expand Down Expand Up @@ -125,9 +127,19 @@ private ManagedTypes resolveManagedTypes(RegisteredBean registeredBean) {
*/
protected BeanRegistrationAotContribution contribute(AotContext aotContext, ManagedTypes managedTypes,
RegisteredBean registeredBean) {
return new ManagedTypesRegistrationAotContribution(aotContext, managedTypes, registeredBean, this::contributeType);
return new ManagedTypesRegistrationAotContribution(aotContext, managedTypes, registeredBean,
typeCollectorCustomizer(), this::contributeType);
}

/**
* Customization hook to configure {@link TypeCollector}.
*
* @return a {@link Consumer} to customize the {@link TypeCollector}, must not be {@literal null}.
* @since 4.0
*/
protected Consumer<TypeCollector> typeCollectorCustomizer() {
return typeCollector -> {};
}
/**
* Hook to contribute configuration for a given {@literal type}.
*
Expand All @@ -142,14 +154,27 @@ protected void contributeType(ResolvableType type, GenerationContext generationC

Set<String> annotationNamespaces = Collections.singleton(TypeContributor.DATA_NAMESPACE);

aotContext.typeConfiguration(type, config -> config.forDataBinding() //
.contributeAccessors() //
.forQuerydsl().contribute(environment.get(), generationContext));
configureTypeContribution(type.toClass(), aotContext);

aotContext.typeConfiguration(type, config -> {
config.contribute(environment.get(), generationContext);
});

TypeUtils.resolveUsedAnnotations(type.toClass()).forEach(
annotation -> TypeContributor.contribute(annotation.getType(), annotationNamespaces, generationContext));
}

/**
* Customization hook to configure the {@link TypeContributor} used to register the given {@literal type}.
*
* @param type the class to configure the contribution for.
* @param aotContext AOT context for type configuration.
* @since 4.0
*/
protected void configureTypeContribution(Class<?> type, AotContext aotContext) {
aotContext.typeConfiguration(type, config -> config.forDataBinding().contributeAccessors().forQuerydsl());
}

protected boolean isMatch(@Nullable Class<?> beanType, @Nullable String beanName) {
return matchesByType(beanType) && matchesPrefix(beanName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

import javax.lang.model.element.Modifier;

import org.jspecify.annotations.Nullable;

import org.springframework.aot.generate.AccessControl;
import org.springframework.aot.generate.GeneratedMethod;
import org.springframework.aot.generate.GenerationContext;
Expand Down Expand Up @@ -74,17 +75,18 @@
class ManagedTypesRegistrationAotContribution implements RegisteredBeanAotContribution {

private final AotContext aotContext;
private final ManagedTypes managedTypes;
private final Lazy<List<Class<?>>> sourceTypes;
private final Consumer<TypeCollector> typeCollectorCustomizer;
private final TypeRegistration contributionAction;
private final RegisteredBean source;

public ManagedTypesRegistrationAotContribution(AotContext aotContext, ManagedTypes managedTypes,
RegisteredBean registeredBean, TypeRegistration contributionAction) {
RegisteredBean registeredBean, Consumer<TypeCollector> typeCollectorCustomizer,
TypeRegistration contributionAction) {

this.aotContext = aotContext;
this.managedTypes = managedTypes;
this.sourceTypes = Lazy.of(managedTypes::toList);
this.typeCollectorCustomizer = typeCollectorCustomizer;
this.contributionAction = contributionAction;
this.source = registeredBean;
}
Expand All @@ -95,18 +97,15 @@ public void applyTo(GenerationContext generationContext, BeanRegistrationCode be
List<Class<?>> types = sourceTypes.get();

if (!types.isEmpty()) {
TypeCollector.inspect(types).forEach(type -> contributionAction.register(type, generationContext, aotContext));
TypeCollector.inspect(typeCollectorCustomizer, types)
.forEach(type -> contributionAction.register(type, generationContext, aotContext));
}
}

@Override
public BeanRegistrationCodeFragments customizeBeanRegistrationCodeFragments(GenerationContext generationContext,
BeanRegistrationCodeFragments codeFragments) {

if (managedTypes == null) {
return codeFragments;
}

ManagedTypesInstanceCodeFragment fragment = new ManagedTypesInstanceCodeFragment(sourceTypes.get(), source,
codeFragments);
return fragment.canGenerateCode() ? fragment : codeFragments;
Expand Down
18 changes: 13 additions & 5 deletions src/main/java/org/springframework/data/util/Predicates.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,9 @@
public interface Predicates {

Predicate<Member> IS_ENUM_MEMBER = member -> member.getDeclaringClass().isEnum();
Predicate<Member> IS_HIBERNATE_MEMBER = member -> member.getName().startsWith("$$_hibernate"); // this
// should
// go
// into
// JPA

@Deprecated(since = "4.0", forRemoval = true)
Predicate<Member> IS_HIBERNATE_MEMBER = member -> member.getName().startsWith("$$_hibernate");
Predicate<Member> IS_OBJECT_MEMBER = member -> Object.class.equals(member.getDeclaringClass());
Predicate<Member> IS_JAVA = member -> member.getDeclaringClass().getPackageName().startsWith("java.");
Predicate<Member> IS_NATIVE = member -> Modifier.isNative(member.getModifiers());
Expand All @@ -51,6 +49,16 @@ public interface Predicates {

Predicate<Method> IS_BRIDGE_METHOD = Method::isBridge;

/**
* A {@link Predicate} that introspects the declaring class of the member.
*
* @return a {@link Predicate} that introspects the declaring class of the member.
* @since 4.0
*/
static <T extends Member> Predicate<T> declaringClass(Predicate<Class<?>> predicate) {
return t -> predicate.test(t.getDeclaringClass());
}

/**
* A {@link Predicate} that yields always {@code true}.
*
Expand Down
Loading