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:
Oliver Gierke
2014-12-18 18:53:10 +01:00
parent 846ab10b01
commit a179e4e7df
2 changed files with 37 additions and 0 deletions

View File

@@ -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 @@ 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()

View File

@@ -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);