Skip to content

Commit 47270d4

Browse files
committed
tidy up TypecheckUtil
1 parent 58a6afe commit 47270d4

File tree

1 file changed

+67
-70
lines changed

1 file changed

+67
-70
lines changed

hibernate-core/src/main/java/org/hibernate/query/sqm/internal/TypecheckUtil.java

Lines changed: 67 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.hibernate.type.BasicPluralType;
3030
import org.hibernate.type.BasicType;
3131
import org.hibernate.type.QueryParameterJavaObjectType;
32+
import org.hibernate.type.descriptor.converter.spi.BasicValueConverter;
3233
import org.hibernate.type.descriptor.jdbc.JdbcType;
3334

3435
import java.time.temporal.Temporal;
@@ -137,8 +138,8 @@ public static boolean areTypesComparable(
137138

138139
// for tuple constructors, we must check each element
139140

140-
if ( lhsDomainType instanceof TupleType && rhsDomainType instanceof TupleType ) {
141-
return areTupleTypesComparable(bindingContext, (TupleType<?>) lhsDomainType, (TupleType<?>) rhsDomainType );
141+
if ( lhsDomainType instanceof TupleType<?> lhsTuple && rhsDomainType instanceof TupleType<?> rhsTuple ) {
142+
return areTupleTypesComparable( bindingContext, lhsTuple, rhsTuple );
142143
}
143144

144145
// allow comparing an embeddable against a tuple literal
@@ -151,18 +152,18 @@ public static boolean areTypesComparable(
151152

152153
// entities can be compared if they belong to the same inheritance hierarchy
153154

154-
if ( lhsDomainType instanceof EntityType && rhsDomainType instanceof EntityType ) {
155-
return areEntityTypesComparable( (EntityType<?>) lhsDomainType, (EntityType<?>) rhsDomainType, bindingContext);
155+
if ( lhsDomainType instanceof EntityType<?> lhsEntity && rhsDomainType instanceof EntityType<?> rhsEntity ) {
156+
return areEntityTypesComparable( lhsEntity, rhsEntity, bindingContext);
156157
}
157158

158159
// entities can be compared to discriminators if they belong to
159160
// the same inheritance hierarchy
160161

161-
if ( lhsDomainType instanceof EntityDiscriminatorSqmPathSource ) {
162-
return isDiscriminatorTypeComparable( (EntityDiscriminatorSqmPathSource<?>) lhsDomainType, rhsDomainType, bindingContext);
162+
if ( lhsDomainType instanceof EntityDiscriminatorSqmPathSource<?> discriminatorSource ) {
163+
return isDiscriminatorTypeComparable( discriminatorSource, rhsDomainType, bindingContext);
163164
}
164-
if ( rhsDomainType instanceof EntityDiscriminatorSqmPathSource ) {
165-
return isDiscriminatorTypeComparable( (EntityDiscriminatorSqmPathSource<?>) rhsDomainType, lhsDomainType, bindingContext);
165+
if ( rhsDomainType instanceof EntityDiscriminatorSqmPathSource<?> discriminatorSource ) {
166+
return isDiscriminatorTypeComparable( discriminatorSource, lhsDomainType, bindingContext);
166167
}
167168

168169
// Treat the expressions as comparable if they belong to the same
@@ -171,10 +172,9 @@ public static boolean areTypesComparable(
171172
// decent approach which allows comparison between literals and
172173
// enums, user-defined types, etc.
173174

174-
if ( lhsDomainType instanceof JdbcMapping && rhsDomainType instanceof JdbcMapping ) {
175-
if ( areJdbcMappingsComparable( (JdbcMapping) lhsDomainType, (JdbcMapping) rhsDomainType, bindingContext) ) {
176-
return true;
177-
}
175+
if ( lhsDomainType instanceof JdbcMapping lhsMapping && rhsDomainType instanceof JdbcMapping rhsMapping
176+
&& areJdbcMappingsComparable( lhsMapping, rhsMapping, bindingContext ) ) {
177+
return true;
178178
}
179179

180180
// Workaround: these are needed for a handful of slightly "weird" cases
@@ -183,7 +183,7 @@ public static boolean areTypesComparable(
183183
// sort of hole warned about above, and accepts many things which are
184184
// not well-typed.
185185

186-
// TODO: sort all this out, and remove this branch
186+
// // TODO: sort all this out, and remove this branch
187187
if ( isSameJavaType( lhsType, rhsType ) ) {
188188
return true;
189189
}
@@ -202,28 +202,27 @@ private static boolean areJdbcMappingsComparable(
202202
else if ( lhsJdbcMapping.getValueConverter() != null || rhsJdbcMapping.getValueConverter() != null ) {
203203
final JdbcMapping lhsDomainMapping = getDomainJdbcType( lhsJdbcMapping, bindingContext);
204204
final JdbcMapping rhsDomainMapping = getDomainJdbcType( rhsJdbcMapping, bindingContext);
205-
return lhsDomainMapping != null && rhsDomainMapping != null && areJdbcTypesComparable(
206-
lhsDomainMapping.getJdbcType(),
207-
rhsDomainMapping.getJdbcType()
208-
);
205+
return lhsDomainMapping != null && rhsDomainMapping != null
206+
&& areJdbcTypesComparable( lhsDomainMapping.getJdbcType(), rhsDomainMapping.getJdbcType() );
209207
}
210208
return false;
211209
}
212210

213211
private static boolean areJdbcTypesComparable(JdbcType lhsJdbcType, JdbcType rhsJdbcType) {
214212
return lhsJdbcType.getJdbcTypeCode() == rhsJdbcType.getJdbcTypeCode()
215-
// "families" of implicitly-convertible JDBC types
216-
// (this list might need to be extended in future)
217-
|| lhsJdbcType.isStringLike() && rhsJdbcType.isStringLike()
218-
|| lhsJdbcType.isTemporal() && rhsJdbcType.isTemporal()
219-
|| lhsJdbcType.isNumber() && rhsJdbcType.isNumber();
213+
// "families" of implicitly-convertible JDBC types
214+
// (this list might need to be extended in future)
215+
|| lhsJdbcType.isStringLike() && rhsJdbcType.isStringLike()
216+
|| lhsJdbcType.isTemporal() && rhsJdbcType.isTemporal()
217+
|| lhsJdbcType.isNumber() && rhsJdbcType.isNumber();
220218
}
221219

222220
private static JdbcMapping getDomainJdbcType(JdbcMapping jdbcMapping, BindingContext bindingContext) {
223-
if ( jdbcMapping.getValueConverter() != null ) {
221+
final BasicValueConverter<?,?> valueConverter = jdbcMapping.getValueConverter();
222+
if ( valueConverter != null ) {
224223
final BasicType<?> basicType =
225224
bindingContext.getTypeConfiguration()
226-
.getBasicTypeForJavaType( jdbcMapping.getValueConverter().getDomainJavaType().getJavaType() );
225+
.getBasicTypeForJavaType( valueConverter.getDomainJavaType().getJavaType() );
227226
if ( basicType != null ) {
228227
return basicType.getJdbcMapping();
229228
}
@@ -232,17 +231,14 @@ private static JdbcMapping getDomainJdbcType(JdbcMapping jdbcMapping, BindingCon
232231
}
233232

234233
private static EmbeddableDomainType<?> getEmbeddableType(SqmExpressible<?> expressible) {
235-
return expressible instanceof EmbeddableDomainType<?> ? (EmbeddableDomainType<?>) expressible : null;
234+
return expressible instanceof EmbeddableDomainType<?> embeddableDomainType ? embeddableDomainType : null;
236235
}
237236

238237
private static boolean areEmbeddableTypesComparable(
239238
EmbeddableDomainType<?> lhsType,
240239
EmbeddableDomainType<?> rhsType) {
241-
if ( rhsType.getJavaType() == lhsType.getJavaType() ) {
242-
return true;
243-
}
244-
245-
return lhsType.isPolymorphic() && getRootEmbeddableType( lhsType ) == getRootEmbeddableType( rhsType );
240+
return rhsType.getJavaType() == lhsType.getJavaType()
241+
|| lhsType.isPolymorphic() && getRootEmbeddableType( lhsType ) == getRootEmbeddableType( rhsType );
246242
}
247243

248244
private static ManagedDomainType<?> getRootEmbeddableType(EmbeddableDomainType<?> embeddableType) {
@@ -262,7 +258,7 @@ private static boolean areTupleTypesComparable(
262258
}
263259
else {
264260
for ( int i = 0; i < lhsTuple.componentCount(); i++ ) {
265-
if ( !areTypesComparable( lhsTuple.get(i), rhsTuple.get(i), bindingContext) ) {
261+
if ( !areTypesComparable( lhsTuple.get(i), rhsTuple.get(i), bindingContext ) ) {
266262
return false;
267263
}
268264
}
@@ -273,29 +269,28 @@ private static boolean areTupleTypesComparable(
273269
private static boolean areEntityTypesComparable(
274270
EntityType<?> lhsType, EntityType<?> rhsType,
275271
BindingContext bindingContext) {
276-
EntityPersister lhsEntity = getEntityDescriptor(bindingContext, lhsType.getName() );
277-
EntityPersister rhsEntity = getEntityDescriptor(bindingContext, rhsType.getName() );
272+
final EntityPersister lhsEntity = getEntityDescriptor( bindingContext, lhsType.getName() );
273+
final EntityPersister rhsEntity = getEntityDescriptor( bindingContext, rhsType.getName() );
278274
return lhsEntity.getRootEntityName().equals( rhsEntity.getRootEntityName() );
279275
}
280276

281277
private static boolean isDiscriminatorTypeComparable(
282278
EntityDiscriminatorSqmPathSource<?> lhsDiscriminator, SqmExpressible<?> rhsType,
283279
BindingContext bindingContext) {
284-
String entityName = lhsDiscriminator.getEntityDomainType().getHibernateEntityName();
285-
EntityPersister lhsEntity = bindingContext.getMappingMetamodel().getEntityDescriptor( entityName );
286-
if ( rhsType instanceof EntityType ) {
287-
String rhsEntityName = ((EntityType<?>) rhsType).getName();
288-
EntityPersister rhsEntity = getEntityDescriptor(bindingContext, rhsEntityName );
280+
final String entityName = lhsDiscriminator.getEntityDomainType().getHibernateEntityName();
281+
final EntityPersister lhsEntity = bindingContext.getMappingMetamodel().getEntityDescriptor( entityName );
282+
if ( rhsType instanceof EntityType<?> entityType ) {
283+
final String rhsEntityName = entityType.getName();
284+
final EntityPersister rhsEntity = getEntityDescriptor( bindingContext, rhsEntityName );
289285
return lhsEntity.getRootEntityName().equals( rhsEntity.getRootEntityName() );
290286
}
291-
else if ( rhsType instanceof EntityDiscriminatorSqmPathSource ) {
292-
EntityDiscriminatorSqmPathSource<?> discriminator = (EntityDiscriminatorSqmPathSource<?>) rhsType;
293-
String rhsEntityName = discriminator.getEntityDomainType().getHibernateEntityName();
294-
EntityPersister rhsEntity = bindingContext.getMappingMetamodel().getEntityDescriptor( rhsEntityName );
287+
else if ( rhsType instanceof EntityDiscriminatorSqmPathSource<?> discriminator ) {
288+
final String rhsEntityName = discriminator.getEntityDomainType().getHibernateEntityName();
289+
final EntityPersister rhsEntity = bindingContext.getMappingMetamodel().getEntityDescriptor( rhsEntityName );
295290
return rhsEntity.getRootEntityName().equals( lhsEntity.getRootEntityName() );
296291
}
297292
else {
298-
BasicType<?> discriminatorType = (BasicType<?>)
293+
final BasicType<?> discriminatorType = (BasicType<?>)
299294
lhsDiscriminator.getEntityMapping().getDiscriminatorMapping().getMappedType();
300295
return areTypesComparable( discriminatorType, rhsType, bindingContext);
301296
}
@@ -317,8 +312,9 @@ private static boolean isTypeAssignable(
317312

318313
// entities can be assigned if they belong to the same inheritance hierarchy
319314

320-
if ( targetType instanceof EntityType && expressionType instanceof EntityType ) {
321-
return isEntityTypeAssignable( (EntityType<?>) targetType, (EntityType<?>) expressionType, bindingContext);
315+
if ( targetType instanceof EntityType<?> targetEntity
316+
&& expressionType instanceof EntityType<?> expressionEntity ) {
317+
return isEntityTypeAssignable( targetEntity, expressionEntity, bindingContext);
322318
}
323319

324320
// Treat the expression as assignable to the target path if they belong
@@ -327,11 +323,11 @@ private static boolean isTypeAssignable(
327323
// decent approach which allows comparison between literals and enums,
328324
// user-defined types, etc.
329325

330-
DomainType<?> lhsDomainType = targetType.getSqmType();
331-
DomainType<?> rhsDomainType = expressionType.getSqmType();
332-
if ( lhsDomainType instanceof JdbcMapping && rhsDomainType instanceof JdbcMapping ) {
333-
JdbcType lhsJdbcType = ((JdbcMapping) lhsDomainType).getJdbcType();
334-
JdbcType rhsJdbcType = ((JdbcMapping) rhsDomainType).getJdbcType();
326+
final DomainType<?> lhsDomainType = targetType.getSqmType();
327+
final DomainType<?> rhsDomainType = expressionType.getSqmType();
328+
if ( lhsDomainType instanceof JdbcMapping lhsMapping && rhsDomainType instanceof JdbcMapping rhsMapping ) {
329+
final JdbcType lhsJdbcType = lhsMapping.getJdbcType();
330+
final JdbcType rhsJdbcType = rhsMapping.getJdbcType();
335331
if ( lhsJdbcType.getJdbcTypeCode() == rhsJdbcType.getJdbcTypeCode()
336332
// "families" of implicitly-convertible JDBC types
337333
// (this list might need to be extended in future)
@@ -367,8 +363,8 @@ private static boolean isSameJavaType(SqmExpressible<?> leftType, SqmExpressible
367363
private static boolean isEntityTypeAssignable(
368364
EntityType<?> lhsType, EntityType<?> rhsType,
369365
BindingContext bindingContext) {
370-
EntityPersister lhsEntity = getEntityDescriptor(bindingContext, lhsType.getName() );
371-
EntityPersister rhsEntity = getEntityDescriptor(bindingContext, rhsType.getName() );
366+
final EntityPersister lhsEntity = getEntityDescriptor( bindingContext, lhsType.getName() );
367+
final EntityPersister rhsEntity = getEntityDescriptor( bindingContext, rhsType.getName() );
372368
return lhsEntity.isSubclassEntityName( rhsEntity.getEntityName() );
373369
}
374370

@@ -384,23 +380,23 @@ public static void assertComparable(Expression<?> x, Expression<?> y, BindingCon
384380
final SqmExpression<?> left = (SqmExpression<?>) x;
385381
final SqmExpression<?> right = (SqmExpression<?>) y;
386382
final Integer leftTupleLength = left.getTupleLength();
387-
final Integer rightTupleLength;
388-
if ( leftTupleLength != null && ( rightTupleLength = right.getTupleLength() ) != null
383+
final Integer rightTupleLength = right.getTupleLength();
384+
if ( leftTupleLength != null && rightTupleLength != null
389385
&& leftTupleLength.intValue() != rightTupleLength.intValue() ) {
390386
throw new SemanticException( "Cannot compare tuples of different lengths" );
391387
}
392388

393-
// SqmMemerOfPredicate is the only one allowing multi-valued paths, its comparability is now evaluated in areTypesComparable
389+
// SqmMemberOfPredicate is the only one allowing multivalued paths, its comparability is now evaluated in areTypesComparable
394390
// i.e. without calling this method, so we can check this here for other Predicates that do call this
395391
if ( left instanceof SqmPluralValuedSimplePath || right instanceof SqmPluralValuedSimplePath ) {
396-
throw new SemanticException( "Multi valued paths are only allowed for the member of operator" );
392+
throw new SemanticException( "Multivalued paths are only allowed for the 'member of' operator" );
397393
}
398394

399395
// allow comparing literal null to things
400396
if ( !( left instanceof SqmLiteralNull ) && !( right instanceof SqmLiteralNull ) ) {
401397
final SqmExpressible<?> leftType = left.getExpressible();
402398
final SqmExpressible<?> rightType = right.getExpressible();
403-
if ( !areTypesComparable( leftType, rightType, bindingContext) ) {
399+
if ( !areTypesComparable( leftType, rightType, bindingContext ) ) {
404400
throw new SemanticException(
405401
String.format(
406402
"Cannot compare left expression of type '%s' with right expression of type '%s'",
@@ -424,8 +420,8 @@ public static void assertAssignable(
424420
// TODO: check that the target path is nullable
425421
}
426422
else {
427-
SqmPathSource<?> targetType = targetPath.getNodeType();
428-
SqmExpressible<?> expressionType = expression.getNodeType();
423+
final SqmPathSource<?> targetType = targetPath.getNodeType();
424+
final SqmExpressible<?> expressionType = expression.getNodeType();
429425
if ( !isTypeAssignable( targetType, expressionType, bindingContext) ) {
430426
throw new SemanticException(
431427
String.format(
@@ -542,20 +538,21 @@ else if ( isNumberArray( leftNodeType ) ) {
542538
}
543539

544540
public static boolean isNumberArray(SqmExpressible<?> expressible) {
545-
final DomainType<?> domainType;
546-
if ( expressible != null && ( domainType = expressible.getSqmType() ) != null ) {
547-
return domainType instanceof BasicPluralType<?, ?> && Number.class.isAssignableFrom(
548-
( (BasicPluralType<?, ?>) domainType ).getElementType().getJavaType()
549-
);
541+
if ( expressible != null ) {
542+
final DomainType<?> domainType = expressible.getSqmType();
543+
if ( domainType != null ) {
544+
return domainType instanceof BasicPluralType<?, ?> basicPluralType
545+
&& Number.class.isAssignableFrom( basicPluralType.getElementType().getJavaType() );
546+
}
550547
}
551548
return false;
552549
}
553550

554551
public static void assertString(SqmExpression<?> expression) {
555552
final SqmExpressible<?> nodeType = expression.getNodeType();
556553
if ( nodeType != null ) {
557-
final DomainType<?> domainType = nodeType.getSqmType();
558-
if ( !( domainType instanceof JdbcMapping ) || !( (JdbcMapping) domainType ).getJdbcType().isStringLike() ) {
554+
if ( !( nodeType.getSqmType() instanceof JdbcMapping jdbcMapping )
555+
|| !jdbcMapping.getJdbcType().isStringLike() ) {
559556
throw new SemanticException(
560557
"Operand of 'like' is of type '" + nodeType.getTypeName() +
561558
"' which is not a string (its JDBC type code is not string-like)"
@@ -567,8 +564,8 @@ public static void assertString(SqmExpression<?> expression) {
567564
public static void assertDuration(SqmExpression<?> expression) {
568565
final SqmExpressible<?> nodeType = expression.getNodeType();
569566
if ( nodeType != null ) {
570-
final DomainType<?> domainType = nodeType.getSqmType();
571-
if ( !( domainType instanceof JdbcMapping ) || !( (JdbcMapping) domainType ).getJdbcType().isDuration() ) {
567+
if ( !( nodeType.getSqmType() instanceof JdbcMapping jdbcMapping )
568+
|| !jdbcMapping.getJdbcType().isDuration() ) {
572569
throw new SemanticException(
573570
"Operand of 'by' is of type '" + nodeType.getTypeName() +
574571
"' which is not a duration (its JDBC type code is not duration-like)"
@@ -580,8 +577,8 @@ public static void assertDuration(SqmExpression<?> expression) {
580577
public static void assertNumeric(SqmExpression<?> expression, UnaryArithmeticOperator op) {
581578
final SqmExpressible<?> nodeType = expression.getNodeType();
582579
if ( nodeType != null ) {
583-
final DomainType<?> domainType = nodeType.getSqmType();
584-
if ( !( domainType instanceof JdbcMapping ) || !( (JdbcMapping) domainType ).getJdbcType().isNumber() ) {
580+
if ( !( nodeType.getSqmType() instanceof JdbcMapping jdbcMapping )
581+
|| !jdbcMapping.getJdbcType().isNumber() ) {
585582
throw new SemanticException(
586583
"Operand of " + op.getOperatorChar() + " is of type '" + nodeType.getTypeName() +
587584
"' which is not a numeric type (its JDBC type code is not numeric)"

0 commit comments

Comments
 (0)