DATACMNS-867 - Optionals in CrudMethods API.
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
package org.springframework.data.repository.core;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
@@ -37,7 +38,7 @@ public interface CrudMethods {
|
||||
* @return the method to save entities or {@literal null} if noen exposed.
|
||||
* @see #hasSaveMethod()
|
||||
*/
|
||||
Method getSaveMethod();
|
||||
Optional<Method> getSaveMethod();
|
||||
|
||||
/**
|
||||
* Returns whether the repository exposes a save method at all.
|
||||
@@ -53,7 +54,7 @@ public interface CrudMethods {
|
||||
* @return the find all method of the repository or {@literal null} if not available.
|
||||
* @see #hasFindAllMethod()
|
||||
*/
|
||||
Method getFindAllMethod();
|
||||
Optional<Method> getFindAllMethod();
|
||||
|
||||
/**
|
||||
* Returns whether the repository exposes a find all method at all.
|
||||
@@ -69,7 +70,7 @@ public interface CrudMethods {
|
||||
* @return the find one method of the repository or {@literal null} if not available.
|
||||
* @see #hasFindOneMethod()
|
||||
*/
|
||||
Method getFindOneMethod();
|
||||
Optional<Method> getFindOneMethod();
|
||||
|
||||
/**
|
||||
* Returns whether the repository exposes a find one method.
|
||||
@@ -84,7 +85,7 @@ public interface CrudMethods {
|
||||
* @return the delete method of the repository or {@literal null} if not available.
|
||||
* @see #hasDelete()
|
||||
*/
|
||||
Method getDeleteMethod();
|
||||
Optional<Method> getDeleteMethod();
|
||||
|
||||
/**
|
||||
* Returns whether the repository esposes a delete method.
|
||||
|
||||
@@ -27,6 +27,7 @@ import org.springframework.data.repository.core.RepositoryMetadata;
|
||||
import org.springframework.data.repository.util.QueryExecutionConverters;
|
||||
import org.springframework.data.repository.util.ReactiveWrappers;
|
||||
import org.springframework.data.util.ClassTypeInformation;
|
||||
import org.springframework.data.util.Lazy;
|
||||
import org.springframework.data.util.ReflectionUtils;
|
||||
import org.springframework.data.util.TypeInformation;
|
||||
import org.springframework.util.Assert;
|
||||
@@ -41,7 +42,7 @@ public abstract class AbstractRepositoryMetadata implements RepositoryMetadata {
|
||||
|
||||
private final TypeInformation<?> typeInformation;
|
||||
private final Class<?> repositoryInterface;
|
||||
private CrudMethods crudMethods;
|
||||
private final Lazy<CrudMethods> crudMethods;
|
||||
|
||||
/**
|
||||
* Creates a new {@link AbstractRepositoryMetadata}.
|
||||
@@ -55,6 +56,7 @@ public abstract class AbstractRepositoryMetadata implements RepositoryMetadata {
|
||||
|
||||
this.repositoryInterface = repositoryInterface;
|
||||
this.typeInformation = ClassTypeInformation.from(repositoryInterface);
|
||||
this.crudMethods = Lazy.of(() -> new DefaultCrudMethods(this));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -94,12 +96,7 @@ public abstract class AbstractRepositoryMetadata implements RepositoryMetadata {
|
||||
*/
|
||||
@Override
|
||||
public CrudMethods getCrudMethods() {
|
||||
|
||||
if (this.crudMethods == null) {
|
||||
this.crudMethods = new DefaultCrudMethods(this);
|
||||
}
|
||||
|
||||
return this.crudMethods;
|
||||
return this.crudMethods.get();
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -109,13 +106,9 @@ public abstract class AbstractRepositoryMetadata implements RepositoryMetadata {
|
||||
@Override
|
||||
public boolean isPagingRepository() {
|
||||
|
||||
Method findAllMethod = getCrudMethods().getFindAllMethod();
|
||||
|
||||
if (findAllMethod == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return Arrays.asList(findAllMethod.getParameterTypes()).contains(Pageable.class);
|
||||
return getCrudMethods().getFindAllMethod()//
|
||||
.map(it -> Arrays.asList(it.getParameterTypes()).contains(Pageable.class))//
|
||||
.orElse(false);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -21,6 +21,7 @@ import static org.springframework.util.ReflectionUtils.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
@@ -48,10 +49,10 @@ public class DefaultCrudMethods implements CrudMethods {
|
||||
private static final String FIND_ALL = "findAll";
|
||||
private static final String DELETE = "delete";
|
||||
|
||||
private final Method findAllMethod;
|
||||
private final Method findOneMethod;
|
||||
private final Method saveMethod;
|
||||
private final Method deleteMethod;
|
||||
private final Optional<Method> findAllMethod;
|
||||
private final Optional<Method> findOneMethod;
|
||||
private final Optional<Method> saveMethod;
|
||||
private final Optional<Method> deleteMethod;
|
||||
|
||||
/**
|
||||
* Creates a new {@link DefaultCrudMethods} using the given {@link RepositoryMetadata}.
|
||||
@@ -78,7 +79,7 @@ public class DefaultCrudMethods implements CrudMethods {
|
||||
* @param metadata must not be {@literal null}.
|
||||
* @return the most suitable method or {@literal null} if no method could be found.
|
||||
*/
|
||||
private Method selectMostSuitableSaveMethod(RepositoryMetadata metadata) {
|
||||
private Optional<Method> selectMostSuitableSaveMethod(RepositoryMetadata metadata) {
|
||||
|
||||
for (Class<?> type : asList(metadata.getDomainType(), Object.class)) {
|
||||
|
||||
@@ -89,7 +90,7 @@ public class DefaultCrudMethods implements CrudMethods {
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -104,7 +105,7 @@ public class DefaultCrudMethods implements CrudMethods {
|
||||
* @param metadata must not be {@literal null}.
|
||||
* @return the most suitable method or {@literal null} if no method could be found.
|
||||
*/
|
||||
private Method selectMostSuitableDeleteMethod(RepositoryMetadata metadata) {
|
||||
private Optional<Method> selectMostSuitableDeleteMethod(RepositoryMetadata metadata) {
|
||||
|
||||
for (Class<?> type : asList(metadata.getDomainType(), metadata.getIdType(), Serializable.class, Iterable.class)) {
|
||||
|
||||
@@ -115,7 +116,7 @@ public class DefaultCrudMethods implements CrudMethods {
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -129,7 +130,7 @@ public class DefaultCrudMethods implements CrudMethods {
|
||||
* @param metadata must not be {@literal null}.
|
||||
* @return the most suitable method or {@literal null} if no method could be found.
|
||||
*/
|
||||
private Method selectMostSuitableFindAllMethod(RepositoryMetadata metadata) {
|
||||
private Optional<Method> selectMostSuitableFindAllMethod(RepositoryMetadata metadata) {
|
||||
|
||||
for (Class<?> type : asList(Pageable.class, Sort.class)) {
|
||||
|
||||
@@ -147,7 +148,7 @@ public class DefaultCrudMethods implements CrudMethods {
|
||||
return getMostSpecificMethod(findMethod(CrudRepository.class, FIND_ALL), metadata.getRepositoryInterface());
|
||||
}
|
||||
|
||||
return null;
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -160,7 +161,7 @@ public class DefaultCrudMethods implements CrudMethods {
|
||||
* @param metadata must not be {@literal null}.
|
||||
* @return the most suitable method or {@literal null} if no method could be found.
|
||||
*/
|
||||
private Method selectMostSuitableFindOneMethod(RepositoryMetadata metadata) {
|
||||
private Optional<Method> selectMostSuitableFindOneMethod(RepositoryMetadata metadata) {
|
||||
|
||||
for (Class<?> type : asList(metadata.getIdType(), Serializable.class)) {
|
||||
|
||||
@@ -171,7 +172,7 @@ public class DefaultCrudMethods implements CrudMethods {
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -183,16 +184,12 @@ public class DefaultCrudMethods implements CrudMethods {
|
||||
* @see ClassUtils#getMostSpecificMethod(Method, Class)
|
||||
* @return
|
||||
*/
|
||||
private static Method getMostSpecificMethod(Method method, Class<?> type) {
|
||||
private static Optional<Method> getMostSpecificMethod(Method method, Class<?> type) {
|
||||
|
||||
Method result = ClassUtils.getMostSpecificMethod(method, type);
|
||||
|
||||
if (result == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
ReflectionUtils.makeAccessible(result);
|
||||
return result;
|
||||
return Optional.ofNullable(ClassUtils.getMostSpecificMethod(method, type)).map(it -> {
|
||||
ReflectionUtils.makeAccessible(it);
|
||||
return it;
|
||||
});
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -200,7 +197,7 @@ public class DefaultCrudMethods implements CrudMethods {
|
||||
* @see org.springframework.data.repository.core.support.CrudMethods#getSaveMethod()
|
||||
*/
|
||||
@Override
|
||||
public Method getSaveMethod() {
|
||||
public Optional<Method> getSaveMethod() {
|
||||
return saveMethod;
|
||||
}
|
||||
|
||||
@@ -210,7 +207,7 @@ public class DefaultCrudMethods implements CrudMethods {
|
||||
*/
|
||||
@Override
|
||||
public boolean hasSaveMethod() {
|
||||
return saveMethod != null;
|
||||
return saveMethod.isPresent();
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -218,7 +215,7 @@ public class DefaultCrudMethods implements CrudMethods {
|
||||
* @see org.springframework.data.repository.core.support.CrudMethods#getFindAllMethod()
|
||||
*/
|
||||
@Override
|
||||
public Method getFindAllMethod() {
|
||||
public Optional<Method> getFindAllMethod() {
|
||||
return findAllMethod;
|
||||
}
|
||||
|
||||
@@ -228,7 +225,7 @@ public class DefaultCrudMethods implements CrudMethods {
|
||||
*/
|
||||
@Override
|
||||
public boolean hasFindAllMethod() {
|
||||
return findAllMethod != null;
|
||||
return findAllMethod.isPresent();
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -236,7 +233,7 @@ public class DefaultCrudMethods implements CrudMethods {
|
||||
* @see org.springframework.data.repository.core.support.CrudMethods#getFindOneMethod()
|
||||
*/
|
||||
@Override
|
||||
public Method getFindOneMethod() {
|
||||
public Optional<Method> getFindOneMethod() {
|
||||
return findOneMethod;
|
||||
}
|
||||
|
||||
@@ -246,7 +243,7 @@ public class DefaultCrudMethods implements CrudMethods {
|
||||
*/
|
||||
@Override
|
||||
public boolean hasFindOneMethod() {
|
||||
return findOneMethod != null;
|
||||
return findOneMethod.isPresent();
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -255,7 +252,7 @@ public class DefaultCrudMethods implements CrudMethods {
|
||||
*/
|
||||
@Override
|
||||
public boolean hasDelete() {
|
||||
return this.deleteMethod != null;
|
||||
return this.deleteMethod.isPresent();
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -263,7 +260,7 @@ public class DefaultCrudMethods implements CrudMethods {
|
||||
* @see org.springframework.data.repository.core.support.CrudMethods#getDeleteMethod()
|
||||
*/
|
||||
@Override
|
||||
public Method getDeleteMethod() {
|
||||
public Optional<Method> getDeleteMethod() {
|
||||
return this.deleteMethod;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ package org.springframework.data.repository.support;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
@@ -114,7 +115,7 @@ class CrudRepositoryInvoker extends ReflectionRepositoryInvoker {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isRedeclaredMethod(Method method) {
|
||||
return !method.getDeclaringClass().equals(CrudRepository.class);
|
||||
protected boolean isRedeclaredMethod(Optional<Method> method) {
|
||||
return method.map(it -> !it.getDeclaringClass().equals(CrudRepository.class)).orElse(false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,10 +113,12 @@ class ReflectionRepositoryInvoker implements RepositoryInvoker {
|
||||
* @see org.springframework.data.rest.core.invoke.RepositoryInvoker#invokeSave(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T invokeSave(T object) {
|
||||
|
||||
Assert.state(hasSaveMethod(), "Repository doesn't have a save-method declared!");
|
||||
return invoke(methods.getSaveMethod(), object);
|
||||
return methods.getSaveMethod()//
|
||||
.map(it -> (T) invoke(it, object))//
|
||||
.orElseThrow(() -> new IllegalStateException("Repository doesn't have a save-method declared!"));
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -133,10 +135,12 @@ class ReflectionRepositoryInvoker implements RepositoryInvoker {
|
||||
* @see org.springframework.data.rest.core.invoke.RepositoryInvoker#invokeFindOne(java.io.Serializable)
|
||||
*/
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T invokeFindOne(Serializable id) {
|
||||
|
||||
Assert.state(hasFindOneMethod(), "Repository doesn't have a find-one-method declared!");
|
||||
return invoke(methods.getFindOneMethod(), convertId(id));
|
||||
return methods.getFindOneMethod()//
|
||||
.map(it -> (T) invoke(it, convertId(id)))//
|
||||
.orElseThrow(() -> new IllegalStateException("Repository doesn't have a find-one-method declared!"));
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -156,9 +160,9 @@ class ReflectionRepositoryInvoker implements RepositoryInvoker {
|
||||
public void invokeDelete(Serializable id) {
|
||||
|
||||
Assert.notNull(id, "Identifier must not be null!");
|
||||
Assert.state(hasDeleteMethod(), "Repository doesn't have a delete-method declared!");
|
||||
|
||||
Method method = methods.getDeleteMethod();
|
||||
Method method = methods.getDeleteMethod()
|
||||
.orElseThrow(() -> new IllegalStateException("Repository doesn't have a delete-method declared!"));
|
||||
Class<?> parameterType = method.getParameterTypes()[0];
|
||||
List<Class<? extends Serializable>> idTypes = Arrays.asList(idType, Serializable.class);
|
||||
|
||||
@@ -260,9 +264,8 @@ class ReflectionRepositoryInvoker implements RepositoryInvoker {
|
||||
|
||||
protected Iterable<Object> invokePagedFindAllReflectively(Pageable pageable) {
|
||||
|
||||
Assert.state(hasFindAllMethod(), "Repository doesn't have a find-all-method declared!");
|
||||
|
||||
Method method = methods.getFindAllMethod();
|
||||
Method method = methods.getFindAllMethod()
|
||||
.orElseThrow(() -> new IllegalStateException("Repository doesn't have a find-all-method declared!"));
|
||||
Class<?>[] types = method.getParameterTypes();
|
||||
|
||||
if (types.length == 0) {
|
||||
@@ -278,9 +281,8 @@ class ReflectionRepositoryInvoker implements RepositoryInvoker {
|
||||
|
||||
protected Iterable<Object> invokeSortedFindAllReflectively(Sort sort) {
|
||||
|
||||
Assert.state(hasFindAllMethod(), "Repository doesn't have a find-all-method declared!");
|
||||
|
||||
Method method = methods.getFindAllMethod();
|
||||
Method method = methods.getFindAllMethod()
|
||||
.orElseThrow(() -> new IllegalStateException("Repository doesn't have a find-all-method declared!"));
|
||||
Class<?>[] types = method.getParameterTypes();
|
||||
|
||||
if (types.length == 0) {
|
||||
|
||||
Reference in New Issue
Block a user