Class SimplifyBooleanExpressionVisitor

java.lang.Object
org.openrewrite.TreeVisitor<org.openrewrite.java.tree.J,org.openrewrite.ExecutionContext>
org.openrewrite.java.JavaVisitor<org.openrewrite.ExecutionContext>
org.openrewrite.java.cleanup.SimplifyBooleanExpressionVisitor

public class SimplifyBooleanExpressionVisitor extends JavaVisitor<org.openrewrite.ExecutionContext>
  • Constructor Details

    • SimplifyBooleanExpressionVisitor

      public SimplifyBooleanExpressionVisitor()
  • Method Details

    • visitBinary

      public org.openrewrite.java.tree.J visitBinary(org.openrewrite.java.tree.J.Binary binary, org.openrewrite.ExecutionContext ctx)
      Overrides:
      visitBinary in class JavaVisitor<org.openrewrite.ExecutionContext>
    • visitParentheses

      public <T extends org.openrewrite.java.tree.J> org.openrewrite.java.tree.J visitParentheses(org.openrewrite.java.tree.J.Parentheses<T> parens, org.openrewrite.ExecutionContext ctx)
      Overrides:
      visitParentheses in class JavaVisitor<org.openrewrite.ExecutionContext>
    • visitUnary

      public org.openrewrite.java.tree.J visitUnary(org.openrewrite.java.tree.J.Unary unary, org.openrewrite.ExecutionContext ctx)
      Overrides:
      visitUnary in class JavaVisitor<org.openrewrite.ExecutionContext>
    • visitTernary

      public org.openrewrite.java.tree.J visitTernary(org.openrewrite.java.tree.J.Ternary ternary, org.openrewrite.ExecutionContext executionContext)
      Overrides:
      visitTernary in class JavaVisitor<org.openrewrite.ExecutionContext>
    • visitMethodInvocation

      public org.openrewrite.java.tree.J visitMethodInvocation(org.openrewrite.java.tree.J.MethodInvocation method, org.openrewrite.ExecutionContext executionContext)
      Overrides:
      visitMethodInvocation in class JavaVisitor<org.openrewrite.ExecutionContext>
    • shouldSimplifyEqualsOn

      protected boolean shouldSimplifyEqualsOn(org.openrewrite.java.tree.J j)
      Determines whether an equals comparison with a boolean literal can be simplified.

      In Java, x == true can always be simplified to x. In Kotlin and other languages, nullable types like Boolean? compared with == true have different semantics than using the value directly, e.g. nullableBoolean == true evaluates to false when null, whereas using nullableBoolean directly would be a type error.

      For non-Java languages, simplification is only allowed when the expression type is primitive boolean (non-nullable).

      Parameters:
      j - the expression to simplify
      Returns:
      true if the equals comparison can be safely simplified