Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support @RepositoryDefinition for Spring Data repositories #10629

Merged
merged 1 commit into from
Jul 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.RepositoryDefinition;
import org.springframework.data.repository.query.Param;
import org.springframework.data.repository.query.QueryByExampleExecutor;

public final class DotNames {

Expand All @@ -39,8 +39,8 @@ public final class DotNames {
.createSimple(PagingAndSortingRepository.class.getName());
public static final DotName SPRING_DATA_JPA_REPOSITORY = DotName
.createSimple(JpaRepository.class.getName());
public static final DotName SPRING_DATA_QUERY_EXAMPLE_EXECUTOR = DotName
.createSimple(QueryByExampleExecutor.class.getName());
public static final DotName SPRING_DATA_REPOSITORY_DEFINITION = DotName
.createSimple(RepositoryDefinition.class.getName());

public static final Set<DotName> SUPPORTED_REPOSITORIES = new HashSet<>(Arrays.asList(
SPRING_DATA_JPA_REPOSITORY, SPRING_DATA_PAGING_REPOSITORY, SPRING_DATA_CRUD_REPOSITORY, SPRING_DATA_REPOSITORY));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.ConfigProvider;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
import org.jboss.jandex.IndexView;
Expand Down Expand Up @@ -92,13 +94,36 @@ void build(CombinedIndexBuildItem index,

detectAndLogSpecificSpringPropertiesIfExist();

IndexView indexIndex = index.getIndex();
List<ClassInfo> interfacesExtendingCrudRepository = getAllInterfacesExtending(DotNames.SUPPORTED_REPOSITORIES,
indexIndex);
IndexView indexView = index.getIndex();
List<ClassInfo> interfacesExtendingRepository = getAllInterfacesExtending(DotNames.SUPPORTED_REPOSITORIES,
indexView);

removeNoRepositoryBeanClasses(interfacesExtendingCrudRepository);
addRepositoryDefinitionInstances(indexView, interfacesExtendingRepository);

removeNoRepositoryBeanClasses(interfacesExtendingRepository);
implementCrudRepositories(generatedBeans, generatedClasses, additionalBeans, reflectiveClasses,
interfacesExtendingCrudRepository, indexIndex);
interfacesExtendingRepository, indexView);
}

// classes annotated with @RepositoryDefinition behave exactly as if they extended Repository
private void addRepositoryDefinitionInstances(IndexView indexView, List<ClassInfo> interfacesExtendingRepository) {
Collection<AnnotationInstance> repositoryDefinitions = indexView
.getAnnotations(DotNames.SPRING_DATA_REPOSITORY_DEFINITION);
for (AnnotationInstance repositoryDefinition : repositoryDefinitions) {
AnnotationTarget target = repositoryDefinition.target();
if (target.kind() != AnnotationTarget.Kind.CLASS) {
continue;
}
ClassInfo classInfo = target.asClass();
Set<DotName> supportedRepositories = DotNames.SUPPORTED_REPOSITORIES;
for (DotName supportedRepository : supportedRepositories) {
if (classInfo.interfaceNames().contains(supportedRepository)) {
throw new IllegalArgumentException("Class " + classInfo.name()
+ " which is annotated with @RepositoryDefinition cannot also extend " + supportedRepository);
}
}
interfacesExtendingRepository.add(classInfo);
}
}

private void detectAndLogSpecificSpringPropertiesIfExist() {
Expand Down Expand Up @@ -156,8 +181,8 @@ private void detectAndLogSpecificSpringPropertiesIfExist() {
}
}

private void removeNoRepositoryBeanClasses(List<ClassInfo> interfacesExtendingCrudRepository) {
Iterator<ClassInfo> iterator = interfacesExtendingCrudRepository.iterator();
private void removeNoRepositoryBeanClasses(List<ClassInfo> interfacesExtendingRepository) {
Iterator<ClassInfo> iterator = interfacesExtendingRepository.iterator();
while (iterator.hasNext()) {
ClassInfo next = iterator.next();
if (next.classAnnotation(DotNames.SPRING_DATA_NO_REPOSITORY_BEAN) != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.ClassType;
import org.jboss.jandex.DotName;
Expand Down Expand Up @@ -116,6 +117,13 @@ public void implementCrudRepository(ClassInfo repositoryToImplement) {
}

private Map.Entry<DotName, DotName> extractIdAndEntityTypes(ClassInfo repositoryToImplement) {
AnnotationInstance repositoryDefinitionInstance = repositoryToImplement
.classAnnotation(DotNames.SPRING_DATA_REPOSITORY_DEFINITION);
if (repositoryDefinitionInstance != null) {
return new AbstractMap.SimpleEntry<>(repositoryDefinitionInstance.value("idClass").asClass().name(),
repositoryDefinitionInstance.value("domainClass").asClass().name());
}

DotName entityDotName = null;
DotName idDotName = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import java.util.List;

import org.springframework.data.repository.Repository;
import org.springframework.data.repository.RepositoryDefinition;

public interface PostCommentRepository extends Repository<PostComment, Long> {
@RepositoryDefinition(domainClass = PostComment.class, idClass = Long.class)
public interface PostCommentRepository {

List<PostComment> findAll();

List<PostComment> findByPostId(Long postId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,11 @@ public List<Post> findAllByOrganization(@PathParam("org") String org) {
public List<PostComment> findByPostId(@PathParam("id") Long id) {
return postCommentRepository.findByPostId(id);
}

@GET
@Produces("application/json")
@Path("/postComment/all")
public List<PostComment> findAll() {
return postCommentRepository.findAll();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,11 @@ void testPostCommentByPostId() {
.statusCode(200)
.body("size()", is(0));
}

@Test
void testAllPostComment() {
when().get("/post/postComment/all").then()
.statusCode(200)
.body("size()", is(2));
}
}