Skip to content

Fix: An error "fullyQualified" is null happened when generating boot-2.7-3.0-upgrade-report #480

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
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
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public interface Method {

Visibility getVisibility();

String getReturnValue();
Optional<String> getReturnValue();

void rename(String methodPattern, String newName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.java.tree.Statement;
import org.openrewrite.java.tree.TypeTree;
import org.openrewrite.java.tree.TypeUtils;
import org.springframework.sbm.java.api.Annotation;
import org.springframework.sbm.java.api.Method;
import org.springframework.sbm.java.api.MethodParam;
import org.springframework.sbm.java.api.Visibility;
import org.springframework.sbm.java.refactoring.JavaRefactoring;
import org.springframework.sbm.project.resource.RewriteSourceFileHolder;
import org.springframework.sbm.project.resource.SbmApplicationProperties;
import org.springframework.sbm.support.openrewrite.GenericOpenRewriteRecipe;
import org.springframework.sbm.support.openrewrite.java.AddAnnotationVisitor;
import org.springframework.sbm.support.openrewrite.java.RemoveAnnotationVisitor;
Expand Down Expand Up @@ -158,9 +158,13 @@ public Visibility getVisibility() {
}

@Override
public String getReturnValue() {
JavaType.FullyQualified fullyQualified = TypeUtils.asFullyQualified(getMethodDecl().getReturnTypeExpression().getType());
return fullyQualified.getFullyQualifiedName();
public Optional<String> getReturnValue() {
TypeTree returnTypeExpression = getMethodDecl().getReturnTypeExpression();
if (returnTypeExpression == null || returnTypeExpression.getType() == JavaType.Primitive.Void) {
return Optional.empty();
}

return Optional.of(TypeUtils.asFullyQualified(returnTypeExpression.getType()).getFullyQualifiedName());
}

// FIXME: renaming method should not require a methodPattern in this context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
package org.springframework.sbm.java.impl;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.sbm.java.api.*;
import org.springframework.sbm.project.resource.TestProjectContext;
import org.springframework.sbm.testhelper.common.utils.TestDiff;

import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

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

Expand Down Expand Up @@ -72,6 +72,31 @@ void testAddAnnotation() {
.isEqualTo(expected);
}

@Test
void getReturnValueHandlesVoidMethods() {
String sourceCode = """
public class Foo {
void bar() {
}
}
""";

JavaSource javaSource = TestProjectContext.buildProjectContext()
.withJavaSources(sourceCode)
.build()
.getProjectJavaSources()
.list()
.get(0);

List<Method> method = javaSource.getTypes().stream()
.map(Type::getMethods)
.flatMap(List::stream)
.collect(Collectors.toList());

assertThat(method).hasSize(1);
assertThat(method.get(0).getReturnValue()).isEmpty();
}

@Test
void testAddAnnotationWithParameter() {
String sourceCode =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,17 @@ public List<MatchingMethod> apply(ProjectResourceSet projectResourceSet) {
.filter(t -> t
.getMethods()
.stream()
.anyMatch(m -> m.getReturnValue().equals(returnValueFqName) && m.hasAnnotation(
SPRING_BEAN_ANNOTATION)))
.anyMatch(m -> m.getReturnValue().isPresent() &&
m.getReturnValue().get().equals(returnValueFqName) &&
m.hasAnnotation(SPRING_BEAN_ANNOTATION)
))
.forEach(t -> t
.getMethods()
.stream()
.filter(m -> m.getReturnValue().equals(returnValueFqName))
.filter(m -> m.getReturnValue().get().equals(returnValueFqName))
.forEach(m -> matches.add(new MatchingMethod(js, t, m))));
});

return matches;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

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

import org.assertj.core.api.Assertions;
import org.intellij.lang.annotations.Language;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
Expand All @@ -26,7 +24,6 @@
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;

class SpringBeanMethodDeclarationFinderTest {

Expand Down Expand Up @@ -84,7 +81,7 @@ void shouldReturnTheMatchingBeanDeclaration() {
assertThat(matches).hasSize(1);
assertThat(matches.get(0).getJavaSource().getSourcePath().toString()).isEqualTo("src/main/java/MyConfiguration.java");
assertThat(matches.get(0).getType().getFullyQualifiedName()).isEqualTo("MyConfiguration");
assertThat(matches.get(0).getMethod().getReturnValue()).isEqualTo("a.b.c.SomeBean");
assertThat(matches.get(0).getMethod().getReturnValue().get()).isEqualTo("a.b.c.SomeBean");
assertThat(matches.get(0).getMethod().getName()).isEqualTo("someBean");
}
}
Expand Down Expand Up @@ -154,15 +151,43 @@ void shouldReturnTheMatchingBeanDeclarations() {
assertThat(matches).hasSize(2);
assertThat(matches.get(0).getJavaSource().getSourcePath().toString()).isEqualTo("src/main/java/MyConfiguration.java");
assertThat(matches.get(0).getType().getFullyQualifiedName()).isEqualTo("MyConfiguration");
assertThat(matches.get(0).getMethod().getReturnValue()).isEqualTo("a.b.c.SomeBean");
assertThat(matches.get(0).getMethod().getReturnValue()).isPresent();
assertThat(matches.get(0).getMethod().getReturnValue().get()).isEqualTo("a.b.c.SomeBean");
assertThat(matches.get(0).getMethod().getName()).isEqualTo("someBean");
assertThat(matches.get(1).getJavaSource().getSourcePath().toString()).isEqualTo("src/main/java/MyConfiguration2.java");
assertThat(matches.get(1).getType().getFullyQualifiedName()).isEqualTo("MyConfiguration2");
assertThat(matches.get(1).getMethod().getName()).isEqualTo("someBean2");
assertThat(matches.get(1).getMethod().getReturnValue()).isEqualTo("a.b.c.SomeBean");
assertThat(matches.get(1).getMethod().getReturnValue()).isPresent();
assertThat(matches.get(1).getMethod().getReturnValue().get()).isEqualTo("a.b.c.SomeBean");
}
}


@Nested
class WithVoidBeans {

@Test
void shouldReturnEmptyListWithVoidReturnTypeOnBean() {

builder.addJavaSource("src/main/java",
"""
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Bean;

@Configuration
public class MyConfiguration {
@Bean
void someBean() {
return null;
}
}
"""
)
.withBuildFileHavingDependencies("org.springframework:spring-context:5.3.22");

List<MatchingMethod> output = builder.build().search(sut);
assertThat(output).isEmpty();
}
}

}
}