Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove inputstream finalizer #245

Merged
merged 12 commits into from
Jul 27, 2023
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* 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.JavaVisitor;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.search.UsesJavaVersion;
import org.openrewrite.java.search.UsesMethod;
import org.openrewrite.java.search.UsesType;
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.List;
import java.util.Set;

@Value
@EqualsAndHashCode(callSuper = true)
public class RemoveFinalizeFromFileStream extends Recipe {

private static final MethodMatcher JAVA_IO_FILEINPUTSTREAM = new MethodMatcher("java.io.FileInputStream finalize()", true);
private static final MethodMatcher JAVA_IO_FILEOUTPUTSTREAM = new MethodMatcher("java.io. FileOutputStream finalize()", true);
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
joanvr marked this conversation as resolved.
Show resolved Hide resolved

private static final MethodMatcher METHOD_MATCHER = new MethodMatcher("java.lang.Object finalize()");
timtebeek marked this conversation as resolved.
Show resolved Hide resolved

@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");
}

timtebeek marked this conversation as resolved.
Show resolved Hide resolved
@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {

return Preconditions.check(
Preconditions.and(
new UsesJavaVersion<>(9,11),
Preconditions.or(
new UsesMethod<>(JAVA_IO_FILEINPUTSTREAM),
new UsesMethod<>(JAVA_IO_FILEOUTPUTSTREAM))),
new JavaVisitor<ExecutionContext>() {

@Override
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext executionContext) {
J.MethodInvocation mi = (J.MethodInvocation) super.visitMethodInvocation(method, executionContext);

if (JAVA_IO_FILEINPUTSTREAM.matches(mi) || JAVA_IO_FILEOUTPUTSTREAM.matches(mi)) {
Expression select = mi.getSelect();
if (select == null) {
J.ClassDeclaration cd = getCursor().firstEnclosingOrThrow(J.ClassDeclaration.class);
if (shouldRemoveFinalize(cd.getType())) {
return mi.withName(mi.getName().withSimpleName("close"));
}
} else {
if (shouldRemoveFinalize(select.getType())) {
List<J> sideEffects = select.getSideEffects();
if (sideEffects.isEmpty()) {
return mi.withName(mi.getName().withSimpleName("close"));
}
if (sideEffects.size() == 1) {
return sideEffects.get(0).withPrefix(mi.getPrefix());
}
}
}
}

return mi;
}

private boolean shouldRemoveFinalize(JavaType type) {
return TypeUtils.isAssignableTo(JAVA_IO_FILEINPUTSTREAM.toString(), type)
|| TypeUtils.isAssignableTo(JAVA_IO_FILEOUTPUTSTREAM.toString(), type);
}
timtebeek marked this conversation as resolved.
Show resolved Hide resolved

});
}
}
21 changes: 21 additions & 0 deletions src/main/java/org/openrewrite/java/migrate/io/package-info.java
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;
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
/*
* 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.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.java.Assertions.java;
import static org.openrewrite.java.Assertions.version;

class RemoveFinalizeFromFileStreamTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new RemoveFinalizeFromFileStream());
}

@Test
void removeFinalizerForFileInputStream() {
//language=java
rewriteRun(
java(
"""
import java.io.FileInputStream;

class FooBar extends FileInputStream{
public void test(){
FooBar obj = new FooBar();
obj.finalize();
}
}
""",
"""
import java.io.FileInputStream;

class FooBar extends FileInputStream{
public void test(){
FooBar obj = new FooBar();
obj.close();
}
}
"""
)
);
}

@Test
void noChangeWithCloseForFileInputStream() {
//language=java
rewriteRun(
version(
java(
"""
import java.io.FileInputStream;

class FooBar extends FileInputStream{
public void test(){
FooBar obj = new FooBar();
obj.close();
}
}
"""
),
12
)
);
}

@Test
void noFinalizerUsedForFileInputStream() {
//language=java
rewriteRun(
version(
java(
"""
import java.io.FileInputStream;

class FooBar extends FileInputStream{
public void test(){
FooBar obj = new FooBar();
}
}
"""
),
12
)
);
}

@Test
void removeFinalizerForFileOutputStream() {
//language=java
rewriteRun(
version(
java(
"""
import java.io.FileOutputStream;

class FooBar extends FileOutputStream{
public void test(){
FooBar obj = new FooBar();
obj.finalize();
}
}
""",
"""
import java.io.FileOutputStream;

class FooBar extends FileOutputStream{
public void test(){
FooBar obj = new FooBar();
obj.close();
}
}
"""
),
12
)
);
}

@Test
void noChangeWithCloseForFileOutputStream() {
//language=java
rewriteRun(
version(
java(
"""
import java.io.FileOutputStream;

class FooBar extends FileOutputStream{
public void test(){
FooBar obj = new FooBar();
obj.close();
}
}
"""
),
12
)
);
}

@Test
void noFinalizerUsedForFileOutputStream() {
//language=java
rewriteRun(
version(
java(
"""
import java.io.FileOutputStream;

class FooBar extends FileOutputStream{
public void test(){
FooBar obj = new FooBar();
}
}
"""
),
12
)
);
}
}
Loading