Skip to content

Commit

Permalink
HSEARCH-3101 Put the "expectedClass" parameter first in BeanProvider/…
Browse files Browse the repository at this point in the history
…BeanResolver

Just for consistency with SPIs we're about to introduce.
  • Loading branch information
yrodiere committed Dec 4, 2018
1 parent ca3938f commit b71608d
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 67 deletions.
Expand Up @@ -133,7 +133,7 @@ private ElasticsearchAnalysisDefinitionRegistry getAnalysisDefinitionRegistry(Ev
.as(
ElasticsearchAnalysisConfigurer.class,
reference -> beanProvider
.getBean( reference, ElasticsearchAnalysisConfigurer.class )
.getBean( ElasticsearchAnalysisConfigurer.class, reference )
)
.build();
return analysisConfigurerProperty.get( propertySource )
Expand Down
Expand Up @@ -163,7 +163,7 @@ private LuceneAnalysisDefinitionRegistry getAnalysisDefinitionRegistry(EventCont
.as(
LuceneAnalysisConfigurer.class,
reference -> beanProvider
.getBean( reference, LuceneAnalysisConfigurer.class )
.getBean( LuceneAnalysisConfigurer.class, reference )
)
.build();
return analysisConfigurerProperty.get( propertySource )
Expand Down
Expand Up @@ -98,7 +98,7 @@ private BackendBuildingState<?> createBackend(String backendName) {
String backendType = BACKEND_TYPE.get( backendPropertySource ).get();

BeanProvider beanProvider = rootBuildContext.getServiceManager().getBeanProvider();
BackendFactory backendFactory = beanProvider.getBean( backendType, BackendFactory.class );
BackendFactory backendFactory = beanProvider.getBean( BackendFactory.class, backendType );
BackendBuildContext backendBuildContext = new BackendBuildContextImpl( rootBuildContext );

BackendImplementor<?> backend = backendFactory.create( backendName, backendBuildContext, backendPropertySource );
Expand Down
Expand Up @@ -28,32 +28,32 @@ public interface BeanProvider {

/**
* Retrieve a bean referenced by its type.
* @param typeReference The type used as a reference to the bean to retrieve.
* @param expectedClass The expected class. Must be non-null. The returned bean will implement this class.
* @param <T> The expected return type.
* @param expectedClass The expected class. Must be non-null. The returned bean will implement this class.
* @param typeReference The type used as a reference to the bean to retrieve.
* @return The resolved bean.
* @throws SearchException if the reference is invalid (null) or the bean cannot be resolved.
*/
<T> T getBean(Class<?> typeReference, Class<T> expectedClass);
<T> T getBean(Class<T> expectedClass, Class<?> typeReference);

/**
* Retrieve a bean referenced by its type.
* @param nameReference The name used as a reference to the bean to retrieve. Must be non-null and non-empty.
* @param expectedClass The expected class. Must be non-null. The returned bean will implement this class.
* @param <T> The expected return type.
* @param expectedClass The expected class. Must be non-null. The returned bean will implement this class.
* @param nameReference The name used as a reference to the bean to retrieve. Must be non-null and non-empty.
* @return The resolved bean.
* @throws SearchException if the reference is invalid (null or empty) or the bean cannot be resolved.
*/
<T> T getBean(String nameReference, Class<T> expectedClass);
<T> T getBean(Class<T> expectedClass, String nameReference);

/**
* Retrieve a bean referenced by its type, name, or both, depending on the content of the {@link BeanReference}.
* @param reference The reference to the bean to retrieve. Must be non-null.
* @param expectedClass The expected class. Must be non-null. The returned bean will implement this class.
* @param <T> The expected return type.
* @param expectedClass The expected class. Must be non-null. The returned bean will implement this class.
* @param reference The reference to the bean to retrieve. Must be non-null.
* @return The resolved bean.
* @throws SearchException if the reference is invalid (null or empty) or the bean cannot be resolved.
*/
<T> T getBean(BeanReference reference, Class<T> expectedClass);
<T> T getBean(Class<T> expectedClass, BeanReference reference);

}
Expand Up @@ -11,18 +11,18 @@
*/
public interface BeanReference {

/**
* @return The name of the referenced bean.
* {@code null} implies no name reference.
*/
String getName();

/**
* @return The type of the referenced bean.
* {@code null} implies no type reference.
*/
Class<?> getType();

/**
* @return The name of the referenced bean.
* {@code null} implies no name reference.
*/
String getName();

/**
* Create a {@link BeanReference} referencing a bean by its name.
* <p>
Expand All @@ -33,7 +33,7 @@ public interface BeanReference {
* @return The corresponding {@link BeanReference}.
*/
static BeanReference ofName(String name) {
return new ImmutableBeanReference( name, null );
return new ImmutableBeanReference( null, name );
}

/**
Expand All @@ -43,18 +43,18 @@ static BeanReference ofName(String name) {
* @return The corresponding {@link BeanReference}.
*/
static BeanReference ofType(Class<?> type) {
return new ImmutableBeanReference( null, type );
return new ImmutableBeanReference( type, null );
}

/**
* Create a {@link BeanReference} referencing a bean by its name and type.
*
* @param name The bean name.
* @param type The bean type.
* @param name The bean name.
* @return The corresponding {@link BeanReference}.
*/
static BeanReference of(String name, Class<?> type) {
return new ImmutableBeanReference( name, type );
static BeanReference of(Class<?> type, String name) {
return new ImmutableBeanReference( type, name );
}

}
Expand Up @@ -14,40 +14,39 @@
*/
final class ImmutableBeanReference implements BeanReference {

private final String name;

private final Class<?> type;
private final String name;

ImmutableBeanReference(String name, Class<?> type) {
this.name = name;
ImmutableBeanReference(Class<?> type, String name) {
this.type = type;
this.name = name;
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append( "<" );
StringJoiner joiner = new StringJoiner( ", " );
if ( type != null ) {
joiner.add( "type=" + type );
}
if ( name != null ) {
// Add this even if name is empty
joiner.add( "name=" + name );
}
if ( type != null ) {
joiner.add( "type=" + type );
}
builder.append( joiner );
builder.append( ">" );
return builder.toString();
}

@Override
public String getName() {
return name;
public Class<?> getType() {
return type;
}

@Override
public Class<?> getType() {
return type;
public String getName() {
return name;
}

}
Expand Up @@ -26,23 +26,23 @@ public BeanProviderImpl(BeanResolver beanResolver) {
}

@Override
public <T> T getBean(Class<?> typeReference, Class<T> expectedClass) {
public <T> T getBean(Class<T> expectedClass, Class<?> typeReference) {
if ( typeReference == null ) {
throw log.emptyBeanReferenceTypeNull();
}
return beanResolver.resolve( typeReference, expectedClass );
return beanResolver.resolve( expectedClass, typeReference );
}

@Override
public <T> T getBean(String nameReference, Class<T> expectedClass) {
public <T> T getBean(Class<T> expectedClass, String nameReference) {
if ( StringHelper.isEmpty( nameReference ) ) {
throw log.emptyBeanReferenceNameNullOrEmpty();
}
return beanResolver.resolve( nameReference, expectedClass );
return beanResolver.resolve( expectedClass, nameReference );
}

@Override
public <T> T getBean(BeanReference reference, Class<T> expectedClass) {
public <T> T getBean(Class<T> expectedClass, BeanReference reference) {
if ( reference == null ) {
throw log.emptyBeanReferenceNull();
}
Expand All @@ -53,13 +53,13 @@ public <T> T getBean(BeanReference reference, Class<T> expectedClass) {
boolean typeProvided = typeReference != null;

if ( nameProvided && typeProvided ) {
return beanResolver.resolve( nameReference, typeReference, expectedClass );
return beanResolver.resolve( expectedClass, nameReference, typeReference );
}
else if ( nameProvided ) {
return beanResolver.resolve( nameReference, expectedClass );
return beanResolver.resolve( expectedClass, nameReference );
}
else if ( typeProvided ) {
return beanResolver.resolve( typeReference, expectedClass );
return beanResolver.resolve( expectedClass, typeReference );
}
else {
throw log.emptyBeanReferenceNoNameNoType();
Expand Down
Expand Up @@ -31,35 +31,35 @@ public interface BeanResolver extends AutoCloseable {

/**
* Resolve a bean by its type.
* @param <T> The expected return type.
* @param expectedClass The expected class. The returned bean must implement this class.
* @param typeReference The type of the bean to resolve.
* Depending on the implementation, this could be a superclass or superinterface of the resolved bean.
* @param expectedClass The expected class. The returned bean must implement this class.
* @param <T> The expected return type.
* @return The resolved bean.
* @throws SearchException if the bean cannot be resolved.
*/
<T> T resolve(Class<?> typeReference, Class<T> expectedClass);
<T> T resolve(Class<T> expectedClass, Class<?> typeReference);

/**
* Resolve a bean by its name.
* @param nameReference The name of the bean to resolve.
* @param expectedClass The expected class. The returned bean must implement this class.
* @param <T> The expected return type.
* @param expectedClass The expected class. The returned bean must implement this class.
* @param nameReference The name of the bean to resolve.
* @return The resolved bean.
* @throws SearchException if the bean cannot be resolved.
*/
<T> T resolve(String nameReference, Class<T> expectedClass);
<T> T resolve(Class<T> expectedClass, String nameReference);

/**
* Resolve a bean by its name <em>and</em> type.
* @param <T> The expected return type.
* @param expectedClass The expected class. The returned bean must implement this class.
* @param nameReference The name of the bean to resolve.
* @param typeReference The type of the bean to resolve.
* Depending on the implementation, this could be a superclass or superinterface of the resolved bean.
* @param expectedClass The expected class. The returned bean must implement this class.
* @param <T> The expected return type.
* @return The resolved bean.
* @throws SearchException if the bean cannot be resolved.
*/
<T> T resolve(String nameReference, Class<?> typeReference, Class<T> expectedClass);
<T> T resolve(Class<T> expectedClass, String nameReference, Class<?> typeReference);

}
Expand Up @@ -33,7 +33,7 @@ public void close() {
}

@Override
public <T> T resolve(Class<?> classOrFactoryClass, Class<T> expectedClass) {
public <T> T resolve(Class<T> expectedClass, Class<?> classOrFactoryClass) {
Object instance = ClassLoaderHelper.untypedInstanceFromClass( classOrFactoryClass, expectedClass.getName() );

// TODO support @Factory annotation
Expand All @@ -42,16 +42,16 @@ public <T> T resolve(Class<?> classOrFactoryClass, Class<T> expectedClass) {
}

@Override
public <T> T resolve(String classOrFactoryClassName, Class<T> expectedClass) {
public <T> T resolve(Class<T> expectedClass, String classOrFactoryClassName) {
Class<?> classOrFactoryClass = ClassLoaderHelper.classForName(
expectedClass, classOrFactoryClassName, expectedClass.getName(), classResolver
);

return resolve( classOrFactoryClass, expectedClass );
return resolve( expectedClass, classOrFactoryClass );
}

@Override
public <T> T resolve(String nameReference, Class<?> typeReference, Class<T> expectedClass) {
public <T> T resolve(Class<T> expectedClass, String nameReference, Class<?> typeReference) {
throw log.resolveBeanUsingBothNameAndType( nameReference, typeReference );
}

Expand Down
Expand Up @@ -54,7 +54,7 @@ public <B> B produceBeanInstance(Class<B> aClass) {

@Override
public <B> B produceBeanInstance(String s, Class<B> aClass) {
return delegate.resolve( s, aClass );
return delegate.resolve( aClass, s );
}
};
}
Expand All @@ -68,21 +68,21 @@ public void close() {
}

@Override
public <T> T resolve(Class<?> typeReference, Class<T> expectedClass) {
public <T> T resolve(Class<T> expectedClass, Class<?> typeReference) {
ContainedBean<?> containedBean = beanContainer.getBean( typeReference, LIFECYCLE_OPTIONS, fallbackInstanceProducer );
register( containedBean );
return expectedClass.cast( containedBean.getBeanInstance() );
}

@Override
public <T> T resolve(String nameReference, Class<T> expectedClass) {
public <T> T resolve(Class<T> expectedClass, String nameReference) {
ContainedBean<T> containedBean = beanContainer.getBean( nameReference, expectedClass, LIFECYCLE_OPTIONS, fallbackInstanceProducer );
register( containedBean );
return containedBean.getBeanInstance();
}

@Override
public <T> T resolve(String nameReference, Class<?> typeReference, Class<T> expectedClass) {
public <T> T resolve(Class<T> expectedClass, String nameReference, Class<?> typeReference) {
ContainedBean<?> containedBean = beanContainer.getBean( nameReference, typeReference, LIFECYCLE_OPTIONS, fallbackInstanceProducer );
register( containedBean );
return expectedClass.cast( containedBean.getBeanInstance() );
Expand Down
Expand Up @@ -112,7 +112,8 @@ public void configure(MappingBuildContext buildContext, ConfigurationPropertySou
ConfigurationProperty.forKey( SearchOrmSettings.Radicals.MAPPING_CONFIGURER )
.as(
HibernateOrmSearchMappingConfigurer.class,
reference -> beanProvider.getBean( reference, HibernateOrmSearchMappingConfigurer.class )
reference -> beanProvider.getBean(
HibernateOrmSearchMappingConfigurer.class, reference )
)
.build();
mappingConfigurerProperty.get( propertySource )
Expand Down
Expand Up @@ -30,6 +30,6 @@ public String toString() {
@Override
public T build(BridgeBuildContext buildContext) {
BeanProvider beanProvider = buildContext.getBeanProvider();
return beanProvider.getBean( beanReference, expectedType );
return beanProvider.getBean( expectedType, beanReference );
}
}
Expand Up @@ -170,7 +170,7 @@ public <C, V> Optional<ContainerValueExtractor<? super C, V>> tryCreate(
for ( Class<? extends ContainerValueExtractor> extractorClass :
boundPath.getExtractorPath().getExplicitExtractorClasses() ) {
ContainerValueExtractor<?, ?> newExtractor =
beanProvider.getBean( extractorClass, ContainerValueExtractor.class );
beanProvider.getBean( ContainerValueExtractor.class, extractorClass );
if ( extractor == null ) {
// First extractor: must be able to process type C
extractor = (ContainerValueExtractor<? super C, ?>) newExtractor;
Expand Down

0 comments on commit b71608d

Please sign in to comment.