diff --git a/src/main/java/org/openrewrite/staticanalysis/TryWithResources.java b/src/main/java/org/openrewrite/staticanalysis/TryWithResources.java new file mode 100644 index 000000000..c5a17be5a --- /dev/null +++ b/src/main/java/org/openrewrite/staticanalysis/TryWithResources.java @@ -0,0 +1,480 @@ +/* + * Copyright 2025 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.openrewrite.ExecutionContext;
+import org.openrewrite.Recipe;
+import org.openrewrite.Tree;
+import org.openrewrite.TreeVisitor;
+import org.openrewrite.internal.ListUtils;
+import org.openrewrite.java.JavaIsoVisitor;
+import org.openrewrite.java.tree.*;
+import org.openrewrite.marker.Markers;
+
+import java.util.*;
+
+/**
+ * Transforms code using manual resource management with finally blocks to use the Java 7+ try-with-resources pattern.
+ * This transformation improves code safety and readability by ensuring resources are properly closed.
+ */
+public class TryWithResources extends Recipe {
+
+ private static final JavaType.ShallowClass AUTO_CLOSEABLE_TYPE = JavaType.ShallowClass.build("java.lang.AutoCloseable");
+
+ @Override
+ public String getDisplayName() {
+ return "Use try-with-resources";
+ }
+
+ @Override
+ public String getDescription() {
+ return "Converts code using manual resource management with finally blocks to use the Java 7+ try-with-resources pattern. " +
+ "This transformation improves code safety and readability by ensuring resources are properly closed.";
+ }
+
+ @Override
+ public TreeVisitor, ExecutionContext> getVisitor() {
+ return new JavaIsoVisitor
+ * 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 TryWithResourcesTest implements RewriteTest {
+ @Override
+ public void defaults(RecipeSpec spec) {
+ spec.recipe(new TryWithResources());
+ }
+
+ @DocumentExample
+ @Test
+ void basicTransformation() {
+ rewriteRun(
+ java(
+ """
+ import java.io.*;
+
+ class Test {
+ void method() throws IOException {
+ InputStream in = new FileInputStream("file.txt");
+ try {
+ int data = in.read();
+ // Process data
+ } finally {
+ in.close();
+ }
+ }
+ }
+ """,
+ """
+ import java.io.*;
+
+ class Test {
+ void method() throws IOException {
+ try (InputStream in = new FileInputStream("file.txt")) {
+ int data = in.read();
+ // Process data
+ }
+ }
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void multipleResources() {
+ rewriteRun(
+ java(
+ """
+ import java.io.*;
+
+ class Test {
+ void method() throws IOException {
+ InputStream in = new FileInputStream("input.txt");
+ OutputStream out = new FileOutputStream("output.txt");
+ try {
+ int data = in.read();
+ out.write(data);
+ } finally {
+ in.close();
+ out.close();
+ }
+ }
+ }
+ """,
+ """
+ import java.io.*;
+
+ class Test {
+ void method() throws IOException {
+ try (InputStream in = new FileInputStream("input.txt");
+ OutputStream out = new FileOutputStream("output.txt")) {
+ int data = in.read();
+ out.write(data);
+ }
+ }
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void nullCheckInFinally() {
+ rewriteRun(
+ java(
+ """
+ import java.io.*;
+
+ class Test {
+ void method() throws IOException {
+ InputStream in = new FileInputStream("file.txt");
+ try {
+ int data = in.read();
+ // Process data
+ } finally {
+ if (in != null) {
+ in.close();
+ }
+ }
+ }
+ }
+ """,
+ """
+ import java.io.*;
+
+ class Test {
+ void method() throws IOException {
+ try (InputStream in = new FileInputStream("file.txt")) {
+ int data = in.read();
+ // Process data
+ }
+ }
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void complexFinallyBlock() {
+ rewriteRun(
+ java(
+ """
+ import java.io.*;
+
+ class Test {
+ void method() throws IOException {
+ InputStream in = new FileInputStream("file.txt");
+ try {
+ int data = in.read();
+ // Process data
+ } finally {
+ in.close();
+ System.out.println("Processing complete");
+ }
+ }
+ }
+ """,
+ """
+ import java.io.*;
+
+ class Test {
+ void method() throws IOException {
+ try (InputStream in = new FileInputStream("file.txt")) {
+ int data = in.read();
+ // Process data
+ } finally {
+ System.out.println("Processing complete");
+ }
+ }
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void nestedTryBlocks() {
+ rewriteRun(
+ java(
+ """
+ import java.io.*;
+
+ class Test {
+ void method() throws IOException {
+ InputStream in = new FileInputStream("file.txt");
+ try {
+ OutputStream out = new FileOutputStream("output.txt");
+ try {
+ int data = in.read();
+ out.write(data);
+ } finally {
+ out.close();
+ }
+ } finally {
+ in.close();
+ }
+ }
+ }
+ """,
+ """
+ import java.io.*;
+
+ class Test {
+ void method() throws IOException {
+ try (InputStream in = new FileInputStream("file.txt")) {
+ try (OutputStream out = new FileOutputStream("output.txt")) {
+ int data = in.read();
+ out.write(data);
+ }
+ }
+ }
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void tryCatchFinally() {
+ rewriteRun(
+ java(
+ """
+ import java.io.*;
+
+ class Test {
+ void method() {
+ InputStream in = null;
+ try {
+ in = new FileInputStream("file.txt");
+ int data = in.read();
+ // Process data
+ } catch (IOException e) {
+ e.printStackTrace();
+ } finally {
+ if (in != null) {
+ try {
+ in.close();
+ } catch (IOException e) {
+ // Ignore
+ }
+ }
+ }
+ }
+ }
+ """,
+ """
+ import java.io.*;
+
+ class Test {
+ void method() {
+ try (InputStream in = new FileInputStream("file.txt")) {
+ int data = in.read();
+ // Process data
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void doNotTransformWhenResourceNotClosed() {
+ rewriteRun(
+ java(
+ """
+ import java.io.*;
+
+ class Test {
+ void method() throws IOException {
+ InputStream in = new FileInputStream("file.txt");
+ try {
+ int data = in.read();
+ // Process data
+ } finally {
+ // Resource not closed
+ System.out.println("Processing complete");
+ }
+ }
+ }
+ """
+ )
+ );
+ }
+}