Skip to content

Commit b40f6d7

Browse files
Fix: An error "fullyQualified" is null happened when generating boot-2.7-3.0-upgrade-report (#480)
1 parent 5c1f22f commit b40f6d7

File tree

5 files changed

+73
-16
lines changed

5 files changed

+73
-16
lines changed

components/sbm-core/src/main/java/org/springframework/sbm/java/api/Method.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public interface Method {
5151

5252
Visibility getVisibility();
5353

54-
String getReturnValue();
54+
Optional<String> getReturnValue();
5555

5656
void rename(String methodPattern, String newName);
5757

components/sbm-core/src/main/java/org/springframework/sbm/java/impl/OpenRewriteMethod.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222
import org.openrewrite.java.tree.J;
2323
import org.openrewrite.java.tree.JavaType;
2424
import org.openrewrite.java.tree.Statement;
25+
import org.openrewrite.java.tree.TypeTree;
2526
import org.openrewrite.java.tree.TypeUtils;
2627
import org.springframework.sbm.java.api.Annotation;
2728
import org.springframework.sbm.java.api.Method;
2829
import org.springframework.sbm.java.api.MethodParam;
2930
import org.springframework.sbm.java.api.Visibility;
3031
import org.springframework.sbm.java.refactoring.JavaRefactoring;
3132
import org.springframework.sbm.project.resource.RewriteSourceFileHolder;
32-
import org.springframework.sbm.project.resource.SbmApplicationProperties;
3333
import org.springframework.sbm.support.openrewrite.GenericOpenRewriteRecipe;
3434
import org.springframework.sbm.support.openrewrite.java.AddAnnotationVisitor;
3535
import org.springframework.sbm.support.openrewrite.java.RemoveAnnotationVisitor;
@@ -158,9 +158,13 @@ public Visibility getVisibility() {
158158
}
159159

160160
@Override
161-
public String getReturnValue() {
162-
JavaType.FullyQualified fullyQualified = TypeUtils.asFullyQualified(getMethodDecl().getReturnTypeExpression().getType());
163-
return fullyQualified.getFullyQualifiedName();
161+
public Optional<String> getReturnValue() {
162+
TypeTree returnTypeExpression = getMethodDecl().getReturnTypeExpression();
163+
if (returnTypeExpression == null || returnTypeExpression.getType() == JavaType.Primitive.Void) {
164+
return Optional.empty();
165+
}
166+
167+
return Optional.of(TypeUtils.asFullyQualified(returnTypeExpression.getType()).getFullyQualifiedName());
164168
}
165169

166170
// FIXME: renaming method should not require a methodPattern in this context

components/sbm-core/src/test/java/org/springframework/sbm/java/impl/OpenRewriteMethodTest.java

+26-1
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
package org.springframework.sbm.java.impl;
1717

1818
import org.assertj.core.api.Assertions;
19-
import org.junit.jupiter.api.Disabled;
2019
import org.junit.jupiter.api.Test;
2120
import org.springframework.sbm.java.api.*;
2221
import org.springframework.sbm.project.resource.TestProjectContext;
2322
import org.springframework.sbm.testhelper.common.utils.TestDiff;
2423

2524
import java.util.List;
2625
import java.util.regex.Pattern;
26+
import java.util.stream.Collectors;
2727

2828
import static org.assertj.core.api.Assertions.assertThat;
2929

@@ -72,6 +72,31 @@ void testAddAnnotation() {
7272
.isEqualTo(expected);
7373
}
7474

75+
@Test
76+
void getReturnValueHandlesVoidMethods() {
77+
String sourceCode = """
78+
public class Foo {
79+
void bar() {
80+
}
81+
}
82+
""";
83+
84+
JavaSource javaSource = TestProjectContext.buildProjectContext()
85+
.withJavaSources(sourceCode)
86+
.build()
87+
.getProjectJavaSources()
88+
.list()
89+
.get(0);
90+
91+
List<Method> method = javaSource.getTypes().stream()
92+
.map(Type::getMethods)
93+
.flatMap(List::stream)
94+
.collect(Collectors.toList());
95+
96+
assertThat(method).hasSize(1);
97+
assertThat(method.get(0).getReturnValue()).isEmpty();
98+
}
99+
75100
@Test
76101
void testAddAnnotationWithParameter() {
77102
String sourceCode =

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,17 @@ public List<MatchingMethod> apply(ProjectResourceSet projectResourceSet) {
4949
.filter(t -> t
5050
.getMethods()
5151
.stream()
52-
.anyMatch(m -> m.getReturnValue().equals(returnValueFqName) && m.hasAnnotation(
53-
SPRING_BEAN_ANNOTATION)))
52+
.anyMatch(m -> m.getReturnValue().isPresent() &&
53+
m.getReturnValue().get().equals(returnValueFqName) &&
54+
m.hasAnnotation(SPRING_BEAN_ANNOTATION)
55+
))
5456
.forEach(t -> t
5557
.getMethods()
5658
.stream()
57-
.filter(m -> m.getReturnValue().equals(returnValueFqName))
59+
.filter(m -> m.getReturnValue().get().equals(returnValueFqName))
5860
.forEach(m -> matches.add(new MatchingMethod(js, t, m))));
5961
});
62+
6063
return matches;
6164
}
6265
}

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

+32-7
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

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

19-
import org.assertj.core.api.Assertions;
20-
import org.intellij.lang.annotations.Language;
2119
import org.junit.jupiter.api.BeforeEach;
2220
import org.junit.jupiter.api.Nested;
2321
import org.junit.jupiter.api.Test;
@@ -26,7 +24,6 @@
2624
import java.util.List;
2725

2826
import static org.assertj.core.api.Assertions.assertThat;
29-
import static org.junit.jupiter.api.Assertions.*;
3027

3128
class SpringBeanMethodDeclarationFinderTest {
3229

@@ -84,7 +81,7 @@ void shouldReturnTheMatchingBeanDeclaration() {
8481
assertThat(matches).hasSize(1);
8582
assertThat(matches.get(0).getJavaSource().getSourcePath().toString()).isEqualTo("src/main/java/MyConfiguration.java");
8683
assertThat(matches.get(0).getType().getFullyQualifiedName()).isEqualTo("MyConfiguration");
87-
assertThat(matches.get(0).getMethod().getReturnValue()).isEqualTo("a.b.c.SomeBean");
84+
assertThat(matches.get(0).getMethod().getReturnValue().get()).isEqualTo("a.b.c.SomeBean");
8885
assertThat(matches.get(0).getMethod().getName()).isEqualTo("someBean");
8986
}
9087
}
@@ -154,15 +151,43 @@ void shouldReturnTheMatchingBeanDeclarations() {
154151
assertThat(matches).hasSize(2);
155152
assertThat(matches.get(0).getJavaSource().getSourcePath().toString()).isEqualTo("src/main/java/MyConfiguration.java");
156153
assertThat(matches.get(0).getType().getFullyQualifiedName()).isEqualTo("MyConfiguration");
157-
assertThat(matches.get(0).getMethod().getReturnValue()).isEqualTo("a.b.c.SomeBean");
154+
assertThat(matches.get(0).getMethod().getReturnValue()).isPresent();
155+
assertThat(matches.get(0).getMethod().getReturnValue().get()).isEqualTo("a.b.c.SomeBean");
158156
assertThat(matches.get(0).getMethod().getName()).isEqualTo("someBean");
159157
assertThat(matches.get(1).getJavaSource().getSourcePath().toString()).isEqualTo("src/main/java/MyConfiguration2.java");
160158
assertThat(matches.get(1).getType().getFullyQualifiedName()).isEqualTo("MyConfiguration2");
161159
assertThat(matches.get(1).getMethod().getName()).isEqualTo("someBean2");
162-
assertThat(matches.get(1).getMethod().getReturnValue()).isEqualTo("a.b.c.SomeBean");
160+
assertThat(matches.get(1).getMethod().getReturnValue()).isPresent();
161+
assertThat(matches.get(1).getMethod().getReturnValue().get()).isEqualTo("a.b.c.SomeBean");
163162
}
164163
}
165164

166165

166+
@Nested
167+
class WithVoidBeans {
168+
169+
@Test
170+
void shouldReturnEmptyListWithVoidReturnTypeOnBean() {
171+
172+
builder.addJavaSource("src/main/java",
173+
"""
174+
import org.springframework.context.annotation.Configuration;
175+
import org.springframework.context.annotation.Bean;
176+
177+
@Configuration
178+
public class MyConfiguration {
179+
@Bean
180+
void someBean() {
181+
return null;
182+
}
183+
}
184+
"""
185+
)
186+
.withBuildFileHavingDependencies("org.springframework:spring-context:5.3.22");
187+
188+
List<MatchingMethod> output = builder.build().search(sut);
189+
assertThat(output).isEmpty();
190+
}
191+
}
167192

168-
}
193+
}

0 commit comments

Comments
 (0)