|
| 1 | +/* |
| 2 | + * Copyright 2021 - 2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.sbm.boot.upgrade_27_30.report.helper; |
| 18 | + |
| 19 | +import org.jetbrains.annotations.NotNull; |
| 20 | +import org.openrewrite.ExecutionContext; |
| 21 | +import org.openrewrite.java.JavaIsoVisitor; |
| 22 | +import org.openrewrite.java.tree.J; |
| 23 | +import org.springframework.sbm.boot.upgrade_27_30.report.SpringBootUpgradeReportSection; |
| 24 | +import org.springframework.sbm.engine.context.ProjectContext; |
| 25 | +import org.springframework.sbm.project.resource.RewriteSourceFileHolder; |
| 26 | +import org.springframework.sbm.support.openrewrite.GenericOpenRewriteRecipe; |
| 27 | + |
| 28 | +import java.util.HashMap; |
| 29 | +import java.util.List; |
| 30 | +import java.util.Map; |
| 31 | +import java.util.stream.Collectors; |
| 32 | + |
| 33 | +public class PagingAndSortingHelper implements SpringBootUpgradeReportSection.Helper<List<String>> { |
| 34 | + private List<String> pagingAndSortingRepo; |
| 35 | + private List<String> reactivePagingAndSortingRepo; |
| 36 | + private List<String> rxJavaSortingRepo; |
| 37 | + |
| 38 | + @Override |
| 39 | + public String getDescription() { |
| 40 | + return null; |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public boolean evaluate(ProjectContext context) { |
| 45 | + //CrudRepositoryExtension |
| 46 | + List<RewriteSourceFileHolder<J.CompilationUnit>> pagingAndSortingFileHolders = |
| 47 | + context.getProjectJavaSources().find(pagingAndSortingFinders("org.springframework.data.repository.PagingAndSortingRepository")); |
| 48 | + List<RewriteSourceFileHolder<J.CompilationUnit>> reactiveSortingFileHolders = |
| 49 | + context.getProjectJavaSources().find(pagingAndSortingFinders("org.springframework.data.repository.reactive.ReactiveSortingRepository")); |
| 50 | + List<RewriteSourceFileHolder<J.CompilationUnit>> rxJavaSortingFileHolders = |
| 51 | + context.getProjectJavaSources().find(pagingAndSortingFinders("org.springframework.data.repository.reactive.RxJava3SortingRepository")); |
| 52 | + |
| 53 | + pagingAndSortingRepo = pagingAndSortingFileHolders |
| 54 | + .stream() |
| 55 | + .map(k -> k.getAbsolutePath().toString()).toList(); |
| 56 | + |
| 57 | + reactivePagingAndSortingRepo = reactiveSortingFileHolders.stream() |
| 58 | + .map(k -> k.getAbsolutePath().toString()).collect(Collectors.toList()); |
| 59 | + |
| 60 | + rxJavaSortingRepo = rxJavaSortingFileHolders.stream() |
| 61 | + .map(k -> k.getAbsolutePath().toString()).collect(Collectors.toList()); |
| 62 | + |
| 63 | + return !pagingAndSortingFileHolders.isEmpty() |
| 64 | + || !reactiveSortingFileHolders.isEmpty() |
| 65 | + || !rxJavaSortingFileHolders.isEmpty(); |
| 66 | + } |
| 67 | + |
| 68 | + @NotNull |
| 69 | + private GenericOpenRewriteRecipe<JavaIsoVisitor<ExecutionContext>> pagingAndSortingFinders(String clazz) { |
| 70 | + return new GenericOpenRewriteRecipe<>(() -> new JavaIsoVisitor<>() { |
| 71 | + @Override |
| 72 | + @NotNull |
| 73 | + public J.ClassDeclaration visitClassDeclaration(@NotNull J.ClassDeclaration classDecl, @NotNull ExecutionContext executionContext) { |
| 74 | + return doesItExtendPagingAndSorting(classDecl) ? applyThisRecipe(classDecl) : ceaseVisit(classDecl); |
| 75 | + } |
| 76 | + |
| 77 | + private boolean doesItExtendPagingAndSorting(J.ClassDeclaration classDecl) { |
| 78 | + if (classDecl.getImplements() == null) { |
| 79 | + return false; |
| 80 | + } |
| 81 | + return classDecl.getType().getInterfaces().stream() |
| 82 | + .anyMatch(impl -> impl.getFullyQualifiedName().equals(clazz)); |
| 83 | + } |
| 84 | + |
| 85 | + private J.ClassDeclaration ceaseVisit(J.ClassDeclaration classDecl) { |
| 86 | + return classDecl; |
| 87 | + } |
| 88 | + |
| 89 | + @NotNull |
| 90 | + private J.ClassDeclaration applyThisRecipe(J.ClassDeclaration classDecl) { |
| 91 | + return classDecl.withMarkers(classDecl.getMarkers().searchResult()); |
| 92 | + } |
| 93 | + }); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public Map<String, List<String>> getData() { |
| 98 | + Map<String, List<String>> map = new HashMap<>(); |
| 99 | + map.put("pagingAndSortingRepos", pagingAndSortingRepo); |
| 100 | + map.put("reactivePagingAndSortingRepos", reactivePagingAndSortingRepo); |
| 101 | + map.put("rxJavaSortingRepos", rxJavaSortingRepo); |
| 102 | + |
| 103 | + return map; |
| 104 | + } |
| 105 | +} |
0 commit comments