DATACMNS-483 - Support for wrapper types as return types for repository methods.

The repository method invocation mechanism now post-processes the result of the actual method invocation in case it's not type-compatible with the return type of the invoked method and invokes a conversion if applicable. This allows us to register custom converters for well-known wrapper types. We currently support Optional from both Google Guava and JDK 8.

Implementation notice

We use some JDK 8 type stubs from the Spring Data Build project to be able to compile using JDKs < 8. This is necessary as we need to remain compatible with Spring 3.2.x for the Dijkstra release train and our test executions still run into some issues with it a Java 8 runtime (mostly ASM related). These issues were reported in [0, 1] and we'll probably get off the hack for Dijkstra GA by upgrading to Spring 3.2.9.

[0] https://jira.spring.io/browse/SPR-11719
[1] https://jira.spring.io/browse/SPR-11718
This commit is contained in:
Oliver Gierke
2014-04-22 14:20:54 +02:00
parent 165c0595e3
commit c141fb771d
9 changed files with 368 additions and 22 deletions

View File

@@ -15,9 +15,11 @@
*/
package org.springframework.data.repository.core.support;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.Collection;
import org.junit.Test;
@@ -28,6 +30,8 @@ import org.springframework.data.repository.Repository;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.repository.util.ClassUtils;
import com.google.common.base.Optional;
/**
* Unit tests for {@link DefaultRepositoryMetadata}.
*
@@ -101,6 +105,18 @@ public class DefaultRepositoryMetadataUnitTests {
assertEquals(Long.class, metadata.getIdType());
}
/**
* @see DATACMNS-483
*/
@Test
public void discoversDomainTypeOnReturnTypeWrapper() throws Exception {
RepositoryMetadata metadata = new DefaultRepositoryMetadata(OptionalRepository.class);
Method method = OptionalRepository.class.getMethod("findByEmailAddress", String.class);
assertThat(metadata.getReturnedDomainClass(method), is(typeCompatibleWith(User.class)));
}
@SuppressWarnings("unused")
private class User {
@@ -167,4 +183,9 @@ public class DefaultRepositoryMetadataUnitTests {
static interface ConcreteRepository extends IdTypeFixingRepository<User> {
}
static interface OptionalRepository extends Repository<User, Long> {
Optional<User> findByEmailAddress(String emailAddress);
}
}

View File

@@ -0,0 +1,68 @@
/*
* 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.repository.util;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.util.ClassUtils;
import com.google.common.base.Optional;
/**
* Unit tests for {@link QueryExecutionConverters}.
*
* @author Oliver Gierke
*/
public class QueryExecutionConvertersUnitTests {
DefaultConversionService conversionService;
@Before
public void setUp() {
this.conversionService = new DefaultConversionService();
QueryExecutionConverters.registerConvertersIn(conversionService);
}
/**
* @see DATACMNS-483
*/
@Test
@SuppressWarnings("unchecked")
public void turnsNullIntoGuavaOptional() {
Optional<Object> optional = conversionService.convert(new NullableWrapper(null), Optional.class);
assertThat(optional, is(Optional.<Object> absent()));
}
/**
* @see DATACMNS-483
*/
@Test
@SuppressWarnings("unchecked")
public void turnsNullIntoJdk8Optional() {
Assume.assumeThat(ClassUtils.isPresent("java.util.Optional", getClass().getClassLoader()), is(true));
java.util.Optional<Object> optional = conversionService.convert(new NullableWrapper(null), java.util.Optional.class);
assertThat(optional, is(java.util.Optional.<Object> empty()));
}
}