improvements to validation of @HQL method return type

This commit is contained in:
Gavin King 2023-09-05 18:36:15 +02:00
parent fad11299f3
commit 14151fdb97
2 changed files with 29 additions and 3 deletions

View File

@ -441,11 +441,11 @@ public class AnnotationMetaEntity extends AnnotationMeta {
private void addQueryMethod(ExecutableElement method) {
final TypeMirror returnType = method.getReturnType();
final TypeKind kind = returnType.getKind();
if ( kind == TypeKind.VOID || kind.isPrimitive() ) {
if ( kind == TypeKind.VOID || kind == TypeKind.ARRAY || kind.isPrimitive() ) {
addQueryMethod( method, returnType, null );
}
else if ( kind == TypeKind.DECLARED ) {
final DeclaredType declaredType = ununi((DeclaredType) returnType);
final DeclaredType declaredType = ununi( (DeclaredType) returnType );
final TypeElement typeElement = (TypeElement) declaredType.asElement();
final List<? extends TypeMirror> typeArguments = declaredType.getTypeArguments();
switch ( typeArguments.size() ) {
@ -1060,8 +1060,16 @@ public class AnnotationMetaEntity extends AnnotationMeta {
}
else {
// TODO: anything more we can do here? e.g. check constructor
try {
final Class<?> javaResultType = selection.getJavaType();
final TypeElement typeElement = context.getTypeElementForFullyQualifiedName( javaResultType.getName() );
returnTypeCorrect = context.getTypeUtils().isAssignable( returnType, typeElement.asType() );
}
catch (Exception e) {
//ignore
returnTypeCorrect = true;
}
}
if ( !returnTypeCorrect ) {
context.message(method, mirror, value,
"return type of query did not match return type '" + returnType + "' of method",

View File

@ -63,6 +63,24 @@ public interface Dao {
// @HQL("from Book where title like :title")
// SelectionQuery<Book> findByTitleWithOrderingByVarargs(String title, Order<? super Book>... order);
@HQL("select count(*) from Book")
long countBooks();
@HQL("select count(*)>1 from Book")
boolean booksExist();
@HQL("delete from Book")
int deleteBooks();
@HQL("select count(*), count(*)>1 from Book")
Object[] funnyQueryReturningArray();
class Record {
Record(Long count, Boolean exists) {}
}
@HQL("select count(*), count(*)>1 from Book")
Record funnyQueryReturningRecord();
@HQL("from Book where isbn = :isbn")
Book findByIsbn(String isbn);