-
Notifications
You must be signed in to change notification settings - Fork 78
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
+160
−1
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d5206b4
Add NoGuavaByteStreams Refaster recipe using Java 9+
timtebeek 25fb1d5
Minor polish
timtebeek 8f23609
Define a refaster configuration and pass in guava
timtebeek 35ee063
Drop `error_prone_core` qualifier `with-dependencies`
timtebeek ce61091
Include refaster classes in jar
timtebeek db67497
Merge remote-tracking branch 'origin/main' into add-NoGuavaByteStream…
timtebeek 2af8051
Use `sourceSets.registering` instead of `create`
timtebeek 7d2e4d1
Merge branch 'main' into add-NoGuavaByteStreams-recipe
timtebeek 9cabac2
Apply suggestions from code review
timtebeek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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") | ||
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(); | ||
} | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
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,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(); | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.