Skip to content

Commit 5ed4e83

Browse files
GooolerCopilot
andauthored
Support skipping string constant remapping (#1401)
* Add `isSkipStringLiteral` for `Relocator` * Check `mapLiterals` in `RelocatorRemapper` * Make isSkipStringLiteral configurable * Test `canDisableRelocateStringConstants` * Fix `canDisableRelocateStringConstants` * Dump API * Update changelog * Update changelog * Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Cleanups * Mark `skipStringLiteral` as `Input` * Rename `skipStringLiteral` to `skipStringConstants` * Update doc * Update src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/RelocatorRemapper.kt Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update docs/configuration/relocation/README.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Revert "Update src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/RelocatorRemapper.kt" This reverts commit d5060b1. * Update changelog --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 9ada0af commit 5ed4e83

File tree

7 files changed

+128
-18
lines changed

7 files changed

+128
-18
lines changed

api/shadow.api

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ public abstract interface class com/github/jengelman/gradle/plugins/shadow/reloc
112112
public abstract fun applyToSourceContent (Ljava/lang/String;)Ljava/lang/String;
113113
public abstract fun canRelocateClass (Ljava/lang/String;)Z
114114
public abstract fun canRelocatePath (Ljava/lang/String;)Z
115+
public fun getSkipStringConstants ()Z
115116
public abstract fun relocateClass (Lcom/github/jengelman/gradle/plugins/shadow/relocation/RelocateClassContext;)Ljava/lang/String;
116117
public abstract fun relocatePath (Lcom/github/jengelman/gradle/plugins/shadow/relocation/RelocatePathContext;)Ljava/lang/String;
117118
}
@@ -127,18 +128,21 @@ public class com/github/jengelman/gradle/plugins/shadow/relocation/SimpleRelocat
127128
public fun <init> (Ljava/lang/String;Ljava/lang/String;Ljava/util/List;)V
128129
public fun <init> (Ljava/lang/String;Ljava/lang/String;Ljava/util/List;Ljava/util/List;)V
129130
public fun <init> (Ljava/lang/String;Ljava/lang/String;Ljava/util/List;Ljava/util/List;Z)V
130-
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/String;Ljava/util/List;Ljava/util/List;ZILkotlin/jvm/internal/DefaultConstructorMarker;)V
131+
public fun <init> (Ljava/lang/String;Ljava/lang/String;Ljava/util/List;Ljava/util/List;ZZ)V
132+
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/String;Ljava/util/List;Ljava/util/List;ZZILkotlin/jvm/internal/DefaultConstructorMarker;)V
131133
public fun applyToSourceContent (Ljava/lang/String;)Ljava/lang/String;
132134
public fun canRelocateClass (Ljava/lang/String;)Z
133135
public fun canRelocatePath (Ljava/lang/String;)Z
134136
public fun equals (Ljava/lang/Object;)Z
135137
public fun exclude (Ljava/lang/String;)V
136138
public final fun getExcludes ()Ljava/util/Set;
137139
public final fun getIncludes ()Ljava/util/Set;
140+
public fun getSkipStringConstants ()Z
138141
public fun hashCode ()I
139142
public fun include (Ljava/lang/String;)V
140143
public fun relocateClass (Lcom/github/jengelman/gradle/plugins/shadow/relocation/RelocateClassContext;)Ljava/lang/String;
141144
public fun relocatePath (Lcom/github/jengelman/gradle/plugins/shadow/relocation/RelocatePathContext;)Ljava/lang/String;
145+
public fun setSkipStringConstants (Z)V
142146
}
143147

144148
public abstract interface class com/github/jengelman/gradle/plugins/shadow/tasks/DependencyFilter : java/io/Serializable {

docs/changes/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
**Added**
66

7+
- Support skipping string constant remapping. ([#1401](https://github.com/GradleUp/shadow/pull/1401))
78
- Let `assemble` depend on `shadowJar`. ([#1524](https://github.com/GradleUp/shadow/pull/1524))
89
- Fail build when inputting AAR files or using Shadow with AGP. ([#1530](https://github.com/GradleUp/shadow/pull/1530))
910

docs/configuration/relocation/README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,63 @@ expression in `%regex[]` before passing it to `include`/`exclude`.
9393
}
9494
```
9595

96+
## Skipping Relocation for String Constants
97+
98+
If there is a class like:
99+
100+
```java
101+
package foo;
102+
103+
public class Bar {
104+
public static void main(String[] args) {
105+
System.out.println("foo.Bar");
106+
}
107+
}
108+
```
109+
110+
in your project, and you configure the relocation like:
111+
112+
=== "Kotlin"
113+
114+
```kotlin
115+
tasks.shadowJar {
116+
relocate("foo", "my.foo")
117+
}
118+
```
119+
120+
=== "Groovy"
121+
122+
```groovy
123+
tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) {
124+
relocate 'foo', 'my.foo'
125+
}
126+
```
127+
128+
the string constant `"foo.Bar"` will be relocated to `"my.foo.Bar"` by default. This may not be what you want, you can
129+
skip relocating string constants in the classes like:
130+
131+
=== "Kotlin"
132+
133+
```kotlin
134+
tasks.shadowJar {
135+
relocate("foo", "my.foo") {
136+
// Optionally, defaults to `false`.
137+
skipStringConstants = true
138+
}
139+
}
140+
```
141+
142+
=== "Groovy"
143+
144+
```groovy
145+
tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) {
146+
relocate('foo', 'my.foo') {
147+
// Optionally, defaults to `false`.
148+
skipStringConstants = true
149+
}
150+
}
151+
```
152+
96153
## Automatically Relocating Dependencies
97154

98155
Shadow is shipped with a task that can be used to automatically configure all packages from all dependencies to be

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

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -585,16 +585,7 @@ class RelocationTest : BasePluginTest() {
585585

586586
@Test
587587
fun relocateStringConstantsByDefault() {
588-
writeClass {
589-
"""
590-
package my;
591-
public class Main {
592-
public static void main(String[] args) {
593-
System.out.println("junit.framework.Test");
594-
}
595-
}
596-
""".trimIndent()
597-
}
588+
writeClassWithStringRef()
598589
projectScriptPath.appendText(
599590
"""
600591
$shadowJar {
@@ -614,6 +605,46 @@ class RelocationTest : BasePluginTest() {
614605
)
615606
}
616607

608+
@Issue(
609+
"https://github.com/GradleUp/shadow/issues/232",
610+
)
611+
@ParameterizedTest
612+
@ValueSource(booleans = [false, true])
613+
fun canDisableRelocateStringConstants(skipStringConstants: Boolean) {
614+
writeClassWithStringRef()
615+
projectScriptPath.appendText(
616+
"""
617+
$shadowJar {
618+
manifest {
619+
attributes '$mainClassAttributeKey': 'my.Main'
620+
}
621+
relocate('junit', 'foo.junit') {
622+
skipStringConstants = $skipStringConstants
623+
}
624+
}
625+
""".trimIndent(),
626+
)
627+
628+
run(shadowJarTask)
629+
val result = runProcess("java", "-jar", outputShadowJar.use { it.toString() })
630+
631+
val expected = if (skipStringConstants) "junit.framework.Test" else "foo.junit.framework.Test"
632+
assertThat(result).contains(expected)
633+
}
634+
635+
private fun writeClassWithStringRef() {
636+
writeClass {
637+
"""
638+
package my;
639+
public class Main {
640+
public static void main(String[] args) {
641+
System.out.println("junit.framework.Test");
642+
}
643+
}
644+
""".trimIndent()
645+
}
646+
}
647+
617648
private companion object {
618649
@JvmStatic
619650
fun prefixProvider() = listOf(

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,20 @@ internal class RelocatorRemapper(
1919

2020
override fun mapValue(value: Any): Any {
2121
return if (value is String) {
22-
map(value)
22+
mapName(value, mapLiterals = true)
2323
} else {
2424
super.mapValue(value)
2525
}
2626
}
2727

28-
override fun map(name: String): String {
28+
override fun map(internalName: String): String = mapName(internalName)
29+
30+
fun mapPath(path: String): String {
31+
val dotIndex = path.indexOf('.')
32+
return if (dotIndex == -1) path else map(path.take(dotIndex))
33+
}
34+
35+
private fun mapName(name: String, mapLiterals: Boolean = false): String {
2936
var newName = name
3037
var prefix = ""
3138
var suffix = ""
@@ -38,7 +45,9 @@ internal class RelocatorRemapper(
3845
}
3946

4047
for (relocator in relocators) {
41-
if (relocator.canRelocateClass(newName)) {
48+
if (mapLiterals && relocator.skipStringConstants) {
49+
return name
50+
} else if (relocator.canRelocateClass(newName)) {
4251
return prefix + relocator.relocateClass(newName) + suffix
4352
} else if (relocator.canRelocatePath(newName)) {
4453
return prefix + relocator.relocatePath(newName) + suffix
@@ -47,8 +56,4 @@ internal class RelocatorRemapper(
4756

4857
return name
4958
}
50-
51-
fun mapPath(path: String): String {
52-
return map(path.substringBefore('.'))
53-
}
5459
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.jengelman.gradle.plugins.shadow.relocation
22

33
import com.github.jengelman.gradle.plugins.shadow.transformers.CacheableTransformer
4+
import org.gradle.api.tasks.Input
45

56
/**
67
* Modified from [org.apache.maven.plugins.shade.relocation.Relocator.java](https://github.com/apache/maven-shade-plugin/blob/master/src/main/java/org/apache/maven/plugins/shade/relocation/Relocator.java).
@@ -19,6 +20,14 @@ public interface Relocator {
1920

2021
public fun applyToSourceContent(sourceContent: String): String
2122

23+
/**
24+
* Indicates whether this relocator should skip relocating string constants.
25+
*
26+
* Defaults to `false`.
27+
*/
28+
@get:Input
29+
public val skipStringConstants: Boolean get() = false
30+
2231
public companion object {
2332
public val ROLE: String = Relocator::class.java.name
2433
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +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+
@get:Input override var skipStringConstants: Boolean = false,
2223
) : Relocator {
2324
private val pattern: String
2425
private val pathPattern: String
@@ -137,6 +138,7 @@ public open class SimpleRelocator @JvmOverloads constructor(
137138
if (this === other) return true
138139
if (other !is SimpleRelocator) return false
139140
return rawString == other.rawString &&
141+
skipStringConstants == other.skipStringConstants &&
140142
pattern == other.pattern &&
141143
pathPattern == other.pathPattern &&
142144
shadedPattern == other.shadedPattern &&
@@ -149,6 +151,7 @@ public open class SimpleRelocator @JvmOverloads constructor(
149151

150152
override fun hashCode(): Int {
151153
var result = rawString.hashCode()
154+
result = 31 * result + skipStringConstants.hashCode()
152155
result = 31 * result + pattern.hashCode()
153156
result = 31 * result + pathPattern.hashCode()
154157
result = 31 * result + shadedPattern.hashCode()

0 commit comments

Comments
 (0)