|
| 1 | +import org.objectweb.asm.ClassReader |
| 2 | +import org.objectweb.asm.ClassVisitor |
| 3 | +import org.objectweb.asm.ClassWriter |
| 4 | +import org.objectweb.asm.Opcodes |
| 5 | +import org.objectweb.asm.commons.ClassRemapper |
| 6 | +import org.objectweb.asm.commons.Remapper |
| 7 | +import java.io.FilterReader |
| 8 | +import java.io.Reader |
| 9 | +import java.io.StringReader |
| 10 | +import java.nio.charset.StandardCharsets |
| 11 | + |
| 12 | +// support configuration of relocations via extension |
| 13 | +extensions.create("shader", Shader::class) |
| 14 | + |
| 15 | +// automatically apply shading to the main jar task |
| 16 | +tasks.named<Jar>("jar").configure { |
| 17 | + // prune away unused directories after relocation |
| 18 | + exclude { it.isDirectory } |
| 19 | + // apply relocations as we add resources to the jar |
| 20 | + eachFile(Shader()) |
| 21 | + // this charset supports safe filtering of binary content, as chars map 1:1 to bytes |
| 22 | + filteringCharset = "ISO-8859-1" |
| 23 | +} |
| 24 | + |
| 25 | +/** Applies configured relocations as resources are copied into the jar. */ |
| 26 | +open class Shader : Action<FileCopyDetails> { |
| 27 | + companion object { |
| 28 | + var relocations: Map<String, String> = mapOf() |
| 29 | + } |
| 30 | + |
| 31 | + // accepts relocations from build.gradle.kts |
| 32 | + fun relocate(entry: Pair<String, String>) { |
| 33 | + relocations += entry |
| 34 | + } |
| 35 | + |
| 36 | + override fun execute(t: FileCopyDetails) { |
| 37 | + var resourcePath: String = t.path |
| 38 | + // relocate any class-file references |
| 39 | + if (resourcePath.endsWith(".class")) { |
| 40 | + t.filter(ShadeClass::class) |
| 41 | + } |
| 42 | + // relocate file-name for consistency |
| 43 | + relocations.forEach { (oldPath, newPath) -> |
| 44 | + resourcePath = resourcePath.replace(oldPath, newPath) |
| 45 | + } |
| 46 | + t.path = resourcePath |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +/** Filters class-files using remapper from asm-commons. */ |
| 51 | +class ShadeClass(reader: Reader) : FilterReader(shade(reader)) { |
| 52 | + companion object { |
| 53 | + fun shade(reader: Reader): Reader { |
| 54 | + val remapper = object : Remapper(Opcodes.ASM9) { |
| 55 | + override fun map(internalName: String?): String? { |
| 56 | + var result: String? = internalName |
| 57 | + Shader.relocations.forEach { (oldPath, newPath) -> |
| 58 | + result = result?.replace(oldPath, newPath) |
| 59 | + } |
| 60 | + return result |
| 61 | + } |
| 62 | + |
| 63 | + override fun mapValue(value: Any?): Any? { |
| 64 | + return if (value is String) { |
| 65 | + map(value) |
| 66 | + } else { |
| 67 | + super.mapValue(value) |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + // rebuild constant-pool from scratch and make sure every method has a stack-map |
| 72 | + val cw = ClassWriter(ClassWriter.COMPUTE_FRAMES) |
| 73 | + // apply relocation to class-file and upgrade pre-Java 8 class-files to Java 8 |
| 74 | + val cr = ClassReader(reader.readText().toByteArray(StandardCharsets.ISO_8859_1)) |
| 75 | + cr.accept(ClassRemapper(ToJava8(cw), remapper), 0) |
| 76 | + return StringReader(String(cw.toByteArray(), StandardCharsets.ISO_8859_1)) |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +/** Upgrades pre-Java 8 class-files to Java 8, to allow faster verification at runtime. */ |
| 82 | +class ToJava8(cv: ClassVisitor) : ClassVisitor(Opcodes.ASM9, cv) { |
| 83 | + override fun visit( |
| 84 | + version: Int, access: Int, name: String?, signature: String?, superName: String?, interfaces: Array<out String?>? |
| 85 | + ) { |
| 86 | + super.visit(version.coerceAtLeast(Opcodes.V1_8), access, name, signature, superName, interfaces) |
| 87 | + } |
| 88 | +} |
0 commit comments