From c01b28bab3ac9dccdfe85dbcd9fe11c0795a1aa9 Mon Sep 17 00:00:00 2001 From: Patrick Date: Fri, 15 Apr 2022 12:55:41 -0700 Subject: [PATCH] Update ApacheBase64ToJavaBase64 to support converting 'Base64#encodeBase64URLSafe(..)' and `Base64#encodeBase64URLSafeString(encodeBytes)` (fixes #90) --- .../commons/codec/ApacheBase64ToJavaBase64.java | 7 +++++++ .../commons/codec/ApacheBase64ToJavaBase64Test.kt | 14 +++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/openrewrite/java/migrate/apache/commons/codec/ApacheBase64ToJavaBase64.java b/src/main/java/org/openrewrite/java/migrate/apache/commons/codec/ApacheBase64ToJavaBase64.java index 02f8287fdc..36800911f1 100644 --- a/src/main/java/org/openrewrite/java/migrate/apache/commons/codec/ApacheBase64ToJavaBase64.java +++ b/src/main/java/org/openrewrite/java/migrate/apache/commons/codec/ApacheBase64ToJavaBase64.java @@ -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; @@ -53,6 +54,8 @@ protected JavaIsoVisitor 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) { @@ -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(); diff --git a/src/test/kotlin/org/openrewrite/java/migrate/apache/commons/codec/ApacheBase64ToJavaBase64Test.kt b/src/test/kotlin/org/openrewrite/java/migrate/apache/commons/codec/ApacheBase64ToJavaBase64Test.kt index 26c7d2cdfe..73151e77aa 100644 --- a/src/test/kotlin/org/openrewrite/java/migrate/apache/commons/codec/ApacheBase64ToJavaBase64Test.kt +++ b/src/test/kotlin/org/openrewrite/java/migrate/apache/commons/codec/ApacheBase64ToJavaBase64Test.kt @@ -31,7 +31,7 @@ class ApacheBase64ToJavaBase64Test : JavaRecipeTest { .build() @Test - fun toJavaBase64() = assertChanged( + fun toJavaBase64() = assertChanged( before = """ import org.apache.commons.codec.binary.Base64; @@ -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 = """ @@ -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); + } } """ )