File tree Expand file tree Collapse file tree 3 files changed +51
-4
lines changed
functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow
main/kotlin/com/github/jengelman/gradle/plugins/shadow Expand file tree Collapse file tree 3 files changed +51
-4
lines changed Original file line number Diff line number Diff 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 () {
Original file line number Diff line number Diff 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(
Original file line number Diff line number Diff line change @@ -14,7 +14,6 @@ import com.github.jengelman.gradle.plugins.shadow.transformers.ResourceTransform
1414import com.github.jengelman.gradle.plugins.shadow.transformers.TransformerContext
1515import java.io.File
1616import java.lang.classfile.ClassFile
17- import java.lang.constant.ClassDesc
1817import java.util.GregorianCalendar
1918import java.util.zip.ZipException
2019import 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)
You can’t perform that action at this time.
0 commit comments