respect @Nonnull annotation on @Find method parameters

This commit is contained in:
Gavin King 2024-10-04 11:32:52 +02:00
parent dec1eae54a
commit 86824040ec
3 changed files with 22 additions and 13 deletions

View File

@ -1,5 +1,6 @@
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;
@ -16,8 +17,11 @@ public interface Bookshop extends CrudRepository<Book,String> {
@Transactional
List<Book> byPublisher(String publisher_name);
@Find
List<Book> byTitle(@Nonnull String title);
@Query("select isbn where title like ?1 order by isbn")
String[] ssns(@NotBlank String title);
String[] ssns(@NotBlank String title);
@Query("select count(this) where title like ?1 order by isbn")
long count1(@NotNull String title);
@ -25,8 +29,11 @@ public interface Bookshop extends CrudRepository<Book,String> {
@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();
long countAll();
@Query("where isbn in :isbns and type = Book")
List<Book> books(List<String> isbns);

View File

@ -2131,7 +2131,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) {
@ -2984,30 +2985,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(

View File

@ -131,6 +131,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";