@ -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 ( ) ) ;
}