Skip to content

Commit 51e6816

Browse files
CopilotGoooler
andcommitted
Fix class descriptor derivation and annotation string constant relocation
Co-authored-by: Goooler <[email protected]>
1 parent 67cf158 commit 51e6816

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,45 @@ class RelocationTest : BasePluginTest() {
533533
}
534534
}
535535

536+
@Test
537+
fun relocateAnnotationStringConstants() {
538+
writeClass {
539+
"""
540+
package my;
541+
import java.lang.annotation.Retention;
542+
import java.lang.annotation.RetentionPolicy;
543+
@Retention(RetentionPolicy.RUNTIME)
544+
@interface MyAnnotation {
545+
String value();
546+
}
547+
@MyAnnotation("foo.Bar")
548+
public class Main {
549+
public static void main(String[] args) {
550+
MyAnnotation ann = Main.class.getAnnotation(MyAnnotation.class);
551+
System.out.println(ann.value());
552+
}
553+
}
554+
"""
555+
.trimIndent()
556+
}
557+
projectScript.appendText(
558+
"""
559+
$shadowJarTask {
560+
manifest {
561+
attributes '$mainClassAttributeKey': 'my.Main'
562+
}
563+
relocate('foo', 'shadow.foo')
564+
}
565+
"""
566+
.trimIndent()
567+
)
568+
569+
runWithSuccess(shadowJarPath)
570+
val result = runProcess("java", "-jar", outputShadowedJar.use { it.toString() })
571+
572+
assertThat(result).contains("shadow.foo.Bar")
573+
}
574+
536575
@Issue("https://github.com/GradleUp/shadow/issues/1403")
537576
@Test
538577
fun relocateMultiClassSignatureStringConstants() {

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,16 @@ internal class RelocatorRemapper(
436436
AnnotationValue.ofAnnotation(mapAnnotation(valObj.annotation()))
437437
is AnnotationValue.OfArray ->
438438
AnnotationValue.ofArray(valObj.values().map(this::mapAnnotationValue))
439-
is AnnotationValue.OfConstant -> valObj
439+
is AnnotationValue.OfConstant -> {
440+
if (valObj is AnnotationValue.OfString) {
441+
val str = valObj.stringValue()
442+
// mapLiterals=true enables the skipStringConstants check in each relocator.
443+
val mapped = map(str, mapLiterals = true)
444+
if (mapped != str) AnnotationValue.ofString(mapped) else valObj
445+
} else {
446+
valObj
447+
}
448+
}
440449
is AnnotationValue.OfClass -> AnnotationValue.ofClass(mapClassDesc(valObj.classSymbol())!!)
441450
is AnnotationValue.OfEnum ->
442451
AnnotationValue.ofEnum(

src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowCopyAction.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import com.github.jengelman.gradle.plugins.shadow.transformers.ResourceTransform
1414
import com.github.jengelman.gradle.plugins.shadow.transformers.TransformerContext
1515
import java.io.File
1616
import java.lang.classfile.ClassFile
17-
import java.lang.constant.ClassDesc
1817
import java.util.GregorianCalendar
1918
import java.util.zip.ZipException
2019
import kotlin.metadata.jvm.KmModule
@@ -212,14 +211,14 @@ constructor(
212211
file.readBytes().let { bytes ->
213212
var modified = false
214213
val multiReleasePrefix = "^META-INF/versions/\\d+/".toRegex().find(path)?.value.orEmpty()
215-
val internalClassName = path.replace(multiReleasePrefix, "").removeSuffix(".class")
216214
val remapper = RelocatorRemapper(relocators) { modified = true }
217215

218216
val newBytes =
219217
try {
220218
val classFile = ClassFile.of()
221219
val classModel = classFile.parse(bytes)
222-
val newClassDesc = remapper.mapClassDesc(ClassDesc.ofInternalName(internalClassName))!!
220+
val originalClassDesc = classModel.thisClass().asSymbol()
221+
val newClassDesc = remapper.mapClassDesc(originalClassDesc)!!
223222
classFile.transformClass(classModel, newClassDesc, remapper.asClassTransform())
224223
} catch (t: Throwable) {
225224
throw GradleException("Error in Class-File API processing class $path", t)

0 commit comments

Comments
 (0)