From 599a85de111d85ad85145758d11c2f8307f141ca Mon Sep 17 00:00:00 2001 From: Gavin King Date: Fri, 4 Oct 2024 11:32:52 +0200 Subject: [PATCH] respect @Nonnull annotation on @Find method parameters --- .../processor/test/data/eg/Bookshop.java | 7 ++++++ .../annotation/AnnotationMetaEntity.java | 22 +++++++++---------- .../hibernate/processor/util/Constants.java | 2 ++ 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/tooling/metamodel-generator/src/jakartaData/java/org/hibernate/processor/test/data/eg/Bookshop.java b/tooling/metamodel-generator/src/jakartaData/java/org/hibernate/processor/test/data/eg/Bookshop.java index 3a9a56b321..43a266edf0 100644 --- a/tooling/metamodel-generator/src/jakartaData/java/org/hibernate/processor/test/data/eg/Bookshop.java +++ b/tooling/metamodel-generator/src/jakartaData/java/org/hibernate/processor/test/data/eg/Bookshop.java @@ -4,6 +4,7 @@ */ package org.hibernate.processor.test.data.eg; +import jakarta.annotation.Nonnull; import jakarta.data.repository.CrudRepository; import jakarta.data.repository.Find; import jakarta.data.repository.Query; @@ -20,6 +21,9 @@ public interface Bookshop extends CrudRepository { @Transactional List byPublisher(String publisher_name); + @Find + List byTitle(@Nonnull String title); + @Query("select isbn where title like ?1 order by isbn") String[] ssns(@NotBlank String title); @@ -29,6 +33,9 @@ public interface Bookshop extends CrudRepository { @Query("select count(this) where this.title like ?1 order by this.isbn") long count2(String title); + @Query("select length(text) where title = ?1") + int length(@Nonnull String title); + @Query("select count(this)") long countAll(); diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AnnotationMetaEntity.java b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AnnotationMetaEntity.java index 4ee6ef5dda..5e2511bc01 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AnnotationMetaEntity.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AnnotationMetaEntity.java @@ -2109,7 +2109,8 @@ public class AnnotationMetaEntity extends AnnotationMeta { private boolean finderParameterNullable(TypeElement entity, VariableElement param) { final Element member = memberMatchingPath( entity, parameterName( param ) ); - return member == null || isNullable(member); + return isNullable( param ) + && ( member == null || isNullable( member ) ); } private AccessType getAccessType(TypeElement entity) { @@ -2932,30 +2933,29 @@ public class AnnotationMetaEntity extends AnnotationMeta { return false; } case FIELD: + case PARAMETER: if ( member.asType().getKind().isPrimitive() ) { return false; } } - boolean nullable = true; for ( AnnotationMirror mirror : member.getAnnotationMirrors() ) { final TypeElement annotationType = (TypeElement) mirror.getAnnotationType().asElement(); final Name name = annotationType.getQualifiedName(); - if ( name.contentEquals(Constants.ID) ) { - nullable = false; + if ( name.contentEquals(Constants.ID) + || name.contentEquals(Constants.NOT_NULL) + || name.contentEquals(Constants.NONNULL) ) { + return false; } - if ( name.contentEquals("jakarta.validation.constraints.NotNull")) { - nullable = false; - } - if ( name.contentEquals(Constants.BASIC) + else if ( name.contentEquals(Constants.BASIC) || name.contentEquals(Constants.MANY_TO_ONE) || name.contentEquals(Constants.ONE_TO_ONE)) { - AnnotationValue optional = getAnnotationValue(mirror, "optional"); + final AnnotationValue optional = getAnnotationValue(mirror, "optional"); if ( optional != null && optional.getValue().equals(FALSE) ) { - nullable = false; + return false; } } } - return nullable; + return true; } private void checkParameters( diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/util/Constants.java b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/util/Constants.java index 2a4bd1ade2..9cd84c04fb 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/util/Constants.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/util/Constants.java @@ -129,6 +129,8 @@ public final class Constants { public static final String STREAM = "java.util.stream.Stream"; public static final String NULLABLE = "jakarta.annotation.Nullable"; + public static final String NONNULL = "jakarta.annotation.Nonnull"; + public static final String NOT_NULL = "jakarta.validation.constraints.NotNull"; public static final String PANACHE_ORM_REPOSITORY_BASE = "io.quarkus.hibernate.orm.panache.PanacheRepositoryBase"; public static final String PANACHE_ORM_ENTITY_BASE = "io.quarkus.hibernate.orm.panache.PanacheEntityBase";