-
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.
Add NoGuavaByteStreams Refaster recipe using Java 9+
- Loading branch information
Showing
4 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
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
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
65 changes: 65 additions & 0 deletions
65
src/refaster/java/org/openrewrite/java/migrate/guava/NoGuavaByteStreams.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,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(); | ||
} | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
src/test/java/org/openrewrite/java/migrate/guava/NoGuavaByteStreamsTest.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,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(); | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
} |