rewrite-migrate-java icon indicating copy to clipboard operation
rewrite-migrate-java copied to clipboard

MigrateFromJavaxAnnotationApi does not modify annotations to the type level

Open protocol7 opened this issue 3 months ago • 4 comments

What version of OpenRewrite are you using?

  • org.openrewrite:rewrite-core:8.65.0
  • org.openrewrite.recipe:rewrite-migrate-java:3.20.0

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

When arrays are annotated, the annotation is not adjusted to be at the type level when migrating to JSpecify. This causes the nullability analysis to fail with tools like NullAway.

See https://jspecify.dev/docs/user-guide/#type-use-annotation-syntax for a reference on these annotations.

  @Test
  void jspecifyArrayNullable() {
    final Environment env = Environment.builder().scanRuntimeClasspath().build();
    final Recipe recipe =
        env.activateRecipes("org.openrewrite.java.jspecify.MigrateFromJavaxAnnotationApi");
    rewriteRun(
        spec -> spec.recipe(recipe),
        // language=java
        java(
            """
            import javax.annotation.Nullable;

            class Foo {
                    @Nullable
                    byte[] bar() {
                        return null;
                    }

                    void baz(@Nullable byte[] a) {
                        return null;
                    }
            }
            """,
            """
            import org.jspecify.annotations.Nullable;

            class Foo {
                    byte @Nullable [] bar() {
                        return null;
                    }

                    void baz(byte @Nullable [] a) {
                        return null;
                    }
            }
            """));
  }
org.opentest4j.AssertionFailedError: [Unexpected result in "Foo.java":
diff --git a/Foo.java b/Foo.java
index 891ac0b..a4c2b3c 100644
--- a/Foo.java
+++ b/Foo.java
@@ -1,11 +1,12 @@
 import org.jspecify.annotations.Nullable;

 class Foo {
-        byte @Nullable [] bar() {
+        @Nullable
+        byte[] bar() {
             return null;
         }

-        void baz(byte @Nullable [] a) {
+        void baz(@Nullable byte[] a) {
             return null;
         }
 }
\ No newline at end of file
]
expected:
  "import org.jspecify.annotations.Nullable;

  class Foo {
          byte @Nullable [] bar() {
              return null;
          }

          void baz(byte @Nullable [] a) {
              return null;
          }
  }"
 but was:
  "import org.jspecify.annotations.Nullable;

  class Foo {
          @Nullable
          byte[] bar() {
              return null;
          }

          void baz(@Nullable byte[] a) {
              return null;
          }
  }"
	at org.openrewrite.test.RewriteTest.assertContentEquals(RewriteTest.java:630)
	at org.openrewrite.test.RewriteTest.rewriteRun(RewriteTest.java:520)
	at org.openrewrite.test.RewriteTest.rewriteRun(RewriteTest.java:132)

protocol7 avatar Nov 09 '25 09:11 protocol7