Skip to content

ReplaceCollectionToArrayArgWithEmptyArray might fail with NPE #703

@greg-at-moderne

Description

@greg-at-moderne

What version of OpenRewrite are you using?

OR current main (7edbbf01ab8a9b666ef3c2003ee7b8e0c66a4967)
rewrite-static-analysis (428d8b0)

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

    @Test
    void newArrayBraces() {
        rewriteRun(
          //language=java
          java(
            """
              import java.util.Collection;

              class A {
                  void test(Collection<Integer> args){
                      args.toArray(new Integer[]{});
                  }
              }
            """
          )
        );
    }

in src/test/java/org/openrewrite/staticanalysis/ReplaceCollectionToArrayArgWithEmptyArrayTest.java

Observed

            java.lang.NullPointerException
                at java.base/java.util.Objects.requireNonNull(Objects.java:233)
                at org.openrewrite.staticanalysis.ReplaceCollectionToArrayArgWithEmptyArray$ReplaceCollectionToArrayArgWithEmptyArrayVisitor.lambda$visitNewArray$0(ReplaceCollectionToArrayArgWithEmptyArray.java:79)
                at org.openrewrite.staticanalysis.ReplaceCollectionToArrayArgWithEmptyArray$ReplaceCollectionToArrayArgWithEmptyArrayVisitor$$Lambda/0x000000f00146e550.apply(Unknown Source)
                at org.openrewrite.internal.ListUtils.mapFirst(ListUtils.java:166)
                at org.openrewrite.internal.ListUtils.mapFirst(ListUtils.java:183)
                at org.openrewrite.staticanalysis.ReplaceCollectionToArrayArgWithEmptyArray$ReplaceCollectionToArrayArgWithEmptyArrayVisitor.visitNewArray(ReplaceCollectionToArrayArgWithEmptyArray.java:68)
                at org.openrewrite.staticanalysis.ReplaceCollectionToArrayArgWithEmptyArray$ReplaceCollectionToArrayArgWithEmptyArrayVisitor.visitNewArray(ReplaceCollectionToArrayArgWithEmptyArray.java:61)

OSS repro

Metadata

Metadata

Labels

bugSomething isn't workinggood first issueGood for newcomers

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions