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

Removing an annotation from a class may result in an extra empty line #659

Open
wapkch opened this issue Jan 6, 2025 · 1 comment
Open
Labels
bug Something isn't working

Comments

@wapkch
Copy link
Contributor

wapkch commented Jan 6, 2025

What version of OpenRewrite are you using?

I am using

  • rewrite-spring 5.25.0

How are you running OpenRewrite?

Unit Tests.

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

The issue can be reproduced with the following code snippet:

class RemoveEnableBatchProcessingTest implements RewriteTest {

    @Override
    public void defaults(RecipeSpec spec) {
        spec.recipe(new RemoveEnableBatchProcessing())
          .parser(JavaParser.fromJavaVersion().classpath("spring-batch-core", "spring-boot"));
    }

    @Test
    void removeSpringBatchAnnotation() {
        rewriteRun(
          //language=java
          java(
            """
              import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
              import org.springframework.boot.autoconfigure.SpringBootApplication;
                            
              @EnableBatchProcessing
              @SpringBootApplication
              public class Application {
              }
              """,
            """
              import org.springframework.boot.autoconfigure.SpringBootApplication;
                            
              @SpringBootApplication
              public class Application {
              }
              """
          )
        );
    }

    @Test
    void removeSpringBatchAnnotation2() {
        rewriteRun(
          //language=java
          java(
            """
              import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
              import org.springframework.boot.autoconfigure.SpringBootApplication;
                            
              @SpringBootApplication
              @EnableBatchProcessing
              public class Application {
              }
              """,
            """
              import org.springframework.boot.autoconfigure.SpringBootApplication;
                            
              @SpringBootApplication
              public class Application {
              }
              """
          )
        );
    }
}

What did you expect to see?

I expected both test methods (removeSpringBatchAnnotation and removeSpringBatchAnnotation2) to run successfully.

What did you see instead?

The removeSpringBatchAnnotation test method passed successfully, but the removeSpringBatchAnnotation2 method failed with following error:

org.opentest4j.AssertionFailedError: [Unexpected result in "Application.java":
diff --git a/Application.java b/Application.java
index 0eaa8a0..7bbfe15 100644
--- a/Application.java
+++ b/Application.java
@@ -1,5 +1,6 @@ 
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 
+
 @SpringBootApplication
 public class Application {
 }
\ No newline at end of file
] 
expected: 
  "import org.springframework.boot.autoconfigure.SpringBootApplication;
  
  @SpringBootApplication
  public class Application {
  }"
 but was: 
  "import org.springframework.boot.autoconfigure.SpringBootApplication;
  
  
  @SpringBootApplication
  public class Application {
  }"

Are you interested in contributing a fix to OpenRewrite?

I’m not entirely sure what the underlying issue is, but I would be happy to contribute a fix if you can provide some guidance on how to resolve it.

@wapkch wapkch added the bug Something isn't working label Jan 6, 2025
@timtebeek
Copy link
Contributor

hi @wapkch ; thanks for logging a runnable example! To give a bit of context: preceding newlines and comments are stored in a prefix, that can be manipulated by recipes as well.

In this case we remove the annotation by returning null here:


At that point I suspect we manipulate the prefix in openrewrite/rewrite, meaning you end up with the double newlines in some cases. As such the fix is likely upstream, and then related to handling of leading annotations. Any help exploring that would be appreciated, and then benefit all the cases where two annotations are present and one removed.

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: No status
Development

No branches or pull requests

2 participants