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

ReplaceAnnotation replaces with wrong annotation in method with annotated argument and local variable #4441

Closed
timtebeek opened this issue Aug 24, 2024 · 1 comment · Fixed by #4442
Assignees
Labels
bug Something isn't working test provided Already replicated with a unit test, using JUnit pioneer's ExpectedToFail

Comments

@timtebeek
Copy link
Contributor

How are you running OpenRewrite?

Unit test based on https://github.com/openrewrite/rewrite/releases/tag/v8.33.7

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

@Test
void methodWithAnnotatedParameter() {
    rewriteRun(
      spec -> spec.recipe(new ReplaceAnnotation("@org.jetbrains.annotations.NotNull", "@lombok.NonNull", null)),
      java(
        """
          import org.jetbrains.annotations.NotNull;
          import org.jetbrains.annotations.Nullable;
          class ValueAnnotationDuplication {
              void methodName(
                  @Nullable final boolean valueVar) {
                  @NotNull final String nullableVar = "test";
              }
          }
          """,
        """
          import lombok.NonNull;
          import org.jetbrains.annotations.Nullable;
          class ValueAnnotationDuplication {
              void methodName(
                  @Nullable final boolean valueVar) {
                  @NonNull final String nullableVar = "test";
              }
          }
          """
      )
    );
}

What did you expect to see?

Above unit test to pass

What did you see instead?

Incorrect replacement

diff --git a/ValueAnnotationDuplication.java b/ValueAnnotationDuplication.java
index 3b9317e..a586033 100644
--- a/ValueAnnotationDuplication.java
+++ b/ValueAnnotationDuplication.java
@@ -1,8 +1,7 @@ 
-import lombok.NonNull;
 import org.jetbrains.annotations.Nullable;
 class ValueAnnotationDuplication {
     void methodName(
         @Nullable final boolean valueVar) {
-        @NonNull final String nullableVar = "test";
+        @Nullable final String nullableVar = "test";
     }
 }
\ No newline at end of file
@timtebeek timtebeek added the bug Something isn't working label Aug 24, 2024
@timtebeek timtebeek added the test provided Already replicated with a unit test, using JUnit pioneer's ExpectedToFail label Aug 24, 2024
@timtebeek timtebeek moved this to Backlog in OpenRewrite Aug 24, 2024
@timtebeek
Copy link
Contributor Author

Looks to go wrong here because we have no parameters

public <J2 extends J> J2 unsubstitute(J2 j) {
if (parameters.length == 0) {
return j;
}

Both annotations from the parent scope are then returned; after which we replace the first.

List<J.Annotation> gen = substitutions.unsubstitute(templateParser.parseAnnotations(getCursor(), substitutedTemplate));
if (gen.isEmpty()) {
throw new IllegalStateException("Unable to parse annotation from template: \n" +
substitutedTemplate +
"\nUse JavaTemplate.Builder.doBeforeParseTemplate() to see what stub is being generated and include it in any bug report.");
}
return gen.get(0).withPrefix(annotation.getPrefix());

timtebeek added a commit that referenced this issue Aug 24, 2024
@timtebeek timtebeek self-assigned this Aug 24, 2024
@timtebeek timtebeek moved this from Backlog to Ready to Review in OpenRewrite Aug 24, 2024
@github-project-automation github-project-automation bot moved this from Ready to Review to Done in OpenRewrite Aug 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working test provided Already replicated with a unit test, using JUnit pioneer's ExpectedToFail
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

1 participant