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, ExecutionContext> getVisitor() {
- JavaVisitor
- * 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");
- }
- }
- """
- )
- );
- }
-}