|
| 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 | +package org.springframework.sbm.boot.upgrade_27_30; |
| 17 | + |
| 18 | + |
| 19 | +import lombok.Setter; |
| 20 | +import org.jetbrains.annotations.NotNull; |
| 21 | +import org.openrewrite.ExecutionContext; |
| 22 | +import org.openrewrite.Recipe; |
| 23 | +import org.openrewrite.SourceFile; |
| 24 | +import org.openrewrite.internal.ListUtils; |
| 25 | +import org.openrewrite.java.JavaIsoVisitor; |
| 26 | +import org.openrewrite.java.tree.J; |
| 27 | +import org.openrewrite.java.tree.JavaType; |
| 28 | +import org.openrewrite.java.tree.MethodCall; |
| 29 | +import org.openrewrite.java.tree.TypeUtils; |
| 30 | + |
| 31 | +import java.util.HashSet; |
| 32 | +import java.util.List; |
| 33 | +import java.util.Optional; |
| 34 | +import java.util.Set; |
| 35 | + |
| 36 | + |
| 37 | +@Setter |
| 38 | +public class CrudRepositoryExtensionWithReferences extends Recipe { |
| 39 | + |
| 40 | + @Override |
| 41 | + @NotNull |
| 42 | + public String getDisplayName() { |
| 43 | + return "Extends CrudRepository for Interfaces that extends PagingAndSortingRepository"; |
| 44 | + } |
| 45 | + |
| 46 | + public CrudRepositoryExtensionWithReferences() { |
| 47 | + |
| 48 | + } |
| 49 | + |
| 50 | + public CrudRepositoryExtensionWithReferences(String pagingAndSortingRepository, String targetCrudRepository) { |
| 51 | + this.pagingAndSortingRepository = pagingAndSortingRepository; |
| 52 | + this.targetCrudRepository = targetCrudRepository; |
| 53 | + } |
| 54 | + |
| 55 | + private String pagingAndSortingRepository; |
| 56 | + private String targetCrudRepository; |
| 57 | + |
| 58 | + @Override |
| 59 | + protected List<SourceFile> visit(List<SourceFile> allSourceFiles, ExecutionContext ctx) { |
| 60 | + |
| 61 | + Set<String> classesToAddCrudRepository = new HashSet<>(); |
| 62 | + for (SourceFile source : allSourceFiles) { |
| 63 | + |
| 64 | + if (source instanceof J) { |
| 65 | + J cu = (J) source; |
| 66 | + |
| 67 | + new JavaIsoVisitor<Integer>() { |
| 68 | + |
| 69 | + @Override |
| 70 | + public J.MemberReference visitMemberReference(J.MemberReference memberRef, Integer integer) { |
| 71 | + |
| 72 | + JavaType callingClassType = memberRef.getContaining().getType(); |
| 73 | + JavaType.FullyQualified fullyQualified = TypeUtils.asFullyQualified(callingClassType); |
| 74 | + |
| 75 | + if ((fullyQualified != null) |
| 76 | + && shouldApplyCrudExtension(callingClassType, memberRef)) { |
| 77 | + classesToAddCrudRepository.add(fullyQualified.getFullyQualifiedName()); |
| 78 | + } |
| 79 | + |
| 80 | + return super.visitMemberReference(memberRef, integer); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Integer integer) { |
| 85 | + if (method.getSelect() == null) { |
| 86 | + return super.visitMethodInvocation(method, integer); |
| 87 | + } |
| 88 | + |
| 89 | + JavaType callingClassType = method.getSelect().getType(); |
| 90 | + |
| 91 | + if (shouldApplyCrudExtension(callingClassType, method)) { |
| 92 | + JavaType.FullyQualified fullyQualified = TypeUtils.asFullyQualified(callingClassType); |
| 93 | + if (fullyQualified != null) { |
| 94 | + classesToAddCrudRepository.add(fullyQualified.getFullyQualifiedName()); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return super.visitMethodInvocation(method, integer); |
| 99 | + } |
| 100 | + |
| 101 | + private boolean shouldApplyCrudExtension(JavaType callingClassType, MethodCall method) { |
| 102 | + return TypeUtils.isAssignableTo(pagingAndSortingRepository, callingClassType) |
| 103 | + && (method.getMethodType() == null || |
| 104 | + TypeUtils.isAssignableTo(targetCrudRepository, method.getMethodType().getDeclaringType())) |
| 105 | + ; |
| 106 | + } |
| 107 | + }.visit(cu, 0); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + return ListUtils.map(allSourceFiles, sourceFile -> (SourceFile) new JavaIsoVisitor<Integer>() { |
| 112 | + |
| 113 | + @Override |
| 114 | + public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Integer p) { |
| 115 | + JavaType.FullyQualified fullyQualified = TypeUtils.asFullyQualified(classDecl.getType()); |
| 116 | + if ( |
| 117 | + TypeUtils.isAssignableTo(pagingAndSortingRepository, classDecl.getType()) |
| 118 | + && fullyQualified != null |
| 119 | + && classesToAddCrudRepository.contains(fullyQualified.getFullyQualifiedName()) |
| 120 | + ) { |
| 121 | + Optional<JavaType.FullyQualified> pagingInterface = getExtendPagingAndSorting(classDecl); |
| 122 | + if (pagingInterface.isEmpty()) { |
| 123 | + return classDecl; |
| 124 | + } |
| 125 | + List<JavaType> typeParameters = pagingInterface.get().getTypeParameters(); |
| 126 | + doAfterVisit(new ImplementTypedInterface<>(classDecl, targetCrudRepository, typeParameters)); |
| 127 | + |
| 128 | + return classDecl; |
| 129 | + } |
| 130 | + |
| 131 | + return super.visitClassDeclaration(classDecl, p); |
| 132 | + } |
| 133 | + }.visit(sourceFile, 0)); |
| 134 | + } |
| 135 | + |
| 136 | + private Optional<JavaType.FullyQualified> getExtendPagingAndSorting(J.ClassDeclaration classDecl) { |
| 137 | + if (classDecl.getType() == null) { |
| 138 | + return Optional.empty(); |
| 139 | + } |
| 140 | + return classDecl.getType().getInterfaces().stream() |
| 141 | + .filter(impl -> impl.getFullyQualifiedName().equals(pagingAndSortingRepository)) |
| 142 | + .findAny(); |
| 143 | + } |
| 144 | + |
| 145 | + // @Override |
| 146 | +// protected @Nullable TreeVisitor<?, ExecutionContext> getApplicableTest() { |
| 147 | +// return new JavaIsoVisitor<>() { |
| 148 | +// @Override |
| 149 | +// @NotNull |
| 150 | +// public J.ClassDeclaration visitClassDeclaration(@NotNull J.ClassDeclaration classDecl, @NotNull ExecutionContext executionContext) { |
| 151 | +// return doesItExtendPagingAndSorting(classDecl) ? applyThisRecipe(classDecl) : ceaseVisit(classDecl); |
| 152 | +// } |
| 153 | +// |
| 154 | +// private boolean doesItExtendPagingAndSorting(J.ClassDeclaration classDecl) { |
| 155 | +// if (classDecl.getImplements() == null) { |
| 156 | +// return false; |
| 157 | +// } |
| 158 | +// return classDecl.getType().getInterfaces().stream() |
| 159 | +// .anyMatch(impl -> impl.getFullyQualifiedName().equals(pagingAndSortingRepository)); |
| 160 | +// } |
| 161 | +// |
| 162 | +// private J.ClassDeclaration ceaseVisit(J.ClassDeclaration classDecl) { |
| 163 | +// return classDecl; |
| 164 | +// } |
| 165 | +// |
| 166 | +// @NotNull |
| 167 | +// private J.ClassDeclaration applyThisRecipe(J.ClassDeclaration classDecl) { |
| 168 | +// return classDecl.withMarkers(classDecl.getMarkers().searchResult()); |
| 169 | +// } |
| 170 | +// }; |
| 171 | +// } |
| 172 | + |
| 173 | +// @Override |
| 174 | +// @NotNull |
| 175 | +// protected JavaIsoVisitor<ExecutionContext> getVisitor() { |
| 176 | +// return new JavaIsoVisitor<>() { |
| 177 | +// @Override |
| 178 | +// @NotNull |
| 179 | +// public J.ClassDeclaration visitClassDeclaration(@NotNull J.ClassDeclaration classDecl, @NotNull ExecutionContext executionContext) { |
| 180 | +// |
| 181 | +// Optional<JavaType.FullyQualified> pagingInterface = getExtendPagingAndSorting(classDecl); |
| 182 | +// if (pagingInterface.isEmpty()) { |
| 183 | +// return classDecl; |
| 184 | +// } |
| 185 | +// List<JavaType> typeParameters = pagingInterface.get().getTypeParameters(); |
| 186 | +// doAfterVisit(new ImplementTypedInterface<>(classDecl, targetCrudRepository, typeParameters)); |
| 187 | +// return classDecl; |
| 188 | +// } |
| 189 | +// |
| 190 | +// private Optional<JavaType.FullyQualified> getExtendPagingAndSorting(J.ClassDeclaration classDecl) { |
| 191 | +// if (classDecl.getType() == null) { |
| 192 | +// return Optional.empty(); |
| 193 | +// } |
| 194 | +// return classDecl.getType().getInterfaces().stream() |
| 195 | +// .filter(impl -> impl.getFullyQualifiedName().equals(pagingAndSortingRepository)) |
| 196 | +// .findAny(); |
| 197 | +// } |
| 198 | +// }; |
| 199 | +// |
| 200 | +// } |
| 201 | +} |
0 commit comments