more work on flow typing in the JavaTypes

Signed-off-by: Gavin King <gavin@hibernate.org>
This commit is contained in:
Gavin King 2024-10-30 12:06:20 +01:00
parent 16a15ea6ac
commit 7df56dadda
33 changed files with 306 additions and 362 deletions

View File

@ -296,8 +296,7 @@ public class ArrayJavaType<T> extends AbstractArrayJavaType<T[], T> {
}
}
if ( value instanceof Object[] ) {
final Object[] raw = (Object[]) value;
if ( value instanceof Object[] raw ) {
final Class<T> componentClass = getElementJavaType().getJavaTypeClass();
//noinspection unchecked
final T[] wrapped = (T[]) java.lang.reflect.Array.newInstance( componentClass, raw.length );
@ -314,12 +313,12 @@ public class ArrayJavaType<T> extends AbstractArrayJavaType<T[], T> {
}
return wrapped;
}
else if ( value instanceof byte[] ) {
return fromBytes( (byte[]) value );
else if ( value instanceof byte[] bytes ) {
return fromBytes( bytes );
}
else if ( value instanceof BinaryStream ) {
else if ( value instanceof BinaryStream binaryStream ) {
// When the value is a BinaryStream, this is a deserialization request
return fromBytes( ( (BinaryStream) value ).getBytes() );
return fromBytes( binaryStream.getBytes() );
}
else if ( getElementJavaType().isInstance( value ) ) {
// Support binding a single element as parameter value
@ -329,8 +328,7 @@ public class ArrayJavaType<T> extends AbstractArrayJavaType<T[], T> {
wrapped[0] = (T) value;
return wrapped;
}
else if ( value instanceof Collection<?> ) {
final Collection<?> collection = (Collection<?>) value;
else if ( value instanceof Collection<?> collection ) {
//noinspection unchecked
final T[] wrapped = (T[]) java.lang.reflect.Array.newInstance( getElementJavaType().getJavaTypeClass(), collection.size() );
int i = 0;
@ -359,9 +357,8 @@ public class ArrayJavaType<T> extends AbstractArrayJavaType<T[], T> {
}
}
private T[] fromBytes(byte[] value) {
Class<T> elementClass = getElementJavaType().getJavaTypeClass();
byte[] bytes = value;
private T[] fromBytes(byte[] bytes) {
final Class<T> elementClass = getElementJavaType().getJavaTypeClass();
if ( elementClass.isEnum() ) {
final Object[] array = (Object[]) Array.newInstance( elementClass, bytes.length );
for (int i = 0; i < bytes.length; i++ ) {
@ -375,7 +372,7 @@ public class ArrayJavaType<T> extends AbstractArrayJavaType<T[], T> {
else {
// When the value is a byte[], this is a deserialization request
//noinspection unchecked
return (T[]) SerializationHelper.deserialize(value);
return (T[]) SerializationHelper.deserialize(bytes);
}
}
@ -400,7 +397,7 @@ public class ArrayJavaType<T> extends AbstractArrayJavaType<T[], T> {
return null;
}
//noinspection unchecked
T[] copy = (T[]) Array.newInstance( componentClass, value.length );
final T[] copy = (T[]) Array.newInstance( componentClass, value.length );
for ( int i = 0; i < value.length; i ++ ) {
copy[ i ] = componentPlan.deepCopy( value[ i ] );
}

View File

@ -27,8 +27,8 @@ public interface BasicJavaType<T> extends JavaType<T> {
// match legacy behavior
int jdbcTypeCode = JdbcTypeJavaClassMappings.INSTANCE.determineJdbcTypeCodeForJavaClass( getJavaTypeClass() );
final JdbcType descriptor = indicators.getJdbcType( indicators.resolveJdbcTypeCode( jdbcTypeCode ) );
return descriptor instanceof AdjustableJdbcType
? ( (AdjustableJdbcType) descriptor ).resolveIndicatedType( indicators, this )
return descriptor instanceof AdjustableJdbcType adjustableJdbcType
? adjustableJdbcType.resolveIndicatedType( indicators, this )
: descriptor;
}

View File

@ -163,17 +163,13 @@ public class BlobJavaType extends AbstractClassJavaType<Blob> {
if ( value == null ) {
return null;
}
// Support multiple return types from
// org.hibernate.type.descriptor.sql.BlobTypeDescriptor
if ( Blob.class.isAssignableFrom( value.getClass() ) ) {
return options.getLobCreator().wrap( (Blob) value );
else if ( value instanceof Blob blob ) {
return options.getLobCreator().wrap( blob );
}
else if ( byte[].class.isAssignableFrom( value.getClass() ) ) {
return options.getLobCreator().createBlob( ( byte[] ) value);
else if ( value instanceof byte[] bytes ) {
return options.getLobCreator().createBlob( bytes );
}
else if ( InputStream.class.isAssignableFrom( value.getClass() ) ) {
InputStream inputStream = ( InputStream ) value;
else if ( value instanceof InputStream inputStream ) {
try {
return options.getLobCreator().createBlob( inputStream, inputStream.available() );
}

View File

@ -112,8 +112,7 @@ public class BooleanJavaType extends AbstractClassJavaType<Boolean> implements
return booleanValue;
}
if (value instanceof Number number) {
final int intValue = number.intValue();
return intValue != 0;
return number.intValue() != 0;
}
if (value instanceof Character character) {
return isTrue( character );
@ -192,7 +191,7 @@ public class BooleanJavaType extends AbstractClassJavaType<Boolean> implements
if ( converter != null ) {
if ( jdbcType.isString() ) {
@SuppressWarnings("unchecked")
BasicValueConverter<Boolean, ?> stringConverter =
final BasicValueConverter<Boolean, ?> stringConverter =
(BasicValueConverter<Boolean, ?>) converter;
final Object falseValue = stringConverter.toRelationalValue( false );
final Object trueValue = stringConverter.toRelationalValue( true );
@ -201,11 +200,11 @@ public class BooleanJavaType extends AbstractClassJavaType<Boolean> implements
}
else if ( jdbcType.isInteger() ) {
@SuppressWarnings("unchecked")
BasicValueConverter<Boolean, ? extends Number> numericConverter =
final BasicValueConverter<Boolean, ? extends Number> numericConverter =
(BasicValueConverter<Boolean, ? extends Number>) converter;
final Number falseValue = numericConverter.toRelationalValue( false );
final Number trueValue = numericConverter.toRelationalValue( true );
Long[] values = getPossibleNumericValues( numericConverter, falseValue, trueValue );
final Long[] values = getPossibleNumericValues( numericConverter, falseValue, trueValue );
return dialect.getCheckCondition( columnName, values );
}
}
@ -222,7 +221,7 @@ public class BooleanJavaType extends AbstractClassJavaType<Boolean> implements
}
catch ( NullPointerException ignored ) {
}
Long[] values = new Long[nullValue != null ? 3 : 2];
final Long[] values = new Long[nullValue != null ? 3 : 2];
values[0] = falseValue != null ? falseValue.longValue() : null;
values[1] = trueValue != null ? trueValue.longValue() : null;
if ( nullValue != null ) {

View File

@ -149,16 +149,16 @@ public class BooleanPrimitiveArrayJavaType extends AbstractArrayJavaType<boolean
}
}
if ( value instanceof boolean[] ) {
return (boolean[]) value;
if ( value instanceof boolean[] booleans ) {
return booleans;
}
else if ( value instanceof byte[] ) {
else if ( value instanceof byte[] bytes ) {
// When the value is a byte[], this is a deserialization request
return (boolean[]) SerializationHelper.deserialize( (byte[]) value );
return (boolean[]) SerializationHelper.deserialize( bytes );
}
else if ( value instanceof BinaryStream ) {
else if ( value instanceof BinaryStream binaryStream ) {
// When the value is a BinaryStream, this is a deserialization request
return (boolean[]) SerializationHelper.deserialize( ( (BinaryStream) value ).getBytes() );
return (boolean[]) SerializationHelper.deserialize( binaryStream.getBytes() );
}
else if ( value.getClass().isArray() ) {
final boolean[] wrapped = new boolean[Array.getLength( value )];
@ -167,12 +167,11 @@ public class BooleanPrimitiveArrayJavaType extends AbstractArrayJavaType<boolean
}
return wrapped;
}
else if ( value instanceof Boolean ) {
else if ( value instanceof Boolean booleanValue ) {
// Support binding a single element as parameter value
return new boolean[]{ (boolean) value };
return new boolean[]{ booleanValue };
}
else if ( value instanceof Collection<?> ) {
final Collection<?> collection = (Collection<?>) value;
else if ( value instanceof Collection<?> collection ) {
final boolean[] wrapped = new boolean[collection.size()];
int i = 0;
for ( Object e : collection ) {

View File

@ -35,7 +35,7 @@ public class ByteArrayJavaType extends AbstractClassJavaType<Byte[]> {
@Override
public boolean areEqual(Byte[] one, Byte[] another) {
return one == another
|| ( one != null && another != null && Arrays.equals(one, another) );
|| one != null && another != null && Arrays.equals(one, another);
}
@Override
public int extractHashCode(Byte[] bytes) {

View File

@ -79,14 +79,14 @@ public class ByteJavaType extends AbstractClassJavaType<Byte>
if ( value == null ) {
return null;
}
if ( value instanceof Byte ) {
return (Byte) value;
if ( value instanceof Byte byteValue ) {
return byteValue;
}
if ( value instanceof Number ) {
return ( (Number) value ).byteValue();
if ( value instanceof Number number ) {
return number.byteValue();
}
if ( value instanceof String ) {
return Byte.valueOf( ( (String) value ) );
if ( value instanceof String string ) {
return Byte.valueOf( string );
}
throw unknownWrap( value.getClass() );
}
@ -132,41 +132,41 @@ public class ByteJavaType extends AbstractClassJavaType<Byte>
return null;
}
if ( value instanceof Byte ) {
return (Byte) value;
if ( value instanceof Byte byteValue ) {
return byteValue;
}
if ( value instanceof Short ) {
return CoercionHelper.toByte( (Short) value );
if ( value instanceof Short shotValue ) {
return CoercionHelper.toByte( shotValue );
}
if ( value instanceof Integer ) {
return CoercionHelper.toByte( (Integer) value );
if ( value instanceof Integer integerValue ) {
return CoercionHelper.toByte( integerValue );
}
if ( value instanceof Long ) {
return CoercionHelper.toByte( (Long) value );
if ( value instanceof Long longValue ) {
return CoercionHelper.toByte( longValue );
}
if ( value instanceof Double ) {
return CoercionHelper.toByte( (Double) value );
if ( value instanceof Double doubleValue ) {
return CoercionHelper.toByte( doubleValue );
}
if ( value instanceof Float ) {
return CoercionHelper.toByte( (Float) value );
if ( value instanceof Float floatValue ) {
return CoercionHelper.toByte( floatValue );
}
if ( value instanceof BigInteger ) {
return CoercionHelper.toByte( (BigInteger) value );
if ( value instanceof BigInteger bigInteger ) {
return CoercionHelper.toByte( bigInteger );
}
if ( value instanceof BigDecimal ) {
return CoercionHelper.toByte( (BigDecimal) value );
if ( value instanceof BigDecimal bigDecimal ) {
return CoercionHelper.toByte( bigDecimal );
}
if ( value instanceof String ) {
if ( value instanceof String string ) {
return CoercionHelper.coerceWrappingError(
() -> Byte.parseByte( (String) value )
() -> Byte.parseByte( string )
);
}

View File

@ -115,17 +115,17 @@ public class CalendarDateJavaType extends AbstractTemporalJavaType<Calendar> {
if ( value == null ) {
return null;
}
if (value instanceof Calendar) {
return (Calendar) value;
else if (value instanceof Calendar calendar) {
return calendar;
}
if ( !(value instanceof Date)) {
else if ( value instanceof Date date ) {
final Calendar cal = new GregorianCalendar();
cal.setTime( date );
return cal;
}
else {
throw unknownWrap( value.getClass() );
}
Calendar cal = new GregorianCalendar();
cal.setTime( (Date) value );
return cal;
}
@Override

View File

@ -132,17 +132,18 @@ public class CalendarJavaType extends AbstractTemporalJavaType<Calendar> impleme
if ( value == null ) {
return null;
}
if (value instanceof Calendar) {
return (Calendar) value;
else if (value instanceof Calendar calendar) {
return calendar;
}
if ( !(value instanceof java.util.Date)) {
else if ( value instanceof java.util.Date date ) {
final Calendar cal = new GregorianCalendar();
cal.setTime( date );
return cal;
}
else {
throw unknownWrap( value.getClass() );
}
Calendar cal = new GregorianCalendar();
cal.setTime( (java.util.Date) value );
return cal;
}
@Override

View File

@ -117,17 +117,17 @@ public class CalendarTimeJavaType extends AbstractTemporalJavaType<Calendar> {
if ( value == null ) {
return null;
}
if (value instanceof Calendar) {
return (Calendar) value;
else if (value instanceof Calendar calendar) {
return calendar;
}
if ( !(value instanceof Date)) {
else if ( value instanceof Date date) {
final Calendar cal = new GregorianCalendar();
cal.setTime( date );
return cal;
}
else {
throw unknownWrap( value.getClass() );
}
Calendar cal = new GregorianCalendar();
cal.setTime( (Date) value );
return cal;
}
@Override

View File

@ -45,7 +45,7 @@ public class CharacterArrayJavaType extends AbstractClassJavaType<Character[]> {
@Override
public boolean areEqual(Character[] one, Character[] another) {
return one == another
|| ( one != null && another != null && Arrays.equals( one, another ) );
|| one != null && another != null && Arrays.equals( one, another );
}
@Override
@ -97,17 +97,17 @@ public class CharacterArrayJavaType extends AbstractClassJavaType<Character[]> {
if ( value == null ) {
return null;
}
if (value instanceof Character[]) {
return (Character[]) value;
if (value instanceof Character[] characters) {
return characters;
}
if (value instanceof String) {
return wrapChars( ( (String) value ).toCharArray() );
if (value instanceof String string) {
return wrapChars( string.toCharArray() );
}
if (value instanceof Clob) {
return wrapChars( DataHelper.extractString( ( (Clob) value ) ).toCharArray() );
if (value instanceof Clob clob) {
return wrapChars( DataHelper.extractString( clob ).toCharArray() );
}
if (value instanceof Reader) {
return wrapChars( DataHelper.extractString( (Reader) value ).toCharArray() );
if (value instanceof Reader reader) {
return wrapChars( DataHelper.extractString( reader ).toCharArray() );
}
throw unknownWrap( value.getClass() );
}

View File

@ -145,17 +145,13 @@ public class ClobJavaType extends AbstractClassJavaType<Clob> {
if ( value == null ) {
return null;
}
// Support multiple return types from
// org.hibernate.type.descriptor.sql.ClobTypeDescriptor
if ( Clob.class.isAssignableFrom( value.getClass() ) ) {
return options.getLobCreator().wrap( (Clob) value );
else if ( value instanceof Clob clob ) {
return options.getLobCreator().wrap( clob );
}
else if ( String.class.isAssignableFrom( value.getClass() ) ) {
return options.getLobCreator().createClob( ( String ) value);
else if ( value instanceof String string ) {
return options.getLobCreator().createClob( string );
}
else if ( Reader.class.isAssignableFrom( value.getClass() ) ) {
Reader reader = (Reader) value;
else if ( value instanceof Reader reader ) {
return options.getLobCreator().createClob( DataHelper.extractString( reader ) );
}

View File

@ -151,16 +151,16 @@ public class DateJavaType extends AbstractTemporalJavaType<Date> implements Vers
if ( value == null ) {
return null;
}
if (value instanceof Date) {
return (Date) value;
if (value instanceof Date date) {
return date;
}
if (value instanceof Long) {
return new Date( (Long) value );
if (value instanceof Long longValue) {
return new Date( longValue );
}
if (value instanceof Calendar) {
return new Date( ( (Calendar) value ).getTimeInMillis() );
if (value instanceof Calendar calendar) {
return new Date( calendar.getTimeInMillis() );
}
throw unknownWrap( value.getClass() );

View File

@ -89,14 +89,14 @@ public class DoubleJavaType extends AbstractClassJavaType<Double> implements
if ( value == null ) {
return null;
}
if ( value instanceof Double ) {
return (Double) value;
if ( value instanceof Double doubleValue ) {
return doubleValue;
}
if ( value instanceof Number ) {
return ( (Number) value ).doubleValue();
if ( value instanceof Number number ) {
return number.doubleValue();
}
else if ( value instanceof String ) {
return Double.valueOf( ( (String) value ) );
else if ( value instanceof String string ) {
return Double.valueOf( string );
}
throw unknownWrap( value.getClass() );
}
@ -157,41 +157,29 @@ public class DoubleJavaType extends AbstractClassJavaType<Double> implements
return null;
}
if ( value instanceof Double ) {
return ( (Double) value );
if ( value instanceof Double doubleValue ) {
return doubleValue;
}
if ( value instanceof Byte ) {
return ( (Byte) value ).doubleValue();
if ( value instanceof Float floatValue ) {
return CoercionHelper.toDouble( floatValue );
}
if ( value instanceof Short ) {
return ( (Short) value ).doubleValue();
if ( value instanceof BigInteger bigInteger ) {
return CoercionHelper.toDouble( bigInteger );
}
if ( value instanceof Integer ) {
return ( (Integer) value ).doubleValue();
if ( value instanceof BigDecimal bigDecimal ) {
return CoercionHelper.toDouble( bigDecimal );
}
if ( value instanceof Long ) {
return ( (Long) value ).doubleValue();
if ( value instanceof Number number ) {
return number.doubleValue();
}
if ( value instanceof Float ) {
return CoercionHelper.toDouble( (Float) value );
}
if ( value instanceof BigInteger ) {
return CoercionHelper.toDouble( (BigInteger) value );
}
if ( value instanceof BigDecimal ) {
return CoercionHelper.toDouble( (BigDecimal) value );
}
if ( value instanceof String ) {
if ( value instanceof String string ) {
return CoercionHelper.coerceWrappingError(
() -> Double.parseDouble( (String) value )
() -> Double.parseDouble( string )
);
}

View File

@ -149,16 +149,16 @@ public class DoublePrimitiveArrayJavaType extends AbstractArrayJavaType<double[]
}
}
if ( value instanceof double[] ) {
return (double[]) value;
if ( value instanceof double[] doubles ) {
return doubles;
}
else if ( value instanceof byte[] ) {
else if ( value instanceof byte[] bytes ) {
// When the value is a byte[], this is a deserialization request
return (double[]) SerializationHelper.deserialize( (byte[]) value );
return (double[]) SerializationHelper.deserialize( bytes );
}
else if ( value instanceof BinaryStream ) {
else if ( value instanceof BinaryStream binaryStream ) {
// When the value is a BinaryStream, this is a deserialization request
return (double[]) SerializationHelper.deserialize( ( (BinaryStream) value ).getBytes() );
return (double[]) SerializationHelper.deserialize( binaryStream.getBytes() );
}
else if ( value.getClass().isArray() ) {
final double[] wrapped = new double[Array.getLength( value )];
@ -167,12 +167,11 @@ public class DoublePrimitiveArrayJavaType extends AbstractArrayJavaType<double[]
}
return wrapped;
}
else if ( value instanceof Double ) {
else if ( value instanceof Double doubleValue ) {
// Support binding a single element as parameter value
return new double[]{ (double) value };
return new double[]{ doubleValue };
}
else if ( value instanceof Collection<?> ) {
final Collection<?> collection = (Collection<?>) value;
else if ( value instanceof Collection<?> collection ) {
final double[] wrapped = new double[collection.size()];
int i = 0;
for ( Object e : collection ) {

View File

@ -88,14 +88,14 @@ public class FloatJavaType extends AbstractClassJavaType<Float> implements Primi
if ( value == null ) {
return null;
}
if (value instanceof Float) {
return (Float) value;
if (value instanceof Float floatValue) {
return floatValue;
}
if (value instanceof Number) {
return ( (Number) value ).floatValue();
if (value instanceof Number number) {
return number.floatValue();
}
else if (value instanceof String) {
return Float.valueOf( ( (String) value ) );
else if (value instanceof String string) {
return Float.valueOf( string );
}
throw unknownWrap( value.getClass() );
}
@ -154,36 +154,12 @@ public class FloatJavaType extends AbstractClassJavaType<Float> implements Primi
return null;
}
if ( value instanceof Float ) {
return (Float) value;
if ( value instanceof Float floatValue ) {
return floatValue;
}
if ( value instanceof Double ) {
return ( (Double) value ).floatValue();
}
if ( value instanceof Byte ) {
return ( (Byte) value ).floatValue();
}
if ( value instanceof Short ) {
return ( (Short) value ).floatValue();
}
if ( value instanceof Integer ) {
return ( (Integer) value ).floatValue();
}
if ( value instanceof Long ) {
return ( (Long) value ).floatValue();
}
if ( value instanceof BigInteger ) {
return ( (BigInteger) value ).floatValue();
}
if ( value instanceof BigDecimal ) {
return ( (BigDecimal) value ).floatValue();
if ( value instanceof Number number ) {
return number.floatValue();
}
if ( value instanceof String ) {

View File

@ -149,16 +149,16 @@ public class FloatPrimitiveArrayJavaType extends AbstractArrayJavaType<float[],
}
}
if ( value instanceof float[] ) {
return (float[]) value;
if ( value instanceof float[] floats ) {
return floats;
}
else if ( value instanceof byte[] ) {
else if ( value instanceof byte[] bytes ) {
// When the value is a byte[], this is a deserialization request
return (float[]) SerializationHelper.deserialize( (byte[]) value );
return (float[]) SerializationHelper.deserialize( bytes );
}
else if ( value instanceof BinaryStream ) {
else if ( value instanceof BinaryStream binaryStream ) {
// When the value is a BinaryStream, this is a deserialization request
return (float[]) SerializationHelper.deserialize( ( (BinaryStream) value ).getBytes() );
return (float[]) SerializationHelper.deserialize( binaryStream.getBytes() );
}
else if ( value.getClass().isArray() ) {
final float[] wrapped = new float[Array.getLength( value )];
@ -167,12 +167,11 @@ public class FloatPrimitiveArrayJavaType extends AbstractArrayJavaType<float[],
}
return wrapped;
}
else if ( value instanceof Float ) {
else if ( value instanceof Float floatValue) {
// Support binding a single element as parameter value
return new float[]{ (float) value };
return new float[]{ floatValue };
}
else if ( value instanceof Collection<?> ) {
final Collection<?> collection = (Collection<?>) value;
else if ( value instanceof Collection<?> collection ) {
final float[] wrapped = new float[collection.size()];
int i = 0;
for ( Object e : collection ) {

View File

@ -74,20 +74,20 @@ public class InetAddressJavaType extends AbstractClassJavaType<InetAddress> {
if ( value == null ) {
return null;
}
if (value instanceof InetAddress) {
return (InetAddress) value;
if (value instanceof InetAddress inetAddress) {
return inetAddress;
}
if (value instanceof byte[]) {
if (value instanceof byte[] bytes) {
try {
return InetAddress.getByAddress( (byte[]) value );
return InetAddress.getByAddress( bytes );
}
catch (UnknownHostException e) {
throw new IllegalArgumentException( e );
}
}
if (value instanceof String) {
if (value instanceof String string) {
try {
return InetAddress.getByName( (String) value );
return InetAddress.getByName( string );
}
catch (UnknownHostException e) {
throw new IllegalArgumentException( e );

View File

@ -85,14 +85,14 @@ public class IntegerJavaType extends AbstractClassJavaType<Integer>
if ( value == null ) {
return null;
}
if (value instanceof Integer) {
return (Integer) value;
if (value instanceof Integer integer) {
return integer;
}
if (value instanceof Number) {
return ( (Number) value ).intValue();
if (value instanceof Number number) {
return number.intValue();
}
if (value instanceof String) {
return Integer.valueOf( (String) value );
if (value instanceof String string) {
return Integer.valueOf( string );
}
throw unknownWrap( value.getClass() );
}
@ -147,41 +147,41 @@ public class IntegerJavaType extends AbstractClassJavaType<Integer>
return null;
}
if ( value instanceof Integer ) {
return (int) value;
if ( value instanceof Integer integer ) {
return integer;
}
if ( value instanceof Short ) {
return CoercionHelper.toInteger( (short) value );
if ( value instanceof Short shortValue ) {
return CoercionHelper.toInteger( shortValue );
}
if ( value instanceof Byte ) {
return CoercionHelper.toInteger( (byte) value );
if ( value instanceof Byte byteValue ) {
return CoercionHelper.toInteger( byteValue );
}
if ( value instanceof Long ) {
return CoercionHelper.toInteger( (long) value );
if ( value instanceof Long longValue ) {
return CoercionHelper.toInteger( longValue );
}
if ( value instanceof Double ) {
return CoercionHelper.toInteger( (double) value );
if ( value instanceof Double doubleValue ) {
return CoercionHelper.toInteger( doubleValue );
}
if ( value instanceof Float ) {
return CoercionHelper.toInteger( (float) value );
if ( value instanceof Float floatValue ) {
return CoercionHelper.toInteger( floatValue );
}
if ( value instanceof BigInteger ) {
return CoercionHelper.toInteger( (BigInteger) value );
if ( value instanceof BigInteger bigInteger ) {
return CoercionHelper.toInteger( bigInteger );
}
if ( value instanceof BigDecimal ) {
return CoercionHelper.toInteger( (BigDecimal) value );
if ( value instanceof BigDecimal bigDecimal ) {
return CoercionHelper.toInteger( bigDecimal );
}
if ( value instanceof String ) {
if ( value instanceof String string ) {
return CoercionHelper.coerceWrappingError(
() -> Integer.parseInt( (String) value )
() -> Integer.parseInt( string )
);
}

View File

@ -149,16 +149,16 @@ public class IntegerPrimitiveArrayJavaType extends AbstractArrayJavaType<int[],
}
}
if ( value instanceof int[] ) {
return (int[]) value;
if ( value instanceof int[] ints ) {
return ints;
}
else if ( value instanceof byte[] ) {
else if ( value instanceof byte[] bytes ) {
// When the value is a byte[], this is a deserialization request
return (int[]) SerializationHelper.deserialize( (byte[]) value );
return (int[]) SerializationHelper.deserialize( bytes );
}
else if ( value instanceof BinaryStream ) {
else if ( value instanceof BinaryStream binaryStream ) {
// When the value is a BinaryStream, this is a deserialization request
return (int[]) SerializationHelper.deserialize( ( (BinaryStream) value ).getBytes() );
return (int[]) SerializationHelper.deserialize( binaryStream.getBytes() );
}
else if ( value.getClass().isArray() ) {
final int[] wrapped = new int[Array.getLength( value )];
@ -167,12 +167,11 @@ public class IntegerPrimitiveArrayJavaType extends AbstractArrayJavaType<int[],
}
return wrapped;
}
else if ( value instanceof Integer ) {
else if ( value instanceof Integer integer ) {
// Support binding a single element as parameter value
return new int[]{ (int) value };
return new int[]{ integer };
}
else if ( value instanceof Collection<?> ) {
final Collection<?> collection = (Collection<?>) value;
else if ( value instanceof Collection<?> collection ) {
final int[] wrapped = new int[collection.size()];
int i = 0;
for ( Object e : collection ) {

View File

@ -187,24 +187,24 @@ public class JdbcDateJavaType extends AbstractTemporalJavaType<Date> {
return null;
}
if ( value instanceof java.sql.Date ) {
return (java.sql.Date) value;
if ( value instanceof java.sql.Date date ) {
return date;
}
if ( value instanceof Long ) {
return new java.sql.Date( toDateEpoch( (Long) value ) );
if ( value instanceof Long longValue ) {
return new java.sql.Date( toDateEpoch( longValue ) );
}
if ( value instanceof Calendar ) {
return new java.sql.Date( toDateEpoch( ( (Calendar) value ).getTimeInMillis() ) );
if ( value instanceof Calendar calendar ) {
return new java.sql.Date( toDateEpoch( calendar.getTimeInMillis() ) );
}
if ( value instanceof Date ) {
return unwrapSqlDate( (Date) value );
if ( value instanceof Date date ) {
return unwrapSqlDate( date );
}
if ( value instanceof LocalDate ) {
return java.sql.Date.valueOf( (LocalDate) value );
if ( value instanceof LocalDate localDate ) {
return java.sql.Date.valueOf( localDate );
}
throw unknownWrap( value.getClass() );

View File

@ -175,16 +175,15 @@ public class JdbcTimeJavaType extends AbstractTemporalJavaType<Date> {
return null;
}
if ( value instanceof Time ) {
return (Date) value;
if ( value instanceof Time time ) {
return time;
}
if ( value instanceof Date ) {
return new Time( ( (Date) value ).getTime() % 86_400_000 );
if ( value instanceof Date date ) {
return new Time( date.getTime() % 86_400_000 );
}
if ( value instanceof LocalTime ) {
final LocalTime localTime = (LocalTime) value;
if ( value instanceof LocalTime localTime ) {
final Time time = Time.valueOf( localTime );
if ( localTime.getNano() == 0 ) {
return time;
@ -193,12 +192,12 @@ public class JdbcTimeJavaType extends AbstractTemporalJavaType<Date> {
return new Time( time.getTime() + DateTimeUtils.roundToPrecision( localTime.getNano(), 3 ) / 1000000 );
}
if ( value instanceof Long ) {
return new Time( (Long) value );
if ( value instanceof Long longValue ) {
return new Time( longValue );
}
if ( value instanceof Calendar ) {
return new Time( ( (Calendar) value ).getTimeInMillis() % 86_400_000 );
if ( value instanceof Calendar calendar ) {
return new Time( calendar.getTimeInMillis() % 86_400_000 );
}
throw unknownWrap( value.getClass() );

View File

@ -167,24 +167,24 @@ public class JdbcTimestampJavaType extends AbstractTemporalJavaType<Date> implem
if ( value == null ) {
return null;
}
if ( value instanceof Timestamp ) {
return (Timestamp) value;
if ( value instanceof Timestamp timestamp ) {
return timestamp;
}
if ( value instanceof Date ) {
return new Timestamp( ( (Date) value ).getTime() );
if ( value instanceof Date date ) {
return new Timestamp( date.getTime() );
}
if ( value instanceof LocalDateTime ) {
return Timestamp.valueOf( (LocalDateTime) value );
if ( value instanceof LocalDateTime localDateTime ) {
return Timestamp.valueOf( localDateTime );
}
if ( value instanceof Long ) {
return new Timestamp( (Long) value );
if ( value instanceof Long longValue ) {
return new Timestamp( longValue );
}
if ( value instanceof Calendar ) {
return new Timestamp( ( (Calendar) value ).getTimeInMillis() );
if ( value instanceof Calendar calendar ) {
return new Timestamp( calendar.getTimeInMillis() );
}
throw unknownWrap( value.getClass() );

View File

@ -107,11 +107,11 @@ public class LocaleJavaType extends AbstractClassJavaType<Locale> {
if ( value == null ) {
return null;
}
if ( value instanceof Locale ) {
return (Locale) value;
if ( value instanceof Locale locale ) {
return locale;
}
if (value instanceof CharSequence) {
return fromString( (CharSequence) value );
if (value instanceof CharSequence charSequence) {
return fromString( charSequence );
}
throw unknownWrap( value.getClass() );
}

View File

@ -85,14 +85,14 @@ public class LongJavaType extends AbstractClassJavaType<Long>
if ( value == null ) {
return null;
}
if ( value instanceof Long ) {
return (Long) value;
if ( value instanceof Long longValue ) {
return longValue;
}
if ( value instanceof Number ) {
return ( (Number) value ).longValue();
if ( value instanceof Number number ) {
return number.longValue();
}
else if ( value instanceof String ) {
return Long.valueOf( ( (String) value ) );
else if ( value instanceof String string ) {
return Long.valueOf( string );
}
throw unknownWrap( value.getClass() );
}
@ -113,41 +113,41 @@ public class LongJavaType extends AbstractClassJavaType<Long>
return null;
}
if ( value instanceof Long ) {
return ( (Long) value );
if ( value instanceof Long longValue ) {
return longValue;
}
if ( value instanceof Byte ) {
return CoercionHelper.toLong( (Byte) value );
if ( value instanceof Byte byteValue ) {
return CoercionHelper.toLong( byteValue );
}
if ( value instanceof Short ) {
return CoercionHelper.toLong( (Short) value );
if ( value instanceof Short shortValue ) {
return CoercionHelper.toLong( shortValue );
}
if ( value instanceof Integer ) {
return CoercionHelper.toLong( (Integer) value );
if ( value instanceof Integer integerValue ) {
return CoercionHelper.toLong( integerValue );
}
if ( value instanceof Double ) {
return CoercionHelper.toLong( (Double) value );
if ( value instanceof Double doubleValue ) {
return CoercionHelper.toLong( doubleValue );
}
if ( value instanceof Float ) {
return CoercionHelper.toLong( (Float) value );
if ( value instanceof Float floatValue ) {
return CoercionHelper.toLong( floatValue );
}
if ( value instanceof BigInteger ) {
return CoercionHelper.toLong( (BigInteger) value );
if ( value instanceof BigInteger bigInteger ) {
return CoercionHelper.toLong( bigInteger );
}
if ( value instanceof BigDecimal ) {
return CoercionHelper.toLong( (BigDecimal) value );
if ( value instanceof BigDecimal bigDecimal ) {
return CoercionHelper.toLong( bigDecimal );
}
if ( value instanceof String ) {
if ( value instanceof String string ) {
return CoercionHelper.coerceWrappingError(
() -> Long.parseLong( (String) value )
() -> Long.parseLong( string )
);
}

View File

@ -149,16 +149,16 @@ public class LongPrimitiveArrayJavaType extends AbstractArrayJavaType<long[], Lo
}
}
if ( value instanceof long[] ) {
return (long[]) value;
if ( value instanceof long[] longs ) {
return longs;
}
else if ( value instanceof byte[] ) {
else if ( value instanceof byte[] bytes ) {
// When the value is a byte[], this is a deserialization request
return (long[]) SerializationHelper.deserialize( (byte[]) value );
return (long[]) SerializationHelper.deserialize( bytes );
}
else if ( value instanceof BinaryStream ) {
else if ( value instanceof BinaryStream binaryStream ) {
// When the value is a BinaryStream, this is a deserialization request
return (long[]) SerializationHelper.deserialize( ( (BinaryStream) value ).getBytes() );
return (long[]) SerializationHelper.deserialize( binaryStream.getBytes() );
}
else if ( value.getClass().isArray() ) {
final long[] wrapped = new long[Array.getLength( value )];
@ -167,12 +167,11 @@ public class LongPrimitiveArrayJavaType extends AbstractArrayJavaType<long[], Lo
}
return wrapped;
}
else if ( value instanceof Long ) {
else if ( value instanceof Long longValue ) {
// Support binding a single element as parameter value
return new long[]{ (long) value };
return new long[]{ longValue };
}
else if ( value instanceof Collection<?> ) {
final Collection<?> collection = (Collection<?>) value;
else if ( value instanceof Collection<?> collection ) {
final long[] wrapped = new long[collection.size()];
int i = 0;
for ( Object e : collection ) {

View File

@ -113,8 +113,8 @@ public class NClobJavaType extends AbstractClassJavaType<NClob> {
}
private NClob getOrCreateNClob(NClob value, WrapperOptions options) throws SQLException {
if ( value instanceof WrappedNClob ) {
value = ( (WrappedNClob) value ).getWrappedNClob();
if ( value instanceof WrappedNClob wrappedNClob ) {
value = wrappedNClob.getWrappedNClob();
}
if ( options.getDialect().useConnectionToCreateLob() ) {
if ( value.length() == 0 ) {
@ -137,11 +137,10 @@ public class NClobJavaType extends AbstractClassJavaType<NClob> {
// Support multiple return types from
// org.hibernate.type.descriptor.sql.ClobTypeDescriptor
if ( NClob.class.isAssignableFrom( value.getClass() ) ) {
return options.getLobCreator().wrap( (NClob) value );
if ( value instanceof NClob clob ) {
return options.getLobCreator().wrap( clob );
}
else if ( Reader.class.isAssignableFrom( value.getClass() ) ) {
Reader reader = (Reader) value;
else if ( value instanceof Reader reader ) {
return options.getLobCreator().createNClob( DataHelper.extractString( reader ) );
}

View File

@ -89,8 +89,8 @@ public class ObjectArrayJavaType extends AbstractClassJavaType<Object[]> {
if ( value == null ) {
return null;
}
if (value instanceof Object[]) {
return (Object[]) value;
if (value instanceof Object[] objects) {
return objects;
}
throw unknownWrap( value.getClass() );
}

View File

@ -36,7 +36,7 @@ public class PrimitiveByteArrayJavaType extends AbstractClassJavaType<byte[]>
@Override
public boolean areEqual(byte[] one, byte[] another) {
return one == another
|| ( one != null && another != null && Arrays.equals( one, another ) );
|| one != null && another != null && Arrays.equals( one, another );
}
@Override
@ -134,9 +134,9 @@ public class PrimitiveByteArrayJavaType extends AbstractClassJavaType<byte[]>
throw new HibernateException( "Unable to access lob stream", e );
}
}
else if ( value instanceof Byte ) {
else if ( value instanceof Byte byteValue ) {
// Support binding a single element as parameter value
return new byte[]{ (byte) value };
return new byte[]{ byteValue };
}
throw unknownWrap( value.getClass() );

View File

@ -38,7 +38,7 @@ public class PrimitiveCharacterArrayJavaType extends AbstractClassJavaType<char[
@Override
public boolean areEqual(char[] one, char[] another) {
return one == another
|| ( one != null && another != null && Arrays.equals( one, another ) );
|| one != null && another != null && Arrays.equals( one, another );
}
@Override
@ -80,21 +80,21 @@ public class PrimitiveCharacterArrayJavaType extends AbstractClassJavaType<char[
if ( value == null ) {
return null;
}
if (value instanceof char[]) {
return (char[]) value;
if (value instanceof char[] chars) {
return chars;
}
if (value instanceof String) {
return ( (String) value ).toCharArray();
if (value instanceof String string) {
return string.toCharArray();
}
if (value instanceof Clob) {
return DataHelper.extractString( ( (Clob) value ) ).toCharArray();
if (value instanceof Clob clob) {
return DataHelper.extractString( clob ).toCharArray();
}
if (value instanceof Reader) {
return DataHelper.extractString( ( (Reader) value ) ).toCharArray();
if (value instanceof Reader reader) {
return DataHelper.extractString( reader ).toCharArray();
}
else if ( value instanceof Character ) {
else if ( value instanceof Character character ) {
// Support binding a single element as parameter value
return new char[]{ (char) value };
return new char[]{ character };
}
throw unknownWrap( value.getClass() );
}

View File

@ -86,14 +86,14 @@ public class ShortJavaType extends AbstractClassJavaType<Short>
if ( value == null ) {
return null;
}
if (value instanceof Short) {
return (Short) value;
if (value instanceof Short shortValue) {
return shortValue;
}
if (value instanceof Number) {
return ( (Number) value ).shortValue();
if (value instanceof Number number) {
return number.shortValue();
}
if (value instanceof String) {
return Short.valueOf( ( (String) value ) );
if (value instanceof String string) {
return Short.valueOf( string );
}
throw unknownWrap( value.getClass() );
}
@ -139,41 +139,41 @@ public class ShortJavaType extends AbstractClassJavaType<Short>
return null;
}
if ( value instanceof Short ) {
return (short) value;
if ( value instanceof Short shortValue ) {
return shortValue;
}
if ( value instanceof Byte ) {
return CoercionHelper.toShort( (Byte) value );
if ( value instanceof Byte byteValue ) {
return CoercionHelper.toShort( byteValue );
}
if ( value instanceof Integer ) {
return CoercionHelper.toShort( (Integer) value );
if ( value instanceof Integer integerValue ) {
return CoercionHelper.toShort( integerValue );
}
if ( value instanceof Long ) {
return CoercionHelper.toShort( (Long) value );
if ( value instanceof Long longValue ) {
return CoercionHelper.toShort( longValue );
}
if ( value instanceof Double ) {
return CoercionHelper.toShort( (Double) value );
if ( value instanceof Double doubleValue ) {
return CoercionHelper.toShort( doubleValue );
}
if ( value instanceof Float ) {
return CoercionHelper.toShort( (Float) value );
if ( value instanceof Float floatValue ) {
return CoercionHelper.toShort( floatValue );
}
if ( value instanceof BigInteger ) {
return CoercionHelper.toShort( (BigInteger) value );
if ( value instanceof BigInteger bigInteger ) {
return CoercionHelper.toShort( bigInteger );
}
if ( value instanceof BigDecimal ) {
return CoercionHelper.toShort( (BigDecimal) value );
if ( value instanceof BigDecimal bigDecimal ) {
return CoercionHelper.toShort( bigDecimal );
}
if ( value instanceof String ) {
if ( value instanceof String string ) {
return CoercionHelper.coerceWrappingError(
() -> Short.parseShort( (String) value )
() -> Short.parseShort( string )
);
}

View File

@ -149,16 +149,16 @@ public class ShortPrimitiveArrayJavaType extends AbstractArrayJavaType<short[],
}
}
if ( value instanceof short[] ) {
return (short[]) value;
if ( value instanceof short[] shorts ) {
return shorts;
}
else if ( value instanceof byte[] ) {
else if ( value instanceof byte[] bytes ) {
// When the value is a byte[], this is a deserialization request
return (short[]) SerializationHelper.deserialize( (byte[]) value );
return (short[]) SerializationHelper.deserialize( bytes );
}
else if ( value instanceof BinaryStream ) {
else if ( value instanceof BinaryStream binaryStream) {
// When the value is a BinaryStream, this is a deserialization request
return (short[]) SerializationHelper.deserialize( ( (BinaryStream) value ).getBytes() );
return (short[]) SerializationHelper.deserialize( binaryStream.getBytes() );
}
else if ( value.getClass().isArray() ) {
final short[] wrapped = new short[Array.getLength( value )];
@ -167,12 +167,11 @@ public class ShortPrimitiveArrayJavaType extends AbstractArrayJavaType<short[],
}
return wrapped;
}
else if ( value instanceof Short ) {
else if ( value instanceof Short shortValue ) {
// Support binding a single element as parameter value
return new short[]{ (short) value };
return new short[]{ shortValue };
}
else if ( value instanceof Collection<?> ) {
final Collection<?> collection = (Collection<?>) value;
else if ( value instanceof Collection<?> collection ) {
final short[] wrapped = new short[collection.size()];
int i = 0;
for ( Object e : collection ) {

View File

@ -7,7 +7,6 @@ package org.hibernate.type.descriptor.java;
import java.net.MalformedURLException;
import java.net.URL;
import org.hibernate.HibernateException;
import org.hibernate.type.SqlTypes;
import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.jdbc.JdbcType;
@ -44,7 +43,7 @@ public class UrlJavaType extends AbstractClassJavaType<URL> {
return new URL( string.toString() );
}
catch ( MalformedURLException e ) {
throw new HibernateException( "Unable to convert string [" + string + "] to URL : " + e );
throw new CoercionException( "Unable to convert string [" + string + "] to URL : " + e );
}
}