From 27bf0b8523186f2ded4954d1d351617bd11a0ce8 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Mon, 19 Feb 2024 11:47:10 +0100 Subject: [PATCH] make HqlInterpretation generic to eliminate warnings / unchecked casts and delete a deprecated method of an @Incubating API --- .../AbstractSharedSessionContract.java | 10 ++-- .../internal/NamedObjectRepositoryImpl.java | 2 +- .../QueryInterpretationCacheDisabledImpl.java | 12 ++-- .../QueryInterpretationCacheStandardImpl.java | 56 ++++++------------- .../query/spi/HqlInterpretation.java | 6 +- .../query/spi/QueryInterpretationCache.java | 7 +-- .../spi/SimpleHqlInterpretationImpl.java | 8 +-- .../query/sqm/internal/QuerySqmImpl.java | 20 +++---- .../sqm/internal/SqmSelectionQueryImpl.java | 11 +--- 9 files changed, 51 insertions(+), 81 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/internal/AbstractSharedSessionContract.java b/hibernate-core/src/main/java/org/hibernate/internal/AbstractSharedSessionContract.java index 9375c0333b..e89ec05796 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/AbstractSharedSessionContract.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/AbstractSharedSessionContract.java @@ -767,7 +767,7 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont delayedAfterCompletion(); try { - final HqlInterpretation interpretation = interpretHql( hql, resultType ); + final HqlInterpretation interpretation = interpretHql( hql, resultType ); checkSelectionQuery( hql, interpretation ); return createSelectionQuery( hql, resultType, interpretation ); } @@ -777,7 +777,7 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont } } - private SelectionQuery createSelectionQuery(String hql, Class resultType, HqlInterpretation interpretation) { + private SelectionQuery createSelectionQuery(String hql, Class resultType, HqlInterpretation interpretation) { final SqmSelectionQueryImpl query = new SqmSelectionQueryImpl<>( hql, interpretation, resultType, this ); if ( resultType != null ) { checkResultType( resultType, query ); @@ -787,7 +787,7 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont return query; } - protected HqlInterpretation interpretHql(String hql, Class resultType) { + protected HqlInterpretation interpretHql(String hql, Class resultType) { final QueryEngine queryEngine = getFactory().getQueryEngine(); return queryEngine.getInterpretationCache() .resolveHqlInterpretation( @@ -797,7 +797,7 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont ); } - protected static void checkSelectionQuery(String hql, HqlInterpretation hqlInterpretation) { + protected static void checkSelectionQuery(String hql, HqlInterpretation hqlInterpretation) { if ( !( hqlInterpretation.getSqmStatement() instanceof SqmSelectStatement ) ) { throw new IllegalSelectQueryException( "Expecting a selection query, but found `" + hql + "`", hql); } @@ -840,7 +840,7 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont delayedAfterCompletion(); try { - final HqlInterpretation interpretation = interpretHql( queryString, expectedResultType ); + final HqlInterpretation interpretation = interpretHql( queryString, expectedResultType ); final QuerySqmImpl query = new QuerySqmImpl<>( queryString, interpretation, expectedResultType, this ); applyQuerySettingsAndHints( query ); query.setComment( queryString ); diff --git a/hibernate-core/src/main/java/org/hibernate/query/internal/NamedObjectRepositoryImpl.java b/hibernate-core/src/main/java/org/hibernate/query/internal/NamedObjectRepositoryImpl.java index 00ab542e4a..bf8aa463d5 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/internal/NamedObjectRepositoryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/query/internal/NamedObjectRepositoryImpl.java @@ -249,7 +249,7 @@ public class NamedObjectRepositoryImpl implements NamedObjectRepository { interpretationCache.resolveHqlInterpretation( queryString, null, - s -> queryEngine.getHqlTranslator().translate( queryString, null ) + queryEngine.getHqlTranslator() ); } catch ( QueryException e ) { diff --git a/hibernate-core/src/main/java/org/hibernate/query/internal/QueryInterpretationCacheDisabledImpl.java b/hibernate-core/src/main/java/org/hibernate/query/internal/QueryInterpretationCacheDisabledImpl.java index eb563ff6d9..8d85252d13 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/internal/QueryInterpretationCacheDisabledImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/query/internal/QueryInterpretationCacheDisabledImpl.java @@ -10,6 +10,7 @@ import java.util.concurrent.TimeUnit; import java.util.function.Function; import java.util.function.Supplier; +import org.hibernate.query.hql.HqlTranslator; import org.hibernate.query.spi.HqlInterpretation; import org.hibernate.query.spi.NonSelectQueryPlan; import org.hibernate.query.spi.ParameterMetadataImplementor; @@ -60,15 +61,16 @@ public class QueryInterpretationCacheDisabledImpl implements QueryInterpretation } @Override - public HqlInterpretation resolveHqlInterpretation(String queryString, Class expectedResultType, Function> creator) { + public HqlInterpretation resolveHqlInterpretation( + String queryString, Class expectedResultType, HqlTranslator translator) { final StatisticsImplementor statistics = statisticsSupplier.get(); final boolean stats = statistics.isStatisticsEnabled(); final long startTime = stats ? System.nanoTime() : 0L; - final SqmStatement sqmStatement = creator.apply( queryString ); + + final SqmStatement sqmStatement = translator.translate( queryString, expectedResultType ); final DomainParameterXref domainParameterXref; final ParameterMetadataImplementor parameterMetadata; - if ( sqmStatement.getSqmParameters().isEmpty() ) { domainParameterXref = DomainParameterXref.empty(); parameterMetadata = ParameterMetadataImpl.EMPTY; @@ -84,9 +86,9 @@ public class QueryInterpretationCacheDisabledImpl implements QueryInterpretation statistics.queryCompiled( queryString, microseconds ); } - return new HqlInterpretation() { + return new HqlInterpretation<>() { @Override - public SqmStatement getSqmStatement() { + public SqmStatement getSqmStatement() { return sqmStatement; } diff --git a/hibernate-core/src/main/java/org/hibernate/query/internal/QueryInterpretationCacheStandardImpl.java b/hibernate-core/src/main/java/org/hibernate/query/internal/QueryInterpretationCacheStandardImpl.java index 3bb5d9f814..7d1905b9a4 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/internal/QueryInterpretationCacheStandardImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/query/internal/QueryInterpretationCacheStandardImpl.java @@ -40,7 +40,7 @@ public class QueryInterpretationCacheStandardImpl implements QueryInterpretation */ private final BoundedConcurrentHashMap queryPlanCache; - private final BoundedConcurrentHashMap hqlInterpretationCache; + private final BoundedConcurrentHashMap> hqlInterpretationCache; private final BoundedConcurrentHashMap nativeQueryParamCache; private final Supplier statisticsSupplier; @@ -100,69 +100,49 @@ public class QueryInterpretationCacheStandardImpl implements QueryInterpretation } @Override - public HqlInterpretation resolveHqlInterpretation( + public HqlInterpretation resolveHqlInterpretation( String queryString, - Class expectedResultType, - Function> creator) { - return resolveHqlInterpretation( queryString, expectedResultType, new HqlTranslator() { - @Override - public SqmStatement translate(String hql, Class expectedResultType) { - //noinspection unchecked - return (SqmStatement) creator.apply( hql ); - } - } ); - } - - @Override - public HqlInterpretation resolveHqlInterpretation( - String queryString, - Class expectedResultType, + Class expectedResultType, HqlTranslator translator) { log.tracef( "QueryPlan#resolveHqlInterpretation( `%s` )", queryString ); final StatisticsImplementor statistics = statisticsSupplier.get(); - final Object cacheKey; - if ( expectedResultType != null ) { - cacheKey = new HqlInterpretationCacheKey( queryString, expectedResultType ); - } - else { - cacheKey = queryString; - } - final HqlInterpretation existing = hqlInterpretationCache.get( cacheKey ); + final Object cacheKey = expectedResultType != null + ? new HqlInterpretationCacheKey( queryString, expectedResultType ) + : queryString; + + final HqlInterpretation existing = hqlInterpretationCache.get( cacheKey ); if ( existing != null ) { if ( statistics.isStatisticsEnabled() ) { statistics.queryPlanCacheHit( queryString ); } - return existing; + return (HqlInterpretation) existing; } else if ( expectedResultType != null ) { - final HqlInterpretation existingQueryOnly = hqlInterpretationCache.get( queryString ); + final HqlInterpretation existingQueryOnly = hqlInterpretationCache.get( queryString ); if ( existingQueryOnly != null ) { if ( statistics.isStatisticsEnabled() ) { statistics.queryPlanCacheHit( queryString ); } - return existingQueryOnly; + return (HqlInterpretation) existingQueryOnly; } } - final HqlInterpretation hqlInterpretation = createHqlInterpretation( - queryString, - expectedResultType, - translator, - statistics - ); + + final HqlInterpretation hqlInterpretation = + createHqlInterpretation( queryString, expectedResultType, translator, statistics ); hqlInterpretationCache.put( cacheKey, hqlInterpretation ); return hqlInterpretation; } - protected static HqlInterpretation createHqlInterpretation( + protected static HqlInterpretation createHqlInterpretation( String queryString, - Class expectedResultType, + Class expectedResultType, HqlTranslator translator, StatisticsImplementor statistics) { final boolean stats = statistics.isStatisticsEnabled(); final long startTime = stats ? System.nanoTime() : 0L; - final SqmStatement sqmStatement = translator.translate( queryString, expectedResultType ); + final SqmStatement sqmStatement = translator.translate( queryString, expectedResultType ); final ParameterMetadataImplementor parameterMetadata; final DomainParameterXref domainParameterXref; @@ -181,7 +161,7 @@ public class QueryInterpretationCacheStandardImpl implements QueryInterpretation statistics.queryCompiled( queryString, microseconds ); } - return new SimpleHqlInterpretationImpl( sqmStatement, parameterMetadata, domainParameterXref ); + return new SimpleHqlInterpretationImpl<>( sqmStatement, parameterMetadata, domainParameterXref ); } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/query/spi/HqlInterpretation.java b/hibernate-core/src/main/java/org/hibernate/query/spi/HqlInterpretation.java index 9cc93c6095..d10c9ca028 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/spi/HqlInterpretation.java +++ b/hibernate-core/src/main/java/org/hibernate/query/spi/HqlInterpretation.java @@ -11,9 +11,11 @@ import org.hibernate.query.sqm.tree.SqmStatement; /** * @author Steve Ebersole + * + * @param the query result type */ -public interface HqlInterpretation { - SqmStatement getSqmStatement(); +public interface HqlInterpretation { + SqmStatement getSqmStatement(); ParameterMetadataImplementor getParameterMetadata(); diff --git a/hibernate-core/src/main/java/org/hibernate/query/spi/QueryInterpretationCache.java b/hibernate-core/src/main/java/org/hibernate/query/spi/QueryInterpretationCache.java index 2d08e5d098..c108b2103d 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/spi/QueryInterpretationCache.java +++ b/hibernate-core/src/main/java/org/hibernate/query/spi/QueryInterpretationCache.java @@ -37,12 +37,7 @@ public interface QueryInterpretationCache { int getNumberOfCachedHqlInterpretations(); int getNumberOfCachedQueryPlans(); - @Deprecated(forRemoval = true) - HqlInterpretation resolveHqlInterpretation(String queryString, Class expectedResultType, Function> creator); - - default HqlInterpretation resolveHqlInterpretation(String queryString, Class expectedResultType, HqlTranslator translator) { - return resolveHqlInterpretation( queryString, expectedResultType, s -> translator.translate( queryString, expectedResultType ) ); - } + HqlInterpretation resolveHqlInterpretation(String queryString, Class expectedResultType, HqlTranslator translator); SelectQueryPlan resolveSelectQueryPlan(Key key, Supplier> creator); diff --git a/hibernate-core/src/main/java/org/hibernate/query/spi/SimpleHqlInterpretationImpl.java b/hibernate-core/src/main/java/org/hibernate/query/spi/SimpleHqlInterpretationImpl.java index 9e4f3324d7..09dea2707b 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/spi/SimpleHqlInterpretationImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/query/spi/SimpleHqlInterpretationImpl.java @@ -12,13 +12,13 @@ import org.hibernate.query.sqm.tree.SqmStatement; /** * @author Steve Ebersole */ -public class SimpleHqlInterpretationImpl implements HqlInterpretation { - private final SqmStatement sqmStatement; +public class SimpleHqlInterpretationImpl implements HqlInterpretation { + private final SqmStatement sqmStatement; private final ParameterMetadataImplementor parameterMetadata; private final DomainParameterXref domainParameterXref; public SimpleHqlInterpretationImpl( - SqmStatement sqmStatement, + SqmStatement sqmStatement, ParameterMetadataImplementor parameterMetadata, DomainParameterXref domainParameterXref) { this.sqmStatement = sqmStatement; @@ -27,7 +27,7 @@ public class SimpleHqlInterpretationImpl implements HqlInterpretation { } @Override - public SqmStatement getSqmStatement() { + public SqmStatement getSqmStatement() { return sqmStatement; } 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 be67ee0a19..8258257666 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 @@ -167,13 +167,10 @@ public class QuerySqmImpl final QueryEngine queryEngine = session.getFactory().getQueryEngine(); final QueryInterpretationCache interpretationCache = queryEngine.getInterpretationCache(); - final HqlInterpretation hqlInterpretation = interpretationCache.resolveHqlInterpretation( - hql, - expectedResultType, - (s) -> queryEngine.getHqlTranslator().translate( hql, expectedResultType ) - ); + final HqlInterpretation hqlInterpretation = + interpretationCache.resolveHqlInterpretation( hql, expectedResultType, queryEngine.getHqlTranslator() ); - this.sqm = (SqmStatement) hqlInterpretation.getSqmStatement(); + this.sqm = hqlInterpretation.getSqmStatement(); this.parameterMetadata = hqlInterpretation.getParameterMetadata(); this.domainParameterXref = hqlInterpretation.getDomainParameterXref(); @@ -200,17 +197,16 @@ public class QuerySqmImpl /** * Form used for HQL queries */ - @SuppressWarnings("unchecked") public QuerySqmImpl( String hql, - HqlInterpretation hqlInterpretation, + HqlInterpretation hqlInterpretation, Class resultType, SharedSessionContractImplementor session) { super( session ); this.hql = hql; this.resultType = resultType; - this.sqm = (SqmStatement) hqlInterpretation.getSqmStatement(); + this.sqm = hqlInterpretation.getSqmStatement(); this.parameterMetadata = hqlInterpretation.getParameterMetadata(); this.domainParameterXref = hqlInterpretation.getDomainParameterXref(); @@ -897,7 +893,7 @@ public class QuerySqmImpl } @Override - public SqmQueryImplementor setResultListTransformer(ResultListTransformer transformer) { + public SqmQueryImplementor setResultListTransformer(ResultListTransformer transformer) { applyResultListTransformer( transformer ); return this; } @@ -1248,7 +1244,7 @@ public class QuerySqmImpl } @Override - public SqmQueryImplementor setProperties(Map bean) { + public SqmQueryImplementor setProperties(@SuppressWarnings("rawtypes") Map bean) { super.setProperties( bean ); return this; } @@ -1362,7 +1358,7 @@ public class QuerySqmImpl } @Override - public SqmQueryImplementor setParameterList(String name, Collection values) { + public SqmQueryImplementor setParameterList(String name, @SuppressWarnings("rawtypes") Collection values) { super.setParameterList( name, values ); return this; } diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/SqmSelectionQueryImpl.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/SqmSelectionQueryImpl.java index 5455ed3d67..c0d0c35513 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/SqmSelectionQueryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/SqmSelectionQueryImpl.java @@ -101,13 +101,12 @@ public class SqmSelectionQueryImpl extends AbstractSelectionQuery public SqmSelectionQueryImpl( String hql, - HqlInterpretation hqlInterpretation, + HqlInterpretation hqlInterpretation, Class expectedResultType, SharedSessionContractImplementor session) { super( session ); this.hql = hql; - //noinspection unchecked this.sqm = (SqmSelectStatement) hqlInterpretation.getSqmStatement(); this.parameterMetadata = hqlInterpretation.getParameterMetadata(); @@ -166,14 +165,10 @@ public class SqmSelectionQueryImpl extends AbstractSelectionQuery final QueryEngine queryEngine = session.getFactory().getQueryEngine(); final QueryInterpretationCache interpretationCache = queryEngine.getInterpretationCache(); - final HqlInterpretation hqlInterpretation = interpretationCache.resolveHqlInterpretation( - hql, - resultType, - (s) -> queryEngine.getHqlTranslator().translate( hql, resultType ) - ); + final HqlInterpretation hqlInterpretation = + interpretationCache.resolveHqlInterpretation( hql, resultType, queryEngine.getHqlTranslator() ); SqmUtil.verifyIsSelectStatement( hqlInterpretation.getSqmStatement(), hql ); - //noinspection unchecked this.sqm = (SqmSelectStatement) hqlInterpretation.getSqmStatement(); this.parameterMetadata = hqlInterpretation.getParameterMetadata();