Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -501,13 +501,13 @@ private <T> void appendListeners(
EventType<T> eventType) {
final var eventListenerGroup = eventListenerRegistry.getEventListenerGroup( eventType );
for ( String listenerImpl : splitAtCommas( listeners ) ) {
@SuppressWarnings("unchecked")
T listener = (T) instantiate( listenerImpl, classLoaderService );
if ( !eventType.baseListenerInterface().isInstance( listener ) ) {
final var listener = instantiate( listenerImpl, classLoaderService );
final var baseListenerInterface = eventType.baseListenerInterface();
if ( !baseListenerInterface.isInstance( listener ) ) {
throw new HibernateException( "Event listener '" + listenerImpl
+ "' must implement '" + eventType.baseListenerInterface().getName() + "'");
+ "' must implement '" + baseListenerInterface.getName() + "'");
}
eventListenerGroup.appendListener( listener );
eventListenerGroup.appendListener( baseListenerInterface.cast( listener ) );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ private void transferImports() {
if ( !hbmImports.isEmpty() ) {
final var ormRoot = mappingXmlBinding.getRoot();
for ( var hbmImport : hbmImports ) {
final JaxbHqlImportImpl ormImport = new JaxbHqlImportImpl();
final var ormImport = new JaxbHqlImportImpl();
ormRoot.getHqlImports().add( ormImport );
ormImport.setClazz( hbmImport.getClazz() );
ormImport.setRename( hbmImport.getRename() );
Expand Down Expand Up @@ -890,7 +890,8 @@ private static JaxbNamedHqlQueryImpl transformNamedQuery(JaxbHbmNamedQueryType h
query.setQuery( qryString );
}
else {
@SuppressWarnings("unchecked") final var element = (JAXBElement<JaxbHbmQueryParamType>) content;
@SuppressWarnings("unchecked")
final var element = (JAXBElement<JaxbHbmQueryParamType>) content;
final var hbmQueryParam = element.getValue();
final var queryParam = new JaxbQueryParamTypeImpl();
query.getQueryParam().add( queryParam );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,23 @@ public static <X,Y> ConverterDescriptor<X,Y> of(
Class<? extends AttributeConverter<? extends X, ? extends Y>> converterClass,
Boolean autoApply, boolean overrideable, ClassmateContext classmateContext) {
@SuppressWarnings("unchecked") // work around weird fussiness in wildcard capture
final Class<? extends AttributeConverter<X, Y>> converterType =
(Class<? extends AttributeConverter<X, Y>>) converterClass;
final var converterType = (Class<? extends AttributeConverter<X, Y>>) converterClass;
return new ClassBasedConverterDescriptor<>( converterType, autoApply, classmateContext, overrideable );
}

public static <X,Y> ConverterDescriptor<X,Y> of(
Class<? extends AttributeConverter<? extends X, ? extends Y>> converterClass,
ClassmateContext classmateContext) {
@SuppressWarnings("unchecked") // work around weird fussiness in wildcard capture
final Class<? extends AttributeConverter<X, Y>> converterType =
(Class<? extends AttributeConverter<X, Y>>) converterClass;
final var converterType = (Class<? extends AttributeConverter<X, Y>>) converterClass;
return new ClassBasedConverterDescriptor<>( converterType, null, classmateContext, false );
}

public static <X,Y> ConverterDescriptor<X,Y> of(
Class<? extends AttributeConverter<? extends X, ? extends Y>> converterType,
ResolvedType domainTypeToMatch, ResolvedType relationalType, boolean autoApply) {
@SuppressWarnings("unchecked") // work around weird fussiness in wildcard capture
final Class<? extends AttributeConverter<X, Y>> converterClass =
(Class<? extends AttributeConverter<X, Y>>) converterType;
final var converterClass = (Class<? extends AttributeConverter<X, Y>>) converterType;
return new ConverterDescriptorImpl<>( converterClass, domainTypeToMatch, relationalType, autoApply );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static HashMap<String, String> extractParameterMap(Parameter[] parameters

public static JdbcMapping resolveUserType(Class<UserType<?>> userTypeClass, MetadataBuildingContext context) {
final var bootstrapContext = context.getBootstrapContext();
final UserType<?> userType =
final var userType =
context.getBuildingOptions().isAllowExtensionsInCdi()
? bootstrapContext.getManagedBeanRegistry().getBean( userTypeClass ).getBeanInstance()
: FallbackBeanInstanceProducer.INSTANCE.produceBeanInstance( userTypeClass );
Expand Down Expand Up @@ -74,7 +74,7 @@ public static <X,Y> JdbcMapping resolveAttributeConverter(

public static BasicType<?> resolveBasicType(Class<?> type, MetadataBuildingContext context) {
final var typeConfiguration = context.getBootstrapContext().getTypeConfiguration();
final JavaType<?> jtd = typeConfiguration.getJavaTypeRegistry().findDescriptor( type );
final var jtd = typeConfiguration.getJavaTypeRegistry().findDescriptor( type );
if ( jtd != null ) {
final JdbcType jdbcType = jtd.getRecommendedJdbcType(
new JdbcTypeIndicators() {
Expand Down Expand Up @@ -132,7 +132,7 @@ private static JavaType<?> getJavaType(
Class<JavaType<?>> javaTypeClass,
MetadataBuildingContext context,
TypeConfiguration typeConfiguration) {
final JavaType<?> registeredJtd =
final var registeredJtd =
typeConfiguration.getJavaTypeRegistry()
.findDescriptor( javaTypeClass );
if ( registeredJtd != null ) {
Expand All @@ -143,8 +143,7 @@ else if ( !context.getBuildingOptions().isAllowExtensionsInCdi() ) {
}
else {
return context.getBootstrapContext().getManagedBeanRegistry()
.getBean( javaTypeClass )
.getBeanInstance();
.getBean( javaTypeClass ).getBeanInstance();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import jakarta.persistence.NamedAttributeNode;
import jakarta.persistence.NamedEntityGraph;
import jakarta.persistence.NamedSubgraph;
import org.hibernate.AnnotationException;
import org.hibernate.boot.model.NamedGraphCreator;
import org.hibernate.graph.internal.RootGraphImpl;
Expand Down Expand Up @@ -41,23 +40,23 @@ public <T> RootGraphImplementor<T> createEntityGraph(
Function<Class<T>, EntityDomainType<?>> entityDomainClassResolver,
Function<String, EntityDomainType<?>> entityDomainNameResolver) {
//noinspection unchecked
final EntityDomainType<T> rootEntityType =
(EntityDomainType<T>) entityDomainNameResolver.apply( jpaEntityName );
final RootGraphImplementor<T> entityGraph =
final var rootEntityType =
(EntityDomainType<T>)
entityDomainNameResolver.apply( jpaEntityName );
final var entityGraph =
createRootGraph( name, rootEntityType, annotation.includeAllAttributes() );

if ( annotation.subclassSubgraphs() != null ) {
for ( NamedSubgraph subclassSubgraph : annotation.subclassSubgraphs() ) {
final Class<?> subgraphType = subclassSubgraph.type();
final Class<T> graphJavaType = entityGraph.getGraphedType().getJavaType();
final var subclassSubgraphs = annotation.subclassSubgraphs();
if ( subclassSubgraphs != null ) {
for ( var subclassSubgraph : subclassSubgraphs ) {
final var subgraphType = subclassSubgraph.type();
final var graphJavaType = entityGraph.getGraphedType().getJavaType();
if ( !graphJavaType.isAssignableFrom( subgraphType ) ) {
throw new AnnotationException( "Named subgraph type '" + subgraphType.getName()
+ "' is not a subtype of the graph type '" + graphJavaType.getName() + "'" );
+ "' is not a subtype of the graph type '" + graphJavaType.getName() + "'" );
}
@SuppressWarnings("unchecked") // Safe, because we just checked
final Class<? extends T> subtype = (Class<? extends T>) subgraphType;
final GraphImplementor<? extends T> subgraph = entityGraph.addTreatedSubgraph( subtype );
applyNamedAttributeNodes( subclassSubgraph.attributeNodes(), annotation, subgraph );
applyNamedAttributeNodes( subclassSubgraph.attributeNodes(), annotation,
entityGraph.addTreatedSubgraph( subgraphType.asSubclass( graphJavaType ) ) );
}
}

Expand All @@ -72,7 +71,7 @@ private static <T> RootGraphImplementor<T> createRootGraph(
String name,
EntityDomainType<T> rootEntityType,
boolean includeAllAttributes) {
final RootGraphImpl<T> entityGraph = new RootGraphImpl<>( name, rootEntityType );
final var entityGraph = new RootGraphImpl<>( name, rootEntityType );
if ( includeAllAttributes ) {
for ( var attribute : rootEntityType.getAttributes() ) {
entityGraph.addAttributeNodes( attribute );
Expand All @@ -85,7 +84,7 @@ private void applyNamedAttributeNodes(
NamedAttributeNode[] namedAttributeNodes,
NamedEntityGraph namedEntityGraph,
GraphImplementor<?> graphNode) {
for ( NamedAttributeNode namedAttributeNode : namedAttributeNodes ) {
for ( var namedAttributeNode : namedAttributeNodes ) {
final var attributeNode =
(AttributeNodeImplementor<?,?,?>)
graphNode.addAttributeNode( namedAttributeNode.value() );
Expand All @@ -105,7 +104,7 @@ private <T,E,K> void applyNamedSubgraphs(
String subgraphName,
AttributeNodeImplementor<T,E,K> attributeNode,
boolean isKeySubGraph) {
for ( NamedSubgraph namedSubgraph : namedEntityGraph.subgraphs() ) {
for ( var namedSubgraph : namedEntityGraph.subgraphs() ) {
if ( subgraphName.equals( namedSubgraph.name() ) ) {
applyNamedAttributeNodes( namedSubgraph.attributeNodes(), namedEntityGraph,
createSubgraph( attributeNode, isKeySubGraph, namedSubgraph.type() ) );
Expand All @@ -128,27 +127,27 @@ private static SubGraphImplementor<?> createSubgraph(

private static <T, E, K> SubGraphImplementor<?> makeAttributeNodeValueSubgraph(
AttributeNodeImplementor<T, E, K> attributeNode, Class<?> subgraphType) {
final Class<?> attributeValueType =
attributeNode.getAttributeDescriptor().getValueGraphType().getJavaType();
final var attributeValueType =
attributeNode.getAttributeDescriptor()
.getValueGraphType().getJavaType();
if ( !attributeValueType.isAssignableFrom( subgraphType ) ) {
throw new AnnotationException( "Named subgraph type '" + subgraphType.getName()
+ "' is not a subtype of the value type '" + attributeValueType.getName() + "'" );
+ "' is not a subtype of the value type '" + attributeValueType.getName() + "'" );
}
@SuppressWarnings("unchecked") // Safe, because we just checked
final Class<? extends E> castType = (Class<? extends E>) subgraphType;
return attributeNode.addValueSubgraph().addTreatedSubgraph( castType );
return attributeNode.addValueSubgraph().addTreatedSubgraph(
subgraphType.asSubclass( attributeNode.getValueSubgraph().getClassType() ) );
}

private static <T, E, K> SubGraphImplementor<?> makeAttributeNodeKeySubgraph(
AttributeNodeImplementor<T, E, K> attributeNode, Class<?> subgraphType) {
final Class<?> attributeKeyType =
attributeNode.getAttributeDescriptor().getKeyGraphType().getJavaType();
final var attributeKeyType =
attributeNode.getAttributeDescriptor()
.getKeyGraphType().getJavaType();
if ( !attributeKeyType.isAssignableFrom( subgraphType ) ) {
throw new AnnotationException( "Named subgraph type '" + subgraphType.getName()
+ "' is not a subtype of the key type '" + attributeKeyType.getName() + "'" );
+ "' is not a subtype of the key type '" + attributeKeyType.getName() + "'" );
}
@SuppressWarnings("unchecked") // Safe, because we just checked
final Class<? extends K> castType = (Class<? extends K>) subgraphType;
return attributeNode.addKeySubgraph().addTreatedSubgraph( castType );
return attributeNode.addKeySubgraph().addTreatedSubgraph(
subgraphType.asSubclass( attributeNode.getKeySubgraph().getClassType() ) );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,7 @@ public GlobalRegistrationsImpl(ModelsContext sourceModelContext, BootstrapContex

@Override
public <T> T as(Class<T> type) {
//noinspection unchecked
return (T) this;
return type.cast( this );
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,8 @@ public void injectInterceptor(
PersistentAttributeInterceptor interceptor,
SharedSessionContractImplementor session) {
if ( !enhancedForLazyLoading ) {
throw new NotInstrumentedException( "Entity class [" + entityClass.getName() + "] is not enhanced for lazy loading" );
throw new NotInstrumentedException( "Entity class '" + entityClass.getName()
+ "' is not enhanced for lazy loading" );
}

if ( !entityClass.isInstance( entity ) ) {
Expand All @@ -291,7 +292,8 @@ public void injectInterceptor(
@Override
public @Nullable BytecodeLazyAttributeInterceptor extractLazyInterceptor(Object entity) throws NotInstrumentedException {
if ( !enhancedForLazyLoading ) {
throw new NotInstrumentedException( "Entity class [" + entityClass.getName() + "] is not enhanced for lazy loading" );
throw new NotInstrumentedException( "Entity class [" + entityClass.getName()
+ "] is not enhanced for lazy loading" );
}

if ( !entityClass.isInstance( entity ) ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@
import net.bytebuddy.description.modifier.Visibility;
import org.hibernate.AssertionFailure;
import org.hibernate.HibernateException;
import org.hibernate.bytecode.enhance.internal.bytebuddy.EnhancerImplConstants;
import org.hibernate.bytecode.spi.BasicProxyFactory;
import org.hibernate.engine.spi.PrimeAmongSecondarySupertypes;
import org.hibernate.internal.util.collections.ArrayHelper;
import org.hibernate.proxy.ProxyConfiguration;

import net.bytebuddy.dynamic.scaffold.subclass.ConstructorStrategy;

import static org.hibernate.proxy.ProxyConfiguration.INTERCEPTOR_FIELD_NAME;

public class BasicProxyFactoryImpl implements BasicProxyFactory {

private static final Class[] NO_INTERFACES = ArrayHelper.EMPTY_CLASS_ARRAY;
private static final Class<?>[] NO_INTERFACES = ArrayHelper.EMPTY_CLASS_ARRAY;
private static final String PROXY_NAMING_SUFFIX = "HibernateBasicProxy";

private final Class proxyClass;
private final Constructor proxyClassConstructor;
private final Class<?> proxyClass;
private final Constructor<?> proxyClassConstructor;

@SuppressWarnings({ "unchecked", "rawtypes" })
public BasicProxyFactoryImpl(final Class superClass, final Class interfaceClass, final ByteBuddyState byteBuddyState) {
public BasicProxyFactoryImpl(final Class<?> superClass, final Class<?> interfaceClass, final ByteBuddyState byteBuddyState) {
if ( superClass == null && interfaceClass == null ) {
throw new AssertionFailure( "attempting to build proxy without any superclass or interfaces" );
}
Expand All @@ -35,21 +35,21 @@ public BasicProxyFactoryImpl(final Class superClass, final Class interfaceClass,
throw new AssertionFailure( "Ambiguous call: we assume invocation with EITHER a superClass OR an interfaceClass" );
}

final Class<?> superClassOrMainInterface = superClass != null ? superClass : interfaceClass;
final ByteBuddyState.ProxyDefinitionHelpers helpers = byteBuddyState.getProxyDefinitionHelpers();
final EnhancerImplConstants constants = byteBuddyState.getEnhancerConstants();
final var superClassOrMainInterface = superClass != null ? superClass : interfaceClass;
final var helpers = byteBuddyState.getProxyDefinitionHelpers();
final var constants = byteBuddyState.getEnhancerConstants();
final String proxyClassName = superClassOrMainInterface.getName() + "$" + PROXY_NAMING_SUFFIX;

this.proxyClass = byteBuddyState.loadBasicProxy( superClassOrMainInterface, proxyClassName, (byteBuddy, namingStrategy) ->
helpers.appendIgnoreAlsoAtEnd( byteBuddy
.with( namingStrategy )
.subclass( superClass == null ? Object.class : superClass, ConstructorStrategy.Default.DEFAULT_CONSTRUCTOR )
.implement( interfaceClass == null ? NO_INTERFACES : new Class[]{ interfaceClass } )
.defineField( ProxyConfiguration.INTERCEPTOR_FIELD_NAME, ProxyConfiguration.Interceptor.class, Visibility.PRIVATE )
.method( byteBuddyState.getProxyDefinitionHelpers().getVirtualNotFinalizerFilter() )
.intercept( byteBuddyState.getProxyDefinitionHelpers().getDelegateToInterceptorDispatcherMethodDelegation() )
.defineField( INTERCEPTOR_FIELD_NAME, ProxyConfiguration.Interceptor.class, Visibility.PRIVATE )
.method( helpers.getVirtualNotFinalizerFilter() )
.intercept( helpers.getDelegateToInterceptorDispatcherMethodDelegation() )
.implement( constants.INTERFACES_for_ProxyConfiguration )
.intercept( byteBuddyState.getProxyDefinitionHelpers().getInterceptorFieldAccessor() )
.intercept( helpers.getInterceptorFieldAccessor() )
)
);

Expand All @@ -63,21 +63,25 @@ public BasicProxyFactoryImpl(final Class superClass, final Class interfaceClass,

@Override
public Object getProxy() {
final PrimeAmongSecondarySupertypes instance;
final var instance = instantiateProxy();
final var proxyConfiguration = instance.asProxyConfiguration();
if ( proxyConfiguration == null ) {
throw new HibernateException( "Produced proxy does not correctly implement ProxyConfiguration" );
}
// Create a dedicated interceptor for the proxy.
// This is required as the interceptor is stateful.
proxyConfiguration.$$_hibernate_set_interceptor(
new PassThroughInterceptor( proxyClass.getName() ) );
return instance;
}

private PrimeAmongSecondarySupertypes instantiateProxy() {
try {
instance = (PrimeAmongSecondarySupertypes) proxyClassConstructor.newInstance();
return (PrimeAmongSecondarySupertypes) proxyClassConstructor.newInstance();
}
catch (Throwable t) {
throw new HibernateException( "Unable to instantiate proxy instance", t );
}
final ProxyConfiguration proxyConfiguration = instance.asProxyConfiguration();
if ( proxyConfiguration == null ) {
throw new HibernateException( "Produced proxy does not correctly implement ProxyConfiguration" );
}
// Create a dedicated interceptor for the proxy. This is required as the interceptor is stateful.
final ProxyConfiguration.Interceptor interceptor = new PassThroughInterceptor( proxyClass.getName() );
proxyConfiguration.$$_hibernate_set_interceptor( interceptor );
return instance;
}

public boolean isInstance(Object object) {
Expand Down
Loading
Loading