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

Rewrite @ScalaSignature when shading #393

Merged
merged 12 commits into from
May 23, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
87 changes: 69 additions & 18 deletions src/main/scala/org/pantsbuild/jarjar/JJProcessor.scala
Original file line number Diff line number Diff line change
@@ -1,31 +1,82 @@
package org.pantsbuild.jarjar

import org.pantsbuild.jarjar.util.{EntryStruct, JarProcessor}
import java.io.IOException

import org.pantsbuild.jarjar.misplaced.MisplacedClassProcessorFactory
import org.pantsbuild.jarjar.util.{EntryStruct, JarProcessor, JarProcessorChain, JarTransformerChain, RemappingClassTransformer, StandaloneJarProcessor}

import scala.collection.JavaConverters._
import scala.collection.mutable

class JJProcessor(val proc: JarProcessor) {
/**
* Creates a new JJProcessor, which automatically generates the standard zap, keep, remap, etc processors.
* This is a copy of the MainProcessor in JarJar with an added ScalaSigProcessor
*
* @param patterns List of rules to parse.
* @param verbose Whether to verbosely log information.
* @param skipManifest If true, omits the manifest file from the processed jar.
* @param misplacedClassStrategy The strategy to use when processing class files that are in the
* wrong package (see MisplacedClassProcessorFactory.STRATEGY_* constants).
*/
class JJProcessor(val patterns: Seq[PatternElement], val verbose: Boolean, val skipManifest: Boolean, val misplacedClassStrategy: String) extends JarProcessor {

def process(entry: EntryStruct): Boolean = proc.process(entry)
val zapList: Seq[Zap] = patterns.collect { case zap: Zap => zap }
val ruleList: Seq[Rule] = patterns.collect { case rule: Rule => rule }
val keepList: Seq[Keep] = patterns.collect { case keep: Keep => keep }
val renames: mutable.Map[String, String] = collection.mutable.HashMap[String, String]()

def getExcludes(): Set[String] = {
val field = proc.getClass().getDeclaredField("kp")
field.setAccessible(true)
val keepProcessor = field.get(proc)
val kp: KeepProcessor = if (keepList.isEmpty) null else new KeepProcessor(keepList.asJava)

if (keepProcessor == null) Set()
else {
val method = proc.getClass().getDeclaredMethod("getExcludes")
method.setAccessible(true)
method.invoke(proc).asInstanceOf[java.util.Set[String]].asScala.toSet
}
}
val pr = new PackageRemapper(ruleList.asJava, verbose)

}
val processors: mutable.ArrayBuffer[JarProcessor] = collection.mutable.ArrayBuffer[JarProcessor]()
if (skipManifest)
processors += ManifestProcessor.getInstance
if (kp != null)
processors += kp

object JJProcessor {
val misplacedClassProcessor: JarProcessor = MisplacedClassProcessorFactory.getInstance.getProcessorForName(misplacedClassStrategy)
processors += new ZapProcessor(zapList.asJava)
processors += misplacedClassProcessor
processors += new JarTransformerChain(Array[RemappingClassTransformer](new RemappingClassTransformer(pr)))
processors += new ScalaSigProcessor(ruleList)
processors += new MethodSignatureProcessor(pr)
processors += new ResourceProcessor(pr)
val chain = new JarProcessorChain(processors.toArray)

def apply(patterns: Seq[PatternElement], verbose: Boolean, skipManifest: Boolean): JJProcessor =
new JJProcessor(new MainProcessor(patterns.asJava, verbose, skipManifest))
@throws[IOException]
def strip(file: Nothing): Unit = {
if (kp != null) {
val excludes = getExcludes
if (excludes.nonEmpty) StandaloneJarProcessor.run(file, file, new ExcludeProcessor(excludes.asJava, verbose))
}
}

/**
* Returns the <code>.class</code> files to delete. As well the root-parameter as the rename ones
* are taken in consideration, so that the concerned files are not listed in the result.
*
* @return the paths of the files in the jar-archive, including the <code>.class</code> suffix
*/
def getExcludes: Set[String] = if (kp != null) kp.getExcludes.asScala.map { exclude =>
val name = exclude + ".class"
renames.getOrElse(name, name)
}.toSet else Set.empty

/**
*
* @param struct entry struct to process
* @return <code>true</code> if the entry is to include in the output jar
* @throws IOException
*/
@throws[IOException]
def process(struct: EntryStruct): Boolean = {
val name = struct.name
val keepIt = chain.process(struct)
if (keepIt) if (!name.equals(struct.name)) {
if (kp != null) renames.put(name, struct.name)
if (verbose) System.err.println("Renamed " + name + " -> " + struct.name)
} else if (verbose) System.err.println("Removed " + name)
keepIt
}
}
Loading