Skip to content

Commit

Permalink
Add primitive type support to Nullness
Browse files Browse the repository at this point in the history
  • Loading branch information
sdeleuze committed Jan 21, 2025
1 parent d83be7c commit 1115be5
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 6 deletions.
5 changes: 3 additions & 2 deletions framework-docs/modules/ROOT/pages/core/null-safety.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ annotations such as IntelliJ IDEA or Eclipse) and Kotlin where JSpecify annotati
{kotlin-docs}/null-safety.html[Kotlin's null safety].

The {spring-framework-api}/core/Nullness.html[`Nullness` Spring API] can be used at runtime to detect the nullness of a
type usage, a field, a method return type or a parameter. It provides full support for JSpecify annotations and
Kotlin null safety, as well as a pragmatic check on any `@Nullable` annotation (regardless of the package).
type usage, a field, a method return type or a parameter. It provides full support for JSpecify annotations,
Kotlin null safety, Java primitive types, as well as a pragmatic check on any `@Nullable` annotation (regardless of the
package).

[[null-safety-libraries]]
== Annotating libraries with JSpecify annotations
Expand Down
12 changes: 8 additions & 4 deletions spring-core/src/main/java/org/springframework/core/Nullness.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,17 @@
*
* <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> and {@code @Nullable} annotations
* regardless of their package (from Spring, JSR-305 or Jakarta set of annotations for example).
* <a href="https://kotlinlang.org/docs/null-safety.html">Kotlin null safety</a>, {@code @Nullable} annotations
* regardless of their package (from Spring, JSR-305 or Jakarta set of annotations for example) and Java primitive
* types.
*
* @author Sebastien Deleuze
* @since 7.0
*/
public enum Nullness {

/**
* Unspecified nullness (Java and JSpecify {@code @NullUnmarked} defaults).
* Unspecified nullness (Java default for non-primitive types and JSpecify {@code @NullUnmarked} code).
*/
UNSPECIFIED,

Expand All @@ -60,7 +61,7 @@ public enum Nullness {
NULLABLE,

/**
* Will not include null (Kotlin and JSpecify {@code @NullMarked} defaults).
* Will not include null (Kotlin default and JSpecify {@code @NullMarked} code).
*/
NON_NULL;

Expand Down Expand Up @@ -130,6 +131,9 @@ private static boolean hasNullableAnnotation(AnnotatedElement element) {
}

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);
}
if (annotatedType.isAnnotationPresent(Nullable.class)) {
return Nullness.NULLABLE;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,4 +377,20 @@ void customNullableField() throws NoSuchFieldException {
Assertions.assertThat(nullness).isEqualTo(Nullness.NULLABLE);
}

// Primitive types

@Test
void primitiveField() throws NoSuchFieldException {
var field = NullnessFields.class.getDeclaredField("primitiveField");
var nullness = Nullness.forField(field);
Assertions.assertThat(nullness).isEqualTo(Nullness.NON_NULL);
}

@Test
void voidMethod() throws NoSuchMethodException {
var method = JSpecifyProcessor.class.getMethod("voidProcess");
var nullness = Nullness.forMethodReturnType(method);
Assertions.assertThat(nullness).isEqualTo(Nullness.UNSPECIFIED);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ public interface JSpecifyProcessor {

@NullMarked
@NonNull String nonNullMarkedProcess();

void voidProcess();
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ public class NullnessFields {

@org.springframework.core.testfixture.nullness.custom.Nullable
public String customNullableField;

public int primitiveField = 0;
}

0 comments on commit 1115be5

Please sign in to comment.