Skip to content

Commit 311c840

Browse files
committed
use Class.cast() to remove all the horrible unchecked type casts in the JavaTypes
1 parent ed05cdf commit 311c840

File tree

73 files changed

+679
-511
lines changed

Some content is hidden

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

73 files changed

+679
-511
lines changed

hibernate-core/src/main/java/org/hibernate/type/AbstractStandardBasicType.java

Lines changed: 41 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,18 @@ public abstract class AbstractStandardBasicType<T>
5555

5656
public AbstractStandardBasicType(JdbcType jdbcType, JavaType<T> javaType) {
5757
this.jdbcType = jdbcType;
58-
this.sqlTypes = new int[] { jdbcType.getDdlTypeCode() };
5958
this.javaType = javaType;
59+
sqlTypes = new int[] { jdbcType.getDdlTypeCode() };
6060

61-
this.jdbcValueBinder = jdbcType.getBinder( javaType );
62-
this.jdbcValueExtractor = jdbcType.getExtractor( javaType );
63-
this.jdbcLiteralFormatter = jdbcType.getJdbcLiteralFormatter( javaType );
61+
jdbcValueBinder = jdbcType.getBinder( javaType );
62+
jdbcValueExtractor = jdbcType.getExtractor( javaType );
63+
jdbcLiteralFormatter = jdbcType.getJdbcLiteralFormatter( javaType );
6464

6565
//A very simple dispatch optimisation, make these a constant:
66-
this.javaTypeClass = javaType.getJavaTypeClass();
67-
this.mutabilityPlan = javaType.getMutabilityPlan();
68-
this.javatypeComparator = javaType.getComparator();
69-
this.typeForEqualsHashCode = javaType.useObjectEqualsHashCode() ? null : this;
66+
javaTypeClass = javaType.getJavaTypeClass();
67+
mutabilityPlan = javaType.getMutabilityPlan();
68+
javatypeComparator = javaType.getComparator();
69+
typeForEqualsHashCode = javaType.useObjectEqualsHashCode() ? null : this;
7070
}
7171

7272
@Override
@@ -174,7 +174,6 @@ public final boolean isEqual(Object x, Object y, SessionFactoryImplementor facto
174174
}
175175

176176
@Override
177-
@SuppressWarnings("unchecked")
178177
public boolean isEqual(Object one, Object another) {
179178
if ( one == another ) {
180179
return true;
@@ -186,19 +185,15 @@ else if ( typeForEqualsHashCode == null ) {
186185
return one.equals( another );
187186
}
188187
else {
189-
return javaType.areEqual( (T) one, (T) another );
188+
return javaType.areEqual( javaType.cast( one ), javaType.cast( another ) );
190189
}
191190
}
192191

193192
@Override
194-
@SuppressWarnings("unchecked")
195-
public int getHashCode(Object x) {
196-
if ( typeForEqualsHashCode == null ) {
197-
return x.hashCode();
198-
}
199-
else {
200-
return javaType.extractHashCode( (T) x );
201-
}
193+
public int getHashCode(Object object) {
194+
return typeForEqualsHashCode == null
195+
? object.hashCode()
196+
: javaType.extractHashCode( javaType.cast( object ) );
202197
}
203198

204199
@Override
@@ -212,9 +207,8 @@ public final int getHashCode(Object x, SessionFactoryImplementor factory) {
212207
}
213208

214209
@Override
215-
@SuppressWarnings("unchecked")
216210
public final int compare(Object x, Object y) {
217-
return this.javatypeComparator.compare( (T) x, (T) y );
211+
return this.javatypeComparator.compare( javaType.cast( x ) , javaType.cast( y ) );
218212
}
219213

220214
@Override
@@ -228,9 +222,11 @@ public final boolean isDirty(Object old, Object current, boolean[] checkable, Sh
228222
}
229223

230224
protected final boolean isDirty(Object old, Object current) {
231-
// MutableMutabilityPlan.INSTANCE is a special plan for which we always have to assume the value is dirty,
232-
// because we can't actually copy a value, but have no knowledge about the mutability of the java type
233-
return getMutabilityPlan() == MutableMutabilityPlan.INSTANCE || !isSame( old, current );
225+
// MutableMutabilityPlan.INSTANCE is a special plan for which we always
226+
// have to assume the value is dirty, because we can't actually copy a
227+
// value, but have no knowledge about the mutability of the java type
228+
return getMutabilityPlan() == MutableMutabilityPlan.INSTANCE
229+
|| !isSame( old, current );
234230
}
235231

236232
@Override
@@ -247,22 +243,23 @@ public final void nullSafeSet(
247243
PreparedStatement st,
248244
Object value,
249245
int index,
250-
final SharedSessionContractImplementor session) throws SQLException {
251-
//noinspection unchecked
252-
nullSafeSet( st, (T) value, index, (WrapperOptions) session );
246+
final SharedSessionContractImplementor session)
247+
throws SQLException {
248+
nullSafeSet( st, javaType.cast( value ) , index, (WrapperOptions) session );
253249
}
254250

255-
protected void nullSafeSet(PreparedStatement st, T value, int index, WrapperOptions options) throws SQLException {
251+
protected void nullSafeSet(PreparedStatement st, T value, int index, WrapperOptions options)
252+
throws SQLException {
256253
getJdbcValueBinder().bind( st, value, index, options );
257254
}
258255

259256
@Override
260-
@SuppressWarnings("unchecked")
261257
public final String toLoggableString(Object value, SessionFactoryImplementor factory) {
262-
if ( value == LazyPropertyInitializer.UNFETCHED_PROPERTY || !Hibernate.isInitialized( value ) ) {
263-
return "<uninitialized>";
264-
}
265-
return javaType.extractLoggableRepresentation( (T) value );
258+
return value == LazyPropertyInitializer.UNFETCHED_PROPERTY
259+
|| !Hibernate.isInitialized( value )
260+
? "<uninitialized>"
261+
: javaType.extractLoggableRepresentation(
262+
javaType.coerce( value, factory::getTypeConfiguration ) );
266263
}
267264

268265
@Override
@@ -271,19 +268,17 @@ public final boolean isMutable() {
271268
}
272269

273270
@Override
274-
@SuppressWarnings("unchecked")
275271
public final Object deepCopy(Object value, SessionFactoryImplementor factory) {
276-
return deepCopy( (T) value );
272+
return deepCopy( javaType.cast( value ) );
277273
}
278274

279275
protected final T deepCopy(T value) {
280276
return getMutabilityPlan().deepCopy( value );
281277
}
282278

283279
@Override
284-
@SuppressWarnings("unchecked")
285280
public final Serializable disassemble(Object value, SharedSessionContractImplementor session, Object owner) throws HibernateException {
286-
return getMutabilityPlan().disassemble( (T) value, session );
281+
return getMutabilityPlan().disassemble( javaType.cast( value ) , session );
287282
}
288283

289284
@Override
@@ -296,16 +291,14 @@ public final void beforeAssemble(Serializable cached, SharedSessionContractImple
296291
}
297292

298293
@Override
299-
@SuppressWarnings("unchecked")
300294
public final Object replace(Object original, Object target, SharedSessionContractImplementor session, Object owner, Map<Object, Object> copyCache) {
301295
return original == null && target == null
302296
? null
303-
: javaType.getReplacement( (T) original, (T) target, session );
297+
: javaType.getReplacement( javaType.cast( original ) , javaType.cast( target ) , session );
304298

305299
}
306300

307301
@Override
308-
@SuppressWarnings("unchecked")
309302
public Object replace(
310303
Object original,
311304
Object target,
@@ -314,6 +307,9 @@ public Object replace(
314307
Map<Object, Object> copyCache,
315308
ForeignKeyDirection foreignKeyDirection) {
316309
return ForeignKeyDirection.FROM_PARENT == foreignKeyDirection
310+
// TODO: use cast() .. currently failing on embeddable discriminators where
311+
// the concrete class is passed in instead of its disciminator value
312+
// ? javaType.getReplacement( javaType.cast( original ) , javaType.cast( target ) , session )
317313
? javaType.getReplacement( (T) original, (T) target, session )
318314
: target;
319315
}
@@ -325,20 +321,12 @@ public boolean canDoExtraction() {
325321

326322
@Override
327323
public T extract(CallableStatement statement, int startIndex, final SharedSessionContractImplementor session) throws SQLException {
328-
return getJdbcValueExtractor().extract(
329-
statement,
330-
startIndex,
331-
session
332-
);
324+
return getJdbcValueExtractor().extract( statement, startIndex, session );
333325
}
334326

335327
@Override
336328
public T extract(CallableStatement statement, String paramName, final SharedSessionContractImplementor session) throws SQLException {
337-
return getJdbcValueExtractor().extract(
338-
statement,
339-
paramName,
340-
session
341-
);
329+
return getJdbcValueExtractor().extract( statement, paramName, session );
342330
}
343331

344332
@Override
@@ -347,7 +335,8 @@ public void nullSafeSet(
347335
Object value,
348336
int index,
349337
boolean[] settable,
350-
SharedSessionContractImplementor session) throws SQLException {
338+
SharedSessionContractImplementor session)
339+
throws SQLException {
351340

352341
}
353342

@@ -356,9 +345,8 @@ public void nullSafeSet(CallableStatement st, T value, String name, SharedSessio
356345
nullSafeSet( st, value, name, (WrapperOptions) session );
357346
}
358347

359-
@SuppressWarnings("unchecked")
360348
protected final void nullSafeSet(CallableStatement st, Object value, String name, WrapperOptions options) throws SQLException {
361-
getJdbcValueBinder().bind( st, (T) value, name, options );
349+
getJdbcValueBinder().bind( st, javaType.cast( value ) , name, options );
362350
}
363351

364352
@Override
@@ -379,7 +367,7 @@ public CastType getCastType() {
379367
// Due to that, we have to handle some conversions in wrap/unwrap of BooleanJavaType
380368
// and the cast type determination here. Note that we interpret the converter in ConvertedBasicTypeImpl
381369
// to properly determine the correct cast type
382-
final JdbcType jdbcType = getJdbcType();
370+
final var jdbcType = getJdbcType();
383371
final int jdbcTypeCode = jdbcType.getDdlTypeCode();
384372
switch ( jdbcTypeCode ) {
385373
case Types.BIT:

hibernate-core/src/main/java/org/hibernate/type/EntityType.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ private Object getUniqueKey(Object value, SharedSessionContractImplementor sessi
461461
if ( lazyInitializer != null ) {
462462
// If the value is a Proxy and the property access is field, the value returned by
463463
// attributeMapping.getAttributeMetadata().getPropertyAccess().getGetter().get( object )
464-
// is always null except for the id, we need the to use the proxy implementation to
464+
// is always null except for the id, and we need to use the proxy implementation to
465465
// extract the property value.
466466
value = lazyInitializer.getImplementation();
467467
}
@@ -530,7 +530,7 @@ private String loggableString(Object entity, EntityPersister persister) {
530530
return associatedEntityName + "#" + entity;
531531
}
532532
else {
533-
final StringBuilder result = new StringBuilder().append( associatedEntityName );
533+
final var result = new StringBuilder().append( associatedEntityName );
534534
if ( persister.hasIdentifierProperty() ) {
535535
result.append( '#' ).append( identifierString( entity, persister ) );
536536
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public Class<T> getJavaType() {
8181
}
8282

8383
@Override
84-
public Class<T> getJavaTypeClass() {
84+
public final Class<T> getJavaTypeClass() {
8585
return getJavaType();
8686
}
8787

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -256,24 +256,21 @@ public <X> X unwrap(T[] value, Class<X> type, WrapperOptions options) {
256256
}
257257

258258
if ( type.isInstance( value ) ) {
259-
//noinspection unchecked
260-
return (X) value;
259+
return type.cast( value );
261260
}
262261
else if ( type == byte[].class ) {
263-
return (X) toBytes( value );
262+
return type.cast( toBytes( value ) );
264263
}
265264
else if ( type == BinaryStream.class ) {
266-
//noinspection unchecked
267-
return (X) new ArrayBackedBinaryStream( toBytes( value ) );
265+
return type.cast( new ArrayBackedBinaryStream( toBytes( value ) ) );
268266
}
269267
else if ( type.isArray() ) {
270268
final var preferredJavaTypeClass = type.getComponentType();
271-
final Object[] unwrapped = (Object[]) newInstance( preferredJavaTypeClass, value.length );
269+
final var unwrapped = (Object[]) newInstance( preferredJavaTypeClass, value.length );
272270
for ( int i = 0; i < value.length; i++ ) {
273271
unwrapped[i] = getElementJavaType().unwrap( value[i], preferredJavaTypeClass, options );
274272
}
275-
//noinspection unchecked
276-
return (X) unwrapped;
273+
return type.cast( unwrapped );
277274
}
278275

279276
throw unknownUnwrap( type );

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ public boolean isInstance(Object value) {
3737
return value instanceof BigDecimal;
3838
}
3939

40+
@Override
41+
public BigDecimal cast(Object value) {
42+
return (BigDecimal) value;
43+
}
44+
4045
@Override
4146
public boolean areEqual(BigDecimal one, BigDecimal another) {
4247
return one == another
@@ -48,37 +53,36 @@ public int extractHashCode(BigDecimal value) {
4853
return value.intValue();
4954
}
5055

51-
@SuppressWarnings("unchecked")
5256
public <X> X unwrap(BigDecimal value, Class<X> type, WrapperOptions options) {
5357
if ( value == null ) {
5458
return null;
5559
}
5660
if ( BigDecimal.class.isAssignableFrom( type ) ) {
57-
return (X) value;
61+
return type.cast( value );
5862
}
5963
if ( BigInteger.class.isAssignableFrom( type ) ) {
60-
return (X) value.toBigIntegerExact();
64+
return type.cast( value.toBigIntegerExact() );
6165
}
6266
if ( Byte.class.isAssignableFrom( type ) ) {
63-
return (X) Byte.valueOf( value.byteValue() );
67+
return type.cast( value.byteValue() );
6468
}
6569
if ( Short.class.isAssignableFrom( type ) ) {
66-
return (X) Short.valueOf( value.shortValue() );
70+
return type.cast( value.shortValue() );
6771
}
6872
if ( Integer.class.isAssignableFrom( type ) ) {
69-
return (X) Integer.valueOf( value.intValue() );
73+
return type.cast( value.intValue() );
7074
}
7175
if ( Long.class.isAssignableFrom( type ) ) {
72-
return (X) Long.valueOf( value.longValue() );
76+
return type.cast( value.longValue() );
7377
}
7478
if ( Double.class.isAssignableFrom( type ) ) {
75-
return (X) Double.valueOf( value.doubleValue() );
79+
return type.cast( value.doubleValue() );
7680
}
7781
if ( Float.class.isAssignableFrom( type ) ) {
78-
return (X) Float.valueOf( value.floatValue() );
82+
return type.cast( value.floatValue() );
7983
}
8084
if ( String.class.isAssignableFrom( type ) ) {
81-
return (X) value.toString();
85+
return type.cast( value.toString() );
8286
}
8387
throw unknownUnwrap( type );
8488
}

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,43 +39,47 @@ public boolean isInstance(Object value) {
3939
return value instanceof BigInteger;
4040
}
4141

42+
@Override
43+
public BigInteger cast(Object value) {
44+
return (BigInteger) value;
45+
}
46+
4247
@Override
4348
public int extractHashCode(BigInteger value) {
4449
return value.intValue();
4550
}
4651

4752
@Override
48-
@SuppressWarnings("unchecked")
4953
public <X> X unwrap(BigInteger value, Class<X> type, WrapperOptions options) {
5054
if ( value == null ) {
5155
return null;
5256
}
5357
if ( BigInteger.class.isAssignableFrom( type ) ) {
54-
return (X) value;
58+
return type.cast( value );
5559
}
5660
if ( BigDecimal.class.isAssignableFrom( type ) ) {
57-
return (X) new BigDecimal( value );
61+
return type.cast( new BigDecimal( value ) );
5862
}
5963
if ( Byte.class.isAssignableFrom( type ) ) {
60-
return (X) Byte.valueOf( value.byteValue() );
64+
return type.cast( Byte.valueOf( value.byteValue() ) );
6165
}
6266
if ( Short.class.isAssignableFrom( type ) ) {
63-
return (X) Short.valueOf( value.shortValue() );
67+
return type.cast( value.shortValue() );
6468
}
6569
if ( Integer.class.isAssignableFrom( type ) ) {
66-
return (X) Integer.valueOf( value.intValue() );
70+
return type.cast( value.intValue() );
6771
}
6872
if ( Long.class.isAssignableFrom( type ) ) {
69-
return (X) Long.valueOf( value.longValue() );
73+
return type.cast( value.longValue() );
7074
}
7175
if ( Double.class.isAssignableFrom( type ) ) {
72-
return (X) Double.valueOf( value.doubleValue() );
76+
return type.cast( value.doubleValue() );
7377
}
7478
if ( Float.class.isAssignableFrom( type ) ) {
75-
return (X) Float.valueOf( value.floatValue() );
79+
return type.cast( value.floatValue() );
7680
}
7781
if ( String.class.isAssignableFrom( type ) ) {
78-
return (X) value.toString();
82+
return type.cast( value.toString() );
7983
}
8084
throw unknownUnwrap( type );
8185
}

0 commit comments

Comments
 (0)