Skip to content

Commit

Permalink
Minor polish
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek committed Oct 27, 2024
1 parent 610c1c3 commit c81972d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
final JavaTemplate afterFileBoolean = Semantics.expression(
this,
"afterFileBoolean",
(java.io.File f, Boolean b) -> java.nio.file.Files.newBufferedWriter(f.toPath(), b ? java.nio.file.StandardOpenOption.APPEND : java.nio.file.StandardOpenOption.CREATE)
(java.io.File f, Boolean b) -> java.nio.file.Files.newBufferedWriter(f.toPath(), b ?
java.nio.file.StandardOpenOption.APPEND : java.nio.file.StandardOpenOption.CREATE)
).build();

final JavaTemplate beforeStringBoolean = Semantics.expression(
Expand All @@ -95,7 +96,8 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
final JavaTemplate afterStringBoolean = Semantics.expression(
this,
"afterStringBoolean",
(String f, Boolean b) -> java.nio.file.Files.newBufferedWriter(new java.io.File(f).toPath(), b ? java.nio.file.StandardOpenOption.APPEND : java.nio.file.StandardOpenOption.CREATE)
(String f, Boolean b) -> java.nio.file.Files.newBufferedWriter(new java.io.File(f).toPath(), b ?
java.nio.file.StandardOpenOption.APPEND : java.nio.file.StandardOpenOption.CREATE)
).build();

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package org.openrewrite.staticanalysis;

import org.jspecify.annotations.Nullable;
import org.openrewrite.Cursor;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
Expand Down Expand Up @@ -78,9 +77,9 @@ public J visitIf(J.If if_, ExecutionContext ctx) {

// The compile-time constant value of the if condition control parentheses.
final Optional<Boolean> compileTimeConstantBoolean;
if (isLiteralTrue(cp.getTree())) {
if (J.Literal.isLiteralValue(cp.getTree(), Boolean.TRUE)) {
compileTimeConstantBoolean = Optional.of(true);
} else if (isLiteralFalse(cp.getTree())) {
} else if (J.Literal.isLiteralValue(cp.getTree(), Boolean.FALSE)) {
compileTimeConstantBoolean = Optional.of(false);
} else {
// The condition is not a literal, so we can't simplify it.
Expand Down Expand Up @@ -134,26 +133,15 @@ public J visitIf(J.If if_, ExecutionContext ctx) {
}

@SuppressWarnings("unchecked")
static <E extends Expression> E cleanupBooleanExpression(
E expression, Cursor c, ExecutionContext ctx
) {
E ex1 =
(E) new UnnecessaryParenthesesVisitor<>()
.visitNonNull(expression, ctx, c.getParentOrThrow());
ex1 = (E) new SimplifyBooleanExpressionVisitor()
.visitNonNull(ex1, ctx, c.getParentTreeCursor());
if (expression == ex1 || isLiteralFalse(ex1) || isLiteralTrue(ex1)) {
static <E extends Expression> E cleanupBooleanExpression(E expression, Cursor c, ExecutionContext ctx) {
E ex1 = (E) new UnnecessaryParenthesesVisitor<>().visitNonNull(expression, ctx, c.getParentOrThrow());
ex1 = (E) new SimplifyBooleanExpressionVisitor().visitNonNull(ex1, ctx, c.getParentTreeCursor());
if (expression == ex1 ||
J.Literal.isLiteralValue(ex1, Boolean.FALSE) ||
J.Literal.isLiteralValue(ex1, Boolean.TRUE)) {
return ex1;
}
// Run recursively until no further changes are needed
return cleanupBooleanExpression(ex1, c, ctx);
}

private static boolean isLiteralTrue(@Nullable Expression expression) {
return J.Literal.isLiteralValue(expression, Boolean.TRUE);
}

private static boolean isLiteralFalse(@Nullable Expression expression) {
return J.Literal.isLiteralValue(expression, Boolean.FALSE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
@Override
public J visitTernary(J.Ternary ternary, ExecutionContext ctx) {
J.Ternary t = (J.Ternary) super.visitTernary(ternary, ctx);
Expression condition =
SimplifyConstantIfBranchExecution.cleanupBooleanExpression(
t.getCondition(),
getCursor(),
ctx
);
Expression condition = SimplifyConstantIfBranchExecution.cleanupBooleanExpression(t.getCondition(), getCursor(), ctx);
if (J.Literal.isLiteralValue(condition, true)) {
getCursor().getParentTreeCursor().putMessage("AUTO_FORMAT", true);
return t.getTruePart();
Expand Down

0 comments on commit c81972d

Please sign in to comment.