HHH-17772 allow array as return type for @Find methods

as required by Jakarta Data
This commit is contained in:
Gavin King 2024-03-01 12:02:26 +01:00
parent 25d0899f28
commit 7ad39a86e9
3 changed files with 75 additions and 19 deletions

View File

@ -572,6 +572,17 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
} }
else { else {
switch (containerType) { switch (containerType) {
case "[]":
if ( returnTypeName== null ) {
throw new AssertionFailure("missing return type");
}
else {
declaration
.append(".getResultList()\n\t\t\t.toArray(new ")
.append(annotationMetaEntity.importType(returnTypeName))
.append("[0]);");
}
break;
case OPTIONAL: case OPTIONAL:
declaration declaration
.append(".uniqueResultOptional();"); .append(".uniqueResultOptional();");

View File

@ -789,12 +789,34 @@ public class AnnotationMetaEntity extends AnnotationMeta {
ExecutableElement method, ExecutableElement method,
@Nullable TypeMirror returnType, @Nullable TypeMirror returnType,
@Nullable TypeElement containerType) { @Nullable TypeElement containerType) {
if ( returnType == null || returnType.getKind() != TypeKind.DECLARED ) { if ( returnType == null ) {
context.message( method, context.message( method,
"incorrect return type '" + returnType + "' is not an entity type", "missing return type",
Diagnostic.Kind.ERROR ); Diagnostic.Kind.ERROR );
} }
else { else if ( returnType.getKind() == TypeKind.ARRAY ) {
final ArrayType arrayType = (ArrayType) returnType;
final TypeMirror componentType = arrayType.getComponentType();
if ( componentType.getKind() != TypeKind.DECLARED ) {
context.message( method,
"incorrect return type '" + returnType + "' is not an array with entity elements",
Diagnostic.Kind.ERROR );
}
else {
final DeclaredType declaredType = (DeclaredType) componentType;
final TypeElement entity = (TypeElement) declaredType.asElement();
if ( !containsAnnotation( entity, Constants.ENTITY ) ) {
context.message( method,
"incorrect return type '" + returnType + "' is not annotated '@Entity'",
Diagnostic.Kind.ERROR );
}
else {
// multiple results, it has to be a criteria finder
createCriteriaFinder( method, returnType, containerType, entity );
}
}
}
else if ( returnType.getKind() == TypeKind.DECLARED ) {
final DeclaredType declaredType = ununi( (DeclaredType) returnType ); final DeclaredType declaredType = ununi( (DeclaredType) returnType );
final TypeElement entity = (TypeElement) declaredType.asElement(); final TypeElement entity = (TypeElement) declaredType.asElement();
if ( !containsAnnotation( entity, Constants.ENTITY ) ) { if ( !containsAnnotation( entity, Constants.ENTITY ) ) {
@ -834,6 +856,11 @@ public class AnnotationMetaEntity extends AnnotationMeta {
} }
} }
} }
else {
context.message( method,
"incorrect return type '" + returnType + "' is not an entity type",
Diagnostic.Kind.ERROR );
}
} }
/** /**
@ -866,12 +893,25 @@ public class AnnotationMetaEntity extends AnnotationMeta {
} }
} }
} }
// TODO: this is ugly, do something better
// higher up in the call chain
final String containerTypeName;
final String entityTypeName;
if ( returnType.getKind() == TypeKind.ARRAY ) {
final ArrayType arrayType = (ArrayType) returnType;
entityTypeName = arrayType.getComponentType().toString();
containerTypeName = "[]";
}
else {
entityTypeName = returnType.toString();
containerTypeName = containerType == null ? null : containerType.toString();
}
putMember( methodKey, putMember( methodKey,
new CriteriaFinderMethod( new CriteriaFinderMethod(
this, this,
methodName, methodName,
returnType.toString(), entityTypeName,
containerType == null ? null : containerType.toString(), containerTypeName,
paramNames, paramNames,
paramTypes, paramTypes,
parameterNullability(method, entity), parameterNullability(method, entity),

View File

@ -213,21 +213,26 @@ public class CriteriaFinderMethod extends AbstractFinderMethod {
} }
private StringBuilder returnType() { private StringBuilder returnType() {
StringBuilder type = new StringBuilder(); final StringBuilder type = new StringBuilder();
boolean returnsUni = isReactive() if ( "[]".equals(containerType) ) {
&& (containerType == null || LIST.equals(containerType)); type.append(returnTypeName).append("[]");
if ( returnsUni ) {
type.append(annotationMetaEntity.importType(Constants.UNI)).append('<');
} }
if ( containerType != null ) { else {
type.append(annotationMetaEntity.importType(containerType)).append('<'); boolean returnsUni = isReactive()
} && (containerType == null || LIST.equals(containerType));
type.append(annotationMetaEntity.importType(entity)); if ( returnsUni ) {
if ( containerType != null ) { type.append(annotationMetaEntity.importType(Constants.UNI)).append('<');
type.append('>'); }
} if ( containerType != null ) {
if ( returnsUni ) { type.append(annotationMetaEntity.importType(containerType)).append('<');
type.append('>'); }
type.append(annotationMetaEntity.importType(entity));
if ( containerType != null ) {
type.append('>');
}
if ( returnsUni ) {
type.append('>');
}
} }
return type; return type;
} }