-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* recipe for replace finalize() from FileInputStream and FileOutputStream with close() * io fileinputstream and fileoutputstream finalize replacement recipe * license header * fixed recipe based on review comments * recipe formatting changes * Remove excess space * Fix various test compilation issues * Switch to JavaIsoVisitor and simplify implementation * Rename recipes * Simplify test cases as extends is not needed here --------- Co-authored-by: Tim te Beek <[email protected]> Co-authored-by: Tim te Beek <[email protected]>
- Loading branch information
1 parent
4745b92
commit bb41637
Showing
3 changed files
with
329 additions
and
0 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
...in/java/org/openrewrite/java/migrate/io/ReplaceFileInOrOutputStreamFinalizeWithClose.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright 2023 the original author or authors. | ||
* <p> | ||
* 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 | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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.java.migrate.io; | ||
|
||
import lombok.EqualsAndHashCode; | ||
import lombok.Value; | ||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.Preconditions; | ||
import org.openrewrite.Recipe; | ||
import org.openrewrite.TreeVisitor; | ||
import org.openrewrite.java.JavaIsoVisitor; | ||
import org.openrewrite.java.MethodMatcher; | ||
import org.openrewrite.java.search.UsesJavaVersion; | ||
import org.openrewrite.java.search.UsesMethod; | ||
import org.openrewrite.java.tree.Expression; | ||
import org.openrewrite.java.tree.J; | ||
import org.openrewrite.java.tree.JavaType; | ||
import org.openrewrite.java.tree.TypeUtils; | ||
|
||
import java.util.Collections; | ||
import java.util.Set; | ||
|
||
@Value | ||
@EqualsAndHashCode(callSuper = true) | ||
public class ReplaceFileInOrOutputStreamFinalizeWithClose extends Recipe { | ||
|
||
private static final String JAVA_IO_FILE_INPUT_STREAM = "java.io.FileInputStream"; | ||
private static final String JAVA_IO_FILE_OUTPUT_STREAM = "java.io.FileOutputStream"; | ||
private static final MethodMatcher METHOD_MATCHER = new MethodMatcher("java.lang.Object finalize()"); | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "Replace invocations of `finalize()` on `FileInputStream` and `FileOutputStream` with `close()`"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Replace invocations of the deprecated `finalize()` method on `FileInputStream` and `FileOutputStream` with `close()`."; | ||
} | ||
|
||
@Override | ||
public Set<String> getTags() { | ||
return Collections.singleton("JDK-8212050"); | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
return Preconditions.check( | ||
Preconditions.and(new UsesJavaVersion<>(9, 11), new UsesMethod<>(METHOD_MATCHER)), | ||
new JavaIsoVisitor<ExecutionContext>() { | ||
@Override | ||
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext executionContext) { | ||
J.MethodInvocation mi = super.visitMethodInvocation(method, executionContext); | ||
if (METHOD_MATCHER.matches(mi)) { | ||
Expression select = mi.getSelect(); | ||
JavaType type = select != null ? select.getType() : getCursor().firstEnclosingOrThrow(J.ClassDeclaration.class).getType(); | ||
if (TypeUtils.isAssignableTo(JAVA_IO_FILE_INPUT_STREAM, type) | ||
|| TypeUtils.isAssignableTo(JAVA_IO_FILE_OUTPUT_STREAM, type)) { | ||
return mi.withName(mi.getName().withSimpleName("close")); | ||
} | ||
} | ||
return mi; | ||
} | ||
} | ||
); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/org/openrewrite/java/migrate/io/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright 2023 the original author or authors. | ||
* <p> | ||
* 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 | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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. | ||
*/ | ||
@NonNullApi | ||
@NonNullFields | ||
package org.openrewrite.java.migrate.io; | ||
|
||
import org.openrewrite.internal.lang.NonNullApi; | ||
import org.openrewrite.internal.lang.NonNullFields; |
227 changes: 227 additions & 0 deletions
227
...ava/org/openrewrite/java/migrate/io/ReplaceFileInOrOutputStreamFinalizeWithCloseTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,227 @@ | ||
/* | ||
* Copyright 2023 the original author or authors. | ||
* <p> | ||
* 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 | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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.java.migrate.io; | ||
|
||
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; | ||
import static org.openrewrite.java.Assertions.javaVersion; | ||
|
||
class ReplaceFileInOrOutputStreamFinalizeWithCloseTest implements RewriteTest { | ||
|
||
@Override | ||
public void defaults(RecipeSpec spec) { | ||
spec | ||
.recipe(new ReplaceFileInOrOutputStreamFinalizeWithClose()) | ||
.allSources(s -> s.markers(javaVersion(11))); | ||
} | ||
|
||
@Test | ||
@DocumentExample | ||
void removeFinalizerForFileInputStream() { | ||
//language=java | ||
rewriteRun( | ||
java( | ||
""" | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
class FooBar { | ||
public void test() throws IOException { | ||
FileInputStream obj = new FileInputStream("foo"); | ||
obj.finalize(); | ||
} | ||
} | ||
""", | ||
""" | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
class FooBar { | ||
public void test() throws IOException { | ||
FileInputStream obj = new FileInputStream("foo"); | ||
obj.close(); | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void replaceDirectCall() { | ||
//language=java | ||
rewriteRun( | ||
java( | ||
""" | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
class FooBar { | ||
public void test() throws IOException { | ||
new FileInputStream("foo").finalize(); | ||
} | ||
} | ||
""", | ||
""" | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
class FooBar { | ||
public void test() throws IOException { | ||
new FileInputStream("foo").close(); | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void replaceOnExtends() { | ||
//language=java | ||
rewriteRun( | ||
java( | ||
""" | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
class FooBar extends FileInputStream { | ||
FooBar() throws IOException { | ||
super("foo"); | ||
} | ||
public void test() throws IOException { | ||
new FooBar().finalize(); | ||
} | ||
} | ||
""", | ||
""" | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
class FooBar extends FileInputStream { | ||
FooBar() throws IOException { | ||
super("foo"); | ||
} | ||
public void test() throws IOException { | ||
new FooBar().close(); | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void noChangeOnAnyOtherFinalize() { | ||
//language=java | ||
rewriteRun( | ||
java( | ||
""" | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
class FooBar extends FileInputStream { | ||
FooBar() throws IOException { | ||
super("foo"); | ||
} | ||
public void test() { | ||
new Object().finalize(); | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void noChangeWithCloseForFileInputStream() { | ||
//language=java | ||
rewriteRun( | ||
java( | ||
""" | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
class FooBar extends FileInputStream { | ||
FooBar() throws IOException { | ||
super("foo"); | ||
} | ||
public void test() throws IOException { | ||
FileInputStream obj = new FileInputStream("foo"); | ||
obj.close(); | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void noFinalizerUsedForFileInputStream() { | ||
//language=java | ||
rewriteRun( | ||
java( | ||
""" | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
class FooBar { | ||
public void test() throws IOException { | ||
FileInputStream obj = new FileInputStream("foo"); | ||
obj.read(); | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void removeFinalizerForFileOutputStream() { | ||
//language=java | ||
rewriteRun( | ||
java( | ||
""" | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
class FooBar { | ||
public void test() throws IOException { | ||
FileOutputStream obj = new FileOutputStream("foo"); | ||
obj.finalize(); | ||
} | ||
} | ||
""", | ||
""" | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
class FooBar { | ||
public void test() throws IOException { | ||
FileOutputStream obj = new FileOutputStream("foo"); | ||
obj.close(); | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
} |