Skip to content

Commit

Permalink
Add NoGuavaByteStreams Refaster recipe using Java 9+
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek committed Jan 21, 2024
1 parent d12d72b commit d5206b4
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 0 deletions.
13 changes: 13 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,16 @@ dependencies {
testRuntimeOnly("org.codehaus.groovy:groovy:latest.release")
testRuntimeOnly(gradleApi())
}

// Add a source set for refaster rules that allows for Java 9+ syntax
sourceSets {
val refaster = create("refaster") {
java {
compileClasspath += sourceSets.main.get().output + sourceSets.main.get().compileClasspath
runtimeClasspath += sourceSets.main.get().output + sourceSets.main.get().runtimeClasspath
annotationProcessorPath += sourceSets.main.get().annotationProcessorPath
}
}
sourceSets.test.get().compileClasspath += refaster.output
sourceSets.test.get().runtimeClasspath += refaster.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")
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")
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,70 @@
/*
* 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;
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"));
}

@Test
void replace() {
rewriteRun(
//language=java
java(
"""
import com.google.common.io.ByteStreams;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
class InputStreamRulesTest {
long testInputStreamTransferTo() throws IOException {
return ByteStreams.copy(new ByteArrayInputStream(new byte[0]), new ByteArrayOutputStream());
}
byte[] testInputStreamReadAllBytes() throws IOException {
return ByteStreams.toByteArray(new ByteArrayInputStream(new byte[0]));
}
}
""",
"""
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
class InputStreamRulesTest {
long testInputStreamTransferTo() throws IOException {
return new ByteArrayInputStream(new byte[0]).transferTo(new ByteArrayOutputStream());
}
byte[] testInputStreamReadAllBytes() throws IOException {
return new ByteArrayInputStream(new byte[0]).readAllBytes();
}
}
"""
)
);
}
}

0 comments on commit d5206b4

Please sign in to comment.