DATACMNS-1141 - Add missing NonNullApi and Nullable annotations.

Add Nullable annotation to ParsingUtils, Sort.Order and PropertyValueProvider.

Add getRequiredAnnotation(…) to PersistentEntity and PersistentProperty to provide methods returning required, non-null annotations.
This commit is contained in:
Mark Paluch
2017-08-22 10:24:23 +02:00
parent d657b17e74
commit 3a2b6b601b
7 changed files with 90 additions and 10 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2011-2012 the original author or authors.
* Copyright 2011-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -30,6 +30,7 @@ import org.w3c.dom.Element;
* Utility methods for {@link BeanDefinitionParser} implementations.
*
* @author Oliver Gierke
* @author Mark Paluch
*/
public abstract class ParsingUtils {
@@ -138,7 +139,7 @@ public abstract class ParsingUtils {
* @param source
* @return
*/
public static AbstractBeanDefinition getObjectFactoryBeanDefinition(String targetBeanName, Object source) {
public static AbstractBeanDefinition getObjectFactoryBeanDefinition(String targetBeanName, @Nullable Object source) {
Assert.hasText(targetBeanName, "Target bean name must not be null or empty!");

View File

@@ -414,7 +414,7 @@ public class Sort implements Streamable<org.springframework.data.domain.Sort.Ord
* @param direction can be {@literal null}, will default to {@link Sort#DEFAULT_DIRECTION}
* @param property must not be {@literal null} or empty.
*/
public Order(Direction direction, String property) {
public Order(@Nullable Direction direction, String property) {
this(direction, property, DEFAULT_IGNORE_CASE, DEFAULT_NULL_HANDLING);
}
@@ -426,7 +426,7 @@ public class Sort implements Streamable<org.springframework.data.domain.Sort.Ord
* @param property must not be {@literal null} or empty.
* @param nullHandling must not be {@literal null}.
*/
public Order(Direction direction, String property, NullHandling nullHandlingHint) {
public Order(@Nullable Direction direction, String property, NullHandling nullHandlingHint) {
this(direction, property, DEFAULT_IGNORE_CASE, nullHandlingHint);
}
@@ -485,7 +485,7 @@ public class Sort implements Streamable<org.springframework.data.domain.Sort.Ord
* @param nullHandling must not be {@literal null}.
* @since 1.7
*/
private Order(Direction direction, String property, boolean ignoreCase, NullHandling nullHandling) {
private Order(@Nullable Direction direction, String property, boolean ignoreCase, NullHandling nullHandling) {
if (!StringUtils.hasText(property)) {
throw new IllegalArgumentException("Property must not null or empty!");

View File

@@ -249,6 +249,26 @@ public interface PersistentEntity<T, P extends PersistentProperty<P>> extends It
@Nullable
<A extends Annotation> A findAnnotation(Class<A> annotationType);
/**
* Returns the required annotation of the given type on the {@link PersistentEntity}.
*
* @param annotationType must not be {@literal null}.
* @return the annotation.
* @throws IllegalStateException if the required {@code annotationType} is not found.
* @since 2.0
*/
default <A extends Annotation> A getRequiredAnnotation(Class<A> annotationType) throws IllegalStateException {
A annotation = findAnnotation(annotationType);
if (annotation != null) {
return annotation;
}
throw new IllegalStateException(
String.format("Required annotation %s not found for %s!", annotationType, getType()));
}
/**
* Checks whether the annotation of the given type is present on the {@link PersistentEntity}.
*

View File

@@ -269,6 +269,27 @@ public interface PersistentProperty<P extends PersistentProperty<P>> {
@Nullable
<A extends Annotation> A findAnnotation(Class<A> annotationType);
/**
* Looks up the annotation of the given type on the {@link PersistentProperty}. Will inspect accessors and the
* potentially backing field and traverse accessor methods to potentially available super types.
*
* @param annotationType the annotation to look up, must not be {@literal null}.
* @return the annotation of the given type.
* @throws IllegalStateException if the required {@code annotationType} is not found.
* @since 2.0
*/
default <A extends Annotation> A getRequiredAnnotation(Class<A> annotationType) throws IllegalStateException {
A annotation = findAnnotation(annotationType);
if (annotation != null) {
return annotation;
}
throw new IllegalStateException(
String.format("Required annotation %s not found for %s!", annotationType, getName()));
}
/**
* Looks up the annotation of the given type on the property and the owning type if no annotation can be found on it.
* Useful to lookup annotations that can be configured on the type but overridden on an individual property.

View File

@@ -16,11 +16,13 @@
package org.springframework.data.mapping.model;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.lang.Nullable;
/**
* SPI for components to provide values for as {@link PersistentProperty}.
*
* @author Oliver Gierke
* @author Mark Paluch
*/
public interface PropertyValueProvider<P extends PersistentProperty<P>> {
@@ -30,5 +32,6 @@ public interface PropertyValueProvider<P extends PersistentProperty<P>> {
* @param property will never be {@literal null}.
* @return
*/
@Nullable
<T> T getPropertyValue(P property);
}