Repository: GradleUp/shadow
Pull request #1086 (2024-12-04 08:44:57)

Add a factory function for creating `Transformer`s
Refs #1082.


Patch:
diff --git a/api/shadow.api b/api/shadow.api
index 0b2dea4c..9b750800 100644
--- a/api/shadow.api
+++ b/api/shadow.api
@@ -556,7 +556,9 @@ public class com/github/jengelman/gradle/plugins/shadow/transformers/ServiceFile
 }
 
 public abstract interface class com/github/jengelman/gradle/plugins/shadow/transformers/Transformer : org/gradle/api/Named {
+	public static final field Companion Lcom/github/jengelman/gradle/plugins/shadow/transformers/Transformer$Companion;
 	public abstract fun canTransformResource (Lorg/gradle/api/file/FileTreeElement;)Z
+	public static fun create (Ljava/lang/Class;Lorg/gradle/api/model/ObjectFactory;)Lcom/github/jengelman/gradle/plugins/shadow/transformers/Transformer;
 	public fun getName ()Ljava/lang/String;
 	public fun getObjectFactory ()Lorg/gradle/api/model/ObjectFactory;
 	public abstract fun hasTransformedResource ()Z
@@ -564,6 +566,10 @@ public abstract interface class com/github/jengelman/gradle/plugins/shadow/trans
 	public abstract fun transform (Lcom/github/jengelman/gradle/plugins/shadow/transformers/TransformerContext;)V
 }
 
+public final class com/github/jengelman/gradle/plugins/shadow/transformers/Transformer$Companion {
+	public final fun create (Ljava/lang/Class;Lorg/gradle/api/model/ObjectFactory;)Lcom/github/jengelman/gradle/plugins/shadow/transformers/Transformer;
+}
+
 public final class com/github/jengelman/gradle/plugins/shadow/transformers/Transformer$DefaultImpls {
 	public static fun getName (Lcom/github/jengelman/gradle/plugins/shadow/transformers/Transformer;)Ljava/lang/String;
 	public static fun getObjectFactory (Lcom/github/jengelman/gradle/plugins/shadow/transformers/Transformer;)Lorg/gradle/api/model/ObjectFactory;
diff --git a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar.kt b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar.kt
index b50b5239..81133024 100644
--- a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar.kt
+++ b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar.kt
@@ -16,6 +16,7 @@ import com.github.jengelman.gradle.plugins.shadow.transformers.CacheableTransfor
 import com.github.jengelman.gradle.plugins.shadow.transformers.GroovyExtensionModuleTransformer
 import com.github.jengelman.gradle.plugins.shadow.transformers.ServiceFileTransformer
 import com.github.jengelman.gradle.plugins.shadow.transformers.Transformer
+import com.github.jengelman.gradle.plugins.shadow.transformers.Transformer.Companion.create
 import java.util.jar.JarFile
 import org.apache.tools.zip.ZipOutputStream
 import org.gradle.api.Action
@@ -27,7 +28,6 @@ import org.gradle.api.internal.DocumentationRegistry
 import org.gradle.api.internal.file.FileResolver
 import org.gradle.api.internal.file.copy.CopyAction
 import org.gradle.api.internal.file.copy.DefaultCopySpec
-import org.gradle.api.model.ObjectFactory
 import org.gradle.api.provider.ListProperty
 import org.gradle.api.provider.Property
 import org.gradle.api.tasks.CacheableTask
@@ -173,16 +173,7 @@ public abstract class ShadowJar :
   }
 
   override fun <T : Transformer> transform(clazz: Class<T>, action: Action<T>?): ShadowJar = apply {
-    // If the constructor takes a single ObjectFactory, inject it in.
-    val constructor = clazz.constructors.find {
-      it.parameterTypes.singleOrNull() == ObjectFactory::class.java
-    }
-    val transformer = if (constructor != null) {
-      objectFactory.newInstance(clazz)
-    } else {
-      clazz.getDeclaredConstructor().newInstance()
-    }
-    addTransform(transformer, action)
+    addTransform(clazz.create(objectFactory), action)
   }
 
   override fun transform(transformer: Transformer): ShadowJar = apply {
diff --git a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/Transformer.kt b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/Transformer.kt
index 4fea17f8..93fcd0e2 100644
--- a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/Transformer.kt
+++ b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/Transformer.kt
@@ -34,6 +34,21 @@ public interface Transformer : Named {
   @get:Internal
   public val objectFactory: ObjectFactory
     get() = throw NotImplementedError("You have to make sure this has been implemented or injected.")
+
+  public companion object {
+    @JvmStatic
+    public fun <T : Transformer> Class<T>.create(objectFactory: ObjectFactory): T {
+      // If the constructor takes a single ObjectFactory, inject it in.
+      val constructor = constructors.find {
+        it.parameterTypes.singleOrNull() == ObjectFactory::class.java
+      }
+      return if (constructor != null) {
+        objectFactory.newInstance(this@create)
+      } else {
+        getDeclaredConstructor().newInstance()
+      }
+    }
+  }
 }
 
 public object NoOpTransformer : Transformer {


Test patch:
diff --git a/src/test/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ApacheLicenseResourceTransformerTest.kt b/src/test/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ApacheLicenseResourceTransformerTest.kt
index 407721fe..4d3b72b4 100644
--- a/src/test/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ApacheLicenseResourceTransformerTest.kt
+++ b/src/test/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ApacheLicenseResourceTransformerTest.kt
@@ -3,7 +3,6 @@ package com.github.jengelman.gradle.plugins.shadow.transformers
 import assertk.assertThat
 import assertk.assertions.isFalse
 import assertk.assertions.isTrue
-import org.junit.jupiter.api.BeforeEach
 import org.junit.jupiter.api.Test
 
 /**
@@ -15,11 +14,6 @@ class ApacheLicenseResourceTransformerTest : TransformerTestSupport<ApacheLicens
     setupTurkishLocale()
   }
 
-  @BeforeEach
-  fun setup() {
-    transformer = ApacheLicenseResourceTransformer()
-  }
-
   @Test
   fun testCanTransformResource() {
     assertThat(transformer.canTransformResource(getFileElement("META-INF/LICENSE"))).isTrue()
diff --git a/src/test/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ApacheNoticeResourceTransformerTest.kt b/src/test/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ApacheNoticeResourceTransformerTest.kt
index 05d013db..909aa35a 100644
--- a/src/test/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ApacheNoticeResourceTransformerTest.kt
+++ b/src/test/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ApacheNoticeResourceTransformerTest.kt
@@ -4,8 +4,6 @@ import assertk.assertThat
 import assertk.assertions.isFalse
 import assertk.assertions.isTrue
 import assertk.fail
-import com.github.jengelman.gradle.plugins.shadow.util.testObjectFactory
-import org.junit.jupiter.api.BeforeEach
 import org.junit.jupiter.api.Test
 
 /**
@@ -17,11 +15,6 @@ class ApacheNoticeResourceTransformerTest : TransformerTestSupport<ApacheNoticeR
     setupTurkishLocale()
   }
 
-  @BeforeEach
-  fun setup() {
-    transformer = ApacheNoticeResourceTransformer(testObjectFactory)
-  }
-
   @Test
   fun testCanTransformResource() {
     assertThat(transformer.canTransformResource(getFileElement("META-INF/NOTICE"))).isTrue()
diff --git a/src/test/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/AppendingTransformerTest.kt b/src/test/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/AppendingTransformerTest.kt
index b5eee7d8..392e2ed2 100644
--- a/src/test/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/AppendingTransformerTest.kt
+++ b/src/test/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/AppendingTransformerTest.kt
@@ -3,8 +3,6 @@ package com.github.jengelman.gradle.plugins.shadow.transformers
 import assertk.assertThat
 import assertk.assertions.isFalse
 import assertk.assertions.isTrue
-import com.github.jengelman.gradle.plugins.shadow.util.testObjectFactory
-import org.junit.jupiter.api.BeforeEach
 import org.junit.jupiter.api.Test
 
 /**
@@ -16,11 +14,6 @@ class AppendingTransformerTest : TransformerTestSupport<AppendingTransformer>()
     setupTurkishLocale()
   }
 
-  @BeforeEach
-  fun setup() {
-    transformer = AppendingTransformer(testObjectFactory)
-  }
-
   @Test
   fun testCanTransformResource() {
     transformer.resource.set("abcdefghijklmnopqrstuvwxyz")
diff --git a/src/test/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ComponentsXmlResourceTransformerTest.kt b/src/test/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ComponentsXmlResourceTransformerTest.kt
index 9045a89b..d419e84e 100644
--- a/src/test/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ComponentsXmlResourceTransformerTest.kt
+++ b/src/test/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ComponentsXmlResourceTransformerTest.kt
@@ -3,19 +3,12 @@ package com.github.jengelman.gradle.plugins.shadow.transformers
 import assertk.assertThat
 import assertk.assertions.isTrue
 import org.custommonkey.xmlunit.XMLUnit
-import org.junit.jupiter.api.BeforeEach
 import org.junit.jupiter.api.Test
 
 /**
  * Modified from [org.apache.maven.plugins.shade.resource.ComponentsXmlResourceTransformerTest.java](https://github.com/apache/maven-shade-plugin/blob/master/src/test/java/org/apache/maven/plugins/shade/resource/ComponentsXmlResourceTransformerTest.java).
  */
 class ComponentsXmlResourceTransformerTest : TransformerTestSupport<ComponentsXmlResourceTransformer>() {
-
-  @BeforeEach
-  fun setup() {
-    transformer = ComponentsXmlResourceTransformer()
-  }
-
   @Test
   fun testConfigurationMerging() {
     XMLUnit.setNormalizeWhitespace(true)
diff --git a/src/test/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ManifestAppenderTransformerTest.kt b/src/test/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ManifestAppenderTransformerTest.kt
index 9c1e8c08..c36db4cc 100644
--- a/src/test/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ManifestAppenderTransformerTest.kt
+++ b/src/test/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ManifestAppenderTransformerTest.kt
@@ -6,16 +6,9 @@ import assertk.assertions.isFalse
 import assertk.assertions.isGreaterThan
 import assertk.assertions.isNotEmpty
 import assertk.assertions.isTrue
-import com.github.jengelman.gradle.plugins.shadow.util.testObjectFactory
-import org.junit.jupiter.api.BeforeEach
 import org.junit.jupiter.api.Test
 
 class ManifestAppenderTransformerTest : TransformerTestSupport<ManifestAppenderTransformer>() {
-  @BeforeEach
-  fun setup() {
-    transformer = ManifestAppenderTransformer(testObjectFactory)
-  }
-
   @Test
   fun testCanTransformResource() {
     with(transformer) {
diff --git a/src/test/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/PropertiesFileTransformerTest.kt b/src/test/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/PropertiesFileTransformerTest.kt
index 3106cc9b..19128632 100644
--- a/src/test/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/PropertiesFileTransformerTest.kt
+++ b/src/test/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/PropertiesFileTransformerTest.kt
@@ -6,17 +6,9 @@ import assertk.assertions.isEqualTo
 import assertk.assertions.isFalse
 import assertk.assertions.isNotEmpty
 import assertk.assertions.isTrue
-import com.github.jengelman.gradle.plugins.shadow.util.testObjectFactory
-import org.junit.jupiter.api.BeforeEach
 import org.junit.jupiter.api.Test
 
 class PropertiesFileTransformerTest : TransformerTestSupport<PropertiesFileTransformer>() {
-
-  @BeforeEach
-  fun setup() {
-    transformer = PropertiesFileTransformer(testObjectFactory)
-  }
-
   @Test
   fun testHasTransformedResource() {
     transformer.transform(manifestTransformerContext)
diff --git a/src/test/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/TransformerTestSupport.kt b/src/test/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/TransformerTestSupport.kt
index ab0be820..1acc05f1 100644
--- a/src/test/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/TransformerTestSupport.kt
+++ b/src/test/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/TransformerTestSupport.kt
@@ -1,7 +1,10 @@
 package com.github.jengelman.gradle.plugins.shadow.transformers
 
 import com.github.jengelman.gradle.plugins.shadow.internal.createDefaultFileTreeElement
+import com.github.jengelman.gradle.plugins.shadow.transformers.Transformer.Companion.create
+import com.github.jengelman.gradle.plugins.shadow.util.testObjectFactory
 import java.io.InputStream
+import java.lang.reflect.ParameterizedType
 import java.nio.file.Path
 import java.util.Locale
 import java.util.zip.ZipFile
@@ -10,9 +13,11 @@ import kotlin.io.path.outputStream
 import org.apache.tools.zip.ZipOutputStream
 import org.gradle.api.file.FileTreeElement
 import org.gradle.api.file.RelativePath
+import org.junit.jupiter.api.BeforeEach
 
 abstract class TransformerTestSupport<T : Transformer> {
   protected lateinit var transformer: T
+    private set
 
   protected val manifestTransformerContext: TransformerContext
     get() = TransformerContext(MANIFEST_NAME, requireResourceAsStream(MANIFEST_NAME))
@@ -21,6 +26,13 @@ abstract class TransformerTestSupport<T : Transformer> {
     return this::class.java.classLoader.getResourceAsStream(name) ?: error("Resource $name not found.")
   }
 
+  @BeforeEach
+  fun setup() {
+    @Suppress("UNCHECKED_CAST")
+    val clazz = (this::class.java.genericSuperclass as ParameterizedType).actualTypeArguments.first() as Class<T>
+    transformer = clazz.create(testObjectFactory)
+  }
+
   protected companion object {
     const val MANIFEST_NAME: String = "META-INF/MANIFEST.MF"
 
diff --git a/src/test/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/XmlAppendingTransformerTest.kt b/src/test/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/XmlAppendingTransformerTest.kt
index 1eaf7643..b9eddbf3 100644
--- a/src/test/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/XmlAppendingTransformerTest.kt
+++ b/src/test/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/XmlAppendingTransformerTest.kt
@@ -3,8 +3,6 @@ package com.github.jengelman.gradle.plugins.shadow.transformers
 import assertk.assertThat
 import assertk.assertions.isFalse
 import assertk.assertions.isTrue
-import com.github.jengelman.gradle.plugins.shadow.util.testObjectFactory
-import org.junit.jupiter.api.BeforeEach
 import org.junit.jupiter.api.Test
 
 class XmlAppendingTransformerTest : TransformerTestSupport<XmlAppendingTransformer>() {
@@ -13,11 +11,6 @@ class XmlAppendingTransformerTest : TransformerTestSupport<XmlAppendingTransform
     setupTurkishLocale()
   }
 
-  @BeforeEach
-  fun setup() {
-    transformer = XmlAppendingTransformer(testObjectFactory)
-  }
-
   @Test
   fun testCanTransformResource() {
     transformer.resource.set("abcdefghijklmnopqrstuvwxyz")