forked from spring-projects/spring-data-commons
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DATACMNS-98 - Fixed potential ClassCastException for domain type look…
…up for query methods. Refactored the definition of what domain class is returned from a query method into a common AbstractRepositoryMetadata and moved ClassUtils.getReturnedDomainClass into RepositoryMetadata. Moved test cases accordingly. Added getReturnType(Method method) to TypeInformation.
- Loading branch information
Showing
12 changed files
with
253 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...ain/java/org/springframework/data/repository/core/support/AbstractRepositoryMetadata.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright 2011 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 java.lang.reflect.Method; | ||
|
||
import org.springframework.data.repository.core.RepositoryMetadata; | ||
import org.springframework.data.util.ClassTypeInformation; | ||
import org.springframework.data.util.TypeInformation; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* Base class for {@link RepositoryMetadata} implementations. | ||
* | ||
* @author Oliver Gierke | ||
*/ | ||
public abstract class AbstractRepositoryMetadata implements RepositoryMetadata { | ||
|
||
private final TypeInformation<?> typeInformation; | ||
|
||
/** | ||
* Creates a new {@link AbstractRepositoryMetadata}. | ||
* | ||
* @param repositoryInterface must not be {@literal null} and must be an interface. | ||
*/ | ||
public AbstractRepositoryMetadata(Class<?> repositoryInterface) { | ||
|
||
Assert.notNull(repositoryInterface, "Given type must not be null!"); | ||
Assert.isTrue(repositoryInterface.isInterface(), "Given type must be an interface!"); | ||
this.typeInformation = ClassTypeInformation.from(repositoryInterface); | ||
} | ||
|
||
/* | ||
* (non-Javadoc) | ||
* @see org.springframework.data.repository.core.RepositoryMetadata#getReturnedDomainClass(java.lang.reflect.Method) | ||
*/ | ||
public Class<?> getReturnedDomainClass(Method method) { | ||
|
||
TypeInformation<?> returnTypeInfo = typeInformation.getReturnType(method); | ||
Class<?> rawType = returnTypeInfo.getType(); | ||
|
||
return Iterable.class.isAssignableFrom(rawType) ? returnTypeInfo.getComponentType().getType() : rawType; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
.../org/springframework/data/repository/core/support/AbstractRepositoryMetadataUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* | ||
* Copyright 2011 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.*; | ||
import static org.junit.Assert.*; | ||
|
||
import java.io.Serializable; | ||
import java.lang.reflect.Method; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.junit.Test; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.querydsl.User; | ||
import org.springframework.data.repository.Repository; | ||
import org.springframework.data.repository.core.RepositoryMetadata; | ||
|
||
/** | ||
* Unit tests for {@link AbstractRepositoryMetadata}. | ||
* | ||
* @author Oliver Gierke | ||
*/ | ||
public class AbstractRepositoryMetadataUnitTest { | ||
|
||
@Test | ||
public void discoversSimpleReturnTypeCorrectly() throws Exception { | ||
|
||
RepositoryMetadata metadata = new DummyRepositoryMetadata(UserRepository.class); | ||
Method method = UserRepository.class.getMethod("findSingle"); | ||
assertThat(metadata.getReturnedDomainClass(method), is(typeCompatibleWith(User.class))); | ||
} | ||
|
||
/** | ||
* @see DATACMNS-98 | ||
*/ | ||
@Test | ||
public void resolvesTypeParameterReturnType() throws Exception { | ||
RepositoryMetadata metadata = new DummyRepositoryMetadata(ConcreteRepository.class); | ||
Method method = ConcreteRepository.class.getMethod("intermediateMethod"); | ||
assertThat(metadata.getReturnedDomainClass(method), is(typeCompatibleWith(User.class))); | ||
} | ||
|
||
@Test | ||
public void determinesReturnTypeFromPageable() throws Exception { | ||
|
||
RepositoryMetadata metadata = new DummyRepositoryMetadata(ExtendingRepository.class); | ||
Method method = ExtendingRepository.class.getMethod("findByFirstname", Pageable.class, String.class); | ||
assertThat(metadata.getReturnedDomainClass(method), is(typeCompatibleWith(User.class))); | ||
} | ||
|
||
@Test | ||
public void determinesReturnTypeFromGenericType() throws Exception { | ||
RepositoryMetadata metadata = new DummyRepositoryMetadata(ExtendingRepository.class); | ||
Method method = ExtendingRepository.class.getMethod("someMethod"); | ||
assertThat(metadata.getReturnedDomainClass(method), is(typeCompatibleWith(GenericType.class))); | ||
} | ||
|
||
@Test | ||
public void handlesGenericTypeInReturnedCollectionCorrectly() throws SecurityException, NoSuchMethodException { | ||
|
||
RepositoryMetadata metadata = new DummyRepositoryMetadata(ExtendingRepository.class); | ||
Method method = ExtendingRepository.class.getMethod("anotherMethod"); | ||
assertThat(metadata.getReturnedDomainClass(method), is(typeCompatibleWith(Map.class))); | ||
} | ||
|
||
interface UserRepository extends Repository<User, Long> { | ||
|
||
User findSingle(); | ||
} | ||
|
||
interface IntermediateRepository<T> extends Repository<T, Long> { | ||
|
||
List<T> intermediateMethod(); | ||
} | ||
|
||
interface ConcreteRepository extends IntermediateRepository<User> { | ||
|
||
} | ||
|
||
interface ExtendingRepository extends Serializable, UserRepository { | ||
|
||
Page<User> findByFirstname(Pageable pageable, String firstname); | ||
|
||
GenericType<User> someMethod(); | ||
|
||
List<Map<String, Object>> anotherMethod(); | ||
} | ||
|
||
class GenericType<T> { | ||
|
||
} | ||
|
||
class DummyRepositoryMetadata extends AbstractRepositoryMetadata { | ||
|
||
public DummyRepositoryMetadata(Class<?> repositoryInterface) { | ||
super(repositoryInterface); | ||
} | ||
|
||
public Class<?> getIdClass() { | ||
return null; | ||
} | ||
|
||
public Class<?> getDomainClass() { | ||
return null; | ||
} | ||
|
||
public Class<?> getRepositoryInterface() { | ||
return null; | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.