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.x-GH-3387-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 @@ -64,7 +64,7 @@ class DefaultAotContext implements AotContext {

private final Map<Class<?>, ContextualTypeConfiguration> typeConfigurations = new HashMap<>();
private final Environment environment;
private final ReflectiveRuntimeHintsRegistrar runtimeHintsRegistrar = new ReflectiveRuntimeHintsRegistrar();
private final ReflectiveRuntimeHintsRegistrar reflectiveRuntimeHintsRegistrar = new ReflectiveRuntimeHintsRegistrar();

public DefaultAotContext(BeanFactory beanFactory, Environment environment) {
this(beanFactory, environment, new AotMappingContext());
Expand Down Expand Up @@ -250,6 +250,9 @@ private void doContribute(Environment environment, GenerationContext generationC
categories.toArray(MemberCategory[]::new));
}

// check types for presence of @Reflective annotation
reflectiveRuntimeHintsRegistrar.registerRuntimeHints(generationContext.getRuntimeHints(), type);

if (contributeAccessors) {

AccessorContributionConfiguration configuration = AccessorContributionConfiguration.of(environment);
Expand All @@ -259,7 +262,6 @@ private void doContribute(Environment environment, GenerationContext generationC
}

if (forDataBinding) {
runtimeHintsRegistrar.registerRuntimeHints(generationContext.getRuntimeHints(), type);
TypeContributor.contribute(type, Set.of(TypeContributor.DATA_NAMESPACE), generationContext);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import org.springframework.aot.generate.GenerationContext;
import org.springframework.aot.hint.BindingReflectionHintsRegistrar;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.annotation.ReflectiveRuntimeHintsRegistrar;
import org.springframework.core.annotation.MergedAnnotation;

/**
Expand All @@ -32,7 +32,8 @@
public class TypeContributor {

public static final String DATA_NAMESPACE = "org.springframework.data";
public static final BindingReflectionHintsRegistrar REGISTRAR = new BindingReflectionHintsRegistrar();
public static final BindingReflectionHintsRegistrar DATA_BINDING_REGISTRAR = new BindingReflectionHintsRegistrar();
public static final ReflectiveRuntimeHintsRegistrar REFLECTIVE_REGISTRAR = new ReflectiveRuntimeHintsRegistrar();

/**
* Contribute the type with default reflection configuration, skip annotations.
Expand Down Expand Up @@ -67,7 +68,8 @@ public static void contribute(Class<?> type, Predicate<Class<? extends Annotatio
return;
}

REGISTRAR.registerReflectionHints(contribution.getRuntimeHints().reflection(), type);
DATA_BINDING_REGISTRAR.registerReflectionHints(contribution.getRuntimeHints().reflection(), type);
REFLECTIVE_REGISTRAR.registerRuntimeHints(contribution.getRuntimeHints(), type);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
* Copyright 2025 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 static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.aot.hint.predicate.RuntimeHintsPredicates.reflection;

import java.util.List;

import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoSettings;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.annotation.Reflective;
import org.springframework.aot.test.generate.TestGenerationContext;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.mock.env.MockEnvironment;

/**
* Unit tests targeting {@link DefaultAotContext};
*
* @author Christoph Strobl
*/
@MockitoSettings(strictness = org.mockito.quality.Strictness.LENIENT)
public class DefaultAotContextUnitTests {

@Mock BeanFactory beanFactory;

@Mock AotMappingContext mappingContext;

MockEnvironment mockEnvironment = new MockEnvironment();

@Test // GH-3387
void doesNotRegisterReflectionWhenThereIsNothingToRegister() {

DefaultAotContext context = new DefaultAotContext(beanFactory, mockEnvironment, mappingContext);
context.typeConfiguration(Dummy.class, it -> {
// no specific action
});

TestGenerationContext generationContext = new TestGenerationContext();
context.contributeTypeConfigurations(generationContext);

assertThat(generationContext.getRuntimeHints()).matches(reflection().onType(Dummy.class).negate());
}

@Test // GH-3387
void doesNotRegisterReflectionWithCategoryAccordingly() {

DefaultAotContext context = new DefaultAotContext(beanFactory, mockEnvironment, mappingContext);
context.typeConfiguration(Dummy.class, it -> it.forReflectiveAccess(MemberCategory.ACCESS_DECLARED_FIELDS));

TestGenerationContext generationContext = new TestGenerationContext();
context.contributeTypeConfigurations(generationContext);

assertThat(generationContext.getRuntimeHints())
.matches(reflection().onType(Dummy.class).withAnyMemberCategory(MemberCategory.ACCESS_DECLARED_FIELDS));
}

@Test // GH-3387
void registerReflectionIfThereIsAnAtReflectiveAnnotation() throws NoSuchMethodException {

DefaultAotContext context = new DefaultAotContext(beanFactory, mockEnvironment, mappingContext);
context.typeConfiguration(DummyWithAtReflective.class, it -> {

});

TestGenerationContext generationContext = new TestGenerationContext();
context.contributeTypeConfigurations(generationContext);

assertThat(generationContext.getRuntimeHints())
.matches(reflection().onMethodInvocation(DummyWithAtReflective.class.getMethod("reflectiveAnnotated")))
.matches(reflection().onMethodInvocation(DummyWithAtReflective.class.getMethod("getValue")).negate())
.matches(reflection().onMethodInvocation(DummyWithAtReflective.class.getMethod("justAMethod")).negate());
}

@Test // GH-3387
void registerReflectionForGetterSetterIfDataBindingRequested() throws NoSuchMethodException {

DefaultAotContext context = new DefaultAotContext(beanFactory, mockEnvironment, mappingContext);
context.typeConfiguration(DummyWithAtReflective.class, AotTypeConfiguration::forDataBinding);

TestGenerationContext generationContext = new TestGenerationContext();
context.contributeTypeConfigurations(generationContext);

assertThat(generationContext.getRuntimeHints())
.matches(reflection().onMethodInvocation(DummyWithAtReflective.class.getMethod("reflectiveAnnotated")))
.matches(reflection().onMethodInvocation(DummyWithAtReflective.class.getMethod("getValue")))
.matches(reflection().onMethodInvocation(DummyWithAtReflective.class.getMethod("justAMethod")).negate());
}

@Test // GH-3387
void registerReflectionIfThereIsAnAtReflectiveAnnotationInTheSuperType() throws NoSuchMethodException {
DefaultAotContext context = new DefaultAotContext(beanFactory, mockEnvironment, mappingContext);
context.typeConfiguration(ExtendingDummyWithAtReflective.class, it -> {

});

TestGenerationContext generationContext = new TestGenerationContext();
context.contributeTypeConfigurations(generationContext);

assertThat(generationContext.getRuntimeHints())
.matches(reflection().onMethodInvocation(DummyWithAtReflective.class.getMethod("reflectiveAnnotated")))
.matches(reflection().onMethodInvocation(DummyWithAtReflective.class.getMethod("justAMethod")).negate())
.matches(reflection().onMethodInvocation(DummyWithAtReflective.class.getMethod("getValue")).negate());
}

static class Dummy {

String value;

public List<String> justAMethod() {
return null;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}
}

static class DummyWithAtReflective extends Dummy {

@Reflective
public List<String> reflectiveAnnotated() {
return null;
}
}

static class ExtendingDummyWithAtReflective extends DummyWithAtReflective {}

}