Skip to content
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

maybeAddImport fails to recognise usages in try statements #5187

Open
greg-at-moderne opened this issue Mar 18, 2025 · 2 comments
Open

maybeAddImport fails to recognise usages in try statements #5187

greg-at-moderne opened this issue Mar 18, 2025 · 2 comments
Labels
bug Something isn't working

Comments

@greg-at-moderne
Copy link
Contributor

What version of OpenRewrite are you using?

Current main = c5cc221

What is the smallest, simplest way to reproduce the problem?

The following test case added to JavaVisitorTest:

    @Test
    void maybeAddImportInTry() {
        rewriteRun(
          spec -> spec
            .afterTypeValidationOptions(TypeValidation.none())
            .typeValidationOptions(TypeValidation.none())
            .recipes(toRecipe(() -> new JavaIsoVisitor<>() {
                @Override
                public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext p) {
                    if (method.getBody().getStatements().isEmpty()) {
                        maybeAddImport("java.io.PrintWriter");
                        JavaTemplate t = JavaTemplate.builder("try (PrintWriter pw = new PrintWriter(\"/tmp/file\")) { pw.println(\"A\"); }").build();
                        return t.apply(getCursor(), method.getCoordinates().replaceBody());
                    }
                    return super.visitMethodDeclaration(method, p);
                }
          })),
          java(
              """
              public class A {
                void test() {
                }
              }
              """,
              """
              import java.io.PrintWriter;
              
              public class A {
                void test() {
                    try (PrintWriter pw = new PrintWriter("/tmp/file")) {
                        pw.println("A");
                    }
                }
              }
              """
          )
      );
    }

What did you see instead?

Fails with no import added.

Workaround

Change the maybeAddImport line to:

                        maybeAddImport("java.io.PrintWriter", false);

and the test passes.

Context

It seems like org.openrewrite.java.AddImport#hasReference doesn't honour try statements.

@greg-at-moderne greg-at-moderne added the bug Something isn't working label Mar 18, 2025
@greg-at-moderne greg-at-moderne moved this to Backlog in OpenRewrite Mar 18, 2025
@timtebeek
Copy link
Contributor

It looks like your JavaTemplate.builder is missing a call to imports; could that factor in here? Those snippets are compiled in isolation, so they need their classpath (beyond JDK) and imports added to the builder. The best way to troubleshoot these is through doBeforeParseTemplate(System.out::println)

@greg-at-moderne
Copy link
Contributor Author

greg-at-moderne commented Mar 18, 2025

It looks like your JavaTemplate.builder is missing a call to imports; could that factor in here?

It does. But the problem is still relevant even if I don't use JavaTemplate.
JavaTemplate seemed the easiest/smallest to put into the test.

Also the test fails with no import added with the following visitor code as well:

                    if (method.getBody().getStatements().isEmpty()) {
                        JavaTemplate t = JavaTemplate.builder("try (PrintWriter pw = new PrintWriter(\"/tmp/file\")) { pw.println(\"A\"); }")
                          .imports("java.io.PrintWriter")
                          .build();
                        return t.apply(getCursor(), method.getCoordinates().replaceBody());
                    }
                    return super.visitMethodDeclaration(method, p);

which I guess might be another problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: Backlog
Development

No branches or pull requests

2 participants