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;
* @author Christoph Strobl * @author Christoph Strobl
* @since 2.0 * @since 2.0
* @see org.jspecify.annotations.NonNull * @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.ReflectionUtils#isNullable(org.springframework.core.MethodParameter)
* @see org.springframework.data.util.NullableUtils
*/ */
public class MethodInvocationValidator extends NullnessMethodInvocationValidator { public class MethodInvocationValidator extends NullnessMethodInvocationValidator {

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

@ -42,18 +42,18 @@ import org.springframework.util.MultiValueMap;
/** /**
* Utility methods to introspect nullability rules declared in packages, classes and methods. * Utility methods to introspect nullability rules declared in packages, classes and methods.
* <p> * <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 * {@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 * 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 * 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 themself. * package rule. Subpackages do not inherit nullability rules and must be annotated themselves.
* *
* <pre class="code"> * <pre class="code">
* &#64;org.jspecify.annotations.NullMarked * &#64;org.jspecify.annotations.NullMarked
* package com.example; * package com.example;
* </pre> * </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: * annotating the method respectively the parameters:
* *
* <pre class="code"> * <pre class="code">
@ -77,7 +77,10 @@ import org.springframework.util.MultiValueMap;
* @since 2.0 * @since 2.0
* @see NonNullApi * @see NonNullApi
* @see Nullable * @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 { public abstract class NullableUtils {
private static final String NON_NULL_CLASS_NAME = "javax.annotation.Nonnull"; 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;
* @author Christoph Strobl * @author Christoph Strobl
* @since 3.5 * @since 3.5
* @see org.jspecify.annotations.NonNull * @see org.jspecify.annotations.NonNull
* @see org.springframework.core.Nullness
* @see ReflectionUtils#isNullable(MethodParameter) * @see ReflectionUtils#isNullable(MethodParameter)
* @see NullableUtils
* @link <a href="https://www.thedictionaryofobscuresorrows.com/word/nullness">Nullness</a> * @link <a href="https://www.thedictionaryofobscuresorrows.com/word/nullness">Nullness</a>
*/ */
public class NullnessMethodInvocationValidator implements MethodInterceptor { public class NullnessMethodInvocationValidator implements MethodInterceptor {
@ -63,14 +63,12 @@ public class NullnessMethodInvocationValidator implements MethodInterceptor {
*/ */
public static boolean supports(Class<?> type) { public static boolean supports(Class<?> type) {
if (type.getPackage() != null if (type.getPackage() != null && type.getPackage().isAnnotationPresent(NullMarked.class)) {
&& type.getPackage().isAnnotationPresent(NullMarked.class)) {
return true; return true;
} }
return KotlinDetector.isKotlinPresent() && KotlinReflectionUtils.isSupportedKotlinClass(type) return KotlinDetector.isKotlinPresent() && KotlinReflectionUtils.isSupportedKotlinClass(type)
|| NullableUtils.isNonNull(type, ElementType.METHOD) || NullableUtils.isNonNull(type, ElementType.METHOD) || NullableUtils.isNonNull(type, ElementType.PARAMETER);
|| NullableUtils.isNonNull(type, ElementType.PARAMETER);
} }
@Override @Override
@ -195,17 +193,19 @@ public class NullnessMethodInvocationValidator implements MethodInterceptor {
/** /**
* Check method return nullability * Check method return nullability
*
* @param method * @param method
* @return * @return
*/ */
private static boolean allowNullableReturn(@Nullable Method method) { private static boolean allowNullableReturn(@Nullable Method method) {
if(method == null) { if (method == null) {
return false; return false;
} }
KFunction<?> function = KotlinDetector.isKotlinType(method.getDeclaringClass()) ? KFunction<?> function = KotlinDetector.isKotlinType(method.getDeclaringClass())
KotlinReflectionUtils.findKotlinFunction(method) : null; ? KotlinReflectionUtils.findKotlinFunction(method)
: null;
return function != null && function.getReturnType().isMarkedNullable(); return function != null && function.getReturnType().isMarkedNullable();
} }

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

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

Loading…
Cancel
Save