org.hibernate.search.engine.environment.bean.CastingBeanReference Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hibernate-search-engine Show documentation
Show all versions of hibernate-search-engine Show documentation
Hibernate Search engine, always required
The newest version!
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.search.engine.environment.bean;
import org.hibernate.search.util.common.impl.SuppressingCloser;
final class CastingBeanReference implements BeanReference {
private final BeanReference> casted;
private final Class expectedType;
CastingBeanReference(BeanReference> casted, Class expectedType) {
this.casted = casted;
this.expectedType = expectedType;
}
@Override
@SuppressWarnings("unchecked") // Checked using reflection
public BeanHolder resolve(BeanResolver beanResolver) {
BeanHolder> beanHolder = casted.resolve( beanResolver );
try {
// Just let the type throw an exception if something is wrong
expectedType.cast( beanHolder.get() );
// The instance can safely be cast to the expected type, so we can safely do this
return (BeanHolder) beanHolder;
}
catch (Exception e) {
new SuppressingCloser( e ).push( beanHolder );
throw e;
}
}
@Override
@SuppressWarnings("unchecked") // Checked using reflection
public BeanReference extends U> asSubTypeOf(Class expectedType2) {
if ( expectedType2.isAssignableFrom( expectedType ) ) {
return (BeanReference extends U>) this;
}
else {
return casted.asSubTypeOf( expectedType2 );
}
}
}