Skip to content

Commit

Permalink
Update ApacheBase64ToJavaBase64 to support converting 'Base64#encodeB…
Browse files Browse the repository at this point in the history
…ase64URLSafe(..)' and `Base64#encodeBase64URLSafeString(encodeBytes)` (fixes #90)
  • Loading branch information
pway99 committed Apr 15, 2022
1 parent 9581ed7 commit c01b28b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.search.UsesType;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaSourceFile;
import org.openrewrite.java.tree.JavaType;

import java.time.Duration;
Expand Down Expand Up @@ -53,6 +54,8 @@ protected JavaIsoVisitor<ExecutionContext> getVisitor() {
private final MethodMatcher apacheEncodeToString = new MethodMatcher("org.apache.commons.codec.binary.Base64 encodeBase64String(byte[])");
private final MethodMatcher apacheEncode64 = new MethodMatcher("org.apache.commons.codec.binary.Base64 encodeBase64(byte[])");
private final MethodMatcher apacheDecode = new MethodMatcher("org.apache.commons.codec.binary.Base64 decodeBase64(..)");
private final MethodMatcher apacheEncode64UrlSafe = new MethodMatcher("org.apache.commons.codec.binary.Base64 encodeBase64URLSafe(..)");
private final MethodMatcher apacheEncode64UrlSafeString = new MethodMatcher("org.apache.commons.codec.binary.Base64 encodeBase64URLSafeString(..)");

@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext executionContext) {
Expand All @@ -65,6 +68,10 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
templatePrefix = "Base64.getEncoder().encode(#{anyArray()})";
} else if (apacheDecode.matches(mi)) {
templatePrefix = "Base64.getDecoder().decode(#{any(String)})";
} else if (apacheEncode64UrlSafe.matches(mi)) {
templatePrefix = "Base64.getUrlEncoder().withoutPadding().encode(#{anyArray()})";
} else if (apacheEncode64UrlSafeString.matches(mi)) {
templatePrefix = "Base64.getUrlEncoder().withoutPadding().encodeToString(#{anyArray()})";
}
if (templatePrefix != null) {
JavaTemplate t = JavaTemplate.builder(this::getCursor, templatePrefix).imports("java.util.Base64").build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ApacheBase64ToJavaBase64Test : JavaRecipeTest {
.build()

@Test
fun toJavaBase64() = assertChanged(
fun toJavaBase64() = assertChanged(
before = """
import org.apache.commons.codec.binary.Base64;
Expand All @@ -48,6 +48,12 @@ class ApacheBase64ToJavaBase64Test : JavaRecipeTest {
static byte[] encodeBase64(byte[] binaryData) {
return Base64.encodeBase64(binaryData);
}
static byte[] encodeBytesUrlSafe(byte [] encodeBytes) {
return Base64.encodeBase64URLSafe(encodeBytes);
}
static String encodeBytesUrlSafeString(byte [] encodeBytes) {
return Base64.encodeBase64URLSafeString(encodeBytes);
}
}
""",
after = """
Expand All @@ -66,6 +72,12 @@ class ApacheBase64ToJavaBase64Test : JavaRecipeTest {
static byte[] encodeBase64(byte[] binaryData) {
return Base64.getEncoder().encode(binaryData);
}
static byte[] encodeBytesUrlSafe(byte [] encodeBytes) {
return Base64.getUrlEncoder().withoutPadding().encode(encodeBytes);
}
static String encodeBytesUrlSafeString(byte [] encodeBytes) {
return Base64.getUrlEncoder().withoutPadding().encodeToString(encodeBytes);
}
}
"""
)
Expand Down

0 comments on commit c01b28b

Please sign in to comment.