From 8e9b4f78a0f14c0412f0c0d31e4ac0cd4dec9baa Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Wed, 23 Oct 2013 18:23:56 +0200 Subject: [PATCH] DATACMNS-385 - Fixed repository method matching for iterable entities. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When matching the save(Iterable) method of CrudRepository for an entity implementing Iterable we accidentally determined CrudRepository.save(T) as target method if the entity type under consideration implements Iterable. We now explicitly check for the parameter type not being Iterable when matching the domain type generics in matchesGenericType(…). --- .../support/DefaultRepositoryInformation.java | 10 ++++- ...DefaultRepositoryInformationUnitTests.java | 44 +++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/springframework/data/repository/core/support/DefaultRepositoryInformation.java b/src/main/java/org/springframework/data/repository/core/support/DefaultRepositoryInformation.java index 9a2eadae9..fd3b6aade 100644 --- a/src/main/java/org/springframework/data/repository/core/support/DefaultRepositoryInformation.java +++ b/src/main/java/org/springframework/data/repository/core/support/DefaultRepositoryInformation.java @@ -184,7 +184,7 @@ class DefaultRepositoryInformation extends AbstractRepositoryMetadata implements * @return */ private boolean isQueryMethodCandidate(Method method) { - return isQueryAnnotationPresentOn(method) || (!isCustomMethod(method) && !isBaseClassMethod(method)); + return isQueryAnnotationPresentOn(method) || !isCustomMethod(method) && !isBaseClassMethod(method); } /** @@ -352,7 +352,13 @@ class DefaultRepositoryInformation extends AbstractRepositoryMetadata implements Type boundType = variable.getBounds()[0]; String referenceName = boundType instanceof TypeVariable ? boundType.toString() : variable.toString(); - if (DOMAIN_TYPE_NAME.equals(referenceName) && parameterType.isAssignableFrom(entityType)) { + boolean isDomainTypeVariableReference = DOMAIN_TYPE_NAME.equals(referenceName); + boolean parameterMatchesEntityType = parameterType.isAssignableFrom(entityType); + + // We need this check to besure not to match save(Iterable) for entities implementing Iterable + boolean isNotIterable = !parameterType.equals(Iterable.class); + + if (isDomainTypeVariableReference && parameterMatchesEntityType && isNotIterable) { return true; } diff --git a/src/test/java/org/springframework/data/repository/core/support/DefaultRepositoryInformationUnitTests.java b/src/test/java/org/springframework/data/repository/core/support/DefaultRepositoryInformationUnitTests.java index cac78e443..e6af6b44d 100644 --- a/src/test/java/org/springframework/data/repository/core/support/DefaultRepositoryInformationUnitTests.java +++ b/src/test/java/org/springframework/data/repository/core/support/DefaultRepositoryInformationUnitTests.java @@ -1,3 +1,18 @@ +/* + * Copyright 2013 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.core.support; import static org.hamcrest.Matchers.*; @@ -9,6 +24,8 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.lang.reflect.Method; +import java.util.Collections; +import java.util.Iterator; import java.util.List; import org.hamcrest.Matcher; @@ -29,6 +46,8 @@ import org.springframework.data.repository.core.RepositoryMetadata; import org.springframework.data.repository.core.support.DefaultRepositoryMetadataUnitTests.DummyGenericRepositorySupport; /** + * Unit tests for {@link DefaultRepositoryInformation}. + * * @author Oliver Gierke */ @RunWith(MockitoJUnitRunner.class) @@ -174,6 +193,21 @@ public class DefaultRepositoryInformationUnitTests { assertThat(information.getQueryMethods(), hasItem(method)); } + /** + * @see DATACMNS-385 + */ + @Test + public void findsTargetSaveForIterableIfEntityImplementsIterable() throws Exception { + + RepositoryMetadata metadata = new DefaultRepositoryMetadata(BossRepository.class); + RepositoryInformation information = new DefaultRepositoryInformation(metadata, CrudRepository.class, null); + + Method method = BossRepository.class.getMethod("save", Iterable.class); + Method reference = CrudRepository.class.getMethod("save", Iterable.class); + + assertThat(information.getTargetClassMethod(method), is(reference)); + } + private Method getMethodFrom(Class type, String name) { for (Method method : type.getMethods()) { if (method.getName().equals(name)) { @@ -215,6 +249,14 @@ public class DefaultRepositoryInformationUnitTests { } } + static class Boss implements Iterable { + + @Override + public Iterator iterator() { + return Collections. emptySet().iterator(); + } + } + interface BaseRepository extends CrudRepository { S findBySomething(String something); @@ -255,4 +297,6 @@ public class DefaultRepositoryInformationUnitTests { Object save(Object object); } + + interface BossRepository extends CrudRepository {} }