Skip to content

Commit 7df56da

Browse files
committed
more work on flow typing in the JavaTypes
Signed-off-by: Gavin King <[email protected]>
1 parent 16a15ea commit 7df56da

33 files changed

+306
-362
lines changed

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ArrayJavaType.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,7 @@ public <X> T[] wrap(X value, WrapperOptions options) {
296296
}
297297
}
298298

299-
if ( value instanceof Object[] ) {
300-
final Object[] raw = (Object[]) value;
299+
if ( value instanceof Object[] raw ) {
301300
final Class<T> componentClass = getElementJavaType().getJavaTypeClass();
302301
//noinspection unchecked
303302
final T[] wrapped = (T[]) java.lang.reflect.Array.newInstance( componentClass, raw.length );
@@ -314,12 +313,12 @@ public <X> T[] wrap(X value, WrapperOptions options) {
314313
}
315314
return wrapped;
316315
}
317-
else if ( value instanceof byte[] ) {
318-
return fromBytes( (byte[]) value );
316+
else if ( value instanceof byte[] bytes ) {
317+
return fromBytes( bytes );
319318
}
320-
else if ( value instanceof BinaryStream ) {
319+
else if ( value instanceof BinaryStream binaryStream ) {
321320
// When the value is a BinaryStream, this is a deserialization request
322-
return fromBytes( ( (BinaryStream) value ).getBytes() );
321+
return fromBytes( binaryStream.getBytes() );
323322
}
324323
else if ( getElementJavaType().isInstance( value ) ) {
325324
// Support binding a single element as parameter value
@@ -329,8 +328,7 @@ else if ( getElementJavaType().isInstance( value ) ) {
329328
wrapped[0] = (T) value;
330329
return wrapped;
331330
}
332-
else if ( value instanceof Collection<?> ) {
333-
final Collection<?> collection = (Collection<?>) value;
331+
else if ( value instanceof Collection<?> collection ) {
334332
//noinspection unchecked
335333
final T[] wrapped = (T[]) java.lang.reflect.Array.newInstance( getElementJavaType().getJavaTypeClass(), collection.size() );
336334
int i = 0;
@@ -359,9 +357,8 @@ private static <T> byte[] toBytes(T[] value) {
359357
}
360358
}
361359

362-
private T[] fromBytes(byte[] value) {
363-
Class<T> elementClass = getElementJavaType().getJavaTypeClass();
364-
byte[] bytes = value;
360+
private T[] fromBytes(byte[] bytes) {
361+
final Class<T> elementClass = getElementJavaType().getJavaTypeClass();
365362
if ( elementClass.isEnum() ) {
366363
final Object[] array = (Object[]) Array.newInstance( elementClass, bytes.length );
367364
for (int i = 0; i < bytes.length; i++ ) {
@@ -375,7 +372,7 @@ private T[] fromBytes(byte[] value) {
375372
else {
376373
// When the value is a byte[], this is a deserialization request
377374
//noinspection unchecked
378-
return (T[]) SerializationHelper.deserialize(value);
375+
return (T[]) SerializationHelper.deserialize(bytes);
379376
}
380377
}
381378

@@ -400,7 +397,7 @@ public T[] deepCopy(T[] value) {
400397
return null;
401398
}
402399
//noinspection unchecked
403-
T[] copy = (T[]) Array.newInstance( componentClass, value.length );
400+
final T[] copy = (T[]) Array.newInstance( componentClass, value.length );
404401
for ( int i = 0; i < value.length; i ++ ) {
405402
copy[ i ] = componentPlan.deepCopy( value[ i ] );
406403
}

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/BasicJavaType.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ default JdbcType getRecommendedJdbcType(JdbcTypeIndicators indicators) {
2727
// match legacy behavior
2828
int jdbcTypeCode = JdbcTypeJavaClassMappings.INSTANCE.determineJdbcTypeCodeForJavaClass( getJavaTypeClass() );
2929
final JdbcType descriptor = indicators.getJdbcType( indicators.resolveJdbcTypeCode( jdbcTypeCode ) );
30-
return descriptor instanceof AdjustableJdbcType
31-
? ( (AdjustableJdbcType) descriptor ).resolveIndicatedType( indicators, this )
30+
return descriptor instanceof AdjustableJdbcType adjustableJdbcType
31+
? adjustableJdbcType.resolveIndicatedType( indicators, this )
3232
: descriptor;
3333
}
3434

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/BlobJavaType.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,17 +163,13 @@ public <X> Blob wrap(X value, WrapperOptions options) {
163163
if ( value == null ) {
164164
return null;
165165
}
166-
167-
// Support multiple return types from
168-
// org.hibernate.type.descriptor.sql.BlobTypeDescriptor
169-
if ( Blob.class.isAssignableFrom( value.getClass() ) ) {
170-
return options.getLobCreator().wrap( (Blob) value );
166+
else if ( value instanceof Blob blob ) {
167+
return options.getLobCreator().wrap( blob );
171168
}
172-
else if ( byte[].class.isAssignableFrom( value.getClass() ) ) {
173-
return options.getLobCreator().createBlob( ( byte[] ) value);
169+
else if ( value instanceof byte[] bytes ) {
170+
return options.getLobCreator().createBlob( bytes );
174171
}
175-
else if ( InputStream.class.isAssignableFrom( value.getClass() ) ) {
176-
InputStream inputStream = ( InputStream ) value;
172+
else if ( value instanceof InputStream inputStream ) {
177173
try {
178174
return options.getLobCreator().createBlob( inputStream, inputStream.available() );
179175
}

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/BooleanJavaType.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ public <X> Boolean wrap(X value, WrapperOptions options) {
112112
return booleanValue;
113113
}
114114
if (value instanceof Number number) {
115-
final int intValue = number.intValue();
116-
return intValue != 0;
115+
return number.intValue() != 0;
117116
}
118117
if (value instanceof Character character) {
119118
return isTrue( character );
@@ -192,7 +191,7 @@ public String getCheckCondition(String columnName, JdbcType jdbcType, BasicValue
192191
if ( converter != null ) {
193192
if ( jdbcType.isString() ) {
194193
@SuppressWarnings("unchecked")
195-
BasicValueConverter<Boolean, ?> stringConverter =
194+
final BasicValueConverter<Boolean, ?> stringConverter =
196195
(BasicValueConverter<Boolean, ?>) converter;
197196
final Object falseValue = stringConverter.toRelationalValue( false );
198197
final Object trueValue = stringConverter.toRelationalValue( true );
@@ -201,11 +200,11 @@ public String getCheckCondition(String columnName, JdbcType jdbcType, BasicValue
201200
}
202201
else if ( jdbcType.isInteger() ) {
203202
@SuppressWarnings("unchecked")
204-
BasicValueConverter<Boolean, ? extends Number> numericConverter =
203+
final BasicValueConverter<Boolean, ? extends Number> numericConverter =
205204
(BasicValueConverter<Boolean, ? extends Number>) converter;
206205
final Number falseValue = numericConverter.toRelationalValue( false );
207206
final Number trueValue = numericConverter.toRelationalValue( true );
208-
Long[] values = getPossibleNumericValues( numericConverter, falseValue, trueValue );
207+
final Long[] values = getPossibleNumericValues( numericConverter, falseValue, trueValue );
209208
return dialect.getCheckCondition( columnName, values );
210209
}
211210
}
@@ -222,7 +221,7 @@ private static Long[] getPossibleNumericValues(
222221
}
223222
catch ( NullPointerException ignored ) {
224223
}
225-
Long[] values = new Long[nullValue != null ? 3 : 2];
224+
final Long[] values = new Long[nullValue != null ? 3 : 2];
226225
values[0] = falseValue != null ? falseValue.longValue() : null;
227226
values[1] = trueValue != null ? trueValue.longValue() : null;
228227
if ( nullValue != null ) {

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/BooleanPrimitiveArrayJavaType.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,16 @@ public <X> boolean[] wrap(X value, WrapperOptions options) {
149149
}
150150
}
151151

152-
if ( value instanceof boolean[] ) {
153-
return (boolean[]) value;
152+
if ( value instanceof boolean[] booleans ) {
153+
return booleans;
154154
}
155-
else if ( value instanceof byte[] ) {
155+
else if ( value instanceof byte[] bytes ) {
156156
// When the value is a byte[], this is a deserialization request
157-
return (boolean[]) SerializationHelper.deserialize( (byte[]) value );
157+
return (boolean[]) SerializationHelper.deserialize( bytes );
158158
}
159-
else if ( value instanceof BinaryStream ) {
159+
else if ( value instanceof BinaryStream binaryStream ) {
160160
// When the value is a BinaryStream, this is a deserialization request
161-
return (boolean[]) SerializationHelper.deserialize( ( (BinaryStream) value ).getBytes() );
161+
return (boolean[]) SerializationHelper.deserialize( binaryStream.getBytes() );
162162
}
163163
else if ( value.getClass().isArray() ) {
164164
final boolean[] wrapped = new boolean[Array.getLength( value )];
@@ -167,12 +167,11 @@ else if ( value.getClass().isArray() ) {
167167
}
168168
return wrapped;
169169
}
170-
else if ( value instanceof Boolean ) {
170+
else if ( value instanceof Boolean booleanValue ) {
171171
// Support binding a single element as parameter value
172-
return new boolean[]{ (boolean) value };
172+
return new boolean[]{ booleanValue };
173173
}
174-
else if ( value instanceof Collection<?> ) {
175-
final Collection<?> collection = (Collection<?>) value;
174+
else if ( value instanceof Collection<?> collection ) {
176175
final boolean[] wrapped = new boolean[collection.size()];
177176
int i = 0;
178177
for ( Object e : collection ) {

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ByteArrayJavaType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public ByteArrayJavaType() {
3535
@Override
3636
public boolean areEqual(Byte[] one, Byte[] another) {
3737
return one == another
38-
|| ( one != null && another != null && Arrays.equals(one, another) );
38+
|| one != null && another != null && Arrays.equals(one, another);
3939
}
4040
@Override
4141
public int extractHashCode(Byte[] bytes) {

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ByteJavaType.java

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,14 @@ public <X> Byte wrap(X value, WrapperOptions options) {
7979
if ( value == null ) {
8080
return null;
8181
}
82-
if ( value instanceof Byte ) {
83-
return (Byte) value;
82+
if ( value instanceof Byte byteValue ) {
83+
return byteValue;
8484
}
85-
if ( value instanceof Number ) {
86-
return ( (Number) value ).byteValue();
85+
if ( value instanceof Number number ) {
86+
return number.byteValue();
8787
}
88-
if ( value instanceof String ) {
89-
return Byte.valueOf( ( (String) value ) );
88+
if ( value instanceof String string ) {
89+
return Byte.valueOf( string );
9090
}
9191
throw unknownWrap( value.getClass() );
9292
}
@@ -132,41 +132,41 @@ public <X> Byte coerce(X value, CoercionContext coercionContext) {
132132
return null;
133133
}
134134

135-
if ( value instanceof Byte ) {
136-
return (Byte) value;
135+
if ( value instanceof Byte byteValue ) {
136+
return byteValue;
137137
}
138138

139-
if ( value instanceof Short ) {
140-
return CoercionHelper.toByte( (Short) value );
139+
if ( value instanceof Short shotValue ) {
140+
return CoercionHelper.toByte( shotValue );
141141
}
142142

143-
if ( value instanceof Integer ) {
144-
return CoercionHelper.toByte( (Integer) value );
143+
if ( value instanceof Integer integerValue ) {
144+
return CoercionHelper.toByte( integerValue );
145145
}
146146

147-
if ( value instanceof Long ) {
148-
return CoercionHelper.toByte( (Long) value );
147+
if ( value instanceof Long longValue ) {
148+
return CoercionHelper.toByte( longValue );
149149
}
150150

151-
if ( value instanceof Double ) {
152-
return CoercionHelper.toByte( (Double) value );
151+
if ( value instanceof Double doubleValue ) {
152+
return CoercionHelper.toByte( doubleValue );
153153
}
154154

155-
if ( value instanceof Float ) {
156-
return CoercionHelper.toByte( (Float) value );
155+
if ( value instanceof Float floatValue ) {
156+
return CoercionHelper.toByte( floatValue );
157157
}
158158

159-
if ( value instanceof BigInteger ) {
160-
return CoercionHelper.toByte( (BigInteger) value );
159+
if ( value instanceof BigInteger bigInteger ) {
160+
return CoercionHelper.toByte( bigInteger );
161161
}
162162

163-
if ( value instanceof BigDecimal ) {
164-
return CoercionHelper.toByte( (BigDecimal) value );
163+
if ( value instanceof BigDecimal bigDecimal ) {
164+
return CoercionHelper.toByte( bigDecimal );
165165
}
166166

167-
if ( value instanceof String ) {
167+
if ( value instanceof String string ) {
168168
return CoercionHelper.coerceWrappingError(
169-
() -> Byte.parseByte( (String) value )
169+
() -> Byte.parseByte( string )
170170
);
171171
}
172172

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CalendarDateJavaType.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,17 @@ public <X> Calendar wrap(X value, WrapperOptions options) {
115115
if ( value == null ) {
116116
return null;
117117
}
118-
if (value instanceof Calendar) {
119-
return (Calendar) value;
118+
else if (value instanceof Calendar calendar) {
119+
return calendar;
120120
}
121-
122-
if ( !(value instanceof Date)) {
121+
else if ( value instanceof Date date ) {
122+
final Calendar cal = new GregorianCalendar();
123+
cal.setTime( date );
124+
return cal;
125+
}
126+
else {
123127
throw unknownWrap( value.getClass() );
124128
}
125-
126-
Calendar cal = new GregorianCalendar();
127-
cal.setTime( (Date) value );
128-
return cal;
129129
}
130130

131131
@Override

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CalendarJavaType.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,17 +132,18 @@ public <X> Calendar wrap(X value, WrapperOptions options) {
132132
if ( value == null ) {
133133
return null;
134134
}
135-
if (value instanceof Calendar) {
136-
return (Calendar) value;
135+
else if (value instanceof Calendar calendar) {
136+
return calendar;
137137
}
138-
139-
if ( !(value instanceof java.util.Date)) {
138+
else if ( value instanceof java.util.Date date ) {
139+
final Calendar cal = new GregorianCalendar();
140+
cal.setTime( date );
141+
return cal;
142+
}
143+
else {
140144
throw unknownWrap( value.getClass() );
141145
}
142146

143-
Calendar cal = new GregorianCalendar();
144-
cal.setTime( (java.util.Date) value );
145-
return cal;
146147
}
147148

148149
@Override

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CalendarTimeJavaType.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,17 +117,17 @@ public <X> Calendar wrap(X value, WrapperOptions options) {
117117
if ( value == null ) {
118118
return null;
119119
}
120-
if (value instanceof Calendar) {
121-
return (Calendar) value;
120+
else if (value instanceof Calendar calendar) {
121+
return calendar;
122122
}
123-
124-
if ( !(value instanceof Date)) {
123+
else if ( value instanceof Date date) {
124+
final Calendar cal = new GregorianCalendar();
125+
cal.setTime( date );
126+
return cal;
127+
}
128+
else {
125129
throw unknownWrap( value.getClass() );
126130
}
127-
128-
Calendar cal = new GregorianCalendar();
129-
cal.setTime( (Date) value );
130-
return cal;
131131
}
132132

133133
@Override

0 commit comments

Comments
 (0)