From e9a329e45fb2c46295a81ff60ffb13097c278c8e Mon Sep 17 00:00:00 2001 From: Jeff Shaw Date: Tue, 15 Feb 2022 18:12:15 -0500 Subject: [PATCH] Add the ability to rename annotations added by the Scala compiler. --- gradle/dependencies.gradle | 2 ++ .../plugins/shadow/impl/RelocatorRemapper.groovy | 11 +++++++++++ .../plugins/shadow/tasks/ShadowCopyAction.groovy | 3 ++- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/gradle/dependencies.gradle b/gradle/dependencies.gradle index c252c2916..ab6f7803b 100644 --- a/gradle/dependencies.gradle +++ b/gradle/dependencies.gradle @@ -14,6 +14,8 @@ dependencies { implementation('org.vafer:jdependency:2.7.0') { exclude group: 'org.ow2.asm' } + // For Scala annotation remapping + implementation 'com.eed3si9n.jarjarabrams:jarjar-abrams-core_2.13:1.8.1' testImplementation('org.spockframework:spock-core:2.0-groovy-3.0') { exclude group: 'org.codehaus.groovy' diff --git a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/impl/RelocatorRemapper.groovy b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/impl/RelocatorRemapper.groovy index b994463d1..9348c2569 100644 --- a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/impl/RelocatorRemapper.groovy +++ b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/impl/RelocatorRemapper.groovy @@ -25,6 +25,9 @@ import com.github.jengelman.gradle.plugins.shadow.relocation.RelocatePathContext import com.github.jengelman.gradle.plugins.shadow.relocation.Relocator import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowCopyAction.RelativeArchivePath import org.objectweb.asm.commons.Remapper +import scala.Function1 +import scala.Option +import scala.jdk.FunctionWrappers import java.util.regex.Matcher import java.util.regex.Pattern @@ -40,10 +43,18 @@ class RelocatorRemapper extends Remapper { List relocators ShadowStats stats + Function1> scalaRenamer RelocatorRemapper(List relocators, ShadowStats stats) { this.relocators = relocators this.stats = stats + this.scalaRenamer = + new FunctionWrappers.FromJavaFunction( + (String string) -> { + Optional maybe = relocators.stream().filter(x -> x.canRelocateClass(string)).findAny() + Option.apply(maybe.map(it.relocateClass(new RelocateClassContext(string, stats))).orElse(null)) + } + ) } boolean hasRelocators() { diff --git a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowCopyAction.groovy b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowCopyAction.groovy index 3b7747085..17af324ed 100644 --- a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowCopyAction.groovy +++ b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowCopyAction.groovy @@ -1,5 +1,6 @@ package com.github.jengelman.gradle.plugins.shadow.tasks +import com.eed3si9n.jarjarabrams.scalasig.ScalaSigClassVisitor import com.github.jengelman.gradle.plugins.shadow.ShadowStats import com.github.jengelman.gradle.plugins.shadow.impl.RelocatorRemapper import com.github.jengelman.gradle.plugins.shadow.internal.GradleVersionUtil @@ -348,7 +349,7 @@ class ShadowCopyAction implements CopyAction { // that use the constant pool to determine the dependencies of a class. ClassWriter cw = new ClassWriter(0) - ClassVisitor cv = new ClassRemapper(cw, remapper) + ClassVisitor cv = new ScalaSigClassVisitor(new ClassRemapper(cw, remapper), remapper.scalaRenamer) try { cr.accept(cv, ClassReader.EXPAND_FRAMES)