respect @Nonnull annotation on @Find method parameters

This commit is contained in:
Gavin King 2024-10-04 11:32:52 +02:00
parent 4a65c51326
commit 599a85de11
3 changed files with 20 additions and 11 deletions

View File

@ -4,6 +4,7 @@
*/ */
package org.hibernate.processor.test.data.eg; package org.hibernate.processor.test.data.eg;
import jakarta.annotation.Nonnull;
import jakarta.data.repository.CrudRepository; import jakarta.data.repository.CrudRepository;
import jakarta.data.repository.Find; import jakarta.data.repository.Find;
import jakarta.data.repository.Query; import jakarta.data.repository.Query;
@ -20,6 +21,9 @@ public interface Bookshop extends CrudRepository<Book,String> {
@Transactional @Transactional
List<Book> byPublisher(String publisher_name); List<Book> byPublisher(String publisher_name);
@Find
List<Book> byTitle(@Nonnull String title);
@Query("select isbn where title like ?1 order by isbn") @Query("select isbn where title like ?1 order by isbn")
String[] ssns(@NotBlank String title); String[] ssns(@NotBlank String title);
@ -29,6 +33,9 @@ public interface Bookshop extends CrudRepository<Book,String> {
@Query("select count(this) where this.title like ?1 order by this.isbn") @Query("select count(this) where this.title like ?1 order by this.isbn")
long count2(String title); long count2(String title);
@Query("select length(text) where title = ?1")
int length(@Nonnull String title);
@Query("select count(this)") @Query("select count(this)")
long countAll(); long countAll();

View File

@ -2109,7 +2109,8 @@ public class AnnotationMetaEntity extends AnnotationMeta {
private boolean finderParameterNullable(TypeElement entity, VariableElement param) { private boolean finderParameterNullable(TypeElement entity, VariableElement param) {
final Element member = memberMatchingPath( entity, parameterName( 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) { private AccessType getAccessType(TypeElement entity) {
@ -2932,30 +2933,29 @@ public class AnnotationMetaEntity extends AnnotationMeta {
return false; return false;
} }
case FIELD: case FIELD:
case PARAMETER:
if ( member.asType().getKind().isPrimitive() ) { if ( member.asType().getKind().isPrimitive() ) {
return false; return false;
} }
} }
boolean nullable = true;
for ( AnnotationMirror mirror : member.getAnnotationMirrors() ) { for ( AnnotationMirror mirror : member.getAnnotationMirrors() ) {
final TypeElement annotationType = (TypeElement) mirror.getAnnotationType().asElement(); final TypeElement annotationType = (TypeElement) mirror.getAnnotationType().asElement();
final Name name = annotationType.getQualifiedName(); final Name name = annotationType.getQualifiedName();
if ( name.contentEquals(Constants.ID) ) { if ( name.contentEquals(Constants.ID)
nullable = false; || name.contentEquals(Constants.NOT_NULL)
|| name.contentEquals(Constants.NONNULL) ) {
return false;
} }
if ( name.contentEquals("jakarta.validation.constraints.NotNull")) { else if ( name.contentEquals(Constants.BASIC)
nullable = false;
}
if ( name.contentEquals(Constants.BASIC)
|| name.contentEquals(Constants.MANY_TO_ONE) || name.contentEquals(Constants.MANY_TO_ONE)
|| name.contentEquals(Constants.ONE_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) ) { if ( optional != null && optional.getValue().equals(FALSE) ) {
nullable = false; return false;
} }
} }
} }
return nullable; return true;
} }
private void checkParameters( private void checkParameters(

View File

@ -129,6 +129,8 @@ public final class Constants {
public static final String STREAM = "java.util.stream.Stream"; public static final String STREAM = "java.util.stream.Stream";
public static final String NULLABLE = "jakarta.annotation.Nullable"; 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_REPOSITORY_BASE = "io.quarkus.hibernate.orm.panache.PanacheRepositoryBase";
public static final String PANACHE_ORM_ENTITY_BASE = "io.quarkus.hibernate.orm.panache.PanacheEntityBase"; public static final String PANACHE_ORM_ENTITY_BASE = "io.quarkus.hibernate.orm.panache.PanacheEntityBase";