fix generic typing and remove warning suppression

This commit is contained in:
Gavin King 2024-11-16 10:18:27 +01:00
parent 80a1b36477
commit e1f62098e2
3 changed files with 30 additions and 32 deletions

View File

@ -187,15 +187,12 @@ public class BooleanJavaType extends AbstractClassJavaType<Boolean> implements
} }
@Override @Override
public String getCheckCondition(String columnName, JdbcType jdbcType, BasicValueConverter<?, ?> converter, Dialect dialect) { public String getCheckCondition(String columnName, JdbcType jdbcType, BasicValueConverter<Boolean, ?> converter, Dialect dialect) {
if ( converter != null ) { if ( converter != null ) {
if ( jdbcType.isString() ) { if ( jdbcType.isString() ) {
@SuppressWarnings("unchecked") final Object falseValue = converter.toRelationalValue( false );
final BasicValueConverter<Boolean, ?> stringConverter = final Object trueValue = converter.toRelationalValue( true );
(BasicValueConverter<Boolean, ?>) converter; final String[] values = getPossibleStringValues( converter, falseValue, trueValue );
final Object falseValue = stringConverter.toRelationalValue( false );
final Object trueValue = stringConverter.toRelationalValue( true );
final String[] values = getPossibleStringValues( stringConverter, falseValue, trueValue );
return dialect.getCheckCondition( columnName, values ); return dialect.getCheckCondition( columnName, values );
} }
else if ( jdbcType.isInteger() ) { else if ( jdbcType.isInteger() ) {

View File

@ -8,7 +8,6 @@ import java.util.Set;
import org.hibernate.boot.model.process.internal.EnumeratedValueConverter; import org.hibernate.boot.model.process.internal.EnumeratedValueConverter;
import org.hibernate.dialect.Dialect; import org.hibernate.dialect.Dialect;
import org.hibernate.internal.util.collections.CollectionHelper;
import org.hibernate.type.SqlTypes; import org.hibernate.type.SqlTypes;
import org.hibernate.type.descriptor.WrapperOptions; import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.converter.spi.BasicValueConverter; import org.hibernate.type.descriptor.converter.spi.BasicValueConverter;
@ -19,6 +18,7 @@ import org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry;
import jakarta.persistence.EnumType; import jakarta.persistence.EnumType;
import static jakarta.persistence.EnumType.ORDINAL; import static jakarta.persistence.EnumType.ORDINAL;
import static org.hibernate.internal.util.collections.CollectionHelper.setOfSize;
import static org.hibernate.type.SqlTypes.CHAR; import static org.hibernate.type.SqlTypes.CHAR;
import static org.hibernate.type.SqlTypes.ENUM; import static org.hibernate.type.SqlTypes.ENUM;
import static org.hibernate.type.SqlTypes.NAMED_ENUM; import static org.hibernate.type.SqlTypes.NAMED_ENUM;
@ -36,21 +36,21 @@ import static org.hibernate.type.SqlTypes.VARCHAR;
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public class EnumJavaType<T extends Enum<T>> extends AbstractClassJavaType<T> { public class EnumJavaType<T extends Enum<T>> extends AbstractClassJavaType<T> {
public EnumJavaType(Class<T> type) { public EnumJavaType(Class<T> type) {
super( type, ImmutableMutabilityPlan.instance() ); super( type, ImmutableMutabilityPlan.instance() );
} }
@Override @Override
public JdbcType getRecommendedJdbcType(JdbcTypeIndicators context) { public JdbcType getRecommendedJdbcType(JdbcTypeIndicators context) {
final JdbcTypeRegistry jdbcTypeRegistry = context.getTypeConfiguration().getJdbcTypeRegistry(); return context.getTypeConfiguration().getJdbcTypeRegistry().getDescriptor( sqlType( context ) );
final EnumType type = context.getEnumeratedType();
final int sqlType = getSqlType( context, type, jdbcTypeRegistry );
return jdbcTypeRegistry.getDescriptor( sqlType );
} }
private int getSqlType(JdbcTypeIndicators context, EnumType type, JdbcTypeRegistry jdbcTypeRegistry) { private int sqlType(JdbcTypeIndicators context) {
final EnumType enumType = context.getEnumeratedType();
final boolean preferNativeEnumTypes = context.isPreferNativeEnumTypesEnabled(); final boolean preferNativeEnumTypes = context.isPreferNativeEnumTypesEnabled();
return switch ( type == null ? ORDINAL : type ) { final JdbcTypeRegistry jdbcTypeRegistry = context.getTypeConfiguration().getJdbcTypeRegistry();
return switch ( enumType == null ? ORDINAL : enumType ) {
case ORDINAL: case ORDINAL:
if ( preferNativeEnumTypes && jdbcTypeRegistry.hasRegisteredDescriptor( ORDINAL_ENUM ) ) { if ( preferNativeEnumTypes && jdbcTypeRegistry.hasRegisteredDescriptor( ORDINAL_ENUM ) ) {
yield ORDINAL_ENUM; yield ORDINAL_ENUM;
@ -142,8 +142,9 @@ public class EnumJavaType<T extends Enum<T>> extends AbstractClassJavaType<T> {
else if ( value instanceof Number number ) { else if ( value instanceof Number number ) {
return fromLong( number.longValue() ); return fromLong( number.longValue() );
} }
else {
return (T) value; return (T) value;
}
} }
/** /**
@ -255,7 +256,7 @@ public class EnumJavaType<T extends Enum<T>> extends AbstractClassJavaType<T> {
} }
@Override @Override
public String getCheckCondition(String columnName, JdbcType jdbcType, BasicValueConverter<?, ?> converter, Dialect dialect) { public String getCheckCondition(String columnName, JdbcType jdbcType, BasicValueConverter<T, ?> converter, Dialect dialect) {
if ( converter != null if ( converter != null
&& jdbcType.getDefaultSqlTypeCode() != NAMED_ENUM ) { && jdbcType.getDefaultSqlTypeCode() != NAMED_ENUM ) {
return renderConvertedEnumCheckConstraint( columnName, jdbcType, converter, dialect ); return renderConvertedEnumCheckConstraint( columnName, jdbcType, converter, dialect );
@ -272,16 +273,19 @@ public class EnumJavaType<T extends Enum<T>> extends AbstractClassJavaType<T> {
} }
} }
@SuppressWarnings({ "rawtypes", "unchecked" })
private String renderConvertedEnumCheckConstraint( private String renderConvertedEnumCheckConstraint(
String columnName, String columnName,
JdbcType jdbcType, JdbcType jdbcType,
BasicValueConverter<?, ?> converter, BasicValueConverter<T, ?> converter,
Dialect dialect) { Dialect dialect) {
final Set valueSet; final Set<?> valueSet = valueSet( jdbcType, converter );
return valueSet == null ? null : dialect.getCheckCondition( columnName, valueSet, jdbcType );
}
private <R> Set<R> valueSet(JdbcType jdbcType, BasicValueConverter<T,R> converter) {
// for `@EnumeratedValue` we already have the possible values... // for `@EnumeratedValue` we already have the possible values...
if ( converter instanceof EnumeratedValueConverter enumeratedValueConverter ) { if ( converter instanceof EnumeratedValueConverter<T,R> enumeratedValueConverter ) {
valueSet = enumeratedValueConverter.getRelationalValueSet(); return enumeratedValueConverter.getRelationalValueSet();
} }
else { else {
if ( !SqlTypes.isIntegral( jdbcType.getJdbcTypeCode() ) if ( !SqlTypes.isIntegral( jdbcType.getJdbcTypeCode() )
@ -290,17 +294,14 @@ public class EnumJavaType<T extends Enum<T>> extends AbstractClassJavaType<T> {
// INTEGER, SMALLINT, TINYINT, (N)CHAR, (N)VARCHAR, LONG(N)VARCHAR // INTEGER, SMALLINT, TINYINT, (N)CHAR, (N)VARCHAR, LONG(N)VARCHAR
return null; return null;
} }
else {
final Class<T> javaTypeClass = getJavaTypeClass(); final T[] enumConstants = getJavaTypeClass().getEnumConstants();
final T[] enumConstants = javaTypeClass.getEnumConstants(); final Set<R> valueSet = setOfSize( enumConstants.length );
valueSet = CollectionHelper.setOfSize( enumConstants.length ); for ( T enumConstant : enumConstants ) {
for ( T enumConstant : enumConstants ) { valueSet.add( converter.toRelationalValue( enumConstant ) );
//noinspection unchecked }
final Object relationalValue = ( (BasicValueConverter) converter ).toRelationalValue( enumConstant ); return valueSet;
valueSet.add( relationalValue );
} }
} }
return dialect.getCheckCondition( columnName, valueSet, jdbcType );
} }
} }

View File

@ -349,7 +349,7 @@ public interface JavaType<T> extends Serializable {
* @since 6.2 * @since 6.2
*/ */
@Incubating @Incubating
default String getCheckCondition(String columnName, JdbcType jdbcType, BasicValueConverter<?, ?> converter, Dialect dialect) { default String getCheckCondition(String columnName, JdbcType jdbcType, BasicValueConverter<T, ?> converter, Dialect dialect) {
return null; return null;
} }
} }