From a746b8eae69e96951ffc823d29e0d168fcb50579 Mon Sep 17 00:00:00 2001 From: I753089 Date: Mon, 14 Oct 2024 15:54:58 +0200 Subject: [PATCH 1/8] 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/8] 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/8] 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/8] 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/8] 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 c051c6e005d7aadcea6a9c5d8d61e7cded7bff15 Mon Sep 17 00:00:00 2001 From: I753089 Date: Mon, 14 Oct 2024 20:47:03 +0200 Subject: [PATCH 6/8] EmptyLinesTest --- .../staticanalysis/EmptyLinesTest.java | 151 ++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 src/test/java/org/openrewrite/staticanalysis/EmptyLinesTest.java diff --git a/src/test/java/org/openrewrite/staticanalysis/EmptyLinesTest.java b/src/test/java/org/openrewrite/staticanalysis/EmptyLinesTest.java new file mode 100644 index 000000000..e5ef2307f --- /dev/null +++ b/src/test/java/org/openrewrite/staticanalysis/EmptyLinesTest.java @@ -0,0 +1,151 @@ +/* + * 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 EmptyLinesTest implements RewriteTest { + @Override + public void defaults(RecipeSpec spec) { + spec.recipe(new InlineOneTimeUsageVariable()); + } + + @DocumentExample + @Test + void inlineVariable() { + rewriteRun( + //language=java + java( + """ + class Test { + void test() { + } + void test2() { + } + } + """, + """ + class Test { + + void test() { + } + + void test2() { + } + + } + """ + ) + ); + } + + @DocumentExample + @Test + void inlineVariable2() { + rewriteRun( + //language=java + java( + """ + class Test { + + void test() { + } + void test2() { + } + } + """, + """ + class Test { + + void test() { + } + + void test2() { + } + + } + """ + ) + ); + } + + @DocumentExample + @Test + void inlineVariable3() { + rewriteRun( + //language=java + java( + """ + class Test { + void test() { + } + + void test2() { + } + } + """, + """ + class Test { + + void test() { + } + + void test2() { + } + + } + """ + ) + ); + } + + @DocumentExample + @Test + void inlineVariable4() { + rewriteRun( + //language=java + java( + """ + class Test { + + void test() { + } + + void test2() { + } + } + """, + """ + class Test { + + void test() { + } + + void test2() { + } + + } + """ + ) + ); + } + +} From 3bd7ec88ff990551c0650f0abbf4ee710268a081 Mon Sep 17 00:00:00 2001 From: I753089 Date: Mon, 14 Oct 2024 20:50:01 +0200 Subject: [PATCH 7/8] EmptyLines --- .../InlineOneTimeUsageVariable.java | 118 ------------------ .../staticanalysis/EmptyLinesTest.java | 2 +- .../InlineOneTimeUsageVariableTest.java | 86 ------------- 3 files changed, 1 insertion(+), 205 deletions(-) delete mode 100644 src/main/java/org/openrewrite/staticanalysis/InlineOneTimeUsageVariable.java delete 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 deleted file mode 100644 index bf7524038..000000000 --- a/src/main/java/org/openrewrite/staticanalysis/InlineOneTimeUsageVariable.java +++ /dev/null @@ -1,118 +0,0 @@ -/* - * 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.List; -import java.util.Set; - -import static java.time.Duration.ofMinutes; -import static java.util.Objects.requireNonNull; - -public class InlineOneTimeUsageVariable extends Recipe { - - @Override - public String getDisplayName() { - return "Inline variable"; - } - - @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."; - } - - @Override - public Set getTags() { - return Set.of("RSPEC-S1488"); - } - - @Override - public Duration getEstimatedEffortPerOccurrence() { - return 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/EmptyLinesTest.java b/src/test/java/org/openrewrite/staticanalysis/EmptyLinesTest.java index e5ef2307f..a8642356b 100644 --- a/src/test/java/org/openrewrite/staticanalysis/EmptyLinesTest.java +++ b/src/test/java/org/openrewrite/staticanalysis/EmptyLinesTest.java @@ -25,7 +25,7 @@ class EmptyLinesTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { - spec.recipe(new InlineOneTimeUsageVariable()); + // TODO spec.recipe(new EmptyLines()); } @DocumentExample diff --git a/src/test/java/org/openrewrite/staticanalysis/InlineOneTimeUsageVariableTest.java b/src/test/java/org/openrewrite/staticanalysis/InlineOneTimeUsageVariableTest.java deleted file mode 100644 index f8e45d09b..000000000 --- a/src/test/java/org/openrewrite/staticanalysis/InlineOneTimeUsageVariableTest.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * 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 InlineOneTimeUsageVariable()); - } - - @DocumentExample - @SuppressWarnings({"UnnecessaryLocalVariable", "CodeBlock2Expr", "Convert2MethodRef"}) - @Test - void inlineVariable() { - rewriteRun( - //language=java - java( - """ - class Test { - Object test() { - int y = 0; - int n = y; - return n; - } - Object test2() { - int y = 0; - int n = y; - System.out.println(n); - return n; - } - Object test3() { - String s = "0"; - String s2 = s; - return s2; - } - Object test4() { - String s = "0"; - String s2 = s; - String s3 = s2; - return s3; - } - } - """, - """ - class Test { - Object test() { - return 0; - } - Object test2() { - int n = 0; - System.out.println(n); - return n; - } - Object test3() { - return "0"; - } - Object test4() { - return "0"; - } - } - """ - ) - ); - } - -} From 628c42422046aebda6a770d58fa5318298f99bbb Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Mon, 21 Oct 2024 15:01:04 +0200 Subject: [PATCH 8/8] Apply suggestions from code review --- .../openrewrite/staticanalysis/EmptyLinesTest.java | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/test/java/org/openrewrite/staticanalysis/EmptyLinesTest.java b/src/test/java/org/openrewrite/staticanalysis/EmptyLinesTest.java index a8642356b..2a6bf2256 100644 --- a/src/test/java/org/openrewrite/staticanalysis/EmptyLinesTest.java +++ b/src/test/java/org/openrewrite/staticanalysis/EmptyLinesTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2022 the original author or authors. + * 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. @@ -30,7 +30,7 @@ public void defaults(RecipeSpec spec) { @DocumentExample @Test - void inlineVariable() { + void addNewLinesAroundMethods() { rewriteRun( //language=java java( @@ -57,9 +57,8 @@ void test2() { ); } - @DocumentExample @Test - void inlineVariable2() { + void addNewLinesBetweenMethods() { rewriteRun( //language=java java( @@ -87,9 +86,8 @@ void test2() { ); } - @DocumentExample @Test - void inlineVariable3() { + void addNewLinesBeforeMethod() { rewriteRun( //language=java java( @@ -117,9 +115,8 @@ void test2() { ); } - @DocumentExample @Test - void inlineVariable4() { + void addNewLinesAfterMethod() { rewriteRun( //language=java java(