Skip to content

Commit 69b7b8b

Browse files
committed
more use of Class.cast() instead of unchecked casts and fallout
also a bunch of 'var's
1 parent 9d927bb commit 69b7b8b

File tree

47 files changed

+510
-519
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+510
-519
lines changed

hibernate-core/src/main/java/org/hibernate/boot/internal/MetadataImpl.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -501,13 +501,13 @@ private <T> void appendListeners(
501501
EventType<T> eventType) {
502502
final var eventListenerGroup = eventListenerRegistry.getEventListenerGroup( eventType );
503503
for ( String listenerImpl : splitAtCommas( listeners ) ) {
504-
@SuppressWarnings("unchecked")
505-
T listener = (T) instantiate( listenerImpl, classLoaderService );
506-
if ( !eventType.baseListenerInterface().isInstance( listener ) ) {
504+
final var listener = instantiate( listenerImpl, classLoaderService );
505+
final var baseListenerInterface = eventType.baseListenerInterface();
506+
if ( !baseListenerInterface.isInstance( listener ) ) {
507507
throw new HibernateException( "Event listener '" + listenerImpl
508-
+ "' must implement '" + eventType.baseListenerInterface().getName() + "'");
508+
+ "' must implement '" + baseListenerInterface.getName() + "'");
509509
}
510-
eventListenerGroup.appendListener( listener );
510+
eventListenerGroup.appendListener( baseListenerInterface.cast( listener ) );
511511
}
512512
}
513513

hibernate-core/src/main/java/org/hibernate/boot/jaxb/hbm/transform/HbmXmlTransformer.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ private void transferImports() {
713713
if ( !hbmImports.isEmpty() ) {
714714
final var ormRoot = mappingXmlBinding.getRoot();
715715
for ( var hbmImport : hbmImports ) {
716-
final JaxbHqlImportImpl ormImport = new JaxbHqlImportImpl();
716+
final var ormImport = new JaxbHqlImportImpl();
717717
ormRoot.getHqlImports().add( ormImport );
718718
ormImport.setClazz( hbmImport.getClazz() );
719719
ormImport.setRename( hbmImport.getRename() );
@@ -890,7 +890,8 @@ private static JaxbNamedHqlQueryImpl transformNamedQuery(JaxbHbmNamedQueryType h
890890
query.setQuery( qryString );
891891
}
892892
else {
893-
@SuppressWarnings("unchecked") final var element = (JAXBElement<JaxbHbmQueryParamType>) content;
893+
@SuppressWarnings("unchecked")
894+
final var element = (JAXBElement<JaxbHbmQueryParamType>) content;
894895
final var hbmQueryParam = element.getValue();
895896
final var queryParam = new JaxbQueryParamTypeImpl();
896897
query.getQueryParam().add( queryParam );

hibernate-core/src/main/java/org/hibernate/boot/model/convert/internal/ConverterDescriptors.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,23 @@ public static <X,Y> ConverterDescriptor<X,Y> of(
3030
Class<? extends AttributeConverter<? extends X, ? extends Y>> converterClass,
3131
Boolean autoApply, boolean overrideable, ClassmateContext classmateContext) {
3232
@SuppressWarnings("unchecked") // work around weird fussiness in wildcard capture
33-
final Class<? extends AttributeConverter<X, Y>> converterType =
34-
(Class<? extends AttributeConverter<X, Y>>) converterClass;
33+
final var converterType = (Class<? extends AttributeConverter<X, Y>>) converterClass;
3534
return new ClassBasedConverterDescriptor<>( converterType, autoApply, classmateContext, overrideable );
3635
}
3736

3837
public static <X,Y> ConverterDescriptor<X,Y> of(
3938
Class<? extends AttributeConverter<? extends X, ? extends Y>> converterClass,
4039
ClassmateContext classmateContext) {
4140
@SuppressWarnings("unchecked") // work around weird fussiness in wildcard capture
42-
final Class<? extends AttributeConverter<X, Y>> converterType =
43-
(Class<? extends AttributeConverter<X, Y>>) converterClass;
41+
final var converterType = (Class<? extends AttributeConverter<X, Y>>) converterClass;
4442
return new ClassBasedConverterDescriptor<>( converterType, null, classmateContext, false );
4543
}
4644

4745
public static <X,Y> ConverterDescriptor<X,Y> of(
4846
Class<? extends AttributeConverter<? extends X, ? extends Y>> converterType,
4947
ResolvedType domainTypeToMatch, ResolvedType relationalType, boolean autoApply) {
5048
@SuppressWarnings("unchecked") // work around weird fussiness in wildcard capture
51-
final Class<? extends AttributeConverter<X, Y>> converterClass =
52-
(Class<? extends AttributeConverter<X, Y>>) converterType;
49+
final var converterClass = (Class<? extends AttributeConverter<X, Y>>) converterType;
5350
return new ConverterDescriptorImpl<>( converterClass, domainTypeToMatch, relationalType, autoApply );
5451
}
5552
}

hibernate-core/src/main/java/org/hibernate/boot/model/internal/AnnotationHelper.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public static HashMap<String, String> extractParameterMap(Parameter[] parameters
4141

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

7575
public static BasicType<?> resolveBasicType(Class<?> type, MetadataBuildingContext context) {
7676
final var typeConfiguration = context.getBootstrapContext().getTypeConfiguration();
77-
final JavaType<?> jtd = typeConfiguration.getJavaTypeRegistry().findDescriptor( type );
77+
final var jtd = typeConfiguration.getJavaTypeRegistry().findDescriptor( type );
7878
if ( jtd != null ) {
7979
final JdbcType jdbcType = jtd.getRecommendedJdbcType(
8080
new JdbcTypeIndicators() {
@@ -132,7 +132,7 @@ private static JavaType<?> getJavaType(
132132
Class<JavaType<?>> javaTypeClass,
133133
MetadataBuildingContext context,
134134
TypeConfiguration typeConfiguration) {
135-
final JavaType<?> registeredJtd =
135+
final var registeredJtd =
136136
typeConfiguration.getJavaTypeRegistry()
137137
.findDescriptor( javaTypeClass );
138138
if ( registeredJtd != null ) {
@@ -143,8 +143,7 @@ else if ( !context.getBuildingOptions().isAllowExtensionsInCdi() ) {
143143
}
144144
else {
145145
return context.getBootstrapContext().getManagedBeanRegistry()
146-
.getBean( javaTypeClass )
147-
.getBeanInstance();
146+
.getBean( javaTypeClass ).getBeanInstance();
148147
}
149148
}
150149
}

hibernate-core/src/main/java/org/hibernate/boot/model/internal/NamedGraphCreatorJpa.java

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import jakarta.persistence.NamedAttributeNode;
88
import jakarta.persistence.NamedEntityGraph;
9-
import jakarta.persistence.NamedSubgraph;
109
import org.hibernate.AnnotationException;
1110
import org.hibernate.boot.model.NamedGraphCreator;
1211
import org.hibernate.graph.internal.RootGraphImpl;
@@ -41,23 +40,23 @@ public <T> RootGraphImplementor<T> createEntityGraph(
4140
Function<Class<T>, EntityDomainType<?>> entityDomainClassResolver,
4241
Function<String, EntityDomainType<?>> entityDomainNameResolver) {
4342
//noinspection unchecked
44-
final EntityDomainType<T> rootEntityType =
45-
(EntityDomainType<T>) entityDomainNameResolver.apply( jpaEntityName );
46-
final RootGraphImplementor<T> entityGraph =
43+
final var rootEntityType =
44+
(EntityDomainType<T>)
45+
entityDomainNameResolver.apply( jpaEntityName );
46+
final var entityGraph =
4747
createRootGraph( name, rootEntityType, annotation.includeAllAttributes() );
4848

49-
if ( annotation.subclassSubgraphs() != null ) {
50-
for ( NamedSubgraph subclassSubgraph : annotation.subclassSubgraphs() ) {
51-
final Class<?> subgraphType = subclassSubgraph.type();
52-
final Class<T> graphJavaType = entityGraph.getGraphedType().getJavaType();
49+
final var subclassSubgraphs = annotation.subclassSubgraphs();
50+
if ( subclassSubgraphs != null ) {
51+
for ( var subclassSubgraph : subclassSubgraphs ) {
52+
final var subgraphType = subclassSubgraph.type();
53+
final var graphJavaType = entityGraph.getGraphedType().getJavaType();
5354
if ( !graphJavaType.isAssignableFrom( subgraphType ) ) {
5455
throw new AnnotationException( "Named subgraph type '" + subgraphType.getName()
55-
+ "' is not a subtype of the graph type '" + graphJavaType.getName() + "'" );
56+
+ "' is not a subtype of the graph type '" + graphJavaType.getName() + "'" );
5657
}
57-
@SuppressWarnings("unchecked") // Safe, because we just checked
58-
final Class<? extends T> subtype = (Class<? extends T>) subgraphType;
59-
final GraphImplementor<? extends T> subgraph = entityGraph.addTreatedSubgraph( subtype );
60-
applyNamedAttributeNodes( subclassSubgraph.attributeNodes(), annotation, subgraph );
58+
applyNamedAttributeNodes( subclassSubgraph.attributeNodes(), annotation,
59+
entityGraph.addTreatedSubgraph( subgraphType.asSubclass( graphJavaType ) ) );
6160
}
6261
}
6362

@@ -72,7 +71,7 @@ private static <T> RootGraphImplementor<T> createRootGraph(
7271
String name,
7372
EntityDomainType<T> rootEntityType,
7473
boolean includeAllAttributes) {
75-
final RootGraphImpl<T> entityGraph = new RootGraphImpl<>( name, rootEntityType );
74+
final var entityGraph = new RootGraphImpl<>( name, rootEntityType );
7675
if ( includeAllAttributes ) {
7776
for ( var attribute : rootEntityType.getAttributes() ) {
7877
entityGraph.addAttributeNodes( attribute );
@@ -85,7 +84,7 @@ private void applyNamedAttributeNodes(
8584
NamedAttributeNode[] namedAttributeNodes,
8685
NamedEntityGraph namedEntityGraph,
8786
GraphImplementor<?> graphNode) {
88-
for ( NamedAttributeNode namedAttributeNode : namedAttributeNodes ) {
87+
for ( var namedAttributeNode : namedAttributeNodes ) {
8988
final var attributeNode =
9089
(AttributeNodeImplementor<?,?,?>)
9190
graphNode.addAttributeNode( namedAttributeNode.value() );
@@ -105,7 +104,7 @@ private <T,E,K> void applyNamedSubgraphs(
105104
String subgraphName,
106105
AttributeNodeImplementor<T,E,K> attributeNode,
107106
boolean isKeySubGraph) {
108-
for ( NamedSubgraph namedSubgraph : namedEntityGraph.subgraphs() ) {
107+
for ( var namedSubgraph : namedEntityGraph.subgraphs() ) {
109108
if ( subgraphName.equals( namedSubgraph.name() ) ) {
110109
applyNamedAttributeNodes( namedSubgraph.attributeNodes(), namedEntityGraph,
111110
createSubgraph( attributeNode, isKeySubGraph, namedSubgraph.type() ) );
@@ -128,27 +127,27 @@ private static SubGraphImplementor<?> createSubgraph(
128127

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

142141
private static <T, E, K> SubGraphImplementor<?> makeAttributeNodeKeySubgraph(
143142
AttributeNodeImplementor<T, E, K> attributeNode, Class<?> subgraphType) {
144-
final Class<?> attributeKeyType =
145-
attributeNode.getAttributeDescriptor().getKeyGraphType().getJavaType();
143+
final var attributeKeyType =
144+
attributeNode.getAttributeDescriptor()
145+
.getKeyGraphType().getJavaType();
146146
if ( !attributeKeyType.isAssignableFrom( subgraphType ) ) {
147147
throw new AnnotationException( "Named subgraph type '" + subgraphType.getName()
148-
+ "' is not a subtype of the key type '" + attributeKeyType.getName() + "'" );
148+
+ "' is not a subtype of the key type '" + attributeKeyType.getName() + "'" );
149149
}
150-
@SuppressWarnings("unchecked") // Safe, because we just checked
151-
final Class<? extends K> castType = (Class<? extends K>) subgraphType;
152-
return attributeNode.addKeySubgraph().addTreatedSubgraph( castType );
150+
return attributeNode.addKeySubgraph().addTreatedSubgraph(
151+
subgraphType.asSubclass( attributeNode.getKeySubgraph().getClassType() ) );
153152
}
154153
}

hibernate-core/src/main/java/org/hibernate/boot/models/internal/GlobalRegistrationsImpl.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,7 @@ public GlobalRegistrationsImpl(ModelsContext sourceModelContext, BootstrapContex
157157

158158
@Override
159159
public <T> T as(Class<T> type) {
160-
//noinspection unchecked
161-
return (T) this;
160+
return type.cast( this );
162161
}
163162

164163
@Override

hibernate-core/src/main/java/org/hibernate/bytecode/internal/BytecodeEnhancementMetadataPojoImpl.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,8 @@ public void injectInterceptor(
272272
PersistentAttributeInterceptor interceptor,
273273
SharedSessionContractImplementor session) {
274274
if ( !enhancedForLazyLoading ) {
275-
throw new NotInstrumentedException( "Entity class [" + entityClass.getName() + "] is not enhanced for lazy loading" );
275+
throw new NotInstrumentedException( "Entity class '" + entityClass.getName()
276+
+ "' is not enhanced for lazy loading" );
276277
}
277278

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

297299
if ( !entityClass.isInstance( entity ) ) {

hibernate-core/src/main/java/org/hibernate/bytecode/internal/bytebuddy/BasicProxyFactoryImpl.java

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,24 @@
99
import net.bytebuddy.description.modifier.Visibility;
1010
import org.hibernate.AssertionFailure;
1111
import org.hibernate.HibernateException;
12-
import org.hibernate.bytecode.enhance.internal.bytebuddy.EnhancerImplConstants;
1312
import org.hibernate.bytecode.spi.BasicProxyFactory;
1413
import org.hibernate.engine.spi.PrimeAmongSecondarySupertypes;
1514
import org.hibernate.internal.util.collections.ArrayHelper;
1615
import org.hibernate.proxy.ProxyConfiguration;
1716

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

19+
import static org.hibernate.proxy.ProxyConfiguration.INTERCEPTOR_FIELD_NAME;
20+
2021
public class BasicProxyFactoryImpl implements BasicProxyFactory {
2122

22-
private static final Class[] NO_INTERFACES = ArrayHelper.EMPTY_CLASS_ARRAY;
23+
private static final Class<?>[] NO_INTERFACES = ArrayHelper.EMPTY_CLASS_ARRAY;
2324
private static final String PROXY_NAMING_SUFFIX = "HibernateBasicProxy";
2425

25-
private final Class proxyClass;
26-
private final Constructor proxyClassConstructor;
26+
private final Class<?> proxyClass;
27+
private final Constructor<?> proxyClassConstructor;
2728

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

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

4343
this.proxyClass = byteBuddyState.loadBasicProxy( superClassOrMainInterface, proxyClassName, (byteBuddy, namingStrategy) ->
4444
helpers.appendIgnoreAlsoAtEnd( byteBuddy
4545
.with( namingStrategy )
4646
.subclass( superClass == null ? Object.class : superClass, ConstructorStrategy.Default.DEFAULT_CONSTRUCTOR )
4747
.implement( interfaceClass == null ? NO_INTERFACES : new Class[]{ interfaceClass } )
48-
.defineField( ProxyConfiguration.INTERCEPTOR_FIELD_NAME, ProxyConfiguration.Interceptor.class, Visibility.PRIVATE )
49-
.method( byteBuddyState.getProxyDefinitionHelpers().getVirtualNotFinalizerFilter() )
50-
.intercept( byteBuddyState.getProxyDefinitionHelpers().getDelegateToInterceptorDispatcherMethodDelegation() )
48+
.defineField( INTERCEPTOR_FIELD_NAME, ProxyConfiguration.Interceptor.class, Visibility.PRIVATE )
49+
.method( helpers.getVirtualNotFinalizerFilter() )
50+
.intercept( helpers.getDelegateToInterceptorDispatcherMethodDelegation() )
5151
.implement( constants.INTERFACES_for_ProxyConfiguration )
52-
.intercept( byteBuddyState.getProxyDefinitionHelpers().getInterceptorFieldAccessor() )
52+
.intercept( helpers.getInterceptorFieldAccessor() )
5353
)
5454
);
5555

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

6464
@Override
6565
public Object getProxy() {
66-
final PrimeAmongSecondarySupertypes instance;
66+
final var instance = instantiateProxy();
67+
final var proxyConfiguration = instance.asProxyConfiguration();
68+
if ( proxyConfiguration == null ) {
69+
throw new HibernateException( "Produced proxy does not correctly implement ProxyConfiguration" );
70+
}
71+
// Create a dedicated interceptor for the proxy.
72+
// This is required as the interceptor is stateful.
73+
proxyConfiguration.$$_hibernate_set_interceptor(
74+
new PassThroughInterceptor( proxyClass.getName() ) );
75+
return instance;
76+
}
77+
78+
private PrimeAmongSecondarySupertypes instantiateProxy() {
6779
try {
68-
instance = (PrimeAmongSecondarySupertypes) proxyClassConstructor.newInstance();
80+
return (PrimeAmongSecondarySupertypes) proxyClassConstructor.newInstance();
6981
}
7082
catch (Throwable t) {
7183
throw new HibernateException( "Unable to instantiate proxy instance", t );
7284
}
73-
final ProxyConfiguration proxyConfiguration = instance.asProxyConfiguration();
74-
if ( proxyConfiguration == null ) {
75-
throw new HibernateException( "Produced proxy does not correctly implement ProxyConfiguration" );
76-
}
77-
// Create a dedicated interceptor for the proxy. This is required as the interceptor is stateful.
78-
final ProxyConfiguration.Interceptor interceptor = new PassThroughInterceptor( proxyClass.getName() );
79-
proxyConfiguration.$$_hibernate_set_interceptor( interceptor );
80-
return instance;
8185
}
8286

8387
public boolean isInstance(Object object) {

0 commit comments

Comments
 (0)