Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to rename annotations added by the Scala compiler. #756

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -40,10 +43,22 @@ class RelocatorRemapper extends Remapper {

List<Relocator> relocators
ShadowStats stats
Function1<String, Option<String>> scalaRenamer

RelocatorRemapper(List<Relocator> relocators, ShadowStats stats) {
this.relocators = relocators
this.stats = stats
this.scalaRenamer =
new FunctionWrappers.FromJavaFunction(
(String name) -> {
Optional<Relocator> maybeRelocator = relocators.stream().filter(x -> x.canRelocateClass(name)).findAny()
String rewritten =
maybeRelocator
.map(relocator -> relocator.relocateClass(new RelocateClassContext(name, stats)))
.orElse(null)
Option.apply(rewritten)
}
)
}

boolean hasRelocators() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand Down