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

RemoveUnusedImports removes used imports for annotated method parameters #4473

Open
mvitz opened this issue Sep 5, 2024 · 2 comments · May be fixed by #4474
Open

RemoveUnusedImports removes used imports for annotated method parameters #4473

mvitz opened this issue Sep 5, 2024 · 2 comments · May be fixed by #4474
Labels
bug Something isn't working

Comments

@mvitz
Copy link

mvitz commented Sep 5, 2024

What version of OpenRewrite are you using?

I am using

  • Maven plugin v5.39.2

How are you running OpenRewrite?

I am using the Maven plugin, and my project is a single module project.

<plugin>
  <groupId>org.openrewrite.maven</groupId>
  <artifactId>rewrite-maven-plugin</artifactId>
  <version>5.39.2</version>
  <configuration>
    <exportDatatables>true</exportDatatables>
    <activeRecipes>
      <recipe>org.openrewrite.java.RemoveUnusedImports</recipe>
    </activeRecipes>
  </configuration>
</plugin>

The complete bug (configuration and code) is also shown within https://github.com/mvitz/openrewrite-bug-removeunusedimports

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

import java.beans.BeanProperty;
record A(@BeanProperty String a) {}

Although @BeanProperty may not make sense at that place, it was the first annotation within the JDK that I found to make the example as small as possible. A more realistic scenario would be Spring Boots @DefaultValue which is used within the reproducer repository.

What did you expect to see?

The import for the @BeanProperty annotation should not be removed.

What did you see instead?

The import for the @BeanProperty annotation is removed, and the code does not compile afterwards because the import is needed and should not be removed.

What is the full stack trace of any errors you encountered?

Example output can be seen in the reproducer repository (mentioned above).

Are you interested in contributing a fix to OpenRewrite?

Probably I would like to contribute, but if this is something someone with prior knowledge can fix within a few minutes, I would not inisist in fixing that myself.

@mvitz mvitz added the bug Something isn't working label Sep 5, 2024
@timtebeek
Copy link
Contributor

Thanks for the clear example @mvitz ! I wonder if we register annotations on record parameters as TypesInUse. 🤔

Also: have you tried stripping away any elements not necessary to reproduce the issue? There's a lot of extra class annotations & methods that I hope are not needed to reproduce and fix the issue.

Once we've minimized the steps needed to reproduce it's then often easier to write a unit test , as seen here

@DocumentExample
@Test
void removeNamedImport() {
rewriteRun(
java(
"""
import java.util.List;
class A {}
""",
"class A {}")
);
}

@mvitz
Copy link
Author

mvitz commented Sep 5, 2024

@timtebeek sorry for not stripping it any further.

The bug can be reproduced with:

import java.beans.BeanProperty;
record A(@BeanProperty String a) {}

Edit: I have updated the description accordingly and are working on a fix for that.

@timtebeek timtebeek moved this to Backlog in OpenRewrite Sep 5, 2024
@mvitz mvitz linked a pull request Sep 5, 2024 that will close this issue
3 tasks
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

Successfully merging a pull request may close this issue.

2 participants