From a746b8eae69e96951ffc823d29e0d168fcb50579 Mon Sep 17 00:00:00 2001 From: I753089 Date: Mon, 14 Oct 2024 15:54:58 +0200 Subject: [PATCH 1/7] InlineOneTimeUsageVariableTest --- .../InlineOneTimeUsageVariable.java | 116 ++++++++++++++++++ .../InlineOneTimeUsageVariableTest.java | 69 +++++++++++ 2 files changed, 185 insertions(+) create mode 100644 src/main/java/org/openrewrite/staticanalysis/InlineOneTimeUsageVariable.java create mode 100644 src/test/java/org/openrewrite/staticanalysis/InlineOneTimeUsageVariableTest.java diff --git a/src/main/java/org/openrewrite/staticanalysis/InlineOneTimeUsageVariable.java b/src/main/java/org/openrewrite/staticanalysis/InlineOneTimeUsageVariable.java new file mode 100644 index 000000000..9a25cf00f --- /dev/null +++ b/src/main/java/org/openrewrite/staticanalysis/InlineOneTimeUsageVariable.java @@ -0,0 +1,116 @@ +/* + * Copyright 2022 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.TreeVisitor; +import org.openrewrite.internal.ListUtils; +import org.openrewrite.java.JavaIsoVisitor; +import org.openrewrite.java.tree.Expression; +import org.openrewrite.java.tree.J; +import org.openrewrite.java.tree.JavaType; +import org.openrewrite.java.tree.Statement; + +import java.time.Duration; +import java.util.Collections; +import java.util.List; +import java.util.Set; + +import static java.util.Objects.requireNonNull; + +public class InlineOneTimeUsageVariable extends Recipe { + + @Override + public String getDisplayName() { + return "Inline variable"; + } + + @Override + public String getDescription() { + return "Inline variables when they are immediately used to return or throw."; + } + + @Override + public Set getTags() { + return Collections.singleton("RSPEC-S1488"); + } + + @Override + public Duration getEstimatedEffortPerOccurrence() { + return Duration.ofMinutes(2); + } + + @Override + public TreeVisitor getVisitor() { + return new JavaIsoVisitor<>() { + @Override + public J.Block visitBlock(J.Block block, ExecutionContext ctx) { + J.Block bl = super.visitBlock(block, ctx); + List statements = bl.getStatements(); + if (statements.size() > 1) { + String identReturned = identReturned(statements); + if (identReturned != null) { + if (statements.get(statements.size() - 2) instanceof J.VariableDeclarations) { + J.VariableDeclarations varDec = (J.VariableDeclarations) statements.get(statements.size() - 2); + J.VariableDeclarations.NamedVariable identDefinition = varDec.getVariables().get(0); + if (varDec.getLeadingAnnotations().isEmpty() && identDefinition.getSimpleName().equals(identReturned)) { + bl = bl.withStatements(ListUtils.map(statements, (i, statement) -> { + if (i == statements.size() - 2) { + return null; + } else if (i == statements.size() - 1) { + if (statement instanceof J.Return) { + J.Return return_ = (J.Return) statement; + return return_.withExpression(requireNonNull(identDefinition.getInitializer()) + .withPrefix(requireNonNull(return_.getExpression()).getPrefix())) + .withPrefix(varDec.getPrefix().withComments(ListUtils.concatAll(varDec.getComments(), return_.getComments()))); + } else if (statement instanceof J.Throw) { + J.Throw thrown = (J.Throw) statement; + return thrown.withException(requireNonNull(identDefinition.getInitializer()) + .withPrefix(requireNonNull(thrown.getException()).getPrefix())) + .withPrefix(varDec.getPrefix().withComments(ListUtils.concatAll(varDec.getComments(), thrown.getComments()))); + } + } + return statement; + })); + } + } + } + } + return bl; + } + + private @Nullable String identReturned(List stats) { + Statement lastStatement = stats.get(stats.size() - 1); + if (lastStatement instanceof J.Return) { + J.Return return_ = (J.Return) lastStatement; + Expression expression = return_.getExpression(); + if (expression instanceof J.Identifier && + !(expression.getType() instanceof JavaType.Array)) { + return ((J.Identifier) expression).getSimpleName(); + } + } else if (lastStatement instanceof J.Throw) { + J.Throw thr = (J.Throw) lastStatement; + if (thr.getException() instanceof J.Identifier) { + return ((J.Identifier) thr.getException()).getSimpleName(); + } + } + return null; + } + }; + } +} diff --git a/src/test/java/org/openrewrite/staticanalysis/InlineOneTimeUsageVariableTest.java b/src/test/java/org/openrewrite/staticanalysis/InlineOneTimeUsageVariableTest.java new file mode 100644 index 000000000..85a149ac1 --- /dev/null +++ b/src/test/java/org/openrewrite/staticanalysis/InlineOneTimeUsageVariableTest.java @@ -0,0 +1,69 @@ +/* + * Copyright 2022 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; + +class InlineOneTimeUsageVariableTest implements RewriteTest { + @Override + public void defaults(RecipeSpec spec) { + spec.recipe(new InlineVariable()); + } + + @DocumentExample + @SuppressWarnings({"UnnecessaryLocalVariable", "CodeBlock2Expr", "Convert2MethodRef"}) + @Test + void inlineVariable() { + rewriteRun( + //language=java + java( + """ + class Test { + int test() { + int y = 0; + int n = y; + return n; + } + int test2() { + int y = 0; + int n = y; + System.out.println(n); + return n; + } + } + \s""", + """ + class Test { + int test() { + return 0; + } + int test2() { + int n = 0; + System.out.println(n); + return n; + } + } + \s""" + ) + ); + } + +} From e2cefe2c379f3841ad2a5d77c6dadd8b1de0f56c Mon Sep 17 00:00:00 2001 From: I753089 Date: Mon, 14 Oct 2024 15:57:28 +0200 Subject: [PATCH 2/7] InlineOneTimeUsageVariableTest --- .../InlineOneTimeUsageVariableTest.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/test/java/org/openrewrite/staticanalysis/InlineOneTimeUsageVariableTest.java b/src/test/java/org/openrewrite/staticanalysis/InlineOneTimeUsageVariableTest.java index 85a149ac1..37da2a5f1 100644 --- a/src/test/java/org/openrewrite/staticanalysis/InlineOneTimeUsageVariableTest.java +++ b/src/test/java/org/openrewrite/staticanalysis/InlineOneTimeUsageVariableTest.java @@ -48,6 +48,17 @@ int test2() { System.out.println(n); return n; } + int test3() { + String s = "0"; + String s2 = s; + return s2; + } + int test4() { + String s = "0"; + String s2 = s; + String s3 = s2; + return s3; + } } \s""", """ @@ -60,6 +71,12 @@ int test2() { System.out.println(n); return n; } + int test3() { + return "0"; + } + int test4() { + return "0"; + } } \s""" ) From 8c003c984e4beafb66dc6383f03a20424eceeffa Mon Sep 17 00:00:00 2001 From: I753089 Date: Mon, 14 Oct 2024 16:01:35 +0200 Subject: [PATCH 3/7] fix InlineOneTimeUsageVariableTest --- .../InlineOneTimeUsageVariable.java | 5 +- .../InlineOneTimeUsageVariableTest.java | 84 +++++++++---------- 2 files changed, 45 insertions(+), 44 deletions(-) diff --git a/src/main/java/org/openrewrite/staticanalysis/InlineOneTimeUsageVariable.java b/src/main/java/org/openrewrite/staticanalysis/InlineOneTimeUsageVariable.java index 9a25cf00f..e511f649f 100644 --- a/src/main/java/org/openrewrite/staticanalysis/InlineOneTimeUsageVariable.java +++ b/src/main/java/org/openrewrite/staticanalysis/InlineOneTimeUsageVariable.java @@ -57,7 +57,7 @@ public Duration getEstimatedEffortPerOccurrence() { @Override public TreeVisitor getVisitor() { - return new JavaIsoVisitor<>() { + return new JavaIsoVisitor() { @Override public J.Block visitBlock(J.Block block, ExecutionContext ctx) { J.Block bl = super.visitBlock(block, ctx); @@ -66,7 +66,8 @@ public J.Block visitBlock(J.Block block, ExecutionContext ctx) { String identReturned = identReturned(statements); if (identReturned != null) { if (statements.get(statements.size() - 2) instanceof J.VariableDeclarations) { - J.VariableDeclarations varDec = (J.VariableDeclarations) statements.get(statements.size() - 2); + J.VariableDeclarations varDec = + (J.VariableDeclarations) statements.get(statements.size() - 2); J.VariableDeclarations.NamedVariable identDefinition = varDec.getVariables().get(0); if (varDec.getLeadingAnnotations().isEmpty() && identDefinition.getSimpleName().equals(identReturned)) { bl = bl.withStatements(ListUtils.map(statements, (i, statement) -> { diff --git a/src/test/java/org/openrewrite/staticanalysis/InlineOneTimeUsageVariableTest.java b/src/test/java/org/openrewrite/staticanalysis/InlineOneTimeUsageVariableTest.java index 37da2a5f1..3bf921e36 100644 --- a/src/test/java/org/openrewrite/staticanalysis/InlineOneTimeUsageVariableTest.java +++ b/src/test/java/org/openrewrite/staticanalysis/InlineOneTimeUsageVariableTest.java @@ -25,7 +25,7 @@ class InlineOneTimeUsageVariableTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { - spec.recipe(new InlineVariable()); + spec.recipe(new InlineOneTimeUsageVariable()); } @DocumentExample @@ -36,49 +36,49 @@ void inlineVariable() { //language=java java( """ - class Test { - int test() { - int y = 0; - int n = y; - return n; - } - int test2() { - int y = 0; - int n = y; - System.out.println(n); - return n; - } - int test3() { - String s = "0"; - String s2 = s; - return s2; - } - int test4() { - String s = "0"; - String s2 = s; - String s3 = s2; - return s3; - } - } - \s""", + class Test { + int test() { + int y = 0; + int n = y; + return n; + } + int test2() { + int y = 0; + int n = y; + System.out.println(n); + return n; + } + int test3() { + String s = "0"; + String s2 = s; + return s2; + } + int test4() { + String s = "0"; + String s2 = s; + String s3 = s2; + return s3; + } + } + """, """ - class Test { - int test() { - return 0; - } - int test2() { - int n = 0; - System.out.println(n); - return n; - } - int test3() { - return "0"; - } - int test4() { + class Test { + int test() { + return 0; + } + int test2() { + int n = 0; + System.out.println(n); + return n; + } + int test3() { return "0"; - } - } - \s""" + } + int test4() { + return "0"; + } + } + """ ) ); } From 8a7dbc8c97fed3db2645d7f7652a8f45c679840e Mon Sep 17 00:00:00 2001 From: I753089 Date: Mon, 14 Oct 2024 16:03:23 +0200 Subject: [PATCH 4/7] fix --- .../InlineOneTimeUsageVariableTest.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/test/java/org/openrewrite/staticanalysis/InlineOneTimeUsageVariableTest.java b/src/test/java/org/openrewrite/staticanalysis/InlineOneTimeUsageVariableTest.java index 3bf921e36..f8e45d09b 100644 --- a/src/test/java/org/openrewrite/staticanalysis/InlineOneTimeUsageVariableTest.java +++ b/src/test/java/org/openrewrite/staticanalysis/InlineOneTimeUsageVariableTest.java @@ -37,23 +37,23 @@ void inlineVariable() { java( """ class Test { - int test() { + Object test() { int y = 0; int n = y; return n; } - int test2() { + Object test2() { int y = 0; int n = y; System.out.println(n); return n; } - int test3() { + Object test3() { String s = "0"; String s2 = s; return s2; } - int test4() { + Object test4() { String s = "0"; String s2 = s; String s3 = s2; @@ -63,18 +63,18 @@ int test4() { """, """ class Test { - int test() { + Object test() { return 0; } - int test2() { + Object test2() { int n = 0; System.out.println(n); return n; } - int test3() { + Object test3() { return "0"; } - int test4() { + Object test4() { return "0"; } } From 4fa7c37bbfd8c9f9f7e2cf01c1ef0cc742de6c6d Mon Sep 17 00:00:00 2001 From: I753089 Date: Mon, 14 Oct 2024 16:08:22 +0200 Subject: [PATCH 5/7] wip --- .../staticanalysis/InlineOneTimeUsageVariable.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/openrewrite/staticanalysis/InlineOneTimeUsageVariable.java b/src/main/java/org/openrewrite/staticanalysis/InlineOneTimeUsageVariable.java index e511f649f..bf7524038 100644 --- a/src/main/java/org/openrewrite/staticanalysis/InlineOneTimeUsageVariable.java +++ b/src/main/java/org/openrewrite/staticanalysis/InlineOneTimeUsageVariable.java @@ -27,10 +27,10 @@ import org.openrewrite.java.tree.Statement; import java.time.Duration; -import java.util.Collections; import java.util.List; import java.util.Set; +import static java.time.Duration.ofMinutes; import static java.util.Objects.requireNonNull; public class InlineOneTimeUsageVariable extends Recipe { @@ -42,17 +42,18 @@ public String getDisplayName() { @Override public String getDescription() { - return "Inline variables when they are immediately used to return or throw."; + return "Inline ALL variables when they used only once for documentation purposes applying imperative style " + + "and to blow up the code like crazy."; } @Override public Set getTags() { - return Collections.singleton("RSPEC-S1488"); + return Set.of("RSPEC-S1488"); } @Override public Duration getEstimatedEffortPerOccurrence() { - return Duration.ofMinutes(2); + return ofMinutes(2); } @Override From febba1eff9df95aa1cdcea06b75131d7a042356d Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Wed, 4 Dec 2024 13:45:16 +0100 Subject: [PATCH 6/7] Fix compilation --- .../staticanalysis/InlineOneTimeUsageVariable.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/openrewrite/staticanalysis/InlineOneTimeUsageVariable.java b/src/main/java/org/openrewrite/staticanalysis/InlineOneTimeUsageVariable.java index bf7524038..7a6e92251 100644 --- a/src/main/java/org/openrewrite/staticanalysis/InlineOneTimeUsageVariable.java +++ b/src/main/java/org/openrewrite/staticanalysis/InlineOneTimeUsageVariable.java @@ -27,6 +27,7 @@ import org.openrewrite.java.tree.Statement; import java.time.Duration; +import java.util.Collections; import java.util.List; import java.util.Set; @@ -37,18 +38,17 @@ public class InlineOneTimeUsageVariable extends Recipe { @Override public String getDisplayName() { - return "Inline variable"; + return "Inline local variables used only once"; } @Override public String getDescription() { - return "Inline ALL variables when they used only once for documentation purposes applying imperative style " + - "and to blow up the code like crazy."; + return "Inline local variables that are only used once, to reduce the code or clean up after other recipes."; } @Override public Set getTags() { - return Set.of("RSPEC-S1488"); + return Collections.singleton("RSPEC-S1488"); } @Override From 7183eae6ea5889e6152f6758448a4fd044fe30f6 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Wed, 4 Dec 2024 13:46:52 +0100 Subject: [PATCH 7/7] Reduce diff as compared to InlineVariable --- .../staticanalysis/InlineOneTimeUsageVariable.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/openrewrite/staticanalysis/InlineOneTimeUsageVariable.java b/src/main/java/org/openrewrite/staticanalysis/InlineOneTimeUsageVariable.java index 7a6e92251..fe1682610 100644 --- a/src/main/java/org/openrewrite/staticanalysis/InlineOneTimeUsageVariable.java +++ b/src/main/java/org/openrewrite/staticanalysis/InlineOneTimeUsageVariable.java @@ -31,7 +31,6 @@ import java.util.List; import java.util.Set; -import static java.time.Duration.ofMinutes; import static java.util.Objects.requireNonNull; public class InlineOneTimeUsageVariable extends Recipe { @@ -53,7 +52,7 @@ public Set getTags() { @Override public Duration getEstimatedEffortPerOccurrence() { - return ofMinutes(2); + return Duration.ofMinutes(2); } @Override @@ -67,8 +66,7 @@ public J.Block visitBlock(J.Block block, ExecutionContext ctx) { String identReturned = identReturned(statements); if (identReturned != null) { if (statements.get(statements.size() - 2) instanceof J.VariableDeclarations) { - J.VariableDeclarations varDec = - (J.VariableDeclarations) statements.get(statements.size() - 2); + J.VariableDeclarations varDec = (J.VariableDeclarations) statements.get(statements.size() - 2); J.VariableDeclarations.NamedVariable identDefinition = varDec.getVariables().get(0); if (varDec.getLeadingAnnotations().isEmpty() && identDefinition.getSimpleName().equals(identReturned)) { bl = bl.withStatements(ListUtils.map(statements, (i, statement) -> { @@ -102,7 +100,7 @@ public J.Block visitBlock(J.Block block, ExecutionContext ctx) { J.Return return_ = (J.Return) lastStatement; Expression expression = return_.getExpression(); if (expression instanceof J.Identifier && - !(expression.getType() instanceof JavaType.Array)) { + !(expression.getType() instanceof JavaType.Array)) { return ((J.Identifier) expression).getSimpleName(); } } else if (lastStatement instanceof J.Throw) {