Skip to content

Commit c60cd64

Browse files
committed
DATACMNS-794 - Repositories now exposes RepositoryInformation for repository interfaces.
1 parent 755a4cf commit c60cd64

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/main/java/org/springframework/data/repository/support/Repositories.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ private synchronized void cacheRepositoryFactory(String name) {
9494

9595
RepositoryFactoryInformation repositoryFactoryInformation = beanFactory.getBean(name,
9696
RepositoryFactoryInformation.class);
97-
Class<?> domainType = ClassUtils.getUserClass(repositoryFactoryInformation.getRepositoryInformation()
98-
.getDomainType());
97+
Class<?> domainType = ClassUtils
98+
.getUserClass(repositoryFactoryInformation.getRepositoryInformation().getDomainType());
9999

100100
RepositoryInformation information = repositoryFactoryInformation.getRepositoryInformation();
101101
Set<Class<?>> alternativeDomainTypes = information.getAlternativeDomainTypes();
@@ -193,6 +193,28 @@ public RepositoryInformation getRepositoryInformationFor(Class<?> domainClass) {
193193
return information == EMPTY_REPOSITORY_FACTORY_INFO ? null : information.getRepositoryInformation();
194194
}
195195

196+
/**
197+
* Returns the {@link RepositoryInformation} for the given repository interface.
198+
*
199+
* @param repositoryInterface must not be {@literal null}.
200+
* @return the {@link RepositoryInformation} for the given repository interface or {@literal null} there's no
201+
* repository instance registered for the given interface.
202+
* @since 1.12
203+
*/
204+
public RepositoryInformation getRepositoryInformation(Class<?> repositoryInterface) {
205+
206+
for (RepositoryFactoryInformation<Object, Serializable> factoryInformation : repositoryFactoryInfos.values()) {
207+
208+
RepositoryInformation information = factoryInformation.getRepositoryInformation();
209+
210+
if (information.getRepositoryInterface().equals(repositoryInterface)) {
211+
return information;
212+
}
213+
}
214+
215+
return null;
216+
}
217+
196218
/**
197219
* Returns the {@link PersistentEntity} for the given domain class. Might return {@literal null} in case the module
198220
* storing the given domain class does not support the mapping subsystem.

src/test/java/org/springframework/data/repository/support/RepositoriesUnitTests.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*/
1818
package org.springframework.data.repository.support;
1919

20-
import static org.hamcrest.CoreMatchers.*;
20+
import static org.hamcrest.Matchers.*;
2121
import static org.junit.Assert.*;
2222

2323
import java.io.Serializable;
@@ -139,6 +139,18 @@ public void discoversRepositoryForAlternativeDomainType() {
139139
context.close();
140140
}
141141

142+
/**
143+
* @see DATACMNS-794
144+
*/
145+
@Test
146+
public void exposesRepositoryFactoryInformationForRepository() {
147+
148+
RepositoryInformation information = new Repositories(context).getRepositoryInformation(PersonRepository.class);
149+
150+
assertThat(information, is(notNullValue()));
151+
assertThat(information.getRepositoryInterface(), is(typeCompatibleWith(PersonRepository.class)));
152+
}
153+
142154
class Person {}
143155

144156
class Address {}

0 commit comments

Comments
 (0)