Skip to content

Extend SortingAndPaging Repository with Crud Repository #272

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

Merged
merged 2 commits into from
Aug 1, 2022
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
8 changes: 4 additions & 4 deletions .github/workflows/mvn-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: Java CI
on:
push:
branches: '**'
# pull_request:
# branches:
# - main
pull_request:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
Expand All @@ -18,4 +18,4 @@ jobs:
distribution: 'adopt'
cache: 'maven'
- name: Build with Maven
run: mvn --update-snapshots -DtrimStackTrace=false -Dsurefire.useFile=false -DskipITs verify
run: mvn --update-snapshots -DtrimStackTrace=false -Dsurefire.useFile=false -DskipITs verify
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
</dependency>
<dependency>
<groupId>io.reactivex.rxjava3</groupId>
<artifactId>rxjava</artifactId>
<version>3.1.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class BootUpgrade2730Application {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.springboot.example.upgrade;

import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class RepositoryController {
private final StudentRepoPagingAndSorting studentRepoPagingAndSorting;
private final StudentRepoReactiveSorting studentRepoReactiveSorting;
private final StudentRepoRxJava3Sorting studentRepoRxJava3Sorting;

public RepositoryController(StudentRepoPagingAndSorting studentRepoPagingAndSorting, StudentRepoReactiveSorting studentRepoReactiveSorting, StudentRepoRxJava3Sorting studentRepoRxJava3Sorting) {
this.studentRepoPagingAndSorting = studentRepoPagingAndSorting;
this.studentRepoReactiveSorting = studentRepoReactiveSorting;
this.studentRepoRxJava3Sorting = studentRepoRxJava3Sorting;
}

public void actWithRepositories() {
studentRepoPagingAndSorting.save(new Student<String>());
List.of(new Student<String>())
.forEach(studentRepoReactiveSorting::save);
studentRepoRxJava3Sorting.save(new Student<String>());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.springboot.example.upgrade;

import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import org.springframework.data.repository.reactive.ReactiveSortingRepository;

public interface StudentRepoReactiveSorting extends ReactiveSortingRepository<Student<?>, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.springboot.example.upgrade;

import org.springframework.data.repository.reactive.RxJava3SortingRepository;

public interface StudentRepoRxJava3Sorting extends RxJava3SortingRepository<Student<?>, Long> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package org.springframework.sbm.boot.upgrade_27_30;


import lombok.Setter;
import lombok.Value;
import org.jetbrains.annotations.NotNull;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
Expand All @@ -28,16 +30,28 @@
import java.util.List;
import java.util.Optional;


@Setter
public class CrudRepositoryExtension extends Recipe {
public static final String PAGING_AND_SORTING_REPOSITORY = "org.springframework.data.repository.PagingAndSortingRepository";
public static final String CRUD_REPOSITORY = "org.springframework.data.repository.CrudRepository";

@Override
@NotNull
public String getDisplayName() {
return "Extends CrudRepository for Interfaces that extends PagingAndSortingRepository";
}

public CrudRepositoryExtension() {

}

public CrudRepositoryExtension(String pagingAndSortingRepository, String targetCrudRepository) {
this.pagingAndSortingRepository = pagingAndSortingRepository;
this.targetCrudRepository = targetCrudRepository;
}

private String pagingAndSortingRepository;
private String targetCrudRepository;

@Override
protected @Nullable TreeVisitor<?, ExecutionContext> getApplicableTest() {
return new JavaIsoVisitor<>() {
Expand All @@ -48,11 +62,11 @@ public J.ClassDeclaration visitClassDeclaration(@NotNull J.ClassDeclaration clas
}

private boolean doesItExtendPagingAndSorting(J.ClassDeclaration classDecl) {
if (classDecl.getType() == null) {
if (classDecl.getImplements() == null) {
return false;
}
return classDecl.getType().getInterfaces().stream()
.anyMatch(impl -> impl.getFullyQualifiedName().equals(PAGING_AND_SORTING_REPOSITORY));
.anyMatch(impl -> impl.getFullyQualifiedName().equals(pagingAndSortingRepository));
}

private J.ClassDeclaration ceaseVisit(J.ClassDeclaration classDecl) {
Expand All @@ -79,7 +93,7 @@ public J.ClassDeclaration visitClassDeclaration(@NotNull J.ClassDeclaration clas
return classDecl;
}
List<JavaType> typeParameters = pagingInterface.get().getTypeParameters();
doAfterVisit(new ImplementTypedInterface<>(classDecl, CRUD_REPOSITORY, typeParameters));
doAfterVisit(new ImplementTypedInterface<>(classDecl, targetCrudRepository, typeParameters));
return classDecl;
}

Expand All @@ -88,7 +102,7 @@ private Optional<JavaType.FullyQualified> getExtendPagingAndSorting(J.ClassDecla
return Optional.empty();
}
return classDecl.getType().getInterfaces().stream()
.filter(impl -> impl.getFullyQualifiedName().equals(PAGING_AND_SORTING_REPOSITORY))
.filter(impl -> impl.getFullyQualifiedName().equals(pagingAndSortingRepository))
.findAny();
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
/*
* Copyright 2021 - 2022 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
*
* https://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.sbm.boot.upgrade_27_30;


import lombok.Setter;
import org.jetbrains.annotations.NotNull;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.SourceFile;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.java.tree.MethodCall;
import org.openrewrite.java.tree.TypeUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;


@Setter
public class CrudRepositoryExtensionWithReferences extends Recipe {

@Override
@NotNull
public String getDisplayName() {
return "Extends CrudRepository for Interfaces that extends PagingAndSortingRepository";
}

public CrudRepositoryExtensionWithReferences() {

}

public CrudRepositoryExtensionWithReferences(String pagingAndSortingRepository, String targetCrudRepository) {
this.pagingAndSortingRepository = pagingAndSortingRepository;
this.targetCrudRepository = targetCrudRepository;
}

private String pagingAndSortingRepository;
private String targetCrudRepository;

@Override
protected List<SourceFile> visit(List<SourceFile> allSourceFiles, ExecutionContext ctx) {

Set<String> classesToAddCrudRepository = new HashSet<>();
for (SourceFile source : allSourceFiles) {

if (source instanceof J) {
J cu = (J) source;

new JavaIsoVisitor<Integer>() {

@Override
public J.MemberReference visitMemberReference(J.MemberReference memberRef, Integer integer) {

JavaType callingClassType = memberRef.getContaining().getType();
JavaType.FullyQualified fullyQualified = TypeUtils.asFullyQualified(callingClassType);

if ((fullyQualified != null)
&& shouldApplyCrudExtension(callingClassType, memberRef)) {
classesToAddCrudRepository.add(fullyQualified.getFullyQualifiedName());
}

return super.visitMemberReference(memberRef, integer);
}

@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Integer integer) {
if (method.getSelect() == null) {
return super.visitMethodInvocation(method, integer);
}

JavaType callingClassType = method.getSelect().getType();

if (shouldApplyCrudExtension(callingClassType, method)) {
JavaType.FullyQualified fullyQualified = TypeUtils.asFullyQualified(callingClassType);
if (fullyQualified != null) {
classesToAddCrudRepository.add(fullyQualified.getFullyQualifiedName());
}
}

return super.visitMethodInvocation(method, integer);
}

private boolean shouldApplyCrudExtension(JavaType callingClassType, MethodCall method) {
return TypeUtils.isAssignableTo(pagingAndSortingRepository, callingClassType)
&& (method.getMethodType() == null ||
TypeUtils.isAssignableTo(targetCrudRepository, method.getMethodType().getDeclaringType()))
;
}
}.visit(cu, 0);
}
}

return ListUtils.map(allSourceFiles, sourceFile -> (SourceFile) new JavaIsoVisitor<Integer>() {

@Override
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Integer p) {
JavaType.FullyQualified fullyQualified = TypeUtils.asFullyQualified(classDecl.getType());
if (
TypeUtils.isAssignableTo(pagingAndSortingRepository, classDecl.getType())
&& fullyQualified != null
&& classesToAddCrudRepository.contains(fullyQualified.getFullyQualifiedName())
) {
Optional<JavaType.FullyQualified> pagingInterface = getExtendPagingAndSorting(classDecl);
if (pagingInterface.isEmpty()) {
return classDecl;
}
List<JavaType> typeParameters = pagingInterface.get().getTypeParameters();
doAfterVisit(new ImplementTypedInterface<>(classDecl, targetCrudRepository, typeParameters));

return classDecl;
}

return super.visitClassDeclaration(classDecl, p);
}
}.visit(sourceFile, 0));
}

private Optional<JavaType.FullyQualified> getExtendPagingAndSorting(J.ClassDeclaration classDecl) {
if (classDecl.getType() == null) {
return Optional.empty();
}
return classDecl.getType().getInterfaces().stream()
.filter(impl -> impl.getFullyQualifiedName().equals(pagingAndSortingRepository))
.findAny();
}

// @Override
// protected @Nullable TreeVisitor<?, ExecutionContext> getApplicableTest() {
// return new JavaIsoVisitor<>() {
// @Override
// @NotNull
// public J.ClassDeclaration visitClassDeclaration(@NotNull J.ClassDeclaration classDecl, @NotNull ExecutionContext executionContext) {
// return doesItExtendPagingAndSorting(classDecl) ? applyThisRecipe(classDecl) : ceaseVisit(classDecl);
// }
//
// private boolean doesItExtendPagingAndSorting(J.ClassDeclaration classDecl) {
// if (classDecl.getImplements() == null) {
// return false;
// }
// return classDecl.getType().getInterfaces().stream()
// .anyMatch(impl -> impl.getFullyQualifiedName().equals(pagingAndSortingRepository));
// }
//
// private J.ClassDeclaration ceaseVisit(J.ClassDeclaration classDecl) {
// return classDecl;
// }
//
// @NotNull
// private J.ClassDeclaration applyThisRecipe(J.ClassDeclaration classDecl) {
// return classDecl.withMarkers(classDecl.getMarkers().searchResult());
// }
// };
// }

// @Override
// @NotNull
// protected JavaIsoVisitor<ExecutionContext> getVisitor() {
// return new JavaIsoVisitor<>() {
// @Override
// @NotNull
// public J.ClassDeclaration visitClassDeclaration(@NotNull J.ClassDeclaration classDecl, @NotNull ExecutionContext executionContext) {
//
// Optional<JavaType.FullyQualified> pagingInterface = getExtendPagingAndSorting(classDecl);
// if (pagingInterface.isEmpty()) {
// return classDecl;
// }
// List<JavaType> typeParameters = pagingInterface.get().getTypeParameters();
// doAfterVisit(new ImplementTypedInterface<>(classDecl, targetCrudRepository, typeParameters));
// return classDecl;
// }
//
// private Optional<JavaType.FullyQualified> getExtendPagingAndSorting(J.ClassDeclaration classDecl) {
// if (classDecl.getType() == null) {
// return Optional.empty();
// }
// return classDecl.getType().getInterfaces().stream()
// .filter(impl -> impl.getFullyQualifiedName().equals(pagingAndSortingRepository))
// .findAny();
// }
// };
//
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,26 @@
description: Replace javax with new jakarta packages
openRewriteRecipeName: org.openrewrite.java.migrate.JavaxMigrationToJakarta

- type: org.springframework.sbm.engine.recipe.OpenRewriteNamedRecipeAdapter
- type: org.springframework.sbm.engine.recipe.OpenRewriteDeclarativeRecipeAdapter
condition:
type: org.springframework.sbm.boot.upgrade.common.conditions.HasSpringBootParentOfVersion
versionStartingWith: "3.0."
description: Add CrudRepository interface extension additionaly to PagingAndSortingRepository
openRewriteRecipeName: org.springframework.sbm.boot.upgrade_27_30.CrudRepositoryExtension
openRewriteRecipe: |-
type: specs.openrewrite.org/v1beta/recipe
name: org.springframework.sbm.boot.upgrade_27_30.SpringBootPropertiesManual_2_7CrudRepo
displayName: Add CrudRepository interface extension additionaly to PagingAndSortingRepository
description: Add CrudRepository interface extension additionaly to PagingAndSortingRepository
recipeList:
- org.springframework.sbm.boot.upgrade_27_30.CrudRepositoryExtension:
pagingAndSortingRepository: org.springframework.data.repository.PagingAndSortingRepository
targetCrudRepository: org.springframework.data.repository.CrudRepository
- org.springframework.sbm.boot.upgrade_27_30.CrudRepositoryExtension:
pagingAndSortingRepository: org.springframework.data.repository.reactive.ReactiveSortingRepository
targetCrudRepository: org.springframework.data.repository.reactive.ReactiveCrudRepository
- org.springframework.sbm.boot.upgrade_27_30.CrudRepositoryExtension:
pagingAndSortingRepository: org.springframework.data.repository.reactive.RxJava3SortingRepository
targetCrudRepository: org.springframework.data.repository.reactive.RxJava3CrudRepository

- type: org.springframework.sbm.engine.recipe.OpenRewriteDeclarativeRecipeAdapter
condition:
Expand Down
Loading