Repository: GradleUp/shadow
Pull request #1320 (2025-03-07 07:07:33)

Tidy up `RelocateClassContext`


Patch:
diff --git a/api/shadow.api b/api/shadow.api
index bc41314e..ddb51769 100644
--- a/api/shadow.api
+++ b/api/shadow.api
@@ -109,6 +109,11 @@ public final class com/github/jengelman/gradle/plugins/shadow/relocation/Relocat
 	public final synthetic fun unbox-impl ()Ljava/lang/String;
 }
 
+public final class com/github/jengelman/gradle/plugins/shadow/relocation/RelocationContextKt {
+	public static final fun relocateClass (Lcom/github/jengelman/gradle/plugins/shadow/relocation/Relocator;Ljava/lang/String;)Ljava/lang/String;
+	public static final fun relocatePath (Lcom/github/jengelman/gradle/plugins/shadow/relocation/Relocator;Ljava/lang/String;)Ljava/lang/String;
+}
+
 public abstract interface class com/github/jengelman/gradle/plugins/shadow/relocation/Relocator {
 	public static final field Companion Lcom/github/jengelman/gradle/plugins/shadow/relocation/Relocator$Companion;
 	public abstract fun applyToSourceContent (Ljava/lang/String;)Ljava/lang/String;
diff --git a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/RelocatorRemapper.kt b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/RelocatorRemapper.kt
index a35352ce..42c0e69d 100644
--- a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/RelocatorRemapper.kt
+++ b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/RelocatorRemapper.kt
@@ -1,6 +1,8 @@
 package com.github.jengelman.gradle.plugins.shadow.internal
 
 import com.github.jengelman.gradle.plugins.shadow.relocation.Relocator
+import com.github.jengelman.gradle.plugins.shadow.relocation.relocateClass
+import com.github.jengelman.gradle.plugins.shadow.relocation.relocatePath
 import java.util.regex.Pattern
 import org.objectweb.asm.commons.Remapper
 
diff --git a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/Utils.kt b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/Utils.kt
index 05732bab..7f692ed5 100644
--- a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/Utils.kt
+++ b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/Utils.kt
@@ -1,8 +1,5 @@
 package com.github.jengelman.gradle.plugins.shadow.internal
 
-import com.github.jengelman.gradle.plugins.shadow.relocation.RelocateClassContext
-import com.github.jengelman.gradle.plugins.shadow.relocation.RelocatePathContext
-import com.github.jengelman.gradle.plugins.shadow.relocation.Relocator
 import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowCopyAction
 import java.io.ByteArrayInputStream
 import java.io.ByteArrayOutputStream
@@ -52,14 +49,6 @@ internal inline fun zipEntry(
   block()
 }
 
-internal fun Relocator.relocatePath(path: String): String {
-  return relocatePath(RelocatePathContext(path))
-}
-
-internal fun Relocator.relocateClass(className: String): String {
-  return relocateClass(RelocateClassContext(className))
-}
-
 internal fun Properties.inputStream(
   charset: Charset = Charsets.ISO_8859_1,
   comments: String = "",
diff --git a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/relocation/RelocateClassContext.kt b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/relocation/RelocateClassContext.kt
deleted file mode 100644
index 7a85ec70..00000000
--- a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/relocation/RelocateClassContext.kt
+++ /dev/null
@@ -1,6 +0,0 @@
-package com.github.jengelman.gradle.plugins.shadow.relocation
-
-@JvmInline
-public value class RelocateClassContext(
-  public val className: String,
-)
diff --git a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/relocation/RelocatePathContext.kt b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/relocation/RelocatePathContext.kt
deleted file mode 100644
index 7fc49960..00000000
--- a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/relocation/RelocatePathContext.kt
+++ /dev/null
@@ -1,6 +0,0 @@
-package com.github.jengelman.gradle.plugins.shadow.relocation
-
-@JvmInline
-public value class RelocatePathContext(
-  public val path: String,
-)
diff --git a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/relocation/RelocationContext.kt b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/relocation/RelocationContext.kt
new file mode 100644
index 00000000..48f8afa4
--- /dev/null
+++ b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/relocation/RelocationContext.kt
@@ -0,0 +1,19 @@
+package com.github.jengelman.gradle.plugins.shadow.relocation
+
+@JvmInline
+public value class RelocateClassContext(
+  public val className: String,
+)
+
+@JvmInline
+public value class RelocatePathContext(
+  public val path: String,
+)
+
+public fun Relocator.relocateClass(className: String): String {
+  return relocateClass(RelocateClassContext(className))
+}
+
+public fun Relocator.relocatePath(path: String): String {
+  return relocatePath(RelocatePathContext(path))
+}
diff --git a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ComponentsXmlResourceTransformer.kt b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ComponentsXmlResourceTransformer.kt
index b7c29ebb..ab9503b6 100644
--- a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ComponentsXmlResourceTransformer.kt
+++ b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ComponentsXmlResourceTransformer.kt
@@ -1,7 +1,7 @@
 package com.github.jengelman.gradle.plugins.shadow.transformers
 
 import com.github.jengelman.gradle.plugins.shadow.internal.zipEntry
-import com.github.jengelman.gradle.plugins.shadow.relocation.RelocateClassContext
+import com.github.jengelman.gradle.plugins.shadow.relocation.relocateClass
 import java.io.BufferedInputStream
 import java.io.ByteArrayOutputStream
 import java.io.IOException
@@ -108,8 +108,7 @@ public open class ComponentsXmlResourceTransformer : ResourceTransformer {
       if (!className.isNullOrEmpty()) {
         for (relocator in context.relocators) {
           if (relocator.canRelocateClass(className)) {
-            val relocateClassContext = RelocateClassContext(className)
-            return relocator.relocateClass(relocateClassContext)
+            return relocator.relocateClass(className)
           }
         }
       }
diff --git a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/Log4j2PluginsCacheFileTransformer.kt b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/Log4j2PluginsCacheFileTransformer.kt
index d36c1d1e..8f78196c 100644
--- a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/Log4j2PluginsCacheFileTransformer.kt
+++ b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/Log4j2PluginsCacheFileTransformer.kt
@@ -1,8 +1,8 @@
 package com.github.jengelman.gradle.plugins.shadow.transformers
 
 import com.github.jengelman.gradle.plugins.shadow.internal.zipEntry
-import com.github.jengelman.gradle.plugins.shadow.relocation.RelocateClassContext
 import com.github.jengelman.gradle.plugins.shadow.relocation.Relocator
+import com.github.jengelman.gradle.plugins.shadow.relocation.relocateClass
 import java.net.URL
 import java.nio.file.Path
 import java.util.Collections
@@ -69,10 +69,9 @@ public open class Log4j2PluginsCacheFileTransformer : ResourceTransformer {
     pluginCache.allCategories.values.forEach { currentMap ->
       currentMap.values.forEach { currentPluginEntry ->
         val className = currentPluginEntry.className
-        val relocateClassContext = RelocateClassContext(className)
         tempRelocators.firstOrNull { it.canRelocateClass(className) }?.let { relocator ->
           // Then we perform that relocation and update the plugin entry to reflect the new value.
-          currentPluginEntry.className = relocator.relocateClass(relocateClassContext)
+          currentPluginEntry.className = relocator.relocateClass(className)
         }
       }
     }
diff --git a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ServiceFileTransformer.kt b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ServiceFileTransformer.kt
index 8532c3ef..dbd7627e 100644
--- a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ServiceFileTransformer.kt
+++ b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ServiceFileTransformer.kt
@@ -1,7 +1,7 @@
 package com.github.jengelman.gradle.plugins.shadow.transformers
 
 import com.github.jengelman.gradle.plugins.shadow.internal.zipEntry
-import com.github.jengelman.gradle.plugins.shadow.relocation.RelocateClassContext
+import com.github.jengelman.gradle.plugins.shadow.relocation.relocateClass
 import com.github.jengelman.gradle.plugins.shadow.transformers.GroovyExtensionModuleTransformer.Companion.PATH_LEGACY_GROOVY_EXTENSION_MODULE_DESCRIPTOR
 import org.apache.tools.zip.ZipOutputStream
 import org.gradle.api.file.FileTreeElement
@@ -48,8 +48,7 @@ public open class ServiceFileTransformer(
     var resource = context.path.substringAfter("$path/")
     context.relocators.forEach { relocator ->
       if (relocator.canRelocateClass(resource)) {
-        val classContext = RelocateClassContext(className = resource)
-        resource = relocator.relocateClass(classContext)
+        resource = relocator.relocateClass(resource)
         return@forEach
       }
     }
@@ -60,8 +59,7 @@ public open class ServiceFileTransformer(
       var line = it
       context.relocators.forEach { relocator ->
         if (relocator.canRelocateClass(line)) {
-          val lineContext = RelocateClassContext(line)
-          line = relocator.relocateClass(lineContext)
+          line = relocator.relocateClass(line)
         }
       }
       out.add(line)


Test patch:
diff --git a/src/test/kotlin/com/github/jengelman/gradle/plugins/shadow/relocation/SimpleRelocatorTest.kt b/src/test/kotlin/com/github/jengelman/gradle/plugins/shadow/relocation/SimpleRelocatorTest.kt
index 11fd55e4..4141b675 100644
--- a/src/test/kotlin/com/github/jengelman/gradle/plugins/shadow/relocation/SimpleRelocatorTest.kt
+++ b/src/test/kotlin/com/github/jengelman/gradle/plugins/shadow/relocation/SimpleRelocatorTest.kt
@@ -4,8 +4,6 @@ import assertk.assertThat
 import assertk.assertions.isEqualTo
 import assertk.assertions.isFalse
 import assertk.assertions.isTrue
-import com.github.jengelman.gradle.plugins.shadow.internal.relocateClass
-import com.github.jengelman.gradle.plugins.shadow.internal.relocatePath
 import org.junit.jupiter.api.Test
 
 /**