DATAREST-325 - ReflectionRepositoryInvoker now makes method accessible before invoking.

This allows methods in package protected repository interfaces to be invoked successfully.
This commit is contained in:
Oliver Gierke
2014-06-19 18:00:41 +02:00
parent a9305cab33
commit 173d528e47
5 changed files with 107 additions and 1 deletions

View File

@@ -274,6 +274,8 @@ class ReflectionRepositoryInvoker implements RepositoryInvoker {
*/
@SuppressWarnings("unchecked")
private <T> T invoke(Method method, Object... arguments) {
ReflectionUtils.makeAccessible(method);
return (T) ReflectionUtils.invokeMethod(method, repository, arguments);
}

View File

@@ -0,0 +1,31 @@
/*
* Copyright 2014 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.rest.core.domain.jpa;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
/**
* @author Oliver Gierke
*/
@Entity
public class Author {
@Id @GeneratedValue Long id;
String firstname, lastname;
}

View File

@@ -0,0 +1,31 @@
/*
* Copyright 2014 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.rest.core.domain.jpa;
import java.util.List;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
/**
* @author Oliver Gierke
*/
@RepositoryRestResource
interface AuthorRepository extends Repository<Author, Long> {
List<Author> findByFirstnameContaining(@Param("firstname") String firstname);
}

View File

@@ -19,9 +19,11 @@ import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
@@ -33,6 +35,7 @@ import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.repository.support.Repositories;
import org.springframework.data.rest.core.AbstractIntegrationTests;
import org.springframework.data.rest.core.domain.jpa.Author;
import org.springframework.data.rest.core.domain.jpa.Order;
import org.springframework.data.rest.core.domain.jpa.OrderRepository;
import org.springframework.data.rest.core.domain.jpa.Person;
@@ -119,4 +122,21 @@ public class ReflectionRepositoryInvokerIntegrationTests extends AbstractIntegra
Page<?> page = (Page<?>) result;
assertThat(page.getNumberOfElements(), is(1));
}
/**
* @see DATAREST-325
*/
@Test
public void invokesMethodOnPackageProtectedRepository() throws Exception {
Object authorRepository = repositories.getRepositoryFor(Author.class);
RepositoryInformation repositoryInformation = repositories.getRepositoryInformationFor(Author.class);
Method method = repositoryInformation.getRepositoryInterface().getMethod("findByFirstnameContaining", String.class);
Map<String, String[]> parameters = Collections.singletonMap("firstname", new String[] { "Oliver" });
ReflectionRepositoryInvoker invoker = new ReflectionRepositoryInvoker(authorRepository, information,
conversionService);
invoker.invokeQueryMethod(method, parameters, null, null);
}
}

View File

@@ -32,6 +32,7 @@ import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.repository.support.Repositories;
import org.springframework.data.rest.core.Path;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.core.domain.jpa.Author;
import org.springframework.data.rest.core.domain.jpa.CreditCard;
import org.springframework.data.rest.core.domain.jpa.JpaRepositoryConfig;
import org.springframework.data.rest.core.domain.jpa.Person;
@@ -63,7 +64,7 @@ public class ResourceMappingsIntegrationTests {
@Test
public void detectsAllMappings() {
assertThat(mappings, is(Matchers.<ResourceMetadata> iterableWithSize(6)));
assertThat(mappings, is(Matchers.<ResourceMetadata> iterableWithSize(8)));
}
@Test
@@ -139,4 +140,25 @@ public class ResourceMappingsIntegrationTests {
assertThat(methodNames, hasSize(3));
assertThat(methodNames, hasItems("findByFirstName", "findByCreatedGreaterThan", "findByCreatedUsingISO8601Date"));
}
/**
* @see DATAREST-325
*/
@Test
public void exposesMethodResourceMappingInPackageProtectedButExportedRepo() {
ResourceMetadata metadata = mappings.getMappingFor(Author.class);
assertThat(metadata.isExported(), is(true));
SearchResourceMappings searchMappings = metadata.getSearchResourceMappings();
assertThat(searchMappings.isExported(), is(true));
assertThat(searchMappings.getMappedMethod("findByFirstnameContaining"), is(notNullValue()));
for (MethodResourceMapping methodMapping : searchMappings) {
System.out.println(methodMapping.getMethod().getName());
assertThat(methodMapping.isExported(), is(true));
}
}
}