Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,45 @@ class RelocationTest : BasePluginTest() {
}
}

@Test
fun relocateAnnotationStringConstants() {
writeClass {
"""
package my;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation {
String value();
}
@MyAnnotation("foo.Bar")
public class Main {
public static void main(String[] args) {
MyAnnotation ann = Main.class.getAnnotation(MyAnnotation.class);
System.out.println(ann.value());
}
}
"""
.trimIndent()
}
projectScript.appendText(
"""
$shadowJarTask {
manifest {
attributes '$mainClassAttributeKey': 'my.Main'
}
relocate('foo', 'shadow.foo')
}
"""
.trimIndent()
)

runWithSuccess(shadowJarPath)
val result = runProcess("java", "-jar", outputShadowedJar.use { it.toString() })

assertThat(result).contains("shadow.foo.Bar")
}

@Issue("https://github.com/GradleUp/shadow/issues/1403")
@Test
fun relocateMultiClassSignatureStringConstants() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,16 @@ internal class RelocatorRemapper(
AnnotationValue.ofAnnotation(mapAnnotation(valObj.annotation()))
is AnnotationValue.OfArray ->
AnnotationValue.ofArray(valObj.values().map(this::mapAnnotationValue))
is AnnotationValue.OfConstant -> valObj
is AnnotationValue.OfConstant -> {
if (valObj is AnnotationValue.OfString) {
val str = valObj.stringValue()
// mapLiterals=true enables the skipStringConstants check in each relocator.
val mapped = map(str, mapLiterals = true)
if (mapped != str) AnnotationValue.ofString(mapped) else valObj
} else {
valObj
}
}
is AnnotationValue.OfClass -> AnnotationValue.ofClass(mapClassDesc(valObj.classSymbol())!!)
is AnnotationValue.OfEnum ->
AnnotationValue.ofEnum(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import com.github.jengelman.gradle.plugins.shadow.transformers.ResourceTransform
import com.github.jengelman.gradle.plugins.shadow.transformers.TransformerContext
import java.io.File
import java.lang.classfile.ClassFile
import java.lang.constant.ClassDesc
import java.util.GregorianCalendar
import java.util.zip.ZipException
import kotlin.metadata.jvm.KmModule
Expand Down Expand Up @@ -212,14 +211,14 @@ constructor(
file.readBytes().let { bytes ->
var modified = false
val multiReleasePrefix = "^META-INF/versions/\\d+/".toRegex().find(path)?.value.orEmpty()
val internalClassName = path.replace(multiReleasePrefix, "").removeSuffix(".class")
val remapper = RelocatorRemapper(relocators) { modified = true }

val newBytes =
try {
val classFile = ClassFile.of()
val classModel = classFile.parse(bytes)
val newClassDesc = remapper.mapClassDesc(ClassDesc.ofInternalName(internalClassName))!!
val originalClassDesc = classModel.thisClass().asSymbol()
val newClassDesc = remapper.mapClassDesc(originalClassDesc)!!
classFile.transformClass(classModel, newClassDesc, remapper.asClassTransform())
} catch (t: Throwable) {
throw GradleException("Error in Class-File API processing class $path", t)
Expand Down