Skip to content

Commit

Permalink
Polish Nullness
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Jan 23, 2025
1 parent 564eb77 commit 35fede1
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions spring-core/src/main/java/org/springframework/core/Nullness.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,29 @@
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.
*
* <p>The nullness applies to a type usage, a field, a method return type or a parameter.
* <a href="https://jspecify.dev/docs/user-guide/">JSpecify annotations</a> are fully supported, as well as
* <a href="https://kotlinlang.org/docs/null-safety.html">Kotlin null safety</a>, {@code @Nullable} annotations
* regardless of their package and Java primitive types.
* <p>Nullness applies to type usage, a field, a method return type, or a parameter.
* <a href="https://jspecify.dev/docs/user-guide/">JSpecify annotations</a> are
* fully supported, as well as
* <a href="https://kotlinlang.org/docs/null-safety.html">Kotlin null safety</a>,
* {@code @Nullable} annotations regardless of their package, and Java primitive
* types.
*
* <p>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.
* <p>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
*/
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,

Expand All @@ -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
*/
Expand Down Expand Up @@ -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())) {
Expand All @@ -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);
}
Expand Down Expand Up @@ -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()) {
Expand Down

0 comments on commit 35fede1

Please sign in to comment.