Skip to content

Commit

Permalink
Add the ability to rename annotations added by the Scala compiler.
Browse files Browse the repository at this point in the history
  • Loading branch information
shawjef3 committed Feb 15, 2022
1 parent 24c371c commit e9a329e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
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,18 @@ 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 string) -> {
Optional<Relocator> maybe = relocators.stream().filter(x -> x.canRelocateClass(string)).findAny()
Option.apply(maybe.map(it.relocateClass(new RelocateClassContext(string, stats))).orElse(null))
}
)
}

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

0 comments on commit e9a329e

Please sign in to comment.