Skip to content

Commit

Permalink
HSEARCH-5034 Allow passing BeanReference<? extends T> when registerin…
Browse files Browse the repository at this point in the history
…g beans to BeanConfigurationContext

Signed-off-by: Yoann Rodière <yoann@hibernate.org>
  • Loading branch information
yrodiere committed Dec 15, 2023
1 parent bf409c3 commit 9b4d99f
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public <U> BeanReference<? extends U> asSubTypeOf(Class<U> expectedType) {
return (BeanReference<? extends U>) this;
}
else {
// We don't know the concrete type of returned beans, so we'll have to check upon retrieval
return BeanReference.super.asSubTypeOf( expectedType );
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ final class BeanConfigurationContextImpl implements BeanConfigurationContext {
private final Map<Class<?>, BeanReferenceRegistryForType<?>> configuredBeans = new HashMap<>();

@Override
public <T> void define(Class<T> exposedType, BeanReference<T> reference) {
public <T> void define(Class<T> exposedType, BeanReference<? extends T> reference) {
Contracts.assertNotNull( exposedType, "exposedType" );
Contracts.assertNotNull( reference, "reference" );
configuredBeans( exposedType ).add( reference );
}

@Override
public <T> void define(Class<T> exposedType, String name, BeanReference<T> reference) {
public <T> void define(Class<T> exposedType, String name, BeanReference<? extends T> reference) {
Contracts.assertNotNull( exposedType, "exposedType" );
Contracts.assertNotNull( name, "name" );
Contracts.assertNotNull( reference, "reference" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,19 @@ public BeanReference<T> named(String name) {
return named.get( name );
}

void add(BeanReference<T> reference) {
all.add( reference );
@SuppressWarnings("unchecked") // Safe cast from BeanReference<? extends T> to BeanReference<T> as BeanReference is covariant in T
void add(BeanReference<? extends T> reference) {
all.add( (BeanReference<T>) reference );
}

void add(String name, BeanReference<T> reference) {
Object previous = named.putIfAbsent( name, reference );
@SuppressWarnings("unchecked") // Safe cast from BeanReference<? extends T> to BeanReference<T> as BeanReference is covariant in T
void add(String name, BeanReference<? extends T> reference) {
Object previous = named.putIfAbsent( name, (BeanReference<T>) reference );
if ( previous != null ) {
throw new AssertionFailure( String.format( Locale.ROOT,
"Duplicate bean references for name '%1$s': %2$s, %3$s",
name, previous, reference ) );
}
all.add( reference );
all.add( (BeanReference<T>) reference );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public interface BeanConfigurationContext {
* provided that reference is not {@code BeanReference.of( exposedType )} (which would create a cycle).
* @param <T> The exposed type of the bean.
*/
<T> void define(Class<T> exposedType, BeanReference<T> reference);
<T> void define(Class<T> exposedType, BeanReference<? extends T> reference);

/**
* Define a way to resolve a bean referenced by its {@code exposedType} and {@code name}.
Expand All @@ -40,6 +40,6 @@ public interface BeanConfigurationContext {
* provided that reference is not {@code BeanReference.of( exposedType, name )} (which would create a cycle).
* @param <T> The exposed type of the bean.
*/
<T> void define(Class<T> exposedType, String name, BeanReference<T> reference);
<T> void define(Class<T> exposedType, String name, BeanReference<? extends T> reference);

}
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ class BeanResolverImplBaseTest {
@Mock
private BeanReference<RoleType> roleInternalBean1FactoryMock;
@Mock
private BeanReference<RoleType> roleInternalBean2FactoryMock;
private BeanReference<InternalType3> roleInternalBean2FactoryMock;
@Mock
private BeanReference<RoleType> roleInternalBean3FactoryMock;
@Mock
private BeanReference<RoleType> roleInternalBean4FactoryMock;
private BeanReference<InternalType3> roleInternalBean4FactoryMock;

private BeanResolver beanResolver;

Expand Down Expand Up @@ -436,9 +436,9 @@ void resolve_ambiguousInternalBean() {
@Test
void resolveRole() {
BeanHolder<RoleType> beanHolder1 = BeanHolder.of( new InternalType3() );
BeanHolder<RoleType> beanHolder2 = BeanHolder.of( new InternalType3() );
BeanHolder<InternalType3> beanHolder2 = BeanHolder.of( new InternalType3() );
BeanHolder<RoleType> beanHolder3 = BeanHolder.of( new InternalType3() );
BeanHolder<RoleType> beanHolder4 = BeanHolder.of( new InternalType3() );
BeanHolder<InternalType3> beanHolder4 = BeanHolder.of( new InternalType3() );

// resolveRole
when( roleInternalBean1FactoryMock.resolve( any() ) ).thenReturn( beanHolder1 );
Expand Down

0 comments on commit 9b4d99f

Please sign in to comment.