Skip to content

Commit 8682404

Browse files
committed
respect @nonnull annotation on @find method parameters
1 parent dec1eae commit 8682404

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.hibernate.processor.test.data.eg;
22

3+
import jakarta.annotation.Nonnull;
34
import jakarta.data.repository.CrudRepository;
45
import jakarta.data.repository.Find;
56
import jakarta.data.repository.Query;
@@ -16,17 +17,23 @@ public interface Bookshop extends CrudRepository<Book,String> {
1617
@Transactional
1718
List<Book> byPublisher(String publisher_name);
1819

20+
@Find
21+
List<Book> byTitle(@Nonnull String title);
22+
1923
@Query("select isbn where title like ?1 order by isbn")
20-
String[] ssns(@NotBlank String title);
24+
String[] ssns(@NotBlank String title);
2125

2226
@Query("select count(this) where title like ?1 order by isbn")
2327
long count1(@NotNull String title);
2428

2529
@Query("select count(this) where this.title like ?1 order by this.isbn")
2630
long count2(String title);
2731

32+
@Query("select length(text) where title = ?1")
33+
int length(@Nonnull String title);
34+
2835
@Query("select count(this)")
29-
long countAll();
36+
long countAll();
3037

3138
@Query("where isbn in :isbns and type = Book")
3239
List<Book> books(List<String> isbns);

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
@@ -2131,7 +2131,8 @@ private void parameterTypeError(TypeElement entityType, VariableElement param, T
21312131

21322132
private boolean finderParameterNullable(TypeElement entity, VariableElement param) {
21332133
final Element member = memberMatchingPath( entity, parameterName( param ) );
2134-
return member == null || isNullable(member);
2134+
return isNullable( param )
2135+
&& ( member == null || isNullable( member ) );
21352136
}
21362137

21372138
private AccessType getAccessType(TypeElement entity) {
@@ -2984,30 +2985,29 @@ private static boolean isNullable(Element member) {
29842985
return false;
29852986
}
29862987
case FIELD:
2988+
case PARAMETER:
29872989
if ( member.asType().getKind().isPrimitive() ) {
29882990
return false;
29892991
}
29902992
}
2991-
boolean nullable = true;
29922993
for ( AnnotationMirror mirror : member.getAnnotationMirrors() ) {
29932994
final TypeElement annotationType = (TypeElement) mirror.getAnnotationType().asElement();
29942995
final Name name = annotationType.getQualifiedName();
2995-
if ( name.contentEquals(Constants.ID) ) {
2996-
nullable = false;
2997-
}
2998-
if ( name.contentEquals("jakarta.validation.constraints.NotNull")) {
2999-
nullable = false;
2996+
if ( name.contentEquals(Constants.ID)
2997+
|| name.contentEquals(Constants.NOT_NULL)
2998+
|| name.contentEquals(Constants.NONNULL) ) {
2999+
return false;
30003000
}
3001-
if ( name.contentEquals(Constants.BASIC)
3001+
else if ( name.contentEquals(Constants.BASIC)
30023002
|| name.contentEquals(Constants.MANY_TO_ONE)
30033003
|| name.contentEquals(Constants.ONE_TO_ONE)) {
3004-
AnnotationValue optional = getAnnotationValue(mirror, "optional");
3004+
final AnnotationValue optional = getAnnotationValue(mirror, "optional");
30053005
if ( optional != null && optional.getValue().equals(FALSE) ) {
3006-
nullable = false;
3006+
return false;
30073007
}
30083008
}
30093009
}
3010-
return nullable;
3010+
return true;
30113011
}
30123012

30133013
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
@@ -131,6 +131,8 @@ public final class Constants {
131131
public static final String STREAM = "java.util.stream.Stream";
132132

133133
public static final String NULLABLE = "jakarta.annotation.Nullable";
134+
public static final String NONNULL = "jakarta.annotation.Nonnull";
135+
public static final String NOT_NULL = "jakarta.validation.constraints.NotNull";
134136

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

0 commit comments

Comments
 (0)