Refine NullableUtils deprecations.

See #3305
Original pull request: #3307
This commit is contained in:
Mark Paluch
2025-06-12 11:20:28 +02:00
parent cb6793fb52
commit c65f1656d0
4 changed files with 17 additions and 14 deletions

View File

@@ -29,8 +29,8 @@ import org.springframework.data.util.NullnessMethodInvocationValidator;
* @author Christoph Strobl
* @since 2.0
* @see org.jspecify.annotations.NonNull
* @see org.springframework.core.Nullness
* @see org.springframework.data.util.ReflectionUtils#isNullable(org.springframework.core.MethodParameter)
* @see org.springframework.data.util.NullableUtils
*/
public class MethodInvocationValidator extends NullnessMethodInvocationValidator {

View File

@@ -42,18 +42,18 @@ import org.springframework.util.MultiValueMap;
/**
* Utility methods to introspect nullability rules declared in packages, classes and methods.
* <p>
* Nullability rules are declared using {@link NonNullApi}, {@link Nullable}, and JSR-305
* Nullability rules are declared using {@code Nullable}, {@code NonNullApi}, and JSR-305
* {@code javax.annotation.Nonnull} annotations. By default (no annotation use), a package and its types are considered
* allowing {@literal null} values in return values and method parameters. Nullability rules are expressed by annotating
* a package with a JSR-305 meta annotation such as Spring's {@link NonNullApi}. All types of the package inherit the
* package rule. Subpackages do not inherit nullability rules and must be annotated themself.
* a package with a JSR-305 meta annotation such as Spring's {@code NonNullApi}. All types of the package inherit the
* package rule. Subpackages do not inherit nullability rules and must be annotated themselves.
*
* <pre class="code">
* &#64;org.jspecify.annotations.NullMarked
* package com.example;
* </pre>
*
* {@link Nullable} selectively permits {@literal null} values for method return values or method parameters by
* {@code Nullable} selectively permits {@literal null} values for method return values or method parameters by
* annotating the method respectively the parameters:
*
* <pre class="code">
@@ -77,7 +77,10 @@ import org.springframework.util.MultiValueMap;
* @since 2.0
* @see NonNullApi
* @see Nullable
* @deprecated since 4.0 in favor of {@link org.springframework.core.Nullness} fully using JSpecify annotations instead
* of Spring Framework's own {@literal @Nullable} and {@literal @NonNullApi} annotations.
*/
@Deprecated(since = "4.0")
public abstract class NullableUtils {
private static final String NON_NULL_CLASS_NAME = "javax.annotation.Nonnull";

View File

@@ -46,8 +46,8 @@ import org.springframework.util.ObjectUtils;
* @author Christoph Strobl
* @since 3.5
* @see org.jspecify.annotations.NonNull
* @see org.springframework.core.Nullness
* @see ReflectionUtils#isNullable(MethodParameter)
* @see NullableUtils
* @link <a href="https://www.thedictionaryofobscuresorrows.com/word/nullness">Nullness</a>
*/
public class NullnessMethodInvocationValidator implements MethodInterceptor {
@@ -63,14 +63,12 @@ public class NullnessMethodInvocationValidator implements MethodInterceptor {
*/
public static boolean supports(Class<?> type) {
if (type.getPackage() != null
&& type.getPackage().isAnnotationPresent(NullMarked.class)) {
if (type.getPackage() != null && type.getPackage().isAnnotationPresent(NullMarked.class)) {
return true;
}
return KotlinDetector.isKotlinPresent() && KotlinReflectionUtils.isSupportedKotlinClass(type)
|| NullableUtils.isNonNull(type, ElementType.METHOD)
|| NullableUtils.isNonNull(type, ElementType.PARAMETER);
|| NullableUtils.isNonNull(type, ElementType.METHOD) || NullableUtils.isNonNull(type, ElementType.PARAMETER);
}
@Override
@@ -195,17 +193,19 @@ public class NullnessMethodInvocationValidator implements MethodInterceptor {
/**
* Check method return nullability
*
* @param method
* @return
*/
private static boolean allowNullableReturn(@Nullable Method method) {
if(method == null) {
if (method == null) {
return false;
}
KFunction<?> function = KotlinDetector.isKotlinType(method.getDeclaringClass()) ?
KotlinReflectionUtils.findKotlinFunction(method) : null;
KFunction<?> function = KotlinDetector.isKotlinType(method.getDeclaringClass())
? KotlinReflectionUtils.findKotlinFunction(method)
: null;
return function != null && function.getReturnType().isMarkedNullable();
}

View File

@@ -1,5 +1,5 @@
/**
* Support for Kotlin Coroutines repositories.
*/
@org.springframework.lang.NonNullApi
@org.jspecify.annotations.NullMarked
package org.springframework.data.repository.kotlin;