Skip to content

TestMethodsShouldBeVoid might fail with NullPointerException (NPE) on interfaces #874

@greg-at-moderne

Description

@greg-at-moderne

What version of OpenRewrite are you using?

current main of this repo = 9803871
OR = 8.68.1 or similar

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

Add jspecify to test classpath resources:

+    testParserClasspath("org.jspecify:jspecify:1.0.0")

Rebuild test type tables:

./gradlew createTestTypeTable

In TestMethodsShouldBeVoidTest add jspecify:

-            .classpathFromResources(new InMemoryExecutionContext(), "junit-jupiter-api-5", "junit-jupiter-params-5"))
+            .classpathFromResources(new InMemoryExecutionContext(), "junit-jupiter-api-5", "junit-jupiter-params-5", "jspecify"))

And add the following test case:

    @Test
    void interfaces() {
        //language=java
        rewriteRun(
          java(
              """
              import org.jspecify.annotations.Nullable;

              public interface Doer {
                  @Nullable
                  String getFile(String input);
              }
              """
          )
        );
    }

Observed

            Caused by:
            java.lang.NullPointerException
                at java.base/java.util.Objects.requireNonNull(Objects.java:233)
                at org.openrewrite.java.testing.cleanup.TestMethodsShouldBeVoid$1.visitMethodDeclaration(TestMethodsShouldBeVoid.java:83)
                at org.openrewrite.java.testing.cleanup.TestMethodsShouldBeVoid$1.visitMethodDeclaration(TestMethodsShouldBeVoid.java:53)
                at org.openrewrite.java.tree.J$MethodDeclaration.acceptJava(J.java:4021)
                at org.openrewrite.java.tree.J.accept(J.java:55)
                at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:242)
                ... 57 more

Commentary

I think there are at least two problems:

  • one is assuming method body is not null (which might be not true for interfaces)
  • the TEST_ANNOTATION_PATTERN false positive against org.jspecify.annotations.Nullable

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions