retain type use annotation in return types and simplify code

Signed-off-by: Gavin King <gavin@hibernate.org>
This commit is contained in:
Gavin King 2024-04-06 21:35:26 +02:00 committed by Christian Beikov
parent 8053ed9cec
commit 96855d0525
9 changed files with 145 additions and 137 deletions

View File

@ -33,9 +33,10 @@ public abstract class AbstractCriteriaMethod extends AbstractFinderMethod {
boolean addNonnullAnnotation,
boolean convertToDataExceptions,
List<Boolean> multivalued,
List<Boolean> paramPatterns) {
List<Boolean> paramPatterns,
String fullReturnType) {
super(annotationMetaEntity, method, methodName, entity, belongsToDao, sessionType, sessionName, fetchProfiles,
paramNames, paramTypes, orderBys, addNonnullAnnotation, convertToDataExceptions);
paramNames, paramTypes, orderBys, addNonnullAnnotation, convertToDataExceptions, fullReturnType);
this.multivalued = multivalued;
this.paramPatterns = paramPatterns;
}
@ -46,7 +47,7 @@ public abstract class AbstractCriteriaMethod extends AbstractFinderMethod {
final StringBuilder declaration = new StringBuilder();
comment( declaration );
modifiers( declaration );
preamble( declaration, returnType(), paramTypes );
preamble( declaration, paramTypes );
chainSession( declaration );
nullChecks( paramTypes, declaration );
createBuilder(declaration);
@ -64,7 +65,7 @@ public abstract class AbstractCriteriaMethod extends AbstractFinderMethod {
abstract String createCriteriaMethod();
abstract String returnType();
// abstract String returnType();
abstract String createQueryMethod();

View File

@ -13,7 +13,6 @@ import java.util.List;
import java.util.Locale;
import static org.hibernate.processor.util.Constants.HIB_SESSION;
import static org.hibernate.processor.util.Constants.UNI;
import static org.hibernate.processor.util.StringUtil.getUpperUnderscoreCaseFromLowerCamelCase;
/**
@ -36,14 +35,16 @@ public abstract class AbstractFinderMethod extends AbstractQueryMethod {
List<String> paramTypes,
List<OrderBy> orderBys,
boolean addNonnullAnnotation,
boolean convertToDataExceptions) {
boolean convertToDataExceptions,
String fullReturnType) {
super( annotationMetaEntity, method,
methodName,
paramNames, paramTypes, entity,
sessionType, sessionName,
belongsToDao, orderBys,
addNonnullAnnotation,
convertToDataExceptions );
convertToDataExceptions,
fullReturnType );
this.entity = entity;
this.fetchProfiles = fetchProfiles;
}
@ -173,16 +174,6 @@ public abstract class AbstractFinderMethod extends AbstractQueryMethod {
return unwrapped;
}
void preamble(StringBuilder declaration) {
modifiers( declaration );
entityType( declaration );
declaration
.append(" ")
.append(methodName);
parameters( paramTypes, declaration ) ;
declaration.append(" {\n");
}
void tryReturn(StringBuilder declaration) {
if (dataRepository) {
declaration
@ -193,19 +184,19 @@ public abstract class AbstractFinderMethod extends AbstractQueryMethod {
.append(sessionName);
}
private void entityType(StringBuilder declaration) {
if ( isReactive() ) {
declaration
.append(annotationMetaEntity.importType(UNI))
.append('<');
}
declaration
.append(annotationMetaEntity.importType(entity));
if ( isReactive() ) {
declaration
.append('>');
}
}
// private void returnType(StringBuilder declaration) {
// if ( isReactive() ) {
// declaration
// .append(annotationMetaEntity.importType(UNI))
// .append('<');
// }
// declaration
// .append(annotationMetaEntity.importType(entity));
// if ( isReactive() ) {
// declaration
// .append('>');
// }
// }
void modifiers(StringBuilder declaration) {
declaration

View File

@ -49,6 +49,7 @@ public abstract class AbstractQueryMethod extends AbstractAnnotatedMethod {
final List<OrderBy> orderBys;
final boolean addNonnullAnnotation;
final boolean dataRepository;
final String fullReturnType;
AbstractQueryMethod(
AnnotationMetaEntity annotationMetaEntity,
@ -61,7 +62,8 @@ public abstract class AbstractQueryMethod extends AbstractAnnotatedMethod {
boolean belongsToDao,
List<OrderBy> orderBys,
boolean addNonnullAnnotation,
boolean dataRepository) {
boolean dataRepository,
String fullReturnType) {
super(annotationMetaEntity, method, sessionName, sessionType);
this.methodName = methodName;
this.paramNames = paramNames;
@ -71,6 +73,7 @@ public abstract class AbstractQueryMethod extends AbstractAnnotatedMethod {
this.orderBys = orderBys;
this.addNonnullAnnotation = addNonnullAnnotation;
this.dataRepository = dataRepository;
this.fullReturnType = fullReturnType;
}
@Override
@ -107,9 +110,9 @@ public abstract class AbstractQueryMethod extends AbstractAnnotatedMethod {
return type.endsWith("...") ? stripped + "..." : stripped;
}
void preamble(StringBuilder declaration, String returnType, List<String> paramTypes) {
void preamble(StringBuilder declaration, List<String> paramTypes) {
declaration
.append(returnType)
.append(annotationMetaEntity.importType(fullReturnType))
.append(" ")
.append(methodName);
parameters( paramTypes, declaration );

View File

@ -593,7 +593,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
}
else {
// For Panache subtypes, we look at the session type, but no DAO, we want static methods
sessionType = getter.getReturnType().toString();
sessionType = fullReturnType(getter);
}
}
else if ( element.getKind() == ElementKind.INTERFACE
@ -702,7 +702,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
* it.
*/
private String addDaoConstructor(@Nullable ExecutableElement method) {
final String sessionType = method == null ? this.sessionType : method.getReturnType().toString();
final String sessionType = method == null ? this.sessionType : fullReturnType(method);
final String sessionVariableName = getSessionVariableName( sessionType );
final String name = method == null ? sessionVariableName : method.getSimpleName().toString();
@ -1110,10 +1110,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
}
private void addQueryMethod(ExecutableElement method) {
final ExecutableType methodType =
(ExecutableType) context.getTypeUtils()
.asMemberOf((DeclaredType) element.asType(), method);
final TypeMirror returnType = methodType.getReturnType();
final TypeMirror returnType = memberMethodType(method).getReturnType();
final TypeKind kind = returnType.getKind();
if ( kind == TypeKind.VOID || kind == TypeKind.ARRAY || kind.isPrimitive() ) {
addQueryMethod( method, returnType, null );
@ -1562,7 +1559,8 @@ public class AnnotationMetaEntity extends AnnotationMeta {
enabledFetchProfiles( method ),
orderByList( method, entity ),
context.addNonnullAnnotation(),
jakartaDataRepository
jakartaDataRepository,
fullReturnType(method)
)
);
}
@ -1608,7 +1606,6 @@ public class AnnotationMetaEntity extends AnnotationMeta {
this, method,
methodName,
entity.getQualifiedName().toString(),
returnType.toString(),
paramNames,
paramTypes,
parameterNullability(method, entity),
@ -1618,7 +1615,8 @@ public class AnnotationMetaEntity extends AnnotationMeta {
sessionType[0],
sessionType[1],
context.addNonnullAnnotation(),
jakartaDataRepository
jakartaDataRepository,
fullReturnType(method)
)
);
}
@ -1806,7 +1804,8 @@ public class AnnotationMetaEntity extends AnnotationMeta {
sessionType[1],
enabledFetchProfiles( method ),
context.addNonnullAnnotation(),
jakartaDataRepository
jakartaDataRepository,
fullReturnType(method)
)
);
}
@ -1829,7 +1828,8 @@ public class AnnotationMetaEntity extends AnnotationMeta {
enabledFetchProfiles( method ),
orderByList( method, entity ),
context.addNonnullAnnotation(),
jakartaDataRepository
jakartaDataRepository,
fullReturnType(method)
)
);
}
@ -1862,7 +1862,8 @@ public class AnnotationMetaEntity extends AnnotationMeta {
sessionType[1],
profiles,
context.addNonnullAnnotation(),
jakartaDataRepository
jakartaDataRepository,
fullReturnType(method)
)
);
break;
@ -1880,7 +1881,8 @@ public class AnnotationMetaEntity extends AnnotationMeta {
sessionType[1],
profiles,
context.addNonnullAnnotation(),
jakartaDataRepository
jakartaDataRepository,
fullReturnType(method)
)
);
break;
@ -1907,7 +1909,8 @@ public class AnnotationMetaEntity extends AnnotationMeta {
profiles,
orderByList( method, entity ),
context.addNonnullAnnotation(),
jakartaDataRepository
jakartaDataRepository,
fullReturnType(method)
)
);
break;
@ -2265,11 +2268,16 @@ public class AnnotationMetaEntity extends AnnotationMeta {
sessionType[1],
orderBys,
context.addNonnullAnnotation(),
jakartaDataRepository
jakartaDataRepository,
fullReturnType(method)
);
putMember( attribute.getPropertyName() + paramTypes, attribute );
}
private String fullReturnType(ExecutableElement method) {
return typeAsString( memberMethodType(method).getReturnType() );
}
private static String returnTypeClass(TypeMirror returnType) {
switch (returnType.getKind()) {
case DECLARED:
@ -2468,7 +2476,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
}
if ( reactive ) {
// for reactive calls, don't use the returnType param, which has been ununi-ed, we want to check the full one
final String returnTypeName = method.getReturnType().toString();
final String returnTypeName = fullReturnType(method);
return returnTypeName.equals( UNI_VOID )
|| returnTypeName.equals( UNI_BOOLEAN )
|| returnTypeName.equals( UNI_INTEGER );
@ -2824,10 +2832,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
private TypeMirror parameterType(VariableElement parameter) {
final ExecutableElement method =
(ExecutableElement) parameter.getEnclosingElement();
final ExecutableType methodType =
(ExecutableType) context.getTypeUtils()
.asMemberOf((DeclaredType) element.asType(), method);
final TypeMirror type = methodType.getParameterTypes()
final TypeMirror type = memberMethodType(method).getParameterTypes()
.get( method.getParameters().indexOf(parameter) );
switch ( type.getKind() ) {
case TYPEVAR:
@ -2844,6 +2849,11 @@ public class AnnotationMetaEntity extends AnnotationMeta {
}
}
private ExecutableType memberMethodType(ExecutableElement method) {
return (ExecutableType) context.getTypeUtils()
.asMemberOf((DeclaredType) element.asType(), method);
}
private static List<Boolean> parameterPatterns(ExecutableElement method) {
return method.getParameters().stream()
.map(param -> hasAnnotation(param, PATTERN))

View File

@ -17,12 +17,11 @@ import static java.util.Collections.emptyList;
public class CriteriaDeleteMethod extends AbstractCriteriaMethod {
private final List<Boolean> paramNullability;
private final String returnType;
CriteriaDeleteMethod(
AnnotationMetaEntity annotationMetaEntity,
ExecutableElement method,
String methodName, String entity, String returnType,
String methodName, String entity,
List<String> paramNames,
List<String> paramTypes,
List<Boolean> paramNullability,
@ -32,11 +31,12 @@ public class CriteriaDeleteMethod extends AbstractCriteriaMethod {
String sessionType,
String sessionName,
boolean addNonnullAnnotation,
boolean dataRepository) {
boolean dataRepository,
String fullReturnType) {
super( annotationMetaEntity, method, methodName, entity, belongsToDao, sessionType, sessionName, emptyList(),
paramNames, paramTypes, emptyList(), addNonnullAnnotation, dataRepository, multivalued, paramPatterns );
paramNames, paramTypes, emptyList(), addNonnullAnnotation, dataRepository, multivalued, paramPatterns,
fullReturnType);
this.paramNullability = paramNullability;
this.returnType = returnType;
}
@Override
@ -49,10 +49,10 @@ public class CriteriaDeleteMethod extends AbstractCriteriaMethod {
return true;
}
@Override
String returnType() {
return returnType;
}
// @Override
// String returnType() {
// return returnType;
// }
@Override
void executeQuery(StringBuilder declaration, List<String> paramTypes) {
@ -64,7 +64,7 @@ public class CriteriaDeleteMethod extends AbstractCriteriaMethod {
void tryReturn(StringBuilder declaration) {
declaration
.append("\n\ttry {\n\t\t");
if ( !"void".equals(returnType) ) {
if ( !"void".equals(fullReturnType) ) {
declaration
.append("return ");
}

View File

@ -7,8 +7,6 @@
package org.hibernate.processor.annotation;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.hibernate.AssertionFailure;
import org.hibernate.processor.util.Constants;
import javax.lang.model.element.ExecutableElement;
import java.util.List;
@ -36,9 +34,11 @@ public class CriteriaFinderMethod extends AbstractCriteriaMethod {
List<String> fetchProfiles,
List<OrderBy> orderBys,
boolean addNonnullAnnotation,
boolean dataRepository) {
boolean dataRepository,
String fullReturnType) {
super( annotationMetaEntity, method, methodName, entity, belongsToDao, sessionType, sessionName, fetchProfiles,
paramNames, paramTypes, orderBys, addNonnullAnnotation, dataRepository, multivalued, paramPatterns );
paramNames, paramTypes, orderBys, addNonnullAnnotation, dataRepository, multivalued, paramPatterns,
fullReturnType);
this.containerType = containerType;
this.paramNullability = paramNullability;
}
@ -100,31 +100,31 @@ public class CriteriaFinderMethod extends AbstractCriteriaMethod {
return "createQuery";
}
@Override
String returnType() {
final StringBuilder type = new StringBuilder();
if ( "[]".equals(containerType) ) {
if ( returnTypeName == null ) {
throw new AssertionFailure("array return type, but no type name");
}
type.append(annotationMetaEntity.importType(returnTypeName)).append("[]");
}
else {
final boolean returnsUni = isReactive() && isUnifiableReturnType(containerType);
if ( returnsUni ) {
type.append(annotationMetaEntity.importType(Constants.UNI)).append('<');
}
if ( containerType != null ) {
type.append(annotationMetaEntity.importType(containerType)).append('<');
}
type.append(annotationMetaEntity.importType(entity));
if ( containerType != null ) {
type.append('>');
}
if ( returnsUni ) {
type.append('>');
}
}
return type.toString();
}
// @Override
// String returnType() {
// final StringBuilder type = new StringBuilder();
// if ( "[]".equals(containerType) ) {
// if ( returnTypeName == null ) {
// throw new AssertionFailure("array return type, but no type name");
// }
// type.append(annotationMetaEntity.importType(returnTypeName)).append("[]");
// }
// else {
// final boolean returnsUni = isReactive() && isUnifiableReturnType(containerType);
// if ( returnsUni ) {
// type.append(annotationMetaEntity.importType(Constants.UNI)).append('<');
// }
// if ( containerType != null ) {
// type.append(annotationMetaEntity.importType(containerType)).append('<');
// }
// type.append(annotationMetaEntity.importType(entity));
// if ( containerType != null ) {
// type.append('>');
// }
// if ( returnsUni ) {
// type.append('>');
// }
// }
// return type.toString();
// }
}

View File

@ -30,9 +30,10 @@ public class IdFinderMethod extends AbstractFinderMethod {
String sessionName,
List<String> fetchProfiles,
boolean addNonnullAnnotation,
boolean dataRepository) {
boolean dataRepository,
String fullReturnType) {
super( annotationMetaEntity, method, methodName, entity, belongsToDao, sessionType, sessionName, fetchProfiles,
paramNames, paramTypes, emptyList(), addNonnullAnnotation, dataRepository );
paramNames, paramTypes, emptyList(), addNonnullAnnotation, dataRepository, fullReturnType );
int idParameter = idParameter(paramNames, paramTypes);
this.paramName = paramNames.get(idParameter);
this.paramType = paramTypes.get(idParameter);
@ -61,7 +62,8 @@ public class IdFinderMethod extends AbstractFinderMethod {
public String getAttributeDeclarationString() {
final StringBuilder declaration = new StringBuilder();
comment( declaration );
preamble( declaration );
modifiers( declaration );
preamble( declaration, paramTypes );
if ( paramName != null && !isPrimitive(paramType) ) {
nullCheck( declaration, paramName );
}

View File

@ -29,9 +29,10 @@ public class NaturalIdFinderMethod extends AbstractFinderMethod {
String sessionName,
List<String> fetchProfiles,
boolean addNonnullAnnotation,
boolean dataRepository) {
boolean dataRepository,
String fullReturnType) {
super( annotationMetaEntity, method, methodName, entity, belongsToDao, sessionType, sessionName, fetchProfiles,
paramNames, paramTypes, emptyList(), addNonnullAnnotation, dataRepository );
paramNames, paramTypes, emptyList(), addNonnullAnnotation, dataRepository, fullReturnType );
this.paramNullability = paramNullability;
}
@ -50,7 +51,8 @@ public class NaturalIdFinderMethod extends AbstractFinderMethod {
public String getAttributeDeclarationString() {
final StringBuilder declaration = new StringBuilder();
comment( declaration );
preamble( declaration );
modifiers( declaration );
preamble( declaration, paramTypes );
tryReturn( declaration );
unwrapSession( declaration );
if ( isReactive() ) {

View File

@ -7,14 +7,12 @@
package org.hibernate.processor.annotation;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.hibernate.AssertionFailure;
import org.hibernate.internal.util.StringHelper;
import javax.lang.model.element.ExecutableElement;
import java.util.List;
import static org.hibernate.processor.util.Constants.QUERY;
import static org.hibernate.processor.util.Constants.UNI;
import static org.hibernate.processor.util.StringUtil.getUpperUnderscoreCaseFromLowerCamelCase;
/**
@ -48,14 +46,16 @@ public class QueryMethod extends AbstractQueryMethod {
String sessionName,
List<OrderBy> orderBys,
boolean addNonnullAnnotation,
boolean dataRepository) {
boolean dataRepository,
String fullReturnType) {
super( annotationMetaEntity, method,
methodName,
paramNames, paramTypes, returnTypeName,
sessionType, sessionName,
belongsToDao, orderBys,
addNonnullAnnotation,
dataRepository );
dataRepository,
fullReturnType );
this.queryString = queryString;
this.returnTypeClass = returnTypeClass;
this.containerType = containerType;
@ -86,15 +86,14 @@ public class QueryMethod extends AbstractQueryMethod {
@Override
public String getAttributeDeclarationString() {
final List<String> paramTypes = parameterTypes();
final String returnType = returnType();
final StringBuilder declaration = new StringBuilder();
comment( declaration );
modifiers( paramTypes, declaration );
preamble( declaration, returnType, paramTypes );
preamble( declaration, paramTypes );
collectOrdering( declaration, paramTypes );
chainSession( declaration );
tryReturn( declaration, paramTypes, containerType );
castResult( declaration, returnType );
castResult( declaration );
createQuery( declaration );
setParameters( declaration, paramTypes, "");
handlePageParameters( declaration, paramTypes, containerType );
@ -142,14 +141,14 @@ public class QueryMethod extends AbstractQueryMethod {
}
}
private void castResult(StringBuilder declaration, String returnType) {
private void castResult(StringBuilder declaration) {
if ( isNative && returnTypeName != null && containerType == null
&& isUsingEntityManager() ) {
// EntityManager.createNativeQuery() does not return TypedQuery,
// so we need to cast to the entity type
declaration
.append("(")
.append(returnType)
.append(fullReturnType)
.append(") ");
}
}
@ -211,34 +210,34 @@ public class QueryMethod extends AbstractQueryMethod {
.append(")\n");
}
private String returnType() {
final StringBuilder type = new StringBuilder();
if ( "[]".equals(containerType) ) {
if ( returnTypeName == null ) {
throw new AssertionFailure("array return type, but no type name");
}
type.append(annotationMetaEntity.importType(returnTypeName)).append("[]");
}
else {
final boolean returnsUni = isReactive() && isUnifiableReturnType(containerType);
if ( returnsUni ) {
type.append(annotationMetaEntity.importType(UNI)).append('<');
}
if ( containerType != null ) {
type.append(annotationMetaEntity.importType(containerType));
if ( returnTypeName != null ) {
type.append("<").append(annotationMetaEntity.importType(returnTypeName)).append(">");
}
}
else if ( returnTypeName != null ) {
type.append(annotationMetaEntity.importType(returnTypeName));
}
if ( returnsUni ) {
type.append('>');
}
}
return type.toString();
}
// private String returnType() {
// final StringBuilder type = new StringBuilder();
// if ( "[]".equals(containerType) ) {
// if ( returnTypeName == null ) {
// throw new AssertionFailure("array return type, but no type name");
// }
// type.append(annotationMetaEntity.importType(returnTypeName)).append("[]");
// }
// else {
// final boolean returnsUni = isReactive() && isUnifiableReturnType(containerType);
// if ( returnsUni ) {
// type.append(annotationMetaEntity.importType(UNI)).append('<');
// }
// if ( containerType != null ) {
// type.append(annotationMetaEntity.importType(containerType));
// if ( returnTypeName != null ) {
// type.append("<").append(annotationMetaEntity.importType(returnTypeName)).append(">");
// }
// }
// else if ( returnTypeName != null ) {
// type.append(annotationMetaEntity.importType(returnTypeName));
// }
// if ( returnsUni ) {
// type.append('>');
// }
// }
// return type.toString();
// }
private void comment(StringBuilder declaration) {
declaration