Browse Source

Improve registration of the same hint for multiple classes

Based on the feedback in #28977 an easy way to create a list of
type references based on a vararg of classes is helpful when
registering the same hints for several types.
pull/28987/head
Stephane Nicoll 2 years ago
parent
commit
d6afa8df2d
  1. 3
      spring-context-support/src/main/java/org/springframework/scheduling/quartz/LocalTaskExecutorThreadPool.java
  2. 43
      spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerFactoryBeanRuntimeHints.java
  3. 34
      spring-core-test/src/test/java/org/springframework/aot/agent/InstrumentedMethodTests.java
  4. 13
      spring-core/src/main/java/org/springframework/aot/hint/TypeReference.java
  5. 5
      spring-core/src/test/java/org/springframework/aot/hint/ReflectionHintsTests.java
  6. 6
      spring-core/src/test/java/org/springframework/aot/hint/TypeHintTests.java
  7. 123
      spring-core/src/test/java/org/springframework/aot/hint/predicate/ReflectionHintsPredicatesTests.java
  8. 4
      spring-core/src/test/java/org/springframework/aot/nativex/FileNativeConfigurationWriterTests.java
  9. 22
      spring-core/src/test/java/org/springframework/aot/nativex/ReflectionHintsWriterTests.java
  10. 11
      spring-tx/src/main/java/org/springframework/transaction/annotation/TransactionRuntimeHints.java
  11. 18
      spring-web/src/main/java/org/springframework/http/codec/CodecConfigurerRuntimeHints.java

3
spring-context-support/src/main/java/org/springframework/scheduling/quartz/LocalTaskExecutorThreadPool.java

@ -24,6 +24,7 @@ import org.apache.commons.logging.LogFactory; @@ -24,6 +24,7 @@ import org.apache.commons.logging.LogFactory;
import org.quartz.SchedulerConfigException;
import org.quartz.spi.ThreadPool;
import org.springframework.aot.hint.annotation.Reflective;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
@ -45,10 +46,12 @@ public class LocalTaskExecutorThreadPool implements ThreadPool { @@ -45,10 +46,12 @@ public class LocalTaskExecutorThreadPool implements ThreadPool {
@Override
@Reflective
public void setInstanceId(String schedInstId) {
}
@Override
@Reflective
public void setInstanceName(String schedName) {
}

43
spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerFactoryBeanRuntimeHints.java

@ -16,12 +16,14 @@ @@ -16,12 +16,14 @@
package org.springframework.scheduling.quartz;
import java.util.List;
import java.util.function.Consumer;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.aot.hint.TypeHint.Builder;
import org.springframework.aot.hint.TypeReference;
import org.springframework.aot.hint.annotation.ReflectiveRuntimeHintsRegistrar;
import org.springframework.util.ClassUtils;
/**
@ -29,36 +31,29 @@ import org.springframework.util.ClassUtils; @@ -29,36 +31,29 @@ import org.springframework.util.ClassUtils;
* reflection entries are registered.
*
* @author Sebastien Deleuze
* @author Stephane Nicoll
* @since 6.0
*/
public class SchedulerFactoryBeanRuntimeHints implements RuntimeHintsRegistrar {
class SchedulerFactoryBeanRuntimeHints implements RuntimeHintsRegistrar {
private static String SCHEDULER_FACTORY_CLASS_NAME = "org.quartz.impl.StdSchedulerFactory";
private static final String SCHEDULER_FACTORY_CLASS_NAME = "org.quartz.impl.StdSchedulerFactory";
private static TypeReference FACTORY_BEAN_TYPE_REFERENCE = TypeReference.of(SchedulerFactoryBean.class);
private static final TypeReference FACTORY_BEAN_TYPE_REFERENCE = TypeReference.of(SchedulerFactoryBean.class);
private final ReflectiveRuntimeHintsRegistrar reflectiveRegistrar = new ReflectiveRuntimeHintsRegistrar();
@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
if (ClassUtils.isPresent(SCHEDULER_FACTORY_CLASS_NAME, classLoader)) {
hints.reflection().registerType(TypeReference.of(SCHEDULER_FACTORY_CLASS_NAME),
builder -> builder
.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)
.onReachableType(FACTORY_BEAN_TYPE_REFERENCE));
hints.reflection().registerType(ResourceLoaderClassLoadHelper.class,
builder -> builder
.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)
.onReachableType(FACTORY_BEAN_TYPE_REFERENCE));
hints.reflection().registerType(LocalTaskExecutorThreadPool.class,
builder -> builder
.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)
.withMethod("setInstanceId", List.of(TypeReference.of(String.class)), b -> {})
.withMethod("setInstanceName", List.of(TypeReference.of(String.class)), b -> {})
.onReachableType(FACTORY_BEAN_TYPE_REFERENCE));
hints.reflection().registerType(LocalDataSourceJobStore.class,
builder -> builder
.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)
.onReachableType(FACTORY_BEAN_TYPE_REFERENCE));
if (!ClassUtils.isPresent(SCHEDULER_FACTORY_CLASS_NAME, classLoader)) {
return;
}
Consumer<Builder> typeHint = type -> type
.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)
.onReachableType(FACTORY_BEAN_TYPE_REFERENCE);
hints.reflection()
.registerType(TypeReference.of(SCHEDULER_FACTORY_CLASS_NAME), typeHint)
.registerTypes(TypeReference.listOf(ResourceLoaderClassLoadHelper.class,
LocalTaskExecutorThreadPool.class, LocalDataSourceJobStore.class), typeHint);
this.reflectiveRegistrar.registerRuntimeHints(hints, LocalTaskExecutorThreadPool.class);
}
}

34
spring-core-test/src/test/java/org/springframework/aot/agent/InstrumentedMethodTests.java

@ -121,7 +121,7 @@ class InstrumentedMethodTests { @@ -121,7 +121,7 @@ class InstrumentedMethodTests {
this.stringGetConstructor = RecordedInvocation.of(InstrumentedMethod.CLASS_GETCONSTRUCTOR)
.onInstance(String.class).withArgument(new Class[0]).returnValue(String.class.getConstructor()).build();
this.stringGetDeclaredConstructor = RecordedInvocation.of(InstrumentedMethod.CLASS_GETDECLAREDCONSTRUCTOR)
.onInstance(String.class).withArgument(new Class[] {byte[].class, byte.class})
.onInstance(String.class).withArgument(new Class[] { byte[].class, byte.class })
.returnValue(String.class.getDeclaredConstructor(byte[].class, byte.class)).build();
}
@ -201,15 +201,15 @@ class InstrumentedMethodTests { @@ -201,15 +201,15 @@ class InstrumentedMethodTests {
@Test
void classGetDeclaredConstructorShouldMatchInstrospectConstructorHint() {
List<TypeReference> parameterTypes = List.of(TypeReference.of(byte[].class), TypeReference.of(byte.class));
hints.reflection().registerType(String.class, typeHint -> typeHint.withConstructor(parameterTypes, constructorHint -> constructorHint.setModes(ExecutableMode.INTROSPECT)));
hints.reflection().registerType(String.class, typeHint -> typeHint.withConstructor(TypeReference.listOf(byte[].class, byte.class),
constructorHint -> constructorHint.setModes(ExecutableMode.INTROSPECT)));
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETDECLAREDCONSTRUCTOR, this.stringGetDeclaredConstructor);
}
@Test
void classGetDeclaredConstructorShouldMatchInvokeConstructorHint() {
List<TypeReference> parameterTypes = List.of(TypeReference.of(byte[].class), TypeReference.of(byte.class));
hints.reflection().registerType(String.class, typeHint -> typeHint.withConstructor(parameterTypes, constructorHint -> constructorHint.setModes(ExecutableMode.INVOKE)));
hints.reflection().registerType(String.class, typeHint -> typeHint.withConstructor(TypeReference.listOf(byte[].class, byte.class),
constructorHint -> constructorHint.setModes(ExecutableMode.INVOKE)));
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETDECLAREDCONSTRUCTOR, this.stringGetDeclaredConstructor);
}
@ -265,33 +265,33 @@ class InstrumentedMethodTests { @@ -265,33 +265,33 @@ class InstrumentedMethodTests {
.onInstance(String.class).withArguments("toString", new Class[0])
.returnValue(String.class.getMethod("toString")).build();
this.stringGetScaleMethod = RecordedInvocation.of(InstrumentedMethod.CLASS_GETDECLAREDMETHOD)
.onInstance(String.class).withArguments("scale", new Class[] {int.class, float.class})
.onInstance(String.class).withArguments("scale", new Class[] { int.class, float.class })
.returnValue(String.class.getDeclaredMethod("scale", int.class, float.class)).build();
}
@Test
void classGetDeclaredMethodShouldMatchIntrospectDeclaredMethodsHint() throws NoSuchMethodException {
void classGetDeclaredMethodShouldMatchIntrospectDeclaredMethodsHint() {
hints.reflection().registerType(String.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_METHODS));
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETDECLAREDMETHOD, this.stringGetScaleMethod);
}
@Test
void classGetDeclaredMethodShouldNotMatchIntrospectPublicMethodsHint() throws NoSuchMethodException {
void classGetDeclaredMethodShouldNotMatchIntrospectPublicMethodsHint() {
hints.reflection().registerType(String.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS));
assertThatInvocationDoesNotMatch(InstrumentedMethod.CLASS_GETDECLAREDMETHOD, this.stringGetScaleMethod);
}
@Test
void classGetDeclaredMethodShouldMatchIntrospectMethodHint() throws NoSuchMethodException {
List<TypeReference> parameterTypes = List.of(TypeReference.of(int.class), TypeReference.of(float.class));
void classGetDeclaredMethodShouldMatchIntrospectMethodHint() {
List<TypeReference> parameterTypes = TypeReference.listOf(int.class, float.class);
hints.reflection().registerType(String.class, typeHint ->
typeHint.withMethod("scale", parameterTypes, methodHint -> methodHint.withMode(ExecutableMode.INTROSPECT)));
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETDECLAREDMETHOD, this.stringGetScaleMethod);
}
@Test
void classGetDeclaredMethodShouldMatchInvokeMethodHint() throws NoSuchMethodException {
List<TypeReference> parameterTypes = List.of(TypeReference.of(int.class), TypeReference.of(float.class));
void classGetDeclaredMethodShouldMatchInvokeMethodHint() {
List<TypeReference> parameterTypes = TypeReference.listOf(int.class, float.class);
hints.reflection().registerType(String.class, typeHint ->
typeHint.withMethod("scale", parameterTypes, methodHint -> methodHint.withMode(ExecutableMode.INVOKE)));
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETDECLAREDMETHOD, this.stringGetScaleMethod);
@ -383,7 +383,7 @@ class InstrumentedMethodTests { @@ -383,7 +383,7 @@ class InstrumentedMethodTests {
}
@Test
void classGetMethodShouldMatchInvokeMethodHint() throws Exception {
void classGetMethodShouldMatchInvokeMethodHint() {
hints.reflection().registerType(String.class, typeHint ->
typeHint.withMethod("toString", Collections.emptyList(), methodHint -> methodHint.setModes(ExecutableMode.INVOKE)));
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETMETHOD, this.stringGetToStringMethod);
@ -410,9 +410,9 @@ class InstrumentedMethodTests { @@ -410,9 +410,9 @@ class InstrumentedMethodTests {
@Test
void methodInvokeShouldMatchInvokeHintOnMethod() throws NoSuchMethodException {
RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.METHOD_INVOKE)
.onInstance(String.class.getMethod("startsWith", String.class)).withArguments("testString", new Object[] {"test"}).build();
.onInstance(String.class.getMethod("startsWith", String.class)).withArguments("testString", new Object[] { "test" }).build();
hints.reflection().registerType(String.class, typeHint -> typeHint.withMethod("startsWith",
List.of(TypeReference.of(String.class)), methodHint -> methodHint.withMode(ExecutableMode.INVOKE)));
TypeReference.listOf(String.class), methodHint -> methodHint.withMode(ExecutableMode.INVOKE)));
assertThatInvocationMatches(InstrumentedMethod.METHOD_INVOKE, invocation);
}
@ -600,8 +600,8 @@ class InstrumentedMethodTests { @@ -600,8 +600,8 @@ class InstrumentedMethodTests {
@BeforeEach
void setup() {
this.newProxyInstance = RecordedInvocation.of(InstrumentedMethod.PROXY_NEWPROXYINSTANCE)
.withArguments(ClassLoader.getSystemClassLoader(), new Class[] {AutoCloseable.class, Comparator.class}, null)
.returnValue(Proxy.newProxyInstance(ClassLoader.getSystemClassLoader(), new Class[] {AutoCloseable.class, Comparator.class}, (proxy, method, args) -> null))
.withArguments(ClassLoader.getSystemClassLoader(), new Class[] { AutoCloseable.class, Comparator.class }, null)
.returnValue(Proxy.newProxyInstance(ClassLoader.getSystemClassLoader(), new Class[] { AutoCloseable.class, Comparator.class }, (proxy, method, args) -> null))
.build();
}

13
spring-core/src/main/java/org/springframework/aot/hint/TypeReference.java

@ -16,6 +16,9 @@ @@ -16,6 +16,9 @@
package org.springframework.aot.hint;
import java.util.Arrays;
import java.util.List;
import org.springframework.lang.Nullable;
/**
@ -81,4 +84,14 @@ public interface TypeReference { @@ -81,4 +84,14 @@ public interface TypeReference {
return SimpleTypeReference.of(className);
}
/**
* Create a list of {@link TypeReference type references} mapped by the specified
* types.
* @param types the types to map
* @return a list of type references
*/
static List<TypeReference> listOf(Class<?>... types) {
return Arrays.stream(types).map(TypeReference::of).toList();
}
}

5
spring-core/src/test/java/org/springframework/aot/hint/ReflectionHintsTests.java

@ -19,7 +19,6 @@ package org.springframework.aot.hint; @@ -19,7 +19,6 @@ package org.springframework.aot.hint;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.function.Consumer;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
@ -109,8 +108,8 @@ class ReflectionHintsTests { @@ -109,8 +108,8 @@ class ReflectionHintsTests {
@Test
void registerTypesApplyTheSameHints() {
this.reflectionHints.registerTypes(Stream.of(Integer.class, String.class, Double.class)
.map(TypeReference::of).toList(), hint -> hint.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS));
this.reflectionHints.registerTypes(TypeReference.listOf(Integer.class, String.class, Double.class),
hint -> hint.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS));
assertThat(this.reflectionHints.typeHints())
.anySatisfy(
typeWithMemberCategories(Integer.class, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS))

6
spring-core/src/test/java/org/springframework/aot/hint/TypeHintTests.java

@ -82,7 +82,7 @@ class TypeHintTests { @@ -82,7 +82,7 @@ class TypeHintTests {
@Test
void createWithConstructor() {
List<TypeReference> parameterTypes = List.of(TypeReference.of(byte[].class), TypeReference.of(int.class));
List<TypeReference> parameterTypes = TypeReference.listOf(byte[].class, int.class);
TypeHint hint = TypeHint.of(TypeReference.of(String.class)).withConstructor(parameterTypes,
constructorHint -> constructorHint.withMode(ExecutableMode.INVOKE)).build();
assertThat(hint.constructors()).singleElement().satisfies(constructorHint -> {
@ -93,7 +93,7 @@ class TypeHintTests { @@ -93,7 +93,7 @@ class TypeHintTests {
@Test
void createConstructorReuseBuilder() {
List<TypeReference> parameterTypes = List.of(TypeReference.of(byte[].class), TypeReference.of(int.class));
List<TypeReference> parameterTypes = TypeReference.listOf(byte[].class, int.class);
Builder builder = TypeHint.of(TypeReference.of(String.class)).withConstructor(parameterTypes,
constructorHint -> constructorHint.withMode(ExecutableMode.INVOKE));
TypeHint hint = builder.withConstructor(parameterTypes, constructorHint ->
@ -118,7 +118,7 @@ class TypeHintTests { @@ -118,7 +118,7 @@ class TypeHintTests {
@Test
void createWithMethodReuseBuilder() {
List<TypeReference> parameterTypes = List.of(TypeReference.of(char[].class));
List<TypeReference> parameterTypes = TypeReference.listOf(char[].class);
Builder builder = TypeHint.of(TypeReference.of(String.class)).withMethod("valueOf", parameterTypes,
methodHint -> methodHint.withMode(ExecutableMode.INVOKE));
TypeHint hint = builder.withMethod("valueOf", parameterTypes,

123
spring-core/src/test/java/org/springframework/aot/hint/predicate/ReflectionHintsPredicatesTests.java

@ -19,7 +19,6 @@ package org.springframework.aot.hint.predicate; @@ -19,7 +19,6 @@ package org.springframework.aot.hint.predicate;
import java.lang.reflect.Constructor;
import java.util.Collections;
import java.util.List;
import java.util.function.Predicate;
import org.junit.jupiter.api.BeforeAll;
@ -66,41 +65,44 @@ class ReflectionHintsPredicatesTests { @@ -66,41 +65,44 @@ class ReflectionHintsPredicatesTests {
@Test
void reflectionOnClassShouldMatchIntrospection() {
runtimeHints.reflection().registerType(SampleClass.class, builder -> {
});
runtimeHints.reflection().registerType(SampleClass.class, builder -> {});
assertPredicateMatches(reflection.onType(SampleClass.class));
}
@Test
void reflectionOnTypeReferenceShouldMatchIntrospection() {
runtimeHints.reflection().registerType(SampleClass.class, builder -> {
});
runtimeHints.reflection().registerType(SampleClass.class, builder -> {});
assertPredicateMatches(reflection.onType(TypeReference.of(SampleClass.class)));
}
@Test
void reflectionOnDifferentClassShouldNotMatchIntrospection() {
runtimeHints.reflection().registerType(Integer.class, builder -> {
});
runtimeHints.reflection().registerType(Integer.class, builder -> {});
assertPredicateDoesNotMatch(reflection.onType(TypeReference.of(SampleClass.class)));
}
@Test
void typeWithMemberCategoryFailsWithNullCategory() {
runtimeHints.reflection().registerType(SampleClass.class, builder -> builder.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS));
assertThatIllegalArgumentException().isThrownBy(() -> reflection.onType(SampleClass.class).withMemberCategory(null));
runtimeHints.reflection().registerType(SampleClass.class, builder ->
builder.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS));
assertThatIllegalArgumentException().isThrownBy(() ->
reflection.onType(SampleClass.class).withMemberCategory(null));
}
@Test
void typeWithMemberCategoryMatchesCategory() {
runtimeHints.reflection().registerType(SampleClass.class, builder -> builder.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS));
assertPredicateMatches(reflection.onType(SampleClass.class).withMemberCategory(MemberCategory.INTROSPECT_PUBLIC_METHODS));
runtimeHints.reflection().registerType(SampleClass.class,
builder -> builder.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS));
assertPredicateMatches(reflection.onType(SampleClass.class)
.withMemberCategory(MemberCategory.INTROSPECT_PUBLIC_METHODS));
}
@Test
void typeWithMemberCategoryDoesNotMatchOtherCategory() {
runtimeHints.reflection().registerType(SampleClass.class, builder -> builder.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS));
assertPredicateDoesNotMatch(reflection.onType(SampleClass.class).withMemberCategory(MemberCategory.INVOKE_PUBLIC_METHODS));
runtimeHints.reflection().registerType(SampleClass.class,
builder -> builder.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS));
assertPredicateDoesNotMatch(reflection.onType(SampleClass.class)
.withMemberCategory(MemberCategory.INVOKE_PUBLIC_METHODS));
}
@Test
@ -120,20 +122,26 @@ class ReflectionHintsPredicatesTests { @@ -120,20 +122,26 @@ class ReflectionHintsPredicatesTests {
@Test
void typeWithAnyMemberCategoryFailsWithNullCategories() {
runtimeHints.reflection().registerType(SampleClass.class, builder -> builder.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS));
assertThatIllegalArgumentException().isThrownBy(() -> reflection.onType(SampleClass.class).withAnyMemberCategory(new MemberCategory[0]));
runtimeHints.reflection().registerType(SampleClass.class, builder ->
builder.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS));
assertThatIllegalArgumentException().isThrownBy(() ->
reflection.onType(SampleClass.class).withAnyMemberCategory(new MemberCategory[0]));
}
@Test
void typeWithAnyMemberCategoryMatchesCategory() {
runtimeHints.reflection().registerType(SampleClass.class, builder -> builder.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS, MemberCategory.INVOKE_PUBLIC_METHODS));
assertPredicateMatches(reflection.onType(SampleClass.class).withAnyMemberCategory(MemberCategory.INTROSPECT_PUBLIC_METHODS));
runtimeHints.reflection().registerType(SampleClass.class,
builder -> builder.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS, MemberCategory.INVOKE_PUBLIC_METHODS));
assertPredicateMatches(reflection.onType(SampleClass.class)
.withAnyMemberCategory(MemberCategory.INTROSPECT_PUBLIC_METHODS));
}
@Test
void typeWithAnyMemberCategoryDoesNotMatchOtherCategory() {
runtimeHints.reflection().registerType(SampleClass.class, builder -> builder.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS, MemberCategory.INVOKE_PUBLIC_METHODS));
assertPredicateDoesNotMatch(reflection.onType(SampleClass.class).withAnyMemberCategory(MemberCategory.INVOKE_DECLARED_METHODS));
runtimeHints.reflection().registerType(SampleClass.class,
builder -> builder.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS, MemberCategory.INVOKE_PUBLIC_METHODS));
assertPredicateDoesNotMatch(reflection.onType(SampleClass.class)
.withAnyMemberCategory(MemberCategory.INVOKE_DECLARED_METHODS));
}
}
@ -148,140 +156,157 @@ class ReflectionHintsPredicatesTests { @@ -148,140 +156,157 @@ class ReflectionHintsPredicatesTests {
@Test
void constructorIntrospectionMatchesConstructorHint() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withConstructor(Collections.emptyList(), constructorHint -> {
}));
runtimeHints.reflection().registerType(SampleClass.class, typeHint ->
typeHint.withConstructor(Collections.emptyList(), constructorHint -> {}));
assertPredicateMatches(reflection.onConstructor(publicConstructor).introspect());
}
@Test
void constructorIntrospectionMatchesIntrospectPublicConstructors() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS));
runtimeHints.reflection().registerType(SampleClass.class, typeHint ->
typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS));
assertPredicateMatches(reflection.onConstructor(publicConstructor).introspect());
}
@Test
void constructorIntrospectionMatchesInvokePublicConstructors() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS));
runtimeHints.reflection().registerType(SampleClass.class, typeHint ->
typeHint.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS));
assertPredicateMatches(reflection.onConstructor(publicConstructor).introspect());
}
@Test
void constructorIntrospectionMatchesIntrospectDeclaredConstructors() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_CONSTRUCTORS));
runtimeHints.reflection().registerType(SampleClass.class, typeHint ->
typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_CONSTRUCTORS));
assertPredicateMatches(reflection.onConstructor(publicConstructor).introspect());
}
@Test
void constructorIntrospectionMatchesInvokeDeclaredConstructors() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS));
runtimeHints.reflection().registerType(SampleClass.class, typeHint ->
typeHint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS));
assertPredicateMatches(reflection.onConstructor(publicConstructor).introspect());
}
@Test
void constructorInvocationDoesNotMatchConstructorHint() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withConstructor(Collections.emptyList(), constructorHint -> {
}));
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.
withConstructor(Collections.emptyList(), constructorHint -> {}));
assertPredicateDoesNotMatch(reflection.onConstructor(publicConstructor).invoke());
}
@Test
void constructorInvocationMatchesConstructorInvocationHint() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withConstructor(Collections.emptyList(), constructorHint -> constructorHint.withMode(ExecutableMode.INVOKE)));
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.
withConstructor(Collections.emptyList(), constructorHint ->
constructorHint.withMode(ExecutableMode.INVOKE)));
assertPredicateMatches(reflection.onConstructor(publicConstructor).invoke());
}
@Test
void constructorInvocationDoesNotMatchIntrospectPublicConstructors() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS));
runtimeHints.reflection().registerType(SampleClass.class, typeHint ->
typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS));
assertPredicateDoesNotMatch(reflection.onConstructor(publicConstructor).invoke());
}
@Test
void constructorInvocationMatchesInvokePublicConstructors() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS));
runtimeHints.reflection().registerType(SampleClass.class, typeHint ->
typeHint.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS));
assertPredicateMatches(reflection.onConstructor(publicConstructor).invoke());
}
@Test
void constructorInvocationDoesNotMatchIntrospectDeclaredConstructors() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_CONSTRUCTORS));
runtimeHints.reflection().registerType(SampleClass.class, typeHint ->
typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_CONSTRUCTORS));
assertPredicateDoesNotMatch(reflection.onConstructor(publicConstructor).invoke());
}
@Test
void constructorInvocationMatchesInvokeDeclaredConstructors() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS));
runtimeHints.reflection().registerType(SampleClass.class, typeHint ->
typeHint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS));
assertPredicateMatches(reflection.onConstructor(publicConstructor).invoke());
}
@Test
void privateConstructorIntrospectionMatchesConstructorHint() {
List<TypeReference> parameterTypes = Collections.singletonList(TypeReference.of(String.class));
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withConstructor(parameterTypes, constructorHint -> {
}));
runtimeHints.reflection().registerType(SampleClass.class, typeHint ->
typeHint.withConstructor(TypeReference.listOf(String.class), constructorHint -> {}));
assertPredicateMatches(reflection.onConstructor(privateConstructor).introspect());
}
@Test
void privateConstructorIntrospectionDoesNotMatchIntrospectPublicConstructors() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS));
runtimeHints.reflection().registerType(SampleClass.class, typeHint ->
typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS));
assertPredicateDoesNotMatch(reflection.onConstructor(privateConstructor).introspect());
}
@Test
void privateConstructorIntrospectionDoesNotMatchInvokePublicConstructors() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS));
runtimeHints.reflection().registerType(SampleClass.class, typeHint ->
typeHint.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS));
assertPredicateDoesNotMatch(reflection.onConstructor(privateConstructor).introspect());
}
@Test
void privateConstructorIntrospectionMatchesIntrospectDeclaredConstructors() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_CONSTRUCTORS));
runtimeHints.reflection().registerType(SampleClass.class, typeHint ->
typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_CONSTRUCTORS));
assertPredicateMatches(reflection.onConstructor(privateConstructor).introspect());
}
@Test
void privateConstructorIntrospectionMatchesInvokeDeclaredConstructors() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS));
runtimeHints.reflection().registerType(SampleClass.class, typeHint ->
typeHint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS));
assertPredicateMatches(reflection.onConstructor(privateConstructor).introspect());
}
@Test
void privateConstructorInvocationDoesNotMatchConstructorHint() {
List<TypeReference> parameterTypes = Collections.singletonList(TypeReference.of(String.class));
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withConstructor(parameterTypes, constructorHint -> {
}));
runtimeHints.reflection().registerType(SampleClass.class, typeHint ->
typeHint.withConstructor(TypeReference.listOf(String.class), constructorHint -> {}));
assertPredicateDoesNotMatch(reflection.onConstructor(privateConstructor).invoke());
}
@Test
void privateConstructorInvocationMatchesConstructorInvocationHint() {
List<TypeReference> parameterTypes = Collections.singletonList(TypeReference.of(String.class));
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withConstructor(parameterTypes, constructorHint -> constructorHint.withMode(ExecutableMode.INVOKE)));
runtimeHints.reflection().registerType(SampleClass.class, typeHint ->
typeHint.withConstructor(TypeReference.listOf(String.class),
constructorHint -> constructorHint.withMode(ExecutableMode.INVOKE)));
assertPredicateMatches(reflection.onConstructor(privateConstructor).invoke());
}
@Test
void privateConstructorInvocationDoesNotMatchIntrospectPublicConstructors() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS));
runtimeHints.reflection().registerType(SampleClass.class, typeHint ->
typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS));
assertPredicateDoesNotMatch(reflection.onConstructor(privateConstructor).invoke());
}
@Test
void privateConstructorInvocationDoesNotMatchInvokePublicConstructors() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS));
runtimeHints.reflection().registerType(SampleClass.class, typeHint ->
typeHint.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS));
assertPredicateDoesNotMatch(reflection.onConstructor(privateConstructor).invoke());
}
@Test
void privateConstructorInvocationDoesNotMatchIntrospectDeclaredConstructors() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_CONSTRUCTORS));
runtimeHints.reflection().registerType(SampleClass.class, typeHint ->
typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_CONSTRUCTORS));
assertPredicateDoesNotMatch(reflection.onConstructor(privateConstructor).invoke());
}
@Test
void privateConstructorInvocationMatchesInvokeDeclaredConstructors() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS));
runtimeHints.reflection().registerType(SampleClass.class, typeHint ->
typeHint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS));
assertPredicateMatches(reflection.onConstructor(privateConstructor).invoke());
}
@ -460,7 +485,7 @@ class ReflectionHintsPredicatesTests { @@ -460,7 +485,7 @@ class ReflectionHintsPredicatesTests {
@Test
void fieldWriteReflectionMatchesFieldHintWithWrite() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint ->
typeHint.withField("publicField", fieldHint -> fieldHint.allowWrite(true)));
typeHint.withField("publicField", fieldHint -> fieldHint.allowWrite(true)));
assertPredicateMatches(reflection.onField(SampleClass.class, "publicField").allowWrite());
}

4
spring-core/src/test/java/org/springframework/aot/nativex/FileNativeConfigurationWriterTests.java

@ -111,9 +111,9 @@ public class FileNativeConfigurationWriterTests { @@ -111,9 +111,9 @@ public class FileNativeConfigurationWriterTests {
fieldBuilder.allowWrite(true);
fieldBuilder.allowUnsafeAccess(true);
})
.withConstructor(List.of(TypeReference.of(List.class), TypeReference.of(boolean.class), TypeReference.of(MimeType.class)), constructorHint ->
.withConstructor(TypeReference.listOf(List.class, boolean.class, MimeType.class), constructorHint ->
constructorHint.withMode(ExecutableMode.INTROSPECT))
.withMethod("setDefaultCharset", List.of(TypeReference.of(Charset.class)), ctorBuilder -> {})
.withMethod("setDefaultCharset", TypeReference.listOf(Charset.class), ctorBuilder -> {})
.withMethod("getDefaultCharset", Collections.emptyList(), constructorHint ->
constructorHint.withMode(ExecutableMode.INTROSPECT));
});

22
spring-core/src/test/java/org/springframework/aot/nativex/ReflectionHintsWriterTests.java

@ -63,7 +63,7 @@ public class ReflectionHintsWriterTests { @@ -63,7 +63,7 @@ public class ReflectionHintsWriterTests {
fieldBuilder.allowWrite(true);
fieldBuilder.allowUnsafeAccess(true);
})
.withConstructor(List.of(TypeReference.of(List.class), TypeReference.of(boolean.class), TypeReference.of(MimeType.class)), constructorHint ->
.withConstructor(TypeReference.listOf(List.class, boolean.class, MimeType.class), constructorHint ->
constructorHint.withMode(ExecutableMode.INTROSPECT))
.withMethod("setDefaultCharset", List.of(TypeReference.of(Charset.class)), ctorBuilder -> {})
.withMethod("getDefaultCharset", Collections.emptyList(), constructorHint ->
@ -119,8 +119,8 @@ public class ReflectionHintsWriterTests { @@ -119,8 +119,8 @@ public class ReflectionHintsWriterTests {
@Test
void queriedMethods() throws JSONException {
ReflectionHints hints = new ReflectionHints();
hints.registerType(Integer.class, builder -> builder.withMethod("parseInt", List.of(TypeReference.of(String.class)),
b -> b.withMode(ExecutableMode.INTROSPECT)));
hints.registerType(Integer.class, builder -> builder.withMethod("parseInt",
TypeReference.listOf(String.class), b -> b.withMode(ExecutableMode.INTROSPECT)));
assertEquals("""
[
@ -140,8 +140,8 @@ public class ReflectionHintsWriterTests { @@ -140,8 +140,8 @@ public class ReflectionHintsWriterTests {
@Test
void methods() throws JSONException {
ReflectionHints hints = new ReflectionHints();
hints.registerType(Integer.class, builder -> builder.withMethod("parseInt", List.of(TypeReference.of(String.class)),
b -> b.withMode(ExecutableMode.INVOKE)));
hints.registerType(Integer.class, builder -> builder.withMethod("parseInt",
TypeReference.listOf(String.class), b -> b.withMode(ExecutableMode.INVOKE)));
assertEquals("""
[
@ -161,8 +161,8 @@ public class ReflectionHintsWriterTests { @@ -161,8 +161,8 @@ public class ReflectionHintsWriterTests {
@Test
void methodWithInnerClassParameter() throws JSONException {
ReflectionHints hints = new ReflectionHints();
hints.registerType(Integer.class, builder -> builder.withMethod("test", List.of(TypeReference.of(Inner.class)),
b -> b.withMode(ExecutableMode.INVOKE)));
hints.registerType(Integer.class, builder -> builder.withMethod("test",
TypeReference.listOf(Inner.class), b -> b.withMode(ExecutableMode.INVOKE)));
assertEquals("""
[
@ -182,10 +182,10 @@ public class ReflectionHintsWriterTests { @@ -182,10 +182,10 @@ public class ReflectionHintsWriterTests {
@Test
void methodAndQueriedMethods() throws JSONException {
ReflectionHints hints = new ReflectionHints();
hints.registerType(Integer.class, builder -> builder.withMethod("parseInt", List.of(TypeReference.of(String.class)),
b -> b.withMode(ExecutableMode.INVOKE)));
hints.registerType(Integer.class, builder -> builder.withMethod("parseInt", List.of(TypeReference.of(String.class)),
b -> b.withMode(ExecutableMode.INTROSPECT)));
hints.registerType(Integer.class, builder -> builder.withMethod("parseInt",
TypeReference.listOf(String.class), b -> b.withMode(ExecutableMode.INVOKE)));
hints.registerType(Integer.class, builder -> builder.withMethod("parseInt",
TypeReference.listOf(String.class), b -> b.withMode(ExecutableMode.INTROSPECT)));
assertEquals("""
[

11
spring-tx/src/main/java/org/springframework/transaction/annotation/TransactionRuntimeHints.java

@ -16,8 +16,6 @@ @@ -16,8 +16,6 @@
package org.springframework.transaction.annotation;
import java.util.List;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
@ -38,11 +36,8 @@ class TransactionRuntimeHints implements RuntimeHintsRegistrar { @@ -38,11 +36,8 @@ class TransactionRuntimeHints implements RuntimeHintsRegistrar {
@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
RuntimeHintsUtils.registerSynthesizedAnnotation(hints, Transactional.class);
hints.reflection()
.registerTypes(List.of(
TypeReference.of(Isolation.class),
TypeReference.of(Propagation.class),
TypeReference.of(TransactionDefinition.class)),
builder -> builder.withMembers(MemberCategory.DECLARED_FIELDS));
hints.reflection().registerTypes(TypeReference.listOf(
Isolation.class, Propagation.class, TransactionDefinition.class),
builder -> builder.withMembers(MemberCategory.DECLARED_FIELDS));
}
}

18
spring-web/src/main/java/org/springframework/http/codec/CodecConfigurerRuntimeHints.java

@ -16,9 +16,12 @@ @@ -16,9 +16,12 @@
package org.springframework.http.codec;
import java.util.function.Consumer;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.aot.hint.TypeHint.Builder;
import org.springframework.aot.hint.TypeReference;
import org.springframework.http.codec.support.DefaultClientCodecConfigurer;
import org.springframework.http.codec.support.DefaultServerCodecConfigurer;
@ -29,20 +32,21 @@ import org.springframework.lang.Nullable; @@ -29,20 +32,21 @@ import org.springframework.lang.Nullable;
* implementations listed in {@code CodecConfigurer.properties}.
*
* @author Sebastien Deleuze
* @author Stephane Nicoll
* @since 6.0
*/
class CodecConfigurerRuntimeHints implements RuntimeHintsRegistrar {
private static final Consumer<Builder> CODEC_HINT = type -> type
.onReachableType(TypeReference.of(CodecConfigurerFactory.class))
.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS);
@Override
public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
hints.resources().registerPattern("org/springframework/http/codec/CodecConfigurer.properties");
registerType(hints, DefaultClientCodecConfigurer.class);
registerType(hints, DefaultServerCodecConfigurer.class);
hints.reflection().registerTypes(TypeReference.listOf(
DefaultClientCodecConfigurer.class, DefaultServerCodecConfigurer.class),
CODEC_HINT);
}
private void registerType(RuntimeHints hints, Class<?> type) {
hints.reflection().registerType(type, builder ->
builder.onReachableType(TypeReference.of(CodecConfigurerFactory.class))
.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS));
}
}

Loading…
Cancel
Save