From c0a14334d1a3ac7bfaa7a4a797bcb4a788f0aba7 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Mon, 4 Nov 2024 17:01:58 +0100 Subject: [PATCH] some misc cleanups to mapping package Signed-off-by: Gavin King --- .../hibernate/internal/util/StringHelper.java | 13 +--- .../org/hibernate/mapping/BasicValue.java | 77 ++++++++----------- .../org/hibernate/mapping/FetchProfile.java | 15 +--- .../java/org/hibernate/mapping/Formula.java | 6 +- .../org/hibernate/mapping/SimpleValue.java | 46 +++++------ .../java/org/hibernate/mapping/Table.java | 3 +- .../java/org/hibernate/mapping/ToOne.java | 6 +- .../java/org/hibernate/mapping/Value.java | 31 ++++---- 8 files changed, 86 insertions(+), 111 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/internal/util/StringHelper.java b/hibernate-core/src/main/java/org/hibernate/internal/util/StringHelper.java index 71831e74c8..89c0c4e2cb 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/util/StringHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/util/StringHelper.java @@ -819,7 +819,7 @@ public final class StringHelper { return names; } else { - String[] unquoted = new String[length]; + final String[] unquoted = new String[length]; System.arraycopy( names, 0, unquoted, 0, failedIndex ); for ( int i = failedIndex; i < length; i++ ) { unquoted[i] = unquote( names[i], dialect ); @@ -889,8 +889,8 @@ public final class StringHelper { * if both {@code firstExpression} and {@code secondExpression} are empty, then null is returned. */ public static String getNonEmptyOrConjunctionIfBothNonEmpty( String firstExpression, String secondExpression ) { - final boolean isFirstExpressionNonEmpty = StringHelper.isNotEmpty( firstExpression ); - final boolean isSecondExpressionNonEmpty = StringHelper.isNotEmpty( secondExpression ); + final boolean isFirstExpressionNonEmpty = isNotEmpty( firstExpression ); + final boolean isSecondExpressionNonEmpty = isNotEmpty( secondExpression ); if ( isFirstExpressionNonEmpty && isSecondExpressionNonEmpty ) { return "( " + firstExpression + " ) and ( " + secondExpression + " )"; } @@ -918,12 +918,7 @@ public final class StringHelper { * @return The interned string. */ public static String safeInterning(final String string) { - if ( string == null ) { - return null; - } - else { - return string.intern(); - } + return string == null ? null : string.intern(); } } diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/BasicValue.java b/hibernate-core/src/main/java/org/hibernate/mapping/BasicValue.java index bccf68f4c9..9c007ed6ed 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/BasicValue.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/BasicValue.java @@ -17,6 +17,7 @@ import org.hibernate.TimeZoneStorageStrategy; import org.hibernate.annotations.SoftDelete; import org.hibernate.annotations.SoftDeleteType; import org.hibernate.annotations.TimeZoneStorageType; +import org.hibernate.boot.internal.ClassmateContext; import org.hibernate.boot.model.TypeDefinition; import org.hibernate.boot.model.convert.internal.AutoApplicableConverterDescriptorBypassedImpl; import org.hibernate.boot.model.convert.internal.ClassBasedConverterDescriptor; @@ -33,6 +34,7 @@ import org.hibernate.boot.model.process.internal.VersionResolution; import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; import org.hibernate.boot.registry.classloading.spi.ClassLoadingException; +import org.hibernate.boot.spi.InFlightMetadataCollector; import org.hibernate.boot.spi.MetadataBuildingContext; import org.hibernate.dialect.Dialect; import org.hibernate.engine.jdbc.Size; @@ -172,10 +174,8 @@ public class BasicValue extends SimpleValue implements JdbcTypeIndicators, Resol if ( resolution != null ) { throw new IllegalStateException( "BasicValue already resolved" ); } - this.ownerName = className; this.propertyName = propertyName; - super.setTypeUsingReflection( className, propertyName ); } @@ -197,7 +197,6 @@ public class BasicValue extends SimpleValue implements JdbcTypeIndicators, Resol public void setJpaAttributeConverterDescriptor(ConverterDescriptor descriptor) { setAttributeConverterDescriptor( descriptor ); - super.setJpaAttributeConverterDescriptor( descriptor ); } @@ -249,8 +248,10 @@ public class BasicValue extends SimpleValue implements JdbcTypeIndicators, Resol if ( temporalPrecision != null ) { return temporalPrecision; } - final Integer precision = column.getPrecision(); - return precision == null ? NO_COLUMN_PRECISION : precision; + else { + final Integer precision = column.getPrecision(); + return precision == null ? NO_COLUMN_PRECISION : precision; + } } else { return NO_COLUMN_PRECISION; @@ -313,7 +314,6 @@ public class BasicValue extends SimpleValue implements JdbcTypeIndicators, Resol @Override public void addFormula(Formula formula) { super.addFormula( formula ); - checkSelectable( formula ); } @@ -324,7 +324,6 @@ public class BasicValue extends SimpleValue implements JdbcTypeIndicators, Resol public Type getType() throws MappingException { resolve(); assert getResolution() != null; - return getResolution().getLegacyResolvedBasicType(); } @@ -343,32 +342,30 @@ public class BasicValue extends SimpleValue implements JdbcTypeIndicators, Resol if ( resolution != null ) { return resolution; } - - resolution = buildResolution(); - - if ( resolution == null ) { - throw new IllegalStateException( "Unable to resolve BasicValue : " + this ); - } - - final Selectable selectable = getColumn(); - final Size size; - if ( selectable instanceof Column column ) { - resolveColumn( column, getDialect() ); - size = column.calculateColumnSize( getDialect(), getBuildingContext().getMetadataCollector() ); - } else { - size = Size.nil(); + resolution = buildResolution(); + if ( resolution == null ) { + throw new IllegalStateException( "Unable to resolve BasicValue : " + this ); + } + else { + final Size size; + if ( getColumn() instanceof Column column ) { + resolveColumn( column, getDialect() ); + size = column.calculateColumnSize( getDialect(), getMetadataCollector() ); + } + else { + size = Size.nil(); + } + resolution.getJdbcType().addAuxiliaryDatabaseObjects( + resolution.getRelationalJavaType(), + resolution.getValueConverter(), + size, + getMetadataCollector().getDatabase(), + this + ); + return resolution; + } } - - resolution.getJdbcType().addAuxiliaryDatabaseObjects( - resolution.getRelationalJavaType(), - resolution.getValueConverter(), - size, - getBuildingContext().getMetadataCollector().getDatabase(), - this - ); - - return resolution; } @Override @@ -378,7 +375,7 @@ public class BasicValue extends SimpleValue implements JdbcTypeIndicators, Resol resolution.getRelationalJavaType(), getColumn().getText(), getTable().getName(), - getBuildingContext().getMetadataCollector().getDatabase() + getMetadataCollector().getDatabase() ); } @@ -483,26 +480,18 @@ public class BasicValue extends SimpleValue implements JdbcTypeIndicators, Resol SoftDelete.UnspecifiedConversion.class.equals( attributeConverterDescriptor.getAttributeConverterClass() ); if ( conversionWasUnspecified ) { final JdbcType jdbcType = BooleanJdbcType.INSTANCE.resolveIndicatedType( this, javaType); + final ClassmateContext classmateContext = getBuildingContext().getBootstrapContext().getClassmateContext(); if ( jdbcType.isNumber() ) { - return new InstanceBasedConverterDescriptor( - NumericBooleanConverter.INSTANCE, - getBuildingContext().getBootstrapContext().getClassmateContext() - ); + return new InstanceBasedConverterDescriptor( NumericBooleanConverter.INSTANCE, classmateContext ); } else if ( jdbcType.isString() ) { // here we pick 'T' / 'F' storage, though 'Y' / 'N' is equally valid - its 50/50 - return new InstanceBasedConverterDescriptor( - TrueFalseConverter.INSTANCE, - getBuildingContext().getBootstrapContext().getClassmateContext() - ); + return new InstanceBasedConverterDescriptor( TrueFalseConverter.INSTANCE, classmateContext ); } else { // should indicate BIT or BOOLEAN == no conversion needed // - we still create the converter to properly set up JDBC type, etc - return new InstanceBasedConverterDescriptor( - PassThruSoftDeleteConverter.INSTANCE, - getBuildingContext().getBootstrapContext().getClassmateContext() - ); + return new InstanceBasedConverterDescriptor( PassThruSoftDeleteConverter.INSTANCE, classmateContext ); } } else { diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/FetchProfile.java b/hibernate-core/src/main/java/org/hibernate/mapping/FetchProfile.java index 38aa92fb8b..7678028fee 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/FetchProfile.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/FetchProfile.java @@ -68,17 +68,10 @@ public class FetchProfile { } @Override - public boolean equals(Object o) { - if ( this == o ) { - return true; - } - if ( o == null || getClass() != o.getClass() ) { - return false; - } - - FetchProfile that = ( FetchProfile ) o; - - return name.equals( that.name ); + public boolean equals(Object that) { + return this == that + || that instanceof FetchProfile profile + && name.equals( profile.name ); } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/Formula.java b/hibernate-core/src/main/java/org/hibernate/mapping/Formula.java index faf9f19e3a..bd4718eb33 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/Formula.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/Formula.java @@ -94,9 +94,9 @@ public class Formula implements Selectable, Serializable { } @Override - public boolean equals(Object obj) { - return obj instanceof Formula - && ( (Formula) obj ).formula.equals( formula ); + public boolean equals(Object that) { + return that instanceof Formula other + && formula.equals( other.formula ); } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/SimpleValue.java b/hibernate-core/src/main/java/org/hibernate/mapping/SimpleValue.java index 8474cdeebb..623344ad5d 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/SimpleValue.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/SimpleValue.java @@ -149,12 +149,15 @@ public abstract class SimpleValue implements KeyValue { return metadata; } + InFlightMetadataCollector getMetadataCollector() { + return getBuildingContext().getMetadataCollector(); + } + @Override public ServiceRegistry getServiceRegistry() { return getMetadata().getMetadataBuildingOptions().getServiceRegistry(); } - public TypeConfiguration getTypeConfiguration() { return getBuildingContext().getBootstrapContext().getTypeConfiguration(); } @@ -234,8 +237,8 @@ public abstract class SimpleValue implements KeyValue { for ( int i = 0; i < originalOrder.length; i++ ) { final int originalIndex = originalOrder[i]; final Selectable selectable = originalColumns[i]; - if ( selectable instanceof Column ) { - ( (Column) selectable ).setTypeIndex( originalIndex ); + if ( selectable instanceof Column column ) { + column.setTypeIndex( originalIndex ); } columns.set( originalIndex, selectable ); insertability.set( originalIndex, originalInsertability[i] ); @@ -422,9 +425,8 @@ public abstract class SimpleValue implements KeyValue { if ( getColumnSpan() != 1 ) { throw new MappingException( "Identity generation requires exactly one column" ); } - final Selectable column = getColumn(0); - if ( column instanceof Column ) { - ( (Column) column).setIdentity( true ); + else if ( getColumn(0) instanceof Column column ) { + column.setIdentity( true ); } else { throw new MappingException( "Identity generation requires a column" ); @@ -513,10 +515,12 @@ public abstract class SimpleValue implements KeyValue { // considered nullable return true; } - else if ( !( (Column) selectable ).isNullable() ) { - // if there is a single non-nullable column, the Value - // overall is considered non-nullable. - return false; + else if ( selectable instanceof Column column ) { + if ( !column.isNullable() ) { + // if there is a single non-nullable column, the Value + // overall is considered non-nullable. + return false; + } } } // nullable by default @@ -755,7 +759,7 @@ public abstract class SimpleValue implements KeyValue { public void setTypeParameters(Map parameters) { if ( parameters != null ) { - Properties properties = new Properties(); + final Properties properties = new Properties(); properties.putAll( parameters ); setTypeParameters( properties ); } @@ -891,10 +895,9 @@ public abstract class SimpleValue implements KeyValue { // todo : not sure this works for handling @MapKeyEnumerated final Annotation[] annotations = getAnnotations( attributeMember ); - final ClassLoaderService classLoaderService = getMetadata() - .getMetadataBuildingOptions() - .getServiceRegistry() - .requireService( ClassLoaderService.class ); + final ClassLoaderService classLoaderService = + getMetadata().getMetadataBuildingOptions().getServiceRegistry() + .requireService( ClassLoaderService.class ); typeParameters.put( DynamicParameterizedType.PARAMETER_TYPE, new ParameterTypeImpl( @@ -906,7 +909,7 @@ public abstract class SimpleValue implements KeyValue { table.getCatalog(), table.getSchema(), table.getName(), - parseBoolean(typeParameters.getProperty(DynamicParameterizedType.IS_PRIMARY_KEY)), + parseBoolean( typeParameters.getProperty(DynamicParameterizedType.IS_PRIMARY_KEY) ), columnNames, columnLengths ) @@ -944,19 +947,18 @@ public abstract class SimpleValue implements KeyValue { // todo : not sure this works for handling @MapKeyEnumerated final Annotation[] annotations = getAnnotations( attributeMember ); - final ClassLoaderService classLoaderService = getMetadata() - .getMetadataBuildingOptions() - .getServiceRegistry() - .requireService( ClassLoaderService.class ); + final ClassLoaderService classLoaderService = + getMetadata().getMetadataBuildingOptions().getServiceRegistry() + .requireService( ClassLoaderService.class ); return new ParameterTypeImpl( - classLoaderService.classForTypeName(typeParameters.getProperty(DynamicParameterizedType.RETURNED_CLASS)), + classLoaderService.classForTypeName( typeParameters.getProperty(DynamicParameterizedType.RETURNED_CLASS) ), attributeMember != null ? attributeMember.getType() : null, annotations, table.getCatalog(), table.getSchema(), table.getName(), - parseBoolean(typeParameters.getProperty(DynamicParameterizedType.IS_PRIMARY_KEY)), + parseBoolean( typeParameters.getProperty(DynamicParameterizedType.IS_PRIMARY_KEY) ), columnNames, columnLengths ); diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/Table.java b/hibernate-core/src/main/java/org/hibernate/mapping/Table.java index e9c6c96318..893009a406 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/Table.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/Table.java @@ -416,7 +416,8 @@ public class Table implements Serializable, ContributableDatabaseObject { @Override public boolean equals(Object object) { - return object instanceof Table && equals((Table) object); + return object instanceof Table table + && equals( table ); } public boolean equals(Table table) { diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/ToOne.java b/hibernate-core/src/main/java/org/hibernate/mapping/ToOne.java index 577ac70583..317031a8a3 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/ToOne.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/ToOne.java @@ -90,9 +90,9 @@ public abstract class ToOne extends SimpleValue implements Fetchable, SortableVa @Override public void setTypeUsingReflection(String className, String propertyName) throws MappingException { if ( referencedEntityName == null ) { - final ClassLoaderService cls = getMetadata().getMetadataBuildingOptions() - .getServiceRegistry() - .requireService( ClassLoaderService.class ); + final ClassLoaderService cls = + getMetadata().getMetadataBuildingOptions().getServiceRegistry() + .requireService( ClassLoaderService.class ); referencedEntityName = ReflectHelper.reflectedPropertyClass( className, propertyName, cls ).getName(); } } diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/Value.java b/hibernate-core/src/main/java/org/hibernate/mapping/Value.java index a48d8beac9..71c2aed486 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/Value.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/Value.java @@ -91,13 +91,10 @@ public interface Value extends Serializable { final Type[] subtypes = compositeType.getSubtypes(); for ( int i = 0; i < subtypes.length; i++ ) { final Type subtype = subtypes[i]; - final int columnSpan; - if ( subtype instanceof EntityType entityType ) { - columnSpan = getIdType( entityType ).getColumnSpan( factory ); - } - else { - columnSpan = subtype.getColumnSpan( factory ); - } + final int columnSpan = + subtype instanceof EntityType entityType + ? getIdType( entityType ).getColumnSpan( factory ) + : subtype.getColumnSpan( factory ); if ( columnSpan < index ) { index -= columnSpan; } @@ -114,20 +111,18 @@ public interface Value extends Serializable { else if ( elementType instanceof MetaType metaType ) { return (JdbcMapping) metaType.getBaseType(); } - return (JdbcMapping) elementType; + else { + return (JdbcMapping) elementType; + } } private Type getIdType(EntityType entityType) { - final PersistentClass entityBinding = getBuildingContext().getMetadataCollector() - .getEntityBinding( entityType.getAssociatedEntityName() ); - final Type idType; - if ( entityType.isReferenceToPrimaryKey() ) { - idType = entityBinding.getIdentifier().getType(); - } - else { - idType = entityBinding.getProperty( entityType.getRHSUniqueKeyPropertyName() ).getType(); - } - return idType; + final PersistentClass entityBinding = + getBuildingContext().getMetadataCollector() + .getEntityBinding( entityType.getAssociatedEntityName() ); + return entityType.isReferenceToPrimaryKey() + ? entityBinding.getIdentifier().getType() + : entityBinding.getProperty( entityType.getRHSUniqueKeyPropertyName() ).getType(); } FetchMode getFetchMode();