Skip to content

Commit

Permalink
Add "not null" annotations to KiwiResources#verifyExistence methods
Browse files Browse the repository at this point in the history
Make it clear to all code analysis tools that the return value
of the verifyExistence methods that accept an Optional can
never return null; they either return a value or throw an exception.

Closes #1167
  • Loading branch information
sleberknight committed Aug 9, 2024
1 parent 0e4f3d8 commit 53f7e47
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/main/java/org/kiwiproject/jaxrs/KiwiResources.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import jakarta.ws.rs.core.Response;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.kiwiproject.base.KiwiStrings;
import org.kiwiproject.jaxrs.exception.JaxrsBadRequestException;
import org.kiwiproject.jaxrs.exception.JaxrsNotFoundException;
Expand Down Expand Up @@ -67,6 +68,7 @@ public static <T> void verifyExistence(T resourceEntity) {
* @return the entity if the Optional contains a value
* @throws JaxrsNotFoundException if the entity is empty
*/
@NonNull
public static <T> T verifyExistence(Optional<T> resourceEntity) {
verifyExistence(resourceEntity.orElse(null), null);

Expand Down Expand Up @@ -97,6 +99,7 @@ public static <T> void verifyExistence(T resourceEntity, Class<T> entityType, Ob
* @return the entity if the Optional contains a value
* @throws JaxrsNotFoundException if the entity is empty
*/
@NonNull
public static <T> T verifyExistence(Optional<T> resourceEntity, Class<T> entityType, Object identifier) {
var notFoundMessage = JaxrsNotFoundException.buildMessage(entityType.getSimpleName(), identifier);
verifyExistence(resourceEntity.orElse(null), notFoundMessage);
Expand Down Expand Up @@ -145,6 +148,7 @@ public static <T> void verifyExistence(T resourceEntity, String notFoundMessageT
* @return the entity if the Optional contains a value
* @throws JaxrsNotFoundException if the entity is empty
*/
@NonNull
public static <T> T verifyExistence(Optional<T> resourceEntity, String notFoundMessage) {
verifyExistence(resourceEntity.orElse(null), notFoundMessage);

Expand All @@ -163,6 +167,7 @@ public static <T> T verifyExistence(Optional<T> resourceEntity, String notFoundM
* @return the entity if the Optional contains a value
* @throws JaxrsNotFoundException if the entity is empty
*/
@NonNull
public static <T> T verifyExistence(Optional<T> resourceEntity, String notFoundMessageTemplate, Object... args) {
verifyExistence(resourceEntity.orElse(null), notFoundMessageTemplate, args);

Expand Down

0 comments on commit 53f7e47

Please sign in to comment.