Skip to content

Commit 233e9e0

Browse files
committed
Test canDisableRelocateStringConstants
1 parent efccd57 commit 233e9e0

File tree

4 files changed

+52
-5
lines changed

4 files changed

+52
-5
lines changed

src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/RelocationTest.kt

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ import assertk.assertions.isInstanceOf
88
import assertk.assertions.isNotEmpty
99
import assertk.fail
1010
import com.github.jengelman.gradle.plugins.shadow.ShadowJavaPlugin.Companion.SHADOW_JAR_TASK_NAME
11+
import com.github.jengelman.gradle.plugins.shadow.internal.mainClassAttributeKey
1112
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowCopyAction.Companion.CONSTANT_TIME_FOR_ZIP_ENTRIES
1213
import com.github.jengelman.gradle.plugins.shadow.util.Issue
1314
import com.github.jengelman.gradle.plugins.shadow.util.containsOnly
15+
import com.github.jengelman.gradle.plugins.shadow.util.runProcess
1416
import java.net.URLClassLoader
1517
import kotlin.io.path.appendText
1618
import kotlin.io.path.writeText
@@ -581,6 +583,51 @@ class RelocationTest : BasePluginTest() {
581583
}
582584
}
583585

586+
@ParameterizedTest
587+
@ValueSource(booleans = [false, true])
588+
fun canDisableRelocateStringConstants(skipStringLiteral: Boolean) {
589+
writeClass {
590+
"""
591+
package my;
592+
public class Main {
593+
public static final String junit = "junit.framework.Test";
594+
public static void main(String[] args) {
595+
System.out.println(getValue() + junit);
596+
}
597+
// Use this method to force the compiler to not inline the string literal.
598+
private static String getValue() { return "the value is "; }
599+
}
600+
""".trimIndent()
601+
}
602+
projectScriptPath.appendText(
603+
"""
604+
$shadowJar {
605+
manifest {
606+
attributes '$mainClassAttributeKey': 'my.Main'
607+
}
608+
relocate('junit', 'foo.junit') {
609+
skipStringLiteral = $skipStringLiteral
610+
}
611+
}
612+
""".trimIndent(),
613+
)
614+
615+
run(shadowJarTask) {
616+
it.withDebug(true)
617+
}
618+
619+
val pathString = outputShadowJar.use { it.toString() }
620+
val result = runProcess("java", "-jar", pathString)
621+
622+
assertThat(result).contains(
623+
if (skipStringLiteral) {
624+
"the value is junit.framework.Test"
625+
} else {
626+
"the value is foo.junit.framework.Test"
627+
},
628+
)
629+
}
630+
584631
private companion object {
585632
@JvmStatic
586633
fun prefixProvider() = listOf(

src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/RelocatorRemapper.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ internal class RelocatorRemapper(
4242
}
4343

4444
for (relocator in relocators) {
45-
if (mapLiterals && relocator.isSkipStringLiteral) {
45+
if (mapLiterals && relocator.skipStringLiteral) {
4646
return name
4747
} else if (relocator.canRelocateClass(newName)) {
4848
return prefix + relocator.relocateClass(newName) + suffix

src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/relocation/Relocator.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public interface Relocator {
2121
public fun applyToSourceContent(sourceContent: String): String
2222

2323
@get:Internal
24-
public val isSkipStringLiteral: Boolean
24+
public val skipStringLiteral: Boolean
2525

2626
public companion object {
2727
public val ROLE: String = Relocator::class.java.name

src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/relocation/SimpleRelocator.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public open class SimpleRelocator @JvmOverloads constructor(
1919
includes: List<String>? = null,
2020
excludes: List<String>? = null,
2121
private val rawString: Boolean = false,
22-
@Input override var isSkipStringLiteral: Boolean = true,
22+
@get:Input override var skipStringLiteral: Boolean = false,
2323
) : Relocator {
2424
private val pattern: String
2525
private val pathPattern: String
@@ -138,7 +138,7 @@ public open class SimpleRelocator @JvmOverloads constructor(
138138
if (this === other) return true
139139
if (other !is SimpleRelocator) return false
140140
return rawString == other.rawString &&
141-
isSkipStringLiteral == other.isSkipStringLiteral &&
141+
skipStringLiteral == other.skipStringLiteral &&
142142
pattern == other.pattern &&
143143
pathPattern == other.pathPattern &&
144144
shadedPattern == other.shadedPattern &&
@@ -151,7 +151,7 @@ public open class SimpleRelocator @JvmOverloads constructor(
151151

152152
override fun hashCode(): Int {
153153
var result = rawString.hashCode()
154-
result = 31 * result + isSkipStringLiteral.hashCode()
154+
result = 31 * result + skipStringLiteral.hashCode()
155155
result = 31 * result + pattern.hashCode()
156156
result = 31 * result + pathPattern.hashCode()
157157
result = 31 * result + shadedPattern.hashCode()

0 commit comments

Comments
 (0)