diff --git a/src/main/java/org/springframework/data/repository/core/support/DefaultCrudMethods.java b/src/main/java/org/springframework/data/repository/core/support/DefaultCrudMethods.java index 0220d1fbb..a6ce75c1b 100644 --- a/src/main/java/org/springframework/data/repository/core/support/DefaultCrudMethods.java +++ b/src/main/java/org/springframework/data/repository/core/support/DefaultCrudMethods.java @@ -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() diff --git a/src/main/java/org/springframework/data/repository/support/ReflectionRepositoryInvoker.java b/src/main/java/org/springframework/data/repository/support/ReflectionRepositoryInvoker.java index c46fdf8e6..8d3458347 100644 --- a/src/main/java/org/springframework/data/repository/support/ReflectionRepositoryInvoker.java +++ b/src/main/java/org/springframework/data/repository/support/ReflectionRepositoryInvoker.java @@ -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 invoke(Method method, Object... arguments) { - - ReflectionUtils.makeAccessible(method); return (T) ReflectionUtils.invokeMethod(method, repository, arguments); } diff --git a/src/test/java/org/springframework/data/repository/core/support/DefaultCrudMethodsUnitTests.java b/src/test/java/org/springframework/data/repository/core/support/DefaultCrudMethodsUnitTests.java index 823ec788a..1e0ce85fd 100644 --- a/src/test/java/org/springframework/data/repository/core/support/DefaultCrudMethodsUnitTests.java +++ b/src/test/java/org/springframework/data/repository/core/support/DefaultCrudMethodsUnitTests.java @@ -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);