Skip to content

Commit d3206db

Browse files
committed
Report for M1 removals (closes #162)
- Add common SpringBeanMethodDeclarationFinder
1 parent 72712be commit d3206db

File tree

10 files changed

+79
-28
lines changed

10 files changed

+79
-28
lines changed

components/sbm-recipes-boot-upgrade/src/main/java/org/springframework/sbm/boot/upgrade_24_25/actions/Boot_24_25_SpringDataJpaAction.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.sbm.boot.upgrade_24_25.actions;
1717

1818
import org.springframework.sbm.boot.common.finder.MatchingMethod;
19+
import org.springframework.sbm.boot.common.finder.MethodPatternMatchingMethod;
1920
import org.springframework.sbm.boot.upgrade_24_25.conditions.Boot_24_25_SpringDataJpaActionCondition;
2021
import org.springframework.sbm.boot.upgrade_24_25.filter.SpringDataJpaAnalyzer;
2122
import org.springframework.sbm.build.MultiModuleApplicationNotSupportedException;
@@ -48,19 +49,19 @@ public void apply(ProjectContext context) {
4849

4950
private void applyToModule(ProjectContext context) {
5051

51-
List<MatchingMethod> jpaRepositoriesWithGetByIdMethod = springDataJpaAnalyzer.getJpaRepositoriesWithGetByIdMethod(context);
52+
List<MethodPatternMatchingMethod> jpaRepositoriesWithGetByIdMethod = springDataJpaAnalyzer.getJpaRepositoriesWithGetByIdMethod(context);
5253
renameGetByIdMethods(jpaRepositoriesWithGetByIdMethod);
5354

5455
List<MethodCall> callsToGetOneMethods = springDataJpaAnalyzer.findCallsToGetOneMethod(context);
5556
refactorCallsToGetOne(callsToGetOneMethods);
5657

5758
}
5859

59-
private void renameGetByIdMethods(List<MatchingMethod> jpaRepositoriesWithGetByIdMethod) {
60+
private void renameGetByIdMethods(List<MethodPatternMatchingMethod> jpaRepositoriesWithGetByIdMethod) {
6061
jpaRepositoriesWithGetByIdMethod.forEach(repo -> {
61-
String methodPattern = "com.example.springboot24to25example.TaskRepository " + repo.methodPattern();
62-
String newMethodName = "get" + repo.method().getReturnValue() + "ById";
63-
repo.method().rename(methodPattern, newMethodName);
62+
String methodPattern = "com.example.springboot24to25example.TaskRepository " + repo.getMethodPattern();
63+
String newMethodName = "get" + repo.getMethod().getReturnValue() + "ById";
64+
repo.getMethod().rename(methodPattern, newMethodName);
6465
});
6566
}
6667

components/sbm-recipes-boot-upgrade/src/main/java/org/springframework/sbm/boot/upgrade_24_25/conditions/Boot_24_25_SpringDataJpaActionCondition.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.sbm.boot.upgrade_24_25.conditions;
1717

1818
import lombok.extern.slf4j.Slf4j;
19+
import org.springframework.sbm.boot.common.finder.MethodPatternMatchingMethod;
1920
import org.springframework.sbm.boot.upgrade_24_25.filter.SpringDataJpaAnalyzer;
2021
import org.springframework.sbm.engine.context.ProjectContext;
2122
import org.springframework.sbm.engine.recipe.Condition;
@@ -38,7 +39,7 @@ public boolean evaluate(ProjectContext context) {
3839
private boolean evaluateAgainstModule(ProjectContext context) {
3940
SpringDataJpaAnalyzer springDataJpaAnalyzer = new SpringDataJpaAnalyzer();
4041
List<MethodCall> callsToGetOneMethod = springDataJpaAnalyzer.findCallsToGetOneMethod(context);
41-
List<SpringDataJpaAnalyzer.MatchingMethod> jpaRepositoriesWithGetByIdMethod = springDataJpaAnalyzer.getJpaRepositoriesWithGetByIdMethod(context);
42+
List<MethodPatternMatchingMethod> jpaRepositoriesWithGetByIdMethod = springDataJpaAnalyzer.getJpaRepositoriesWithGetByIdMethod(context);
4243

4344
return !(callsToGetOneMethod.isEmpty() || jpaRepositoriesWithGetByIdMethod.isEmpty());
4445
}

components/sbm-recipes-boot-upgrade/src/main/java/org/springframework/sbm/boot/upgrade_24_25/filter/SpringDataJpaAnalyzer.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.sbm.boot.upgrade_24_25.filter;
1717

1818
import org.springframework.sbm.boot.common.finder.MatchingMethod;
19+
import org.springframework.sbm.boot.common.finder.MethodPatternMatchingMethod;
1920
import org.springframework.sbm.java.api.MethodCall;
2021
import org.springframework.sbm.engine.context.ProjectContext;
2122
import org.springframework.sbm.java.api.JavaSourceAndType;
@@ -30,18 +31,18 @@ public List<MethodCall> findCallsToGetOneMethod(ProjectContext context) {
3031
return context.getProjectJavaSources().findMethodCalls("org.springframework.data.jpa.repository.JpaRepository getOne(java.lang.Long)");
3132
}
3233

33-
public List<MatchingMethod> getJpaRepositoriesWithGetByIdMethod(ProjectContext context) {
34+
public List<MethodPatternMatchingMethod> getJpaRepositoriesWithGetByIdMethod(ProjectContext context) {
3435
String jpaRepositoryInterface = "org.springframework.data.jpa.repository.JpaRepository";
3536
List<JavaSourceAndType> jpaRepositories = context.getProjectJavaSources().findTypesImplementing(jpaRepositoryInterface);
3637
// FIXME: type of PK must be retrieved, moves to rewrite when these migrations are provided as OpenRewrite recipes
3738
String methodPattern = "getById(java.lang.Long)";
3839
return findRepositoriesDeclaring(jpaRepositories, methodPattern);
3940
}
4041

41-
private List<MatchingMethod> findRepositoriesDeclaring(List<JavaSourceAndType> jpaRepositories, String methodPattern) {
42+
private List<MethodPatternMatchingMethod> findRepositoriesDeclaring(List<JavaSourceAndType> jpaRepositories, String methodPattern) {
4243
return jpaRepositories.stream()
4344
.filter(jat -> jat.getType().hasMethod(methodPattern))
44-
.map(jat -> new MatchingMethod(jat.getJavaSource(), jat.getType(), jat.getType().getMethod(methodPattern), methodPattern))
45+
.map(jat -> new MethodPatternMatchingMethod(jat.getJavaSource(), jat.getType(), jat.getType().getMethod(methodPattern), methodPattern))
4546
.collect(Collectors.toList());
4647
}
4748

components/sbm-recipes-boot-upgrade/src/main/java/org/springframework/sbm/boot/upgrade_24_25/report/Boot_24_25_SpringDataJpa.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.springframework.core.annotation.Order;
1919
import org.springframework.sbm.boot.UpgradeSectionBuilder;
2020
import org.springframework.sbm.boot.asciidoctor.*;
21+
import org.springframework.sbm.boot.common.finder.MethodPatternMatchingMethod;
2122
import org.springframework.sbm.boot.upgrade_24_25.conditions.Boot_24_25_SpringDataJpaActionCondition;
2223
import org.springframework.sbm.boot.upgrade_24_25.filter.SpringDataJpaAnalyzer;
2324
import org.springframework.sbm.java.api.MethodCall;
@@ -47,15 +48,15 @@ public Section build(ProjectContext projectContext) {
4748

4849
SpringDataJpaAnalyzer springDataJpaAnalyzer = new SpringDataJpaAnalyzer();
4950
List<MethodCall> callsToGetOneMethod = springDataJpaAnalyzer.findCallsToGetOneMethod(projectContext);
50-
List<SpringDataJpaAnalyzer.MatchingMethod> jpaRepositoriesWithGetByIdMethod = springDataJpaAnalyzer.getJpaRepositoriesWithGetByIdMethod(projectContext);
51+
List<MethodPatternMatchingMethod> jpaRepositoriesWithGetByIdMethod = springDataJpaAnalyzer.getJpaRepositoriesWithGetByIdMethod(projectContext);
5152

5253

5354
Table.Builder builder = Table.builder()
5455
.headerCols("File", "Description", "Proposed change");
5556

5657

5758
jpaRepositoriesWithGetByIdMethod.forEach(m -> {
58-
Path relativePath = Path.of(".").toAbsolutePath().relativize(m.getJat().getJavaSource().getResource().getAbsolutePath());
59+
Path relativePath = Path.of(".").toAbsolutePath().relativize(m.getJavaSource().getResource().getAbsolutePath());
5960
String description = String.format("defines `%s` returning `%s`", m.getMethodPattern(), m.getMethod().getReturnValue());
6061
String fix = String.format("Rename method to `get%sById()`", m.getMethod().getReturnValue());
6162
builder.row(relativePath.toString(), description, fix);
@@ -90,7 +91,7 @@ public Section build(ProjectContext projectContext) {
9091
// FIXME: Create TODOs for all findings
9192
.todo(
9293
TodoList.Todo.builder()
93-
.text("rename `" + jpaRepositoriesWithGetByIdMethod.get(0).getJat().getJavaSource().getTypes().get(0).getFullyQualifiedName() + "." + jpaRepositoriesWithGetByIdMethod.get(0).getMethodPattern() + "` to " + String.format("`get%sById()`", jpaRepositoriesWithGetByIdMethod.get(0).getMethod().getReturnValue()))
94+
.text("rename `" + jpaRepositoriesWithGetByIdMethod.get(0).getJavaSource().getTypes().get(0).getFullyQualifiedName() + "." + jpaRepositoriesWithGetByIdMethod.get(0).getMethodPattern() + "` to " + String.format("`get%sById()`", jpaRepositoriesWithGetByIdMethod.get(0).getMethod().getReturnValue()))
9495
.build()
9596
)
9697
.recipeName("boot-2.4-2.5-spring-data-jpa")

components/sbm-recipes-boot-upgrade/src/main/java/org/springframework/sbm/boot/upgrade_27_30/checks/CommonsMultipartResolverSectionBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public Section build(ProjectContext projectContext) {
4343
List<MatchingMethod> matchingMethods = finder.findMatches(projectContext);
4444
return ChangeSection.RelevantChangeSection.builder()
4545
.title("`CommonsMultipartResolver` support has been removed")
46-
.paragraph("Support for Spring Framework’s CommonsMultipartResolver has been removed following its removal in Spring Framework 6")
46+
.paragraph("Support for Spring Framework’s `CommonsMultipartResolver` has been removed following its removal in Spring Framework 6")
4747
.relevanceSection()
4848
.paragraph("The scan found bean declarations of type `CommonsMultipartResolver`")
4949
.todoSection()

components/sbm-recipes-boot-upgrade/src/test/java/org/springframework/sbm/boot/upgrade_24_25/filter/SpringDataJpaAnalyzerTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import org.junit.jupiter.api.Disabled;
1919
import org.junit.jupiter.api.Test;
20+
import org.springframework.sbm.boot.common.finder.MethodPatternMatchingMethod;
2021
import org.springframework.sbm.engine.context.ProjectContext;
2122
import org.springframework.sbm.java.api.MethodCall;
2223
import org.springframework.sbm.java.api.ProjectJavaSources;
@@ -121,7 +122,7 @@ void testGetJpaRepositoriesWithGetByIdMethod() {
121122
.build();
122123

123124
SpringDataJpaAnalyzer sut = new SpringDataJpaAnalyzer();
124-
List<SpringDataJpaAnalyzer.MatchingMethod> match = sut.getJpaRepositoriesWithGetByIdMethod(context);
125+
List<MethodPatternMatchingMethod> match = sut.getJpaRepositoriesWithGetByIdMethod(context);
125126
assertThat(match).hasSize(1);
126127
}
127128
}

components/sbm-support-boot/src/main/java/org/springframework/sbm/boot/common/finder/MatchingMethod.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,16 @@
1616

1717
package org.springframework.sbm.boot.common.finder;
1818

19+
import lombok.Data;
1920
import org.springframework.sbm.java.api.JavaSource;
2021
import org.springframework.sbm.java.api.Method;
2122
import org.springframework.sbm.java.api.Type;
2223

23-
public record MatchingMethod(JavaSource javaSource, Type type, Method method, String methodPattern) { }
24+
@Data
25+
public class MatchingMethod {
26+
27+
private final JavaSource javaSource;
28+
private final Type type;
29+
private final Method method;
30+
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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.common.finder;
18+
19+
import lombok.Data;
20+
import lombok.Getter;
21+
import org.springframework.sbm.java.api.JavaSource;
22+
import org.springframework.sbm.java.api.Method;
23+
import org.springframework.sbm.java.api.Type;
24+
25+
public class MethodPatternMatchingMethod extends MatchingMethod {
26+
27+
@Getter
28+
private final String methodPattern;
29+
30+
public MethodPatternMatchingMethod(JavaSource javaSource, Type type, Method method, String methodPattern) {
31+
super(javaSource, type, method);
32+
this.methodPattern = methodPattern;
33+
}
34+
35+
}

components/sbm-support-boot/src/main/java/org/springframework/sbm/boot/common/finder/SpringBeanMethodDeclarationFinder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class SpringBeanMethodDeclarationFinder implements ProjectResourceFinder<
3535

3636
@Override
3737
public List<MatchingMethod> apply(ProjectResourceSet projectResourceSet) {
38+
3839
List<MatchingMethod> matches = new ArrayList<>();
3940

4041
new JavaSourceListFilter()
@@ -54,7 +55,7 @@ public List<MatchingMethod> apply(ProjectResourceSet projectResourceSet) {
5455
.getMethods()
5556
.stream()
5657
.filter(m -> m.getReturnValue().equals(returnValueFqName))
57-
.forEach(m -> matches.add(new MatchingMethod(js, t, m, methodPattern))));
58+
.forEach(m -> matches.add(new MatchingMethod(js, t, m))));
5859
});
5960
return matches;
6061
}

components/sbm-support-boot/src/test/java/org/springframework/sbm/boot/common/finder/SpringBeanMethodDeclarationFinderTest.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,10 @@ public class SomeBean {}
8282
void shouldReturnTheMatchingBeanDeclaration() {
8383
List<MatchingMethod> matches = builder.build().search(sut);
8484
assertThat(matches).hasSize(1);
85-
assertThat(matches.get(0).javaSource().getSourcePath().toString()).isEqualTo("src/main/java/MyConfiguration.java");
86-
assertThat(matches.get(0).type().getFullyQualifiedName()).isEqualTo("MyConfiguration");
87-
assertThat(matches.get(0).method().getReturnValue()).isEqualTo("a.b.c.SomeBean");
88-
assertThat(matches.get(0).method().getName()).isEqualTo("someBean");
85+
assertThat(matches.get(0).getJavaSource().getSourcePath().toString()).isEqualTo("src/main/java/MyConfiguration.java");
86+
assertThat(matches.get(0).getType().getFullyQualifiedName()).isEqualTo("MyConfiguration");
87+
assertThat(matches.get(0).getMethod().getReturnValue()).isEqualTo("a.b.c.SomeBean");
88+
assertThat(matches.get(0).getMethod().getName()).isEqualTo("someBean");
8989
}
9090
}
9191

@@ -99,6 +99,7 @@ void beforeEach() {
9999
import org.springframework.context.annotation.Configuration;
100100
import org.springframework.context.annotation.Bean;
101101
import a.b.c.SomeBean;
102+
import a.b.c.AnotherBean;
102103
103104
@Configuration
104105
public class MyConfiguration {
@@ -118,6 +119,7 @@ AnotherBean anotherBean() {
118119
import org.springframework.context.annotation.Configuration;
119120
import org.springframework.context.annotation.Bean;
120121
import a.b.c.SomeBean;
122+
import a.b.c.AnotherBean;
121123
122124
@Configuration
123125
public class MyConfiguration2 {
@@ -150,14 +152,14 @@ public class AnotherBean {}
150152
void shouldReturnTheMatchingBeanDeclarations() {
151153
List<MatchingMethod> matches = builder.build().search(sut);
152154
assertThat(matches).hasSize(2);
153-
assertThat(matches.get(0).javaSource().getSourcePath().toString()).isEqualTo("src/main/java/MyConfiguration.java");
154-
assertThat(matches.get(0).type().getFullyQualifiedName()).isEqualTo("MyConfiguration");
155-
assertThat(matches.get(0).method().getReturnValue()).isEqualTo("a.b.c.SomeBean");
156-
assertThat(matches.get(0).method().getName()).isEqualTo("someBean");
157-
assertThat(matches.get(1).javaSource().getSourcePath().toString()).isEqualTo("src/main/java/MyConfiguration2.java");
158-
assertThat(matches.get(1).type().getFullyQualifiedName()).isEqualTo("MyConfiguration2");
159-
assertThat(matches.get(1).method().getName()).isEqualTo("someBean2");
160-
assertThat(matches.get(1).method().getReturnValue()).isEqualTo("a.b.c.SomeBean");
155+
assertThat(matches.get(0).getJavaSource().getSourcePath().toString()).isEqualTo("src/main/java/MyConfiguration.java");
156+
assertThat(matches.get(0).getType().getFullyQualifiedName()).isEqualTo("MyConfiguration");
157+
assertThat(matches.get(0).getMethod().getReturnValue()).isEqualTo("a.b.c.SomeBean");
158+
assertThat(matches.get(0).getMethod().getName()).isEqualTo("someBean");
159+
assertThat(matches.get(1).getJavaSource().getSourcePath().toString()).isEqualTo("src/main/java/MyConfiguration2.java");
160+
assertThat(matches.get(1).getType().getFullyQualifiedName()).isEqualTo("MyConfiguration2");
161+
assertThat(matches.get(1).getMethod().getName()).isEqualTo("someBean2");
162+
assertThat(matches.get(1).getMethod().getReturnValue()).isEqualTo("a.b.c.SomeBean");
161163
}
162164
}
163165

0 commit comments

Comments
 (0)