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

Add NoGuavaByteStreams Refaster recipe using Java 9+ #399

Closed
wants to merge 9 commits into from
22 changes: 21 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ dependencies {
testImplementation("org.projectlombok:lombok:latest.release")

annotationProcessor("org.openrewrite:rewrite-templating:$rewriteVersion")
compileOnly("com.google.errorprone:error_prone_core:2.19.1:with-dependencies") {
compileOnly("com.google.errorprone:error_prone_core:2.19.1") {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise the recipe will pick error_prone_support as the lib that provides the Guava classes, and put that in the generated template classpath leading to mismatches.

exclude("com.google.auto.service", "auto-service-annotations")
}

Expand Down Expand Up @@ -60,3 +60,23 @@ dependencies {
testRuntimeOnly("org.codehaus.groovy:groovy:latest.release")
testRuntimeOnly(gradleApi())
}

// Add a source set for refaster rules that allows for Java 9+ syntax
val refaster by sourceSets.registering {
java {
val main = sourceSets.main.get()
annotationProcessorPath += main.annotationProcessorPath
compileClasspath += main.output + main.compileClasspath
runtimeClasspath += main.output + main.runtimeClasspath
}
}
sourceSets.named("test").configure {
compileClasspath += refaster.get().output.classesDirs
runtimeClasspath += refaster.get().output.classesDirs
}

tasks {
jar {
from(refaster.get().output)
}
}
1 change: 1 addition & 0 deletions src/main/resources/META-INF/rewrite/no-guava.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ tags:
- java11
recipeList:
- org.openrewrite.java.migrate.guava.NoGuava
- org.openrewrite.java.migrate.guava.NoGuavaByteStreamsRecipes
- org.openrewrite.java.migrate.guava.PreferJavaUtilObjectsRequireNonNullElse
---
type: specs.openrewrite.org/v1beta/recipe
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2024 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.guava;

import com.google.common.io.ByteStreams;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import org.openrewrite.java.template.RecipeDescriptor;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

@RecipeDescriptor(name = "No Guava ByteStreams",
description = "Replaces Guava ByteStreams with Java 9+ alternatives.",
tags = "guava")
public class NoGuavaByteStreams {
private NoGuavaByteStreams() {
}

@RecipeDescriptor(
name = "ByteStreams#copy",
description = "Replaces Guava `ByteStreams.copy` with `InputStream.transferTo`.",
tags = "guava")
public static final class InputStreamTransferTo {
@BeforeTemplate
long before(InputStream in, OutputStream out) throws IOException {
return ByteStreams.copy(in, out);
}

@AfterTemplate
long after(InputStream in, OutputStream out) throws IOException {
return in.transferTo(out);
}
}

@RecipeDescriptor(
name = "ByteStreams#toByteArray",
description = "Replaces Guava `ByteStreams.toByteArray` with `InputStream.readAllBytes`.",
tags = "guava")
public static final class InputStreamReadAllBytes {
@BeforeTemplate
byte[] before(InputStream in) throws IOException {
return ByteStreams.toByteArray(in);
}

@AfterTemplate
byte[] after(InputStream in) throws IOException {
return in.readAllBytes();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2024 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.guava;

import org.junit.jupiter.api.Test;
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
import org.openrewrite.DocumentExample;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

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

class NoGuavaByteStreamsTest implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
spec
.recipe(new NoGuavaByteStreamsRecipes())
.parser(JavaParser.fromJavaVersion().classpath("guava"));
}

@DocumentExample
@Test
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
void replace() {
rewriteRun(
//language=java
java(
"""
import com.google.common.io.ByteStreams;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

class Foo {
long testInputStreamTransferTo(InputStream from, OutputStream to) throws IOException {
return ByteStreams.copy(from, to);
}
byte[] testInputStreamReadAllBytes(InputStream from) throws IOException {
return ByteStreams.toByteArray(from);
}
}
""",
"""
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

class Foo {
long testInputStreamTransferTo(InputStream from, OutputStream to) throws IOException {
return from.transferTo(to);
}
byte[] testInputStreamReadAllBytes(InputStream from) throws IOException {
return from.readAllBytes();
}
}
"""
)
);
}
}