Skip to content

Commit 3917398

Browse files
committed
various code improvements including
1. more use of record types 2. use correctly-wildcarded generic type in getXmlMappingBindings() 3. use safe casts in equals() overrides 4. use Objects.hash() 5. deprecated forRemoval an unused class 6. etc.
1 parent b5eac3f commit 3917398

File tree

54 files changed

+724
-955
lines changed

Some content is hidden

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

54 files changed

+724
-955
lines changed

hibernate-core/src/main/java/org/hibernate/Version.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
import org.hibernate.internal.CoreMessageLogger;
88
import org.hibernate.internal.build.AllowSysOut;
99

10-
import org.jboss.logging.Logger;
11-
1210
import java.lang.invoke.MethodHandles;
1311

12+
import static org.jboss.logging.Logger.getMessageLogger;
13+
1414
/**
1515
* Information about the version of Hibernate.
1616
*
@@ -41,7 +41,8 @@ public static String getVersionString() {
4141
* Logs the Hibernate version (using {@link #getVersionString()}) to the logging system.
4242
*/
4343
public static void logVersion() {
44-
Logger.getMessageLogger( MethodHandles.lookup(), CoreMessageLogger.class, Version.class.getName() ).version( getVersionString() );
44+
getMessageLogger( MethodHandles.lookup(), CoreMessageLogger.class, Version.class.getName() )
45+
.version( getVersionString() );
4546
}
4647

4748
/**

hibernate-core/src/main/java/org/hibernate/boot/CacheRegionDefinition.java

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,48 +14,37 @@
1414
*
1515
* @author Steve Ebersole
1616
*/
17-
public class CacheRegionDefinition {
17+
public record CacheRegionDefinition(CacheRegionType regionType,
18+
String role, String usage, String region,
19+
boolean cacheLazy) {
20+
1821
public enum CacheRegionType {
1922
ENTITY,
2023
COLLECTION,
2124
QUERY
2225
}
2326

24-
private final CacheRegionType regionType;
25-
private final String role;
26-
private final String usage;
27-
private final String region;
28-
private final boolean cacheLazy;
29-
30-
public CacheRegionDefinition(
31-
CacheRegionType cacheType,
32-
String role,
33-
String usage,
34-
String region,
35-
boolean cacheLazy) {
36-
this.regionType = cacheType;
37-
this.role = role;
38-
this.usage = usage;
39-
this.region = region;
40-
this.cacheLazy = cacheLazy;
41-
}
42-
27+
@Deprecated(since = "7")
4328
public CacheRegionType getRegionType() {
4429
return regionType;
4530
}
4631

32+
@Deprecated(since = "7")
4733
public String getRole() {
4834
return role;
4935
}
5036

37+
@Deprecated(since = "7")
5138
public String getUsage() {
5239
return usage;
5340
}
5441

42+
@Deprecated(since = "7")
5543
public String getRegion() {
5644
return region;
5745
}
5846

47+
@Deprecated(since = "7")
5948
public boolean isCacheLazy() {
6049
return cacheLazy;
6150
}

hibernate-core/src/main/java/org/hibernate/boot/MetadataSources.java

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -130,26 +130,24 @@ public XmlMappingBinderAccess getXmlMappingBinderAccess() {
130130
/**
131131
* @deprecated Prefer {@linkplain #getMappingXmlBindings()} and/or {@linkplain #getHbmXmlBindings()}
132132
*/
133-
@SuppressWarnings({ "unchecked", "rawtypes" })
134133
@Deprecated(since = "7.0")
135-
public List<Binding<JaxbBindableMappingDescriptor>> getXmlBindings() {
134+
public List<? extends Binding<? extends JaxbBindableMappingDescriptor>> getXmlBindings() {
136135
if ( mappingXmlBindings == null && hbmXmlBindings == null ) {
137136
return emptyList();
138137
}
139-
140-
if ( hbmXmlBindings == null ) {
141-
return (List) mappingXmlBindings;
138+
else if ( hbmXmlBindings == null ) {
139+
return mappingXmlBindings;
142140
}
143-
144-
if ( mappingXmlBindings == null ) {
145-
return (List) hbmXmlBindings;
141+
else if ( mappingXmlBindings == null ) {
142+
return hbmXmlBindings;
143+
}
144+
else {
145+
final ArrayList<Binding<? extends JaxbBindableMappingDescriptor>> combined =
146+
arrayList( mappingXmlBindings.size() + hbmXmlBindings.size() );
147+
combined.addAll( mappingXmlBindings );
148+
combined.addAll( hbmXmlBindings );
149+
return combined;
146150
}
147-
148-
final ArrayList<Binding<JaxbBindableMappingDescriptor>> combined =
149-
arrayList( mappingXmlBindings.size() + hbmXmlBindings.size() );
150-
combined.addAll( (List) mappingXmlBindings );
151-
combined.addAll( (List) hbmXmlBindings );
152-
return combined;
153151
}
154152

155153
public List<Binding<JaxbEntityMappingsImpl>> getMappingXmlBindings() {
@@ -205,7 +203,7 @@ public MetadataBuilder getMetadataBuilder(StandardServiceRegistry serviceRegistr
205203
*/
206204
private MetadataBuilder getCustomBuilderOrDefault(MetadataBuilderImpl defaultBuilder) {
207205

208-
Collection<MetadataBuilderFactory> discoveredBuilderFactories =
206+
final Collection<MetadataBuilderFactory> discoveredBuilderFactories =
209207
serviceRegistry.requireService( ClassLoaderService.class )
210208
.loadJavaServices( MetadataBuilderFactory.class );
211209

@@ -309,9 +307,7 @@ public MetadataSources addQueryImport(String importedName, Class<?> target) {
309307
if ( extraQueryImports == null ) {
310308
extraQueryImports = new HashMap<>();
311309
}
312-
313310
extraQueryImports.put( importedName, target );
314-
315311
return this;
316312
}
317313

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2016,32 +2016,32 @@ private void processCachingOverrides() {
20162016
}
20172017

20182018
for ( CacheRegionDefinition cacheRegionDefinition : bootstrapContext.getCacheRegionDefinitions() ) {
2019-
if ( cacheRegionDefinition.getRegionType() == CacheRegionDefinition.CacheRegionType.ENTITY ) {
2020-
final PersistentClass entityBinding = getEntityBinding( cacheRegionDefinition.getRole() );
2019+
if ( cacheRegionDefinition.regionType() == CacheRegionDefinition.CacheRegionType.ENTITY ) {
2020+
final PersistentClass entityBinding = getEntityBinding( cacheRegionDefinition.role() );
20212021
if ( entityBinding == null ) {
20222022
throw new HibernateException(
2023-
"Cache override referenced an unknown entity : " + cacheRegionDefinition.getRole()
2023+
"Cache override referenced an unknown entity : " + cacheRegionDefinition.role()
20242024
);
20252025
}
20262026
if ( !( entityBinding instanceof RootClass rootClass ) ) {
20272027
throw new HibernateException(
2028-
"Cache override referenced a non-root entity : " + cacheRegionDefinition.getRole()
2028+
"Cache override referenced a non-root entity : " + cacheRegionDefinition.role()
20292029
);
20302030
}
20312031
entityBinding.setCached( true );
2032-
rootClass.setCacheRegionName( cacheRegionDefinition.getRegion() );
2033-
rootClass.setCacheConcurrencyStrategy( cacheRegionDefinition.getUsage() );
2034-
rootClass.setLazyPropertiesCacheable( cacheRegionDefinition.isCacheLazy() );
2032+
rootClass.setCacheRegionName( cacheRegionDefinition.region() );
2033+
rootClass.setCacheConcurrencyStrategy( cacheRegionDefinition.usage() );
2034+
rootClass.setLazyPropertiesCacheable( cacheRegionDefinition.cacheLazy() );
20352035
}
2036-
else if ( cacheRegionDefinition.getRegionType() == CacheRegionDefinition.CacheRegionType.COLLECTION ) {
2037-
final Collection collectionBinding = getCollectionBinding( cacheRegionDefinition.getRole() );
2036+
else if ( cacheRegionDefinition.regionType() == CacheRegionDefinition.CacheRegionType.COLLECTION ) {
2037+
final Collection collectionBinding = getCollectionBinding( cacheRegionDefinition.role() );
20382038
if ( collectionBinding == null ) {
20392039
throw new HibernateException(
2040-
"Cache override referenced an unknown collection role : " + cacheRegionDefinition.getRole()
2040+
"Cache override referenced an unknown collection role : " + cacheRegionDefinition.role()
20412041
);
20422042
}
2043-
collectionBinding.setCacheRegionName( cacheRegionDefinition.getRegion() );
2044-
collectionBinding.setCacheConcurrencyStrategy( cacheRegionDefinition.getUsage() );
2043+
collectionBinding.setCacheRegionName( cacheRegionDefinition.region() );
2044+
collectionBinding.setCacheConcurrencyStrategy( cacheRegionDefinition.usage() );
20452045
}
20462046
}
20472047
}

hibernate-core/src/main/java/org/hibernate/boot/model/CustomSql.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,19 @@
1212
*
1313
* @author Steve Ebersole
1414
*/
15-
public class CustomSql {
16-
private final String sql;
17-
private final boolean isCallable;
18-
private final ExecuteUpdateResultCheckStyle checkStyle;
19-
20-
public CustomSql(String sql, boolean callable, ExecuteUpdateResultCheckStyle checkStyle) {
21-
this.sql = sql;
22-
this.isCallable = callable;
23-
this.checkStyle = checkStyle;
24-
}
15+
public record CustomSql(String sql, boolean callable, ExecuteUpdateResultCheckStyle checkStyle) {
2516

17+
@Deprecated(since = "7")
2618
public String getSql() {
2719
return sql;
2820
}
2921

22+
@Deprecated(since = "7")
3023
public boolean isCallable() {
31-
return isCallable;
24+
return callable;
3225
}
3326

27+
@Deprecated(since = "7")
3428
public ExecuteUpdateResultCheckStyle getCheckStyle() {
3529
return checkStyle;
3630
}

hibernate-core/src/main/java/org/hibernate/boot/model/IdentifierGeneratorDefinition.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,7 @@ public boolean equals(Object o) {
164164

165165
@Override
166166
public int hashCode() {
167-
int result = name != null ? name.hashCode() : 0;
168-
result = 31 * result + ( strategy != null ? strategy.hashCode() : 0 );
169-
result = 31 * result + ( parameters != null ? parameters.hashCode() : 0 );
170-
return result;
167+
return Objects.hash( name, strategy, parameters );
171168
}
172169

173170
@Override

hibernate-core/src/main/java/org/hibernate/boot/model/TypeDefinition.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -337,18 +337,14 @@ public boolean equals(Object o) {
337337
}
338338

339339
return Objects.equals( this.name, that.name )
340-
&& Objects.equals( this.typeImplementorClass, that.typeImplementorClass )
341-
&& Arrays.equals( this.registrationKeys, that.registrationKeys )
342-
&& Objects.equals( this.parameters, that.parameters );
340+
&& Objects.equals( this.typeImplementorClass, that.typeImplementorClass )
341+
&& Arrays.equals( this.registrationKeys, that.registrationKeys )
342+
&& Objects.equals( this.parameters, that.parameters );
343343
}
344344

345345
@Override
346346
public int hashCode() {
347-
int result = name != null ? name.hashCode() : 0;
348-
result = 31 * result + ( typeImplementorClass != null ? typeImplementorClass.hashCode() : 0 );
349-
result = 31 * result + ( registrationKeys != null ? Arrays.hashCode( registrationKeys ) : 0 );
350-
result = 31 * result + ( parameters != null ? parameters.hashCode() : 0 );
351-
return result;
347+
return Objects.hash( name, typeImplementorClass, registrationKeys, parameters );
352348
}
353349

354350
@Override

hibernate-core/src/main/java/org/hibernate/boot/model/convert/spi/RegisteredConversion.java

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,57 +9,55 @@
99

1010
import org.hibernate.boot.model.convert.internal.AutoApplicableConverterDescriptorBypassedImpl;
1111
import org.hibernate.boot.model.convert.internal.AutoApplicableConverterDescriptorStandardImpl;
12-
import org.hibernate.boot.model.convert.internal.ConverterHelper;
12+
import org.hibernate.boot.spi.ClassmateContext;
1313
import org.hibernate.boot.spi.MetadataBuildingContext;
1414
import org.hibernate.type.descriptor.converter.internal.JpaAttributeConverterImpl;
1515
import org.hibernate.type.descriptor.converter.spi.JpaAttributeConverter;
1616
import org.hibernate.resource.beans.spi.ManagedBean;
1717
import org.hibernate.type.descriptor.java.spi.JavaTypeRegistry;
18-
import org.hibernate.type.spi.TypeConfiguration;
1918

2019
import com.fasterxml.classmate.ResolvedType;
2120
import jakarta.persistence.AttributeConverter;
2221

22+
import static org.hibernate.boot.model.convert.internal.ConverterHelper.resolveConverterClassParamTypes;
23+
2324
/**
2425
* A registered conversion.
2526
*
2627
* @see org.hibernate.annotations.ConverterRegistration
2728
*
2829
* @author Steve Ebersole
2930
*/
30-
public class RegisteredConversion {
31-
private final Class<?> explicitDomainType;
32-
private final Class<? extends AttributeConverter<?,?>> converterType;
33-
private final boolean autoApply;
31+
public record RegisteredConversion(
32+
Class<?> explicitDomainType,
33+
Class<? extends AttributeConverter<?,?>> converterType,
34+
boolean autoApply,
35+
ConverterDescriptor converterDescriptor) {
3436

35-
private final ConverterDescriptor converterDescriptor;
37+
public RegisteredConversion {
38+
assert converterType != null;
39+
}
3640

3741
public RegisteredConversion(
3842
Class<?> explicitDomainType,
3943
Class<? extends AttributeConverter<?,?>> converterType,
4044
boolean autoApply,
4145
MetadataBuildingContext context) {
42-
assert converterType != null;
43-
44-
this.explicitDomainType = explicitDomainType;
45-
this.converterType = converterType;
46-
this.autoApply = autoApply;
47-
48-
this.converterDescriptor = determineConverterDescriptor( explicitDomainType, converterType, autoApply, context );
46+
this( explicitDomainType, converterType, autoApply,
47+
determineConverterDescriptor( explicitDomainType, converterType, autoApply, context ) );
4948
}
5049

5150
@Override
5251
public boolean equals(Object o) {
5352
if ( this == o ) {
5453
return true;
5554
}
56-
if ( o == null || getClass() != o.getClass() ) {
55+
if ( !(o instanceof RegisteredConversion that) ) {
5756
return false;
5857
}
59-
RegisteredConversion that = (RegisteredConversion) o;
60-
return autoApply == that.autoApply
61-
&& Objects.equals( explicitDomainType, that.explicitDomainType )
62-
&& converterType.equals( that.converterType );
58+
return this.autoApply == that.autoApply
59+
&& Objects.equals( this.explicitDomainType, that.explicitDomainType )
60+
&& Objects.equals( this.converterType, that.converterType );
6361
}
6462

6563
@Override
@@ -72,19 +70,13 @@ private static ConverterDescriptor determineConverterDescriptor(
7270
Class<? extends AttributeConverter<?, ?>> converterType,
7371
boolean autoApply,
7472
MetadataBuildingContext context) {
75-
final List<ResolvedType> resolvedParamTypes = ConverterHelper.resolveConverterClassParamTypes(
76-
converterType,
77-
context.getBootstrapContext().getClassmateContext()
78-
);
73+
final ClassmateContext classmateContext = context.getBootstrapContext().getClassmateContext();
74+
final List<ResolvedType> resolvedParamTypes = resolveConverterClassParamTypes( converterType, classmateContext );
7975
final ResolvedType relationalType = resolvedParamTypes.get( 1 );
80-
final ResolvedType domainTypeToMatch;
81-
if ( !void.class.equals( explicitDomainType ) ) {
82-
domainTypeToMatch = context.getBootstrapContext().getClassmateContext().getTypeResolver().resolve( explicitDomainType );
83-
}
84-
else {
85-
domainTypeToMatch = resolvedParamTypes.get( 0 );
86-
}
87-
76+
final ResolvedType domainTypeToMatch =
77+
void.class.equals( explicitDomainType )
78+
? resolvedParamTypes.get( 0 )
79+
: classmateContext.getTypeResolver().resolve( explicitDomainType );
8880
return new ConverterDescriptorImpl( converterType, domainTypeToMatch, relationalType, autoApply );
8981
}
9082

@@ -108,8 +100,6 @@ private static class ConverterDescriptorImpl implements ConverterDescriptor {
108100
private final Class<? extends AttributeConverter<?, ?>> converterType;
109101
private final ResolvedType domainTypeToMatch;
110102
private final ResolvedType relationalType;
111-
private final boolean autoApply;
112-
113103
private final AutoApplicableConverterDescriptor autoApplyDescriptor;
114104

115105
public ConverterDescriptorImpl(
@@ -120,8 +110,6 @@ public ConverterDescriptorImpl(
120110
this.converterType = converterType;
121111
this.domainTypeToMatch = domainTypeToMatch;
122112
this.relationalType = relationalType;
123-
this.autoApply = autoApply;
124-
125113
this.autoApplyDescriptor = autoApply
126114
? new AutoApplicableConverterDescriptorStandardImpl( this )
127115
: AutoApplicableConverterDescriptorBypassedImpl.INSTANCE;
@@ -147,17 +135,15 @@ public AutoApplicableConverterDescriptor getAutoApplyDescriptor() {
147135
return autoApplyDescriptor;
148136
}
149137

150-
@SuppressWarnings("unchecked")
151138
@Override
152139
public JpaAttributeConverter<?, ?> createJpaAttributeConverter(JpaAttributeConverterCreationContext context) {
153140
final ManagedBean<? extends AttributeConverter<?, ?>> converterBean =
154141
context.getManagedBeanRegistry().getBean( converterType );
155142

156-
final TypeConfiguration typeConfiguration = context.getTypeConfiguration();
157-
final JavaTypeRegistry javaTypeRegistry = typeConfiguration.getJavaTypeRegistry();
143+
final JavaTypeRegistry javaTypeRegistry = context.getTypeConfiguration().getJavaTypeRegistry();
158144
javaTypeRegistry.resolveDescriptor( domainTypeToMatch.getErasedType() );
159145

160-
//noinspection rawtypes
146+
//noinspection rawtypes, unchecked
161147
return new JpaAttributeConverterImpl(
162148
converterBean,
163149
javaTypeRegistry.getDescriptor( converterBean.getBeanClass() ),

0 commit comments

Comments
 (0)