Skip to content

Commit c01b28b

Browse files
committed
Update ApacheBase64ToJavaBase64 to support converting 'Base64#encodeBase64URLSafe(..)' and Base64#encodeBase64URLSafeString(encodeBytes) (fixes #90)
1 parent 9581ed7 commit c01b28b

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/main/java/org/openrewrite/java/migrate/apache/commons/codec/ApacheBase64ToJavaBase64.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.openrewrite.java.MethodMatcher;
2323
import org.openrewrite.java.search.UsesType;
2424
import org.openrewrite.java.tree.J;
25+
import org.openrewrite.java.tree.JavaSourceFile;
2526
import org.openrewrite.java.tree.JavaType;
2627

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

5760
@Override
5861
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext executionContext) {
@@ -65,6 +68,10 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
6568
templatePrefix = "Base64.getEncoder().encode(#{anyArray()})";
6669
} else if (apacheDecode.matches(mi)) {
6770
templatePrefix = "Base64.getDecoder().decode(#{any(String)})";
71+
} else if (apacheEncode64UrlSafe.matches(mi)) {
72+
templatePrefix = "Base64.getUrlEncoder().withoutPadding().encode(#{anyArray()})";
73+
} else if (apacheEncode64UrlSafeString.matches(mi)) {
74+
templatePrefix = "Base64.getUrlEncoder().withoutPadding().encodeToString(#{anyArray()})";
6875
}
6976
if (templatePrefix != null) {
7077
JavaTemplate t = JavaTemplate.builder(this::getCursor, templatePrefix).imports("java.util.Base64").build();

src/test/kotlin/org/openrewrite/java/migrate/apache/commons/codec/ApacheBase64ToJavaBase64Test.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class ApacheBase64ToJavaBase64Test : JavaRecipeTest {
3131
.build()
3232

3333
@Test
34-
fun toJavaBase64() = assertChanged(
34+
fun toJavaBase64() = assertChanged(
3535
before = """
3636
import org.apache.commons.codec.binary.Base64;
3737
@@ -48,6 +48,12 @@ class ApacheBase64ToJavaBase64Test : JavaRecipeTest {
4848
static byte[] encodeBase64(byte[] binaryData) {
4949
return Base64.encodeBase64(binaryData);
5050
}
51+
static byte[] encodeBytesUrlSafe(byte [] encodeBytes) {
52+
return Base64.encodeBase64URLSafe(encodeBytes);
53+
}
54+
static String encodeBytesUrlSafeString(byte [] encodeBytes) {
55+
return Base64.encodeBase64URLSafeString(encodeBytes);
56+
}
5157
}
5258
""",
5359
after = """
@@ -66,6 +72,12 @@ class ApacheBase64ToJavaBase64Test : JavaRecipeTest {
6672
static byte[] encodeBase64(byte[] binaryData) {
6773
return Base64.getEncoder().encode(binaryData);
6874
}
75+
static byte[] encodeBytesUrlSafe(byte [] encodeBytes) {
76+
return Base64.getUrlEncoder().withoutPadding().encode(encodeBytes);
77+
}
78+
static String encodeBytesUrlSafeString(byte [] encodeBytes) {
79+
return Base64.getUrlEncoder().withoutPadding().encodeToString(encodeBytes);
80+
}
6981
}
7082
"""
7183
)

0 commit comments

Comments
 (0)