Skip to content

Return type of the method declared is not changed #6379

@cmoulliard

Description

@cmoulliard

What version of OpenRewrite are you using?

I am using

  • rewrite-core module: 8.67
  • rewrite-java module: 8.67

Issue description

Here is the scenario tested which is not working. The recipe should allow to replace the return type of the method declared within the JavaIsoVisitor using the visitMethodDeclaration() to check if there a match with the pattern of the method to search and next to set the new type

When we run the test, we can see that the type of the method changed (see system output - AFTER) but the test is complaining (= AssertionFailedError) as it is waiting about the new type but got the original type. Why ?

package dev.snowdrop.openrewrite.quarkus.spring;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.openrewrite.ExecutionContext;
import org.openrewrite.config.CompositeRecipe;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.test.RewriteTest;

import java.util.List;

import static org.openrewrite.java.Assertions.java;
import static org.openrewrite.test.RewriteTest.toRecipe;

public class ReplaceMethodReturnTypeTest implements RewriteTest {

    @Test
    @DisplayName("Replace the return type of a method.")
    void replaceReturnTypeOfMethodDeclared() {
        String methodPattern = "TaskController addMessage(..)";
        String newReturnType = "Object";

        rewriteRun(
            spec -> spec.recipe(toRecipe(() -> new JavaIsoVisitor<>() {
                final MethodMatcher methodMatcher = new MethodMatcher(methodPattern, false);

                @Override
                public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
                    J.MethodDeclaration m = super.visitMethodDeclaration(method, ctx);
                    JavaType.Method type = m.getMethodType();

                    if (methodMatcher.matches(m.getMethodType())) {
                        System.out.println("========== BEFORE ==========");
                        System.out.printf("Method name: %s \n",m.getSimpleName());
                        System.out.printf("Method modifiers: %s \n",m.getModifiers());
                        System.out.printf("Return Type: %s \n",m.getType());
                        System.out.printf("Declaring type: %s \n",m.getMethodType().getDeclaringType());

                        type = type.withReturnType(JavaType.buildType(newReturnType));
                        m = m.withMethodType(type);

                        System.out.println("========== AFTER ==========");
                        System.out.printf("Method name: %s \n",m.getSimpleName());
                        System.out.printf("Method modifiers: %s \n",m.getModifiers());
                        System.out.printf("Return Type: %s \n",m.getType());
                        System.out.printf("Declaring type: %s \n",m.getMethodType().getDeclaringType());
                    }
                    return m;
                }}))
                .expectedCyclesThatMakeChanges(1).cycles(1),
                java("""
						public class TaskController {
						  public String viewHome(String msg) {
						    var res = addMessage(msg);
						  }
						  protected String addMessage(String msg) {
						      return new StringBuilder().append("Hi").append(msg).toString();
						  }
						}
						""", """
						  public class TaskController {
						    public String viewHome(String msg) {
						      var res = addMessage(msg);
						    }
						    protected Object addMessage(String msg) {
						        return new StringBuilder().append("Hi").append(msg).toString();
						    }
						  }
						"""));
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingquestionFurther information is requested

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions