openrewrite / rewrite-static-analysis

OpenRewrite recipes for identifying and fixing static analysis issues.
Apache License 2.0
27 stars 43 forks source link

NullPointerException in CombineSemanticallyEqualCatchBlocks recipe #278

Closed Marklinzi closed 2 months ago

Marklinzi commented 3 months ago

What version of OpenRewrite are you using?

I am using

How are you running OpenRewrite?

I am using the Maven plugin, and my project is a multi module project.

<plugin>
  <groupId>org.openrewrite.maven</groupId>
  <artifactId>rewrite-maven-plugin</artifactId>
  <version>5.25.0</version>
  <configuration>
    <activeRecipes>
      <recipe>org.openrewrite.staticanalysis.CombineSemanticallyEqualCatchBlocks</recipe>
    </activeRecipes>
    <dependencies>
        <dependency>
          <groupId>org.openrewrite.recipe</groupId>
          <artifactId>rewrite-static-analysis</artifactId>
          <version>1.4.0</version>
        </dependency>
      </dependencies>
  </configuration>
</plugin>

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

class Test {
    void method() {
        try {
        } catch (A ex) {
            String s = "foo";
        } catch (B ex) {
            String s = "foo";
        }
    }
}

What did you expect to see?

class Test {
    void method() {
        try {
        } catch (A | B ex) {
            String s = "foo";
        }
    }
}

What did you see instead?

A NullPointerException.

What is the full stack trace of any errors you encountered?

org.openrewrite.internal.RecipeRunException: java.lang.NullPointerException: Cannot invoke "org.openrewrite.java.tree.Space.getComments()" because "space1" is null
    at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:334)
    at org.openrewrite.staticanalysis.CombineSemanticallyEqualCatchBlocks$CombineSemanticallyEqualCatchBlocksVisitor$CommentVisitor.visitBlock(CombineSemanticallyEqualCatchBlocks.java:560)
    at org.openrewrite.staticanalysis.CombineSemanticallyEqualCatchBlocks$CombineSemanticallyEqualCatchBlocksVisitor$CommentVisitor.visitBlock(CombineSemanticallyEqualCatchBlocks.java:292)
    at org.openrewrite.java.tree.J$Block.acceptJava(J.java:838)
    at org.openrewrite.java.tree.J.accept(J.java:59)
    at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:283)
    at org.openrewrite.staticanalysis.CombineSemanticallyEqualCatchBlocks$CombineSemanticallyEqualCatchBlocksVisitor.containSameComments(CombineSemanticallyEqualCatchBlocks.java:279)
    at org.openrewrite.staticanalysis.CombineSemanticallyEqualCatchBlocks$CombineSemanticallyEqualCatchBlocksVisitor.visitTry(CombineSemanticallyEqualCatchBlocks.java:74)
    at org.openrewrite.staticanalysis.CombineSemanticallyEqualCatchBlocks$CombineSemanticallyEqualCatchBlocksVisitor.visitTry(CombineSemanticallyEqualCatchBlocks.java:61)
    at org.openrewrite.java.tree.J$Try.acceptJava(J.java:5329)
    at org.openrewrite.java.tree.J.accept(J.java:59)
    at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:283)
    at org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:366)
    at org.openrewrite.java.JavaVisitor.visitRightPadded(JavaVisitor.java:1375)
    at org.openrewrite.java.JavaVisitor.lambda$visitBlock$4(JavaVisitor.java:401)
    at org.openrewrite.internal.ListUtils.map(ListUtils.java:176)
    at org.openrewrite.java.JavaVisitor.visitBlock(JavaVisitor.java:400)
    at org.openrewrite.java.tree.J$Block.acceptJava(J.java:838)
    at org.openrewrite.java.tree.J.accept(J.java:59)
    at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:283)
    at org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:366)
    at org.openrewrite.java.JavaVisitor.visitMethodDeclaration(JavaVisitor.java:883)
    at org.openrewrite.java.tree.J$MethodDeclaration.acceptJava(J.java:3672)
    at org.openrewrite.java.tree.J.accept(J.java:59)
    at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:283)
    at org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:366)
    at org.openrewrite.java.JavaVisitor.visitRightPadded(JavaVisitor.java:1375)
    at org.openrewrite.java.JavaVisitor.lambda$visitBlock$4(JavaVisitor.java:401)
    at org.openrewrite.internal.ListUtils.map(ListUtils.java:176)
    at org.openrewrite.java.JavaVisitor.visitBlock(JavaVisitor.java:400)
    at org.openrewrite.java.tree.J$Block.acceptJava(J.java:838)
    at org.openrewrite.java.tree.J.accept(J.java:59)
    at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:283)
    at org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:366)
    at org.openrewrite.java.JavaVisitor.visitClassDeclaration(JavaVisitor.java:488)
    at org.openrewrite.java.tree.J$ClassDeclaration.acceptJava(J.java:1290)
    at org.openrewrite.java.tree.J.accept(J.java:59)
    at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:283)
    at org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:366)
    at org.openrewrite.java.JavaVisitor.lambda$visitCompilationUnit$10(JavaVisitor.java:501)
    at org.openrewrite.internal.ListUtils.map(ListUtils.java:176)
    at org.openrewrite.java.JavaVisitor.visitCompilationUnit(JavaVisitor.java:501)
    at org.openrewrite.java.tree.J$CompilationUnit.acceptJava(J.java:1592)
    at org.openrewrite.java.tree.J.accept(J.java:59)
    at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:283)
    at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:184)
    at org.openrewrite.scheduling.RecipeRunCycle.lambda$editSources$5(RecipeRunCycle.java:164)
    at io.micrometer.core.instrument.AbstractTimer.recordCallable(AbstractTimer.java:147)
    at org.openrewrite.table.RecipeRunStats.recordEdit(RecipeRunStats.java:68)
    at org.openrewrite.scheduling.RecipeRunCycle.lambda$editSources$6(RecipeRunCycle.java:161)
    ... 16 more
Caused by: java.lang.NullPointerException: Cannot invoke "org.openrewrite.java.tree.Space.getComments()" because "space1" is null
    at org.openrewrite.staticanalysis.CombineSemanticallyEqualCatchBlocks$CombineSemanticallyEqualCatchBlocksVisitor$CommentVisitor.doesNotContainSameComments(CombineSemanticallyEqualCatchBlocks.java:302)
    at org.openrewrite.staticanalysis.CombineSemanticallyEqualCatchBlocks$CombineSemanticallyEqualCatchBlocksVisitor$CommentVisitor.visitVariableDeclarations(CombineSemanticallyEqualCatchBlocks.java:1696)
    at org.openrewrite.staticanalysis.CombineSemanticallyEqualCatchBlocks$CombineSemanticallyEqualCatchBlocksVisitor$CommentVisitor.visitVariableDeclarations(CombineSemanticallyEqualCatchBlocks.java:292)
    at org.openrewrite.java.tree.J$VariableDeclarations.acceptJava(J.java:5824)
    at org.openrewrite.java.tree.J.accept(J.java:59)
    at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:283)
    ... 65 more

Are you interested in contributing a fix to OpenRewrite?

I will create a PR.