From 14e0f12654d1f55bd78650688729e8b95a7dbe07 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Thu, 7 Nov 2024 14:29:00 +0100 Subject: [PATCH] deal with more casts Signed-off-by: Gavin King --- .../query/sqm/internal/QuerySqmImpl.java | 1 - .../sqm/tree/select/SqmSelectStatement.java | 44 +++++++++++-------- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/QuerySqmImpl.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/QuerySqmImpl.java index de8430f4a1..b2d6121c47 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/QuerySqmImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/QuerySqmImpl.java @@ -63,7 +63,6 @@ import org.hibernate.query.spi.QueryOptions; import org.hibernate.query.spi.QueryParameterBindings; import org.hibernate.query.spi.ScrollableResultsImplementor; import org.hibernate.query.spi.SelectQueryPlan; -import org.hibernate.query.sqm.SqmPathSource; import org.hibernate.query.sqm.internal.SqmInterpretationsKey.InterpretationsKeySource; import org.hibernate.query.sqm.mutation.spi.SqmMultiTableMutationStrategy; import org.hibernate.query.sqm.spi.NamedSqmQueryMemento; diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/select/SqmSelectStatement.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/select/SqmSelectStatement.java index 99029d1828..1e98ce38ce 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/select/SqmSelectStatement.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/select/SqmSelectStatement.java @@ -4,7 +4,6 @@ */ package org.hibernate.query.sqm.tree.select; -import java.util.Collections; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; @@ -40,6 +39,8 @@ import jakarta.persistence.criteria.Predicate; import jakarta.persistence.criteria.Selection; import jakarta.persistence.metamodel.EntityType; +import static java.util.Collections.emptySet; +import static java.util.Collections.unmodifiableSet; import static org.hibernate.query.sqm.spi.SqmCreationHelper.combinePredicates; import static org.hibernate.query.sqm.SqmQuerySource.CRITERIA; import static org.hibernate.query.sqm.tree.SqmCopyContext.noParamCopyContext; @@ -168,8 +169,8 @@ public class SqmSelectStatement extends AbstractSqmSelectQuery implements public SqmQuerySpec getQuerySpec() { if ( querySource == CRITERIA ) { final SqmQueryPart queryPart = getQueryPart(); - if ( queryPart instanceof SqmQuerySpec ) { - return (SqmQuerySpec) queryPart; + if ( queryPart instanceof SqmQuerySpec querySpec ) { + return querySpec; } throw new IllegalStateException( "Query group can't be treated as query spec. Use JpaSelectCriteria#getQueryPart to access query group details" @@ -185,8 +186,8 @@ public class SqmSelectStatement extends AbstractSqmSelectQuery implements } private boolean producesUniqueResults(SqmQueryPart queryPart) { - if ( queryPart instanceof SqmQuerySpec ) { - return ( (SqmQuerySpec) queryPart ).producesUniqueResults(); + if ( queryPart instanceof SqmQuerySpec querySpec ) { + return querySpec.producesUniqueResults(); } else { // For query groups we have to assume that duplicates are possible @@ -199,13 +200,15 @@ public class SqmSelectStatement extends AbstractSqmSelectQuery implements } private boolean containsCollectionFetches(SqmQueryPart queryPart) { - if ( queryPart instanceof SqmQuerySpec ) { - return ( (SqmQuerySpec) queryPart ).containsCollectionFetches(); + if ( queryPart instanceof SqmQuerySpec querySpec ) { + return querySpec.containsCollectionFetches(); + } + else if ( queryPart instanceof SqmQueryGroup queryGroup ) { + // We only have to check the first one + return containsCollectionFetches( queryGroup.getQueryParts().get( 0 ) ); } else { - // We only have to check the first one - final SqmQueryGroup queryGroup = (SqmQueryGroup) queryPart; - return containsCollectionFetches( queryGroup.getQueryParts().get( 0 ) ); + throw new IllegalStateException("No SqmQueryPart"); } } @@ -214,13 +217,15 @@ public class SqmSelectStatement extends AbstractSqmSelectQuery implements } private boolean usesDistinct(SqmQueryPart queryPart) { - if ( queryPart instanceof SqmQuerySpec ) { - return ( (SqmQuerySpec) queryPart ).getSelectClause().isDistinct(); + if ( queryPart instanceof SqmQuerySpec querySpec ) { + return querySpec.getSelectClause().isDistinct(); + } + else if ( queryPart instanceof SqmQueryGroup queryGroup ) { + // We only have to check the first one + return usesDistinct( queryGroup.getQueryParts().get( 0 ) ); } else { - // We only have to check the first one - final SqmQueryGroup queryGroup = (SqmQueryGroup) queryPart; - return usesDistinct( queryGroup.getQueryParts().get( 0 ) ); + throw new IllegalStateException("No SqmQueryPart"); } } @@ -230,8 +235,9 @@ public class SqmSelectStatement extends AbstractSqmSelectQuery implements assert parameters == null : "SqmSelectStatement (as Criteria) should not have collected parameters"; return collectParameters( this ); } - - return parameters == null ? Collections.emptySet() : Collections.unmodifiableSet( parameters ); + else { + return parameters == null ? emptySet() : unmodifiableSet( parameters ); + } } @Override @@ -249,7 +255,6 @@ public class SqmSelectStatement extends AbstractSqmSelectQuery implements if ( parameters == null ) { parameters = new LinkedHashSet<>(); } - parameters.add( parameter ); } @@ -269,7 +274,8 @@ public class SqmSelectStatement extends AbstractSqmSelectQuery implements String name, AbstractQuery baseCriteria, boolean unionDistinct, - Function, AbstractQuery> recursiveCriteriaProducer) { + Function, + AbstractQuery> recursiveCriteriaProducer) { if ( baseCriteria instanceof SqmSubQuery ) { throw new IllegalArgumentException( "Invalid query type provided to root query 'with' method, " +