diff --git a/src/main/java/org/openrewrite/staticanalysis/BufferedWriterCreation.java b/src/main/java/org/openrewrite/staticanalysis/BufferedWriterCreation.java index f1af593a..4085a592 100644 --- a/src/main/java/org/openrewrite/staticanalysis/BufferedWriterCreation.java +++ b/src/main/java/org/openrewrite/staticanalysis/BufferedWriterCreation.java @@ -31,6 +31,7 @@ import java.util.Set; import static org.openrewrite.java.template.internal.AbstractRefasterJavaVisitor.EmbeddingOption.SHORTEN_NAMES; +import static org.openrewrite.java.template.internal.AbstractRefasterJavaVisitor.EmbeddingOption.SIMPLIFY_BOOLEANS; public class BufferedWriterCreation extends Recipe { @@ -139,13 +140,12 @@ public J visitNewClass(J.NewClass elem, ExecutionContext ctx) { JavaTemplate.Matcher matcher; if ((matcher = before.matcher(getCursor())).find()) { maybeRemoveImport("java.io.FileWriter"); - J j = embed( + return embed( after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0), matcher.parameter(1)), getCursor(), ctx, - SHORTEN_NAMES + SHORTEN_NAMES, SIMPLIFY_BOOLEANS ); - return (J) new SimplifyConstantTernaryExecution().getVisitor().visitNonNull(j, ctx, getCursor().getParentOrThrow()); } return null; } diff --git a/src/main/java/org/openrewrite/staticanalysis/SimplifyConstantTernaryExecution.java b/src/main/java/org/openrewrite/staticanalysis/SimplifyConstantTernaryExecution.java deleted file mode 100644 index 7f1dfe32..00000000 --- a/src/main/java/org/openrewrite/staticanalysis/SimplifyConstantTernaryExecution.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright 2024 the original author or authors. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * https://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.openrewrite.staticanalysis; - -import org.jspecify.annotations.Nullable; -import org.openrewrite.ExecutionContext; -import org.openrewrite.Recipe; -import org.openrewrite.Repeat; -import org.openrewrite.TreeVisitor; -import org.openrewrite.java.JavaVisitor; -import org.openrewrite.java.tree.Expression; -import org.openrewrite.java.tree.J; - -import java.time.Duration; - -public class SimplifyConstantTernaryExecution extends Recipe { - @Override - public String getDisplayName() { - return "Simplify constant ternary branch execution"; - } - - @Override - public String getDescription() { - return "Checks for ternary expressions that are always `true` or `false` and simplifies them."; - } - - @Override - public @Nullable Duration getEstimatedEffortPerOccurrence() { - return Duration.ofSeconds(15); - } - - @Override - public TreeVisitor getVisitor() { - JavaVisitor v = new JavaVisitor() { - @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); - if (J.Literal.isLiteralValue(condition, true)) { - getCursor().getParentTreeCursor().putMessage("AUTO_FORMAT", true); - return t.getTruePart(); - } else if (J.Literal.isLiteralValue(condition, false)) { - getCursor().getParentTreeCursor().putMessage("AUTO_FORMAT", true); - return t.getFalsePart(); - } - return t; - } - - @Override - public @Nullable J postVisit(J tree, ExecutionContext ctx) { - if (getCursor().pollMessage("AUTO_FORMAT") != null) { - return autoFormat(tree, ctx); - } - return super.postVisit(tree, ctx); - } - }; - return Repeat.repeatUntilStable(v); - } -} diff --git a/src/test/java/org/openrewrite/staticanalysis/SimplifyConstantTernaryExecutionTest.java b/src/test/java/org/openrewrite/staticanalysis/SimplifyConstantTernaryExecutionTest.java deleted file mode 100644 index c03e2e03..00000000 --- a/src/test/java/org/openrewrite/staticanalysis/SimplifyConstantTernaryExecutionTest.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Copyright 2024 the original author or authors. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * https://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.openrewrite.staticanalysis; - -import org.junit.jupiter.api.Test; -import org.openrewrite.DocumentExample; -import org.openrewrite.test.RecipeSpec; -import org.openrewrite.test.RewriteTest; - -import static org.openrewrite.java.Assertions.java; - -@SuppressWarnings("ConstantConditionalExpression") -class SimplifyConstantTernaryExecutionTest implements RewriteTest { - - @Override - public void defaults(RecipeSpec spec) { - spec.recipe(new SimplifyConstantTernaryExecution()); - } - - @Test - @DocumentExample - void simplifyConstantTernaryExecution() { - rewriteRun( - //language=java - java( - """ - class Test { - void test() { - System.out.println(true ? "foo" : "bar"); - } - } - """, - """ - class Test { - void test() { - System.out.println("foo"); - } - } - """ - ) - ); - } - - @Test - void simplifyUnnecessaryParenthesesTernary() { - rewriteRun( - //language=java - java( - """ - class Test { - void test() { - System.out.println((true) ? "foo" : "bar"); - } - } - """, - """ - class Test { - void test() { - System.out.println("foo"); - } - } - """ - ) - ); - } -}