in src/scala/io/bazel/rules_scala/jmh_support/BenchmarkGenerator.scala [139:183]
private def generateJmhBenchmark(
generatorType: GeneratorType,
sourceJarOut: Path,
resourceJarOut: Path,
benchmarkJarPath: Path,
classpath: List[Path]
): Unit = {
withTempDirectory { tempDir =>
val tmpResourceDir = tempDir.resolve("resources")
val tmpSourceDir = tempDir.resolve("sources")
tmpResourceDir.toFile.mkdir()
tmpSourceDir.toFile.mkdir()
withClassLoader(benchmarkJarPath :: classpath) { isolatedClassLoader =>
val source: GeneratorSource = generatorType match {
case AsmGenerator =>
val generatorSource = new ASMGeneratorSource
generatorSource.processClasses(collectClassesFromJar(benchmarkJarPath).map(_.toFile).asJavaCollection)
generatorSource
case ReflectionGenerator =>
val generatorSource = new RFGeneratorSource
generatorSource.processClasses(
collectClassesFromJar(benchmarkJarPath)
.flatMap(classByPath(_, isolatedClassLoader))
.asJavaCollection
)
generatorSource
}
val generator = new JMHGenerator
val destination = new FileSystemDestination(tmpResourceDir.toFile, tmpSourceDir.toFile)
generator.generate(source, destination)
generator.complete(source, destination)
if (destination.hasErrors) {
throw new GenerationException(
"JHM Benchmark generator failed" +: destination.getErrors.asScala.map(_.toString).toSeq)
}
}
constructJar(sourceJarOut, tmpSourceDir)
constructJar(resourceJarOut, tmpResourceDir)
}
}