openrewrite / openrewrite/rewrite-templating

Unused import left over after argument removed from a method invocation with a refaster recipe

Open
#151 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Java
Stars
18
Forks
9
Avg merge
23m
Merged PRs (30d)
4

Description

Summary

It seems like when a method invocation gets a parameter removed, the RefasterTemplateProcessor is maybeRemoving imports based on method parameter type instead of the argument (call site) type.

What version of OpenRewrite are you using?

  • rewrite 8.56.1
  • rewrite-templating: 1.29.1

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

import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import org.openrewrite.java.template.RecipeDescriptor;

import java.nio.charset.Charset;

@RecipeDescriptor(
        name = "name",
        description = "description."
)
public class RemoveCharsetFromGetBytes {
    @BeforeTemplate
    byte[] getBytesWithCharset(String string, Charset charset) {
        return string.getBytes(charset);
    }

    @AfterTemplate
    byte[] getBytesNoCharset(String string) {
        return string.getBytes();
    }

}
    @Test
    void removeUnusedImport() {
        rewriteRun(
          spec -> spec.recipe(new RemoveCharsetFromGetBytesRecipe()),
          //language=java
          java(
            """
            import java.nio.charset.StandardCharsets;

            class A {
                byte[] method(String s) {
                    return s.getBytes(StandardCharsets.UTF_8);
                }
            }
            """,
            """
            class A {
                byte[] method(String s) {
                    return s.getBytes();
                }
            }
            """)
        );
    }

What did you expect to see?

The specified test fails because import java.nio.charset.StandardCharsets; is still present.

What did you see instead?

import java.nio.charset.StandardCharsets; removed.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in RefasterTemplateProcessor.java around the linked line near 645 and reproduce the issue with the removeUnusedImport test shown in the report. Verify how the removed invocation argument and method parameter affect import cleanup; done means the test's expected output has the correct handling of the unused StandardCharsets import.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.