Browse Source

Refine NullableUtils deprecations.

See #3305
Original pull request: #3307
pull/3314/head
Mark Paluch 6 months ago
parent
commit
c65f1656d0
No known key found for this signature in database
GPG Key ID: 55BC6374BAA9D973
  1. 2
      src/main/java/org/springframework/data/repository/core/support/MethodInvocationValidator.java
  2. 11
      src/main/java/org/springframework/data/util/NullableUtils.java
  3. 16
      src/main/java/org/springframework/data/util/NullnessMethodInvocationValidator.java
  4. 2
      src/main/kotlin/org/springframework/data/repository/kotlin/package-info.java

2
src/main/java/org/springframework/data/repository/core/support/MethodInvocationValidator.java

@ -29,8 +29,8 @@ import org.springframework.data.util.NullnessMethodInvocationValidator; @@ -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 {

11
src/main/java/org/springframework/data/util/NullableUtils.java

@ -42,18 +42,18 @@ import org.springframework.util.MultiValueMap; @@ -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; @@ -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";

16
src/main/java/org/springframework/data/util/NullnessMethodInvocationValidator.java

@ -46,8 +46,8 @@ import org.springframework.util.ObjectUtils; @@ -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 { @@ -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 { @@ -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();
}

2
src/main/kotlin/org/springframework/data/repository/kotlin/package-info.java

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

Loading…
Cancel
Save