Skip to content

Review how we add import when we change the return type of the method #140

@cmoulliard

Description

@cmoulliard

TODO

  • Review the AddThrowException recipe to add the missing import and better format the code
  • Review part of the recipe ChangeMethodReturnType how we add import qhen we change the return type of the method

Idea of code proposed by Tim Te Beeck

package org.openrewrite;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.tree.*;
import org.openrewrite.marker.Markers;
import org.openrewrite.test.RewriteTest;

import static java.util.Collections.singletonList;
import static org.openrewrite.java.Assertions.java;
import static org.openrewrite.test.RewriteTest.toRecipe;

class ReplaceReturnTypeAndAddImportTest implements RewriteTest {
    @Test
    @DisplayName("Replace the return type of a method and import the FQN.")
    void replaceReturnTypeAndAddImportTest() {

        String newImport = "java.util.List";
        rewriteRun(
          spec -> spec.recipe(toRecipe(() -> new JavaIsoVisitor<>() {
              final MethodMatcher methodMatcher = new MethodMatcher("Foo bar(..)", false);

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

                  if (methodMatcher.matches(m.getMethodType()) &&
                    !TypeUtils.isAssignableTo(newImport, m.getReturnTypeExpression().getType())) {
                      maybeAddImport(newImport);
                      m = m.withReturnTypeExpression(getParameterizedType());
                      return autoFormat(m, ctx);
                  }
                  return m;
              }

              private J.ParameterizedType getParameterizedType() {
                  return new J.ParameterizedType(
                    Tree.randomId(),
                    Space.SINGLE_SPACE,
                    Markers.EMPTY,
                    TypeTree.build(newImport),
                    JContainer.build(singletonList(JRightPadded.build(TypeTree.build("java.lang.String")))),
                    JavaType.buildType("java.util.List")
                  );
              }
          })),
          java(
            """
              class Foo {
                Object bar() {
                    return null;
                }
              }
              """, """
                import java.util.List;

                class Foo {
                    List<java.lang.String> bar() {
                        return null;
                    }
                }
              """
          )
        );
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions