From 5d2527383e6da638b0d8bb5a95f929a239965909 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Thu, 29 Feb 2024 20:19:41 +0100 Subject: [PATCH] HHH-17772 cleanup after sorting rework --- .../annotation/AbstractFinderMethod.java | 11 +-- .../annotation/AbstractQueryMethod.java | 89 ++++++++----------- .../annotation/CriteriaFinderMethod.java | 22 +---- .../annotation/IdFinderMethod.java | 3 +- .../annotation/NaturalIdFinderMethod.java | 4 +- .../jpamodelgen/annotation/QueryMethod.java | 20 +---- 6 files changed, 48 insertions(+), 101 deletions(-) diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/AbstractFinderMethod.java b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/AbstractFinderMethod.java index 03cfe93126..e712934482 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/AbstractFinderMethod.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/AbstractFinderMethod.java @@ -22,7 +22,7 @@ public abstract class AbstractFinderMethod extends AbstractQueryMethod { final String entity; final List fetchProfiles; - public AbstractFinderMethod( + AbstractFinderMethod( AnnotationMetaEntity annotationMetaEntity, String methodName, String entity, @@ -32,13 +32,15 @@ public abstract class AbstractFinderMethod extends AbstractQueryMethod { List fetchProfiles, List paramNames, List paramTypes, + List orderBys, boolean addNonnullAnnotation, boolean convertToDataExceptions) { super( annotationMetaEntity, methodName, paramNames, paramTypes, entity, sessionType, sessionName, - belongsToDao, addNonnullAnnotation, + belongsToDao, orderBys, + addNonnullAnnotation, convertToDataExceptions ); this.entity = entity; this.fetchProfiles = fetchProfiles; @@ -204,9 +206,4 @@ public abstract class AbstractFinderMethod extends AbstractQueryMethod { declaration .append(belongsToDao ? "@Override\npublic " : "public static "); } - - @Override - String getSortableEntityClass() { - return entity; - } } diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/AbstractQueryMethod.java b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/AbstractQueryMethod.java index b432ffb2b9..dfee2e7157 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/AbstractQueryMethod.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/AbstractQueryMethod.java @@ -10,30 +10,12 @@ import org.checkerframework.checker.nullness.qual.Nullable; import org.hibernate.AssertionFailure; import org.hibernate.jpamodelgen.model.MetaAttribute; import org.hibernate.jpamodelgen.model.Metamodel; -import org.hibernate.jpamodelgen.util.Constants; import java.util.List; import java.util.stream.Collectors; -import static java.util.Collections.emptyList; import static java.util.stream.Collectors.toList; -import static org.hibernate.jpamodelgen.util.Constants.HIB_KEYED_PAGE; -import static org.hibernate.jpamodelgen.util.Constants.HIB_KEYED_RESULT_LIST; -import static org.hibernate.jpamodelgen.util.Constants.HIB_ORDER; -import static org.hibernate.jpamodelgen.util.Constants.HIB_PAGE; -import static org.hibernate.jpamodelgen.util.Constants.HIB_SELECTION_QUERY; -import static org.hibernate.jpamodelgen.util.Constants.JD_KEYED_PAGE; -import static org.hibernate.jpamodelgen.util.Constants.JD_KEYED_SLICE; -import static org.hibernate.jpamodelgen.util.Constants.JD_LIMIT; -import static org.hibernate.jpamodelgen.util.Constants.JD_ORDER; -import static org.hibernate.jpamodelgen.util.Constants.JD_PAGE; -import static org.hibernate.jpamodelgen.util.Constants.JD_PAGE_REQUEST; -import static org.hibernate.jpamodelgen.util.Constants.JD_SLICE; -import static org.hibernate.jpamodelgen.util.Constants.JD_SORT; -import static org.hibernate.jpamodelgen.util.Constants.LIST; -import static org.hibernate.jpamodelgen.util.Constants.OPTIONAL; -import static org.hibernate.jpamodelgen.util.Constants.SESSION_TYPES; -import static org.hibernate.jpamodelgen.util.Constants.STREAM; +import static org.hibernate.jpamodelgen.util.Constants.*; import static org.hibernate.jpamodelgen.util.TypeUtils.isPrimitive; /** @@ -48,10 +30,11 @@ public abstract class AbstractQueryMethod implements MetaAttribute { final String sessionType; final String sessionName; final boolean belongsToDao; + final List orderBys; final boolean addNonnullAnnotation; final boolean dataRepository; - public AbstractQueryMethod( + AbstractQueryMethod( AnnotationMetaEntity annotationMetaEntity, String methodName, List paramNames, List paramTypes, @@ -59,6 +42,7 @@ public abstract class AbstractQueryMethod implements MetaAttribute { String sessionType, String sessionName, boolean belongsToDao, + List orderBys, boolean addNonnullAnnotation, boolean dataRepository) { this.annotationMetaEntity = annotationMetaEntity; @@ -69,6 +53,7 @@ public abstract class AbstractQueryMethod implements MetaAttribute { this.sessionType = sessionType; this.sessionName = sessionName; this.belongsToDao = belongsToDao; + this.orderBys = orderBys; this.addNonnullAnnotation = addNonnullAnnotation; this.dataRepository = dataRepository; } @@ -112,6 +97,16 @@ public abstract class AbstractQueryMethod implements MetaAttribute { return type.endsWith("...") ? stripped + "..." : stripped; } + void preamble(StringBuilder declaration, StringBuilder returnType, List paramTypes) { + declaration + .append(returnType) + .append(" ") + .append(methodName); + parameters( paramTypes, declaration ); + declaration + .append(" {\n"); + } + void parameters(List paramTypes, StringBuilder declaration) { declaration .append("("); @@ -180,15 +175,15 @@ public abstract class AbstractQueryMethod implements MetaAttribute { } boolean isUsingEntityManager() { - return Constants.ENTITY_MANAGER.equals(sessionType); + return ENTITY_MANAGER.equals(sessionType); } boolean isUsingStatelessSession() { - return Constants.HIB_STATELESS_SESSION.equals(sessionType); + return HIB_STATELESS_SESSION.equals(sessionType); } boolean isReactive() { - return Constants.MUTINY_SESSION.equals(sessionType); + return MUTINY_SESSION.equals(sessionType); } void setPage(StringBuilder declaration, String paramName, String paramType) { @@ -290,17 +285,11 @@ public abstract class AbstractQueryMethod implements MetaAttribute { declaration.append("\n}"); } - @Nullable String getSortableEntityClass() { - return returnTypeName; - } - void unwrapQuery(StringBuilder declaration, boolean unwrapped) { if ( !unwrapped && isUsingEntityManager() ) { declaration - .append("\t\t\t.unwrap("); - final String selectionQuery = annotationMetaEntity.importType(HIB_SELECTION_QUERY); - declaration - .append(selectionQuery) + .append("\t\t\t.unwrap(") + .append(annotationMetaEntity.importType(HIB_SELECTION_QUERY)) .append(".class)\n"); } } @@ -346,8 +335,7 @@ public abstract class AbstractQueryMethod implements MetaAttribute { annotationMetaEntity.staticImport("org.hibernate.query.Order", "by"); annotationMetaEntity.staticImport("org.hibernate.query.Page", "page"); annotationMetaEntity.staticImport(Collectors.class.getName(), "toList"); - final String entityClass = getSortableEntityClass(); - if ( entityClass == null ) { + if ( returnTypeName == null ) { throw new AssertionFailure("entity class cannot be null"); } else { @@ -355,7 +343,7 @@ public abstract class AbstractQueryMethod implements MetaAttribute { .append(MAKE_KEYED_PAGE .replace("pageRequest", parameterName(JD_PAGE_REQUEST, paramTypes, paramNames)) - .replace("Entity", annotationMetaEntity.importType(entityClass))); + .replace("Entity", annotationMetaEntity.importType(returnTypeName))); } } @@ -394,10 +382,6 @@ public abstract class AbstractQueryMethod implements MetaAttribute { void setParameters(StringBuilder declaration, List paramTypes) {} - List getOrderBys() { - return emptyList(); - } - void tryReturn(StringBuilder declaration, List paramTypes, @Nullable String containerType) { if ( isJakartaKeyedSlice(containerType) ) { makeKeyedPage( declaration, paramTypes ); @@ -422,14 +406,13 @@ public abstract class AbstractQueryMethod implements MetaAttribute { .append('\t'); if ( isJakartaKeyedSlice(containerType) || isJakartaSlice(containerType) ) { - final String entityClass = getSortableEntityClass(); - if ( entityClass != null && isUsingEntityManager() ) { + if ( returnTypeName != null && isUsingEntityManager() ) { // this is necessary to avoid losing the type // after unwrapping the Query object declaration .append(annotationMetaEntity.importType(HIB_KEYED_RESULT_LIST)) .append('<') - .append(annotationMetaEntity.importType(entityClass)) + .append(annotationMetaEntity.importType(returnTypeName)) .append('>'); } else { @@ -454,28 +437,27 @@ public abstract class AbstractQueryMethod implements MetaAttribute { setParameters( declaration, paramTypes ); unwrapQuery( declaration, !isUsingEntityManager() ); declaration - .append("\t\t\t\t\t\t.getResultCount();\n"); + .append("\t\t\t.getResultCount();\n"); } void collectOrdering(StringBuilder declaration, List paramTypes) { - final String entityClass = getSortableEntityClass(); if ( hasOrdering(paramTypes) ) { - if ( entityClass != null ) { + if ( returnTypeName != null ) { declaration .append("\tvar _orders = new ") .append(annotationMetaEntity.importType("java.util.ArrayList")) .append("<") .append(annotationMetaEntity.importType(HIB_ORDER)) .append(">();\n"); // static orders declared using @OrderBy must come first - for ( OrderBy orderBy : getOrderBys() ) { + for ( OrderBy orderBy : orderBys ) { declaration .append("\t_orders.add(") .append(annotationMetaEntity.staticImport(HIB_ORDER, "by")) .append('(') - .append(annotationMetaEntity.importType(entityClass)) + .append(annotationMetaEntity.importType(returnTypeName)) .append(".class, \"") .append(orderBy.fieldName) .append("\", ") @@ -517,7 +499,7 @@ public abstract class AbstractQueryMethod implements MetaAttribute { .append("\t\t_orders.add(") .append(annotationMetaEntity.staticImport(HIB_ORDER, "by")) .append('(') - .append(annotationMetaEntity.importType(entityClass)) + .append(annotationMetaEntity.importType(returnTypeName)) .append(".class, _sort.property(),") .append("\n\t\t\t\t\t\t") .append("_sort.isAscending() ? ASCENDING : DESCENDING,") @@ -534,7 +516,7 @@ public abstract class AbstractQueryMethod implements MetaAttribute { .append("\t\t_orders.add(") .append(annotationMetaEntity.staticImport(HIB_ORDER, "by")) .append('(') - .append(annotationMetaEntity.importType(entityClass)) + .append(annotationMetaEntity.importType(returnTypeName)) .append(".class, _sort.property(),") .append("\n\t\t\t\t\t\t") .append("_sort.isAscending() ? ASCENDING : DESCENDING,") @@ -547,7 +529,7 @@ public abstract class AbstractQueryMethod implements MetaAttribute { .append("\t_orders.add(") .append(annotationMetaEntity.staticImport(HIB_ORDER, "by")) .append('(') - .append(annotationMetaEntity.importType(entityClass)) + .append(annotationMetaEntity.importType(returnTypeName)) .append(".class, ") .append(name) .append(".property(),") @@ -565,7 +547,7 @@ public abstract class AbstractQueryMethod implements MetaAttribute { private boolean hasOrdering(List paramTypes) { return paramTypes.stream().anyMatch(AbstractQueryMethod::isOrderParam) - || !getOrderBys().isEmpty(); + || !orderBys.isEmpty(); } boolean unwrapIfNecessary(StringBuilder declaration, @Nullable String containerType, boolean unwrapped) { @@ -628,8 +610,7 @@ public abstract class AbstractQueryMethod implements MetaAttribute { break; case JD_KEYED_SLICE: case JD_KEYED_PAGE: - final String entityClass = getSortableEntityClass(); - if ( entityClass == null ) { + if ( returnTypeName == null ) { throw new AssertionFailure("entity class cannot be null"); } else { @@ -640,7 +621,7 @@ public abstract class AbstractQueryMethod implements MetaAttribute { String fragment = MAKE_KEYED_SLICE .replace("pageRequest", parameterName(JD_PAGE_REQUEST, paramTypes, paramNames)) - .replace("Entity", annotationMetaEntity.importType(entityClass)) + .replace("Entity", annotationMetaEntity.importType(returnTypeName)) .replace("KeysetAwareSliceRecord", implType(containerType)); if ( JD_KEYED_SLICE.equals(containerType) ) { fragment = fragment.replace("_totalResults, ", ""); diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/CriteriaFinderMethod.java b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/CriteriaFinderMethod.java index b58de8628e..53aaa4571a 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/CriteriaFinderMethod.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/CriteriaFinderMethod.java @@ -22,7 +22,6 @@ public class CriteriaFinderMethod extends AbstractFinderMethod { private final @Nullable String containerType; private final List paramNullability; - private final List orderBys; CriteriaFinderMethod( AnnotationMetaEntity annotationMetaEntity, @@ -38,11 +37,9 @@ public class CriteriaFinderMethod extends AbstractFinderMethod { boolean addNonnullAnnotation, boolean dataRepository) { super( annotationMetaEntity, methodName, entity, belongsToDao, sessionType, sessionName, fetchProfiles, - paramNames, paramTypes, addNonnullAnnotation, - dataRepository ); + paramNames, paramTypes, orderBys, addNonnullAnnotation, dataRepository ); this.containerType = containerType; this.paramNullability = paramNullability; - this.orderBys = orderBys; } @Override @@ -55,18 +52,13 @@ public class CriteriaFinderMethod extends AbstractFinderMethod { return containerType == null; } - @Override - List getOrderBys() { - return orderBys; - } - @Override public String getAttributeDeclarationString() { final List paramTypes = parameterTypes(); final StringBuilder declaration = new StringBuilder(); comment( declaration ); modifiers( declaration ); - preamble( declaration, paramTypes ); + preamble( declaration, returnType(), paramTypes ); nullChecks( paramTypes, declaration ); createCriteriaQuery( declaration ); where( declaration, paramTypes ); @@ -77,16 +69,6 @@ public class CriteriaFinderMethod extends AbstractFinderMethod { return declaration.toString(); } - private void preamble(StringBuilder declaration, List paramTypes) { - declaration - .append(returnType()) - .append(" ") - .append(methodName); - parameters(paramTypes, declaration); - declaration - .append(" {\n"); - } - private void executeQuery(StringBuilder declaration, List paramTypes) { declaration .append('\n'); diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/IdFinderMethod.java b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/IdFinderMethod.java index 117718bf6b..5de3129f67 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/IdFinderMethod.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/IdFinderMethod.java @@ -8,6 +8,7 @@ package org.hibernate.jpamodelgen.annotation; import java.util.List; +import static java.util.Collections.emptyList; import static org.hibernate.jpamodelgen.util.TypeUtils.isPrimitive; /** @@ -29,7 +30,7 @@ public class IdFinderMethod extends AbstractFinderMethod { boolean addNonnullAnnotation, boolean dataRepository) { super( annotationMetaEntity, methodName, entity, belongsToDao, sessionType, sessionName, fetchProfiles, - paramNames, paramTypes, addNonnullAnnotation, dataRepository ); + paramNames, paramTypes, emptyList(), addNonnullAnnotation, dataRepository ); int idParameter = idParameter(paramNames, paramTypes); this.paramName = paramNames.get(idParameter); this.paramType = paramTypes.get(idParameter); diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/NaturalIdFinderMethod.java b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/NaturalIdFinderMethod.java index d561dfad64..9a5773477c 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/NaturalIdFinderMethod.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/NaturalIdFinderMethod.java @@ -8,6 +8,8 @@ package org.hibernate.jpamodelgen.annotation; import java.util.List; +import static java.util.Collections.emptyList; + /** * @author Gavin King */ @@ -27,7 +29,7 @@ public class NaturalIdFinderMethod extends AbstractFinderMethod { boolean addNonnullAnnotation, boolean dataRepository) { super( annotationMetaEntity, methodName, entity, belongsToDao, sessionType, sessionName, fetchProfiles, - paramNames, paramTypes, addNonnullAnnotation, dataRepository ); + paramNames, paramTypes, emptyList(), addNonnullAnnotation, dataRepository ); this.paramNullability = paramNullability; } diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/QueryMethod.java b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/QueryMethod.java index 4c80b8821a..dabbc8cbf9 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/QueryMethod.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/QueryMethod.java @@ -24,7 +24,6 @@ public class QueryMethod extends AbstractQueryMethod { private final @Nullable String containerType; private final boolean isUpdate; private final boolean isNative; - private final List orderBys; QueryMethod( AnnotationMetaEntity annotationMetaEntity, @@ -48,13 +47,13 @@ public class QueryMethod extends AbstractQueryMethod { methodName, paramNames, paramTypes, returnTypeName, sessionType, sessionName, - belongsToDao, addNonnullAnnotation, + belongsToDao, orderBys, + addNonnullAnnotation, dataRepository ); this.queryString = queryString; this.containerType = containerType; this.isUpdate = isUpdate; this.isNative = isNative; - this.orderBys = orderBys; } @Override @@ -77,11 +76,6 @@ public class QueryMethod extends AbstractQueryMethod { return containerType == null; } - @Override - List getOrderBys() { - return orderBys; - } - @Override public String getAttributeDeclarationString() { final List paramTypes = parameterTypes(); @@ -137,16 +131,6 @@ public class QueryMethod extends AbstractQueryMethod { } } - private void preamble(StringBuilder declaration, StringBuilder returnType, List paramTypes) { - declaration - .append(returnType) - .append(" ") - .append(methodName); - parameters( paramTypes, declaration ); - declaration - .append(" {\n"); - } - private void execute(StringBuilder declaration, boolean unwrapped) { if ( isUpdate ) { declaration