Skip to content

Commit 599a85d

Browse files
committed
respect @nonnull annotation on @find method parameters
1 parent 4a65c51 commit 599a85d

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

tooling/metamodel-generator/src/jakartaData/java/org/hibernate/processor/test/data/eg/Bookshop.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
package org.hibernate.processor.test.data.eg;
66

7+
import jakarta.annotation.Nonnull;
78
import jakarta.data.repository.CrudRepository;
89
import jakarta.data.repository.Find;
910
import jakarta.data.repository.Query;
@@ -20,6 +21,9 @@ public interface Bookshop extends CrudRepository<Book,String> {
2021
@Transactional
2122
List<Book> byPublisher(String publisher_name);
2223

24+
@Find
25+
List<Book> byTitle(@Nonnull String title);
26+
2327
@Query("select isbn where title like ?1 order by isbn")
2428
String[] ssns(@NotBlank String title);
2529

@@ -29,6 +33,9 @@ public interface Bookshop extends CrudRepository<Book,String> {
2933
@Query("select count(this) where this.title like ?1 order by this.isbn")
3034
long count2(String title);
3135

36+
@Query("select length(text) where title = ?1")
37+
int length(@Nonnull String title);
38+
3239
@Query("select count(this)")
3340
long countAll();
3441

tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AnnotationMetaEntity.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2109,7 +2109,8 @@ private void parameterTypeError(TypeElement entityType, VariableElement param, T
21092109

21102110
private boolean finderParameterNullable(TypeElement entity, VariableElement param) {
21112111
final Element member = memberMatchingPath( entity, parameterName( param ) );
2112-
return member == null || isNullable(member);
2112+
return isNullable( param )
2113+
&& ( member == null || isNullable( member ) );
21132114
}
21142115

21152116
private AccessType getAccessType(TypeElement entity) {
@@ -2932,30 +2933,29 @@ private static boolean isNullable(Element member) {
29322933
return false;
29332934
}
29342935
case FIELD:
2936+
case PARAMETER:
29352937
if ( member.asType().getKind().isPrimitive() ) {
29362938
return false;
29372939
}
29382940
}
2939-
boolean nullable = true;
29402941
for ( AnnotationMirror mirror : member.getAnnotationMirrors() ) {
29412942
final TypeElement annotationType = (TypeElement) mirror.getAnnotationType().asElement();
29422943
final Name name = annotationType.getQualifiedName();
2943-
if ( name.contentEquals(Constants.ID) ) {
2944-
nullable = false;
2945-
}
2946-
if ( name.contentEquals("jakarta.validation.constraints.NotNull")) {
2947-
nullable = false;
2944+
if ( name.contentEquals(Constants.ID)
2945+
|| name.contentEquals(Constants.NOT_NULL)
2946+
|| name.contentEquals(Constants.NONNULL) ) {
2947+
return false;
29482948
}
2949-
if ( name.contentEquals(Constants.BASIC)
2949+
else if ( name.contentEquals(Constants.BASIC)
29502950
|| name.contentEquals(Constants.MANY_TO_ONE)
29512951
|| name.contentEquals(Constants.ONE_TO_ONE)) {
2952-
AnnotationValue optional = getAnnotationValue(mirror, "optional");
2952+
final AnnotationValue optional = getAnnotationValue(mirror, "optional");
29532953
if ( optional != null && optional.getValue().equals(FALSE) ) {
2954-
nullable = false;
2954+
return false;
29552955
}
29562956
}
29572957
}
2958-
return nullable;
2958+
return true;
29592959
}
29602960

29612961
private void checkParameters(

tooling/metamodel-generator/src/main/java/org/hibernate/processor/util/Constants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ public final class Constants {
129129
public static final String STREAM = "java.util.stream.Stream";
130130

131131
public static final String NULLABLE = "jakarta.annotation.Nullable";
132+
public static final String NONNULL = "jakarta.annotation.Nonnull";
133+
public static final String NOT_NULL = "jakarta.validation.constraints.NotNull";
132134

133135
public static final String PANACHE_ORM_REPOSITORY_BASE = "io.quarkus.hibernate.orm.panache.PanacheRepositoryBase";
134136
public static final String PANACHE_ORM_ENTITY_BASE = "io.quarkus.hibernate.orm.panache.PanacheEntityBase";

0 commit comments

Comments
 (0)