DATACMNS-619 - DefaultCrudMethods now exposes accessible methods.
We now make sure that all methods detected are made accessible so that clients using them can reflectively invoke them without additional checks.
This commit is contained in:
@@ -29,6 +29,8 @@ import org.springframework.data.repository.PagingAndSortingRepository;
|
||||
import org.springframework.data.repository.core.CrudMethods;
|
||||
import org.springframework.data.repository.core.RepositoryMetadata;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
/**
|
||||
* Default implementation to discover CRUD methods based on the given {@link RepositoryMetadata}. Will detect methods
|
||||
@@ -167,6 +169,27 @@ public class DefaultCrudMethods implements CrudMethods {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Looks up the most specific method for the given method and type and returns an accessible version of discovered
|
||||
* {@link Method} if found.
|
||||
*
|
||||
* @param method
|
||||
* @param type
|
||||
* @see ClassUtils#getMostSpecificMethod(Method, Class)
|
||||
* @return
|
||||
*/
|
||||
private static Method getMostSpecificMethod(Method method, Class<?> type) {
|
||||
|
||||
Method result = ClassUtils.getMostSpecificMethod(method, type);
|
||||
|
||||
if (result == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
ReflectionUtils.makeAccessible(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.core.support.CrudMethods#getSaveMethod()
|
||||
|
||||
@@ -174,6 +174,8 @@ class ReflectionRepositoryInvoker implements RepositoryInvoker {
|
||||
Assert.notNull(method, "Method must not be null!");
|
||||
Assert.notNull(parameters, "Parameters must not be null!");
|
||||
|
||||
ReflectionUtils.makeAccessible(method);
|
||||
|
||||
return invoke(method, prepareParameters(method, parameters, pageable, sort));
|
||||
}
|
||||
|
||||
@@ -225,8 +227,6 @@ class ReflectionRepositoryInvoker implements RepositoryInvoker {
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private <T> T invoke(Method method, Object... arguments) {
|
||||
|
||||
ReflectionUtils.makeAccessible(method);
|
||||
return (T) ReflectionUtils.invokeMethod(method, repository, arguments);
|
||||
}
|
||||
|
||||
|
||||
@@ -142,6 +142,20 @@ public class DefaultCrudMethodsUnitTests {
|
||||
RepositoryWithDeleteMethodForEntityOverloaded.class.getMethod("delete", Domain.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-619
|
||||
*/
|
||||
@Test
|
||||
public void exposedMethodsAreAccessible() {
|
||||
|
||||
CrudMethods methods = getMethodsFor(RepositoryWithAllCrudMethodOverloaded.class);
|
||||
|
||||
assertThat(methods.getSaveMethod().isAccessible(), is(true));
|
||||
assertThat(methods.getDeleteMethod().isAccessible(), is(true));
|
||||
assertThat(methods.getFindAllMethod().isAccessible(), is(true));
|
||||
assertThat(methods.getFindOneMethod().isAccessible(), is(true));
|
||||
}
|
||||
|
||||
private static CrudMethods getMethodsFor(Class<?> repositoryInterface) {
|
||||
|
||||
RepositoryMetadata metadata = new DefaultRepositoryMetadata(repositoryInterface);
|
||||
|
||||
Reference in New Issue
Block a user