From 35fede1382f19e997e761ea18bdc99394bdae745 Mon Sep 17 00:00:00 2001 From: Sam Brannen <104798+sbrannen@users.noreply.github.com> Date: Thu, 23 Jan 2025 11:16:00 +0100 Subject: [PATCH] Polish Nullness --- .../org/springframework/core/Nullness.java | 33 +++++++++++-------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/Nullness.java b/spring-core/src/main/java/org/springframework/core/Nullness.java index 5e4cac0ba15d..2c0f2f3036dc 100644 --- a/spring-core/src/main/java/org/springframework/core/Nullness.java +++ b/spring-core/src/main/java/org/springframework/core/Nullness.java @@ -37,16 +37,20 @@ import org.jspecify.annotations.Nullable; /** - * Constants that indicate the nullness, as well as related utility methods. + * Constants that indicate nullness, as well as related utility methods. * - *
The nullness applies to a type usage, a field, a method return type or a parameter. - * JSpecify annotations are fully supported, as well as - * Kotlin null safety, {@code @Nullable} annotations - * regardless of their package and Java primitive types. + *
Nullness applies to type usage, a field, a method return type, or a parameter. + * JSpecify annotations are + * fully supported, as well as + * Kotlin null safety, + * {@code @Nullable} annotations regardless of their package, and Java primitive + * types. * - *
JSR-305 annotations as well as Spring ones in the {@code org.springframework.lang} packages such as - * {@code @NonNullApi}, {@code @NonNullFields} and {@code @NonNull} are not supported by this API, except - * {@code @Nullable} which is supported via the package-less check. Migrating to JSpecify is recommended. + *
JSR-305 annotations as well as Spring null safety annotations in the + * {@code org.springframework.lang} package such as {@code @NonNullApi}, + * {@code @NonNullFields}, and {@code @NonNull} are not supported by this API. + * However, {@code @Nullable} is supported via the package-less check. Migrating + * to JSpecify is recommended. * * @author Sebastien Deleuze * @since 7.0 @@ -54,7 +58,8 @@ public enum Nullness { /** - * Unspecified nullness (Java default for non-primitive types and JSpecify {@code @NullUnmarked} code). + * Unspecified nullness (Java default for non-primitive types and JSpecify + * {@code @NullUnmarked} code). */ UNSPECIFIED, @@ -70,7 +75,7 @@ public enum Nullness { /** - * Return the nullness of the given method return type. + * Return the nullness of the return type for the given method. * @param method the source for the method return type * @return the corresponding nullness */ @@ -123,7 +128,8 @@ public static Nullness forField(Field field) { } - // Check method and parameter level @Nullable annotations regardless of the package (including Spring and JSR 305 annotations) + // Check method and parameter level @Nullable annotations regardless of the package + // (including Spring and JSR 305 annotations) private static boolean hasNullableAnnotation(AnnotatedElement element) { for (Annotation annotation : element.getDeclaredAnnotations()) { if ("Nullable".equals(annotation.annotationType().getSimpleName())) { @@ -133,7 +139,9 @@ private static boolean hasNullableAnnotation(AnnotatedElement element) { return false; } - private static Nullness jSpecifyNullness(AnnotatedElement annotatedElement, Class> declaringClass, AnnotatedType annotatedType) { + private static Nullness jSpecifyNullness( + AnnotatedElement annotatedElement, Class> declaringClass, AnnotatedType annotatedType) { + if (annotatedType.getType() instanceof Class> clazz && clazz.isPrimitive()) { return (clazz != void.class ? Nullness.NON_NULL : Nullness.UNSPECIFIED); } @@ -171,7 +179,6 @@ else if (annotatedElement.isAnnotationPresent(NullUnmarked.class)) { */ private static class KotlinDelegate { - public static Nullness forMethodReturnType(Method method) { KFunction> function = ReflectJvmMapping.getKotlinFunction(method); if (function != null && function.getReturnType().isMarkedNullable()) {