forked from EssentialGG/essential-gradle-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StripReferencesTransform.kt
212 lines (184 loc) · 7.96 KB
/
StripReferencesTransform.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package gg.essential.gradle.multiversion
import gg.essential.gradle.util.compatibleKotlinMetadataVersion
import kotlinx.metadata.KmClassifier
import kotlinx.metadata.KmType
import kotlinx.metadata.KmValueParameter
import kotlinx.metadata.jvm.KotlinClassMetadata
import org.gradle.api.Project
import org.gradle.api.artifacts.transform.InputArtifact
import org.gradle.api.artifacts.transform.TransformAction
import org.gradle.api.artifacts.transform.TransformOutputs
import org.gradle.api.artifacts.transform.TransformParameters
import org.gradle.api.attributes.Attribute
import org.gradle.api.file.FileSystemLocation
import org.gradle.api.provider.Provider
import org.gradle.api.provider.SetProperty
import org.gradle.api.tasks.Input
import org.objectweb.asm.AnnotationVisitor
import org.objectweb.asm.ClassReader
import org.objectweb.asm.ClassVisitor
import org.objectweb.asm.ClassWriter
import org.objectweb.asm.FieldVisitor
import org.objectweb.asm.MethodVisitor
import org.objectweb.asm.Opcodes
import org.objectweb.asm.Type
import org.objectweb.asm.tree.AnnotationNode
import java.io.Closeable
import java.io.File
import java.util.jar.JarInputStream
import java.util.jar.JarOutputStream
import java.util.zip.ZipEntry
/**
* Relocates packages and single files in an artifact.
*
* If a package is relocated, the folder containing it will be relocated as a whole.
* File renames take priority over package relocations.
*
* The packages do not have to be part of the artifact, e.g. a completely valid use case would be relocating guava
* packages in an artifact using guava (for actual use, you'd of course also have to relocate the guava artifact itself,
* otherwise the classes referred to after the relocation will not exist). This can be used together with [prebundle] to
* create fat jars which apply at dev time (to e.g. use two different versions of the same library).
*
* To simplify setup, use [registerStripReferencesAttribute].
*/
abstract class StripReferencesTransform : TransformAction<StripReferencesTransform.Parameters> {
interface Parameters : TransformParameters {
@get:Input
val excludes: SetProperty<String>
}
@get:InputArtifact
abstract val input: Provider<FileSystemLocation>
override fun transform(outputs: TransformOutputs) {
val excludes = parameters.excludes.get()
.map { it.replace('.', '/') }
.toSet()
val input = input.get().asFile
val output = outputs.file(input.nameWithoutExtension + "-common.jar")
(input to output).useInOut { jarIn, jarOut ->
while (true) {
val entry = jarIn.nextJarEntry ?: break
val originalBytes = jarIn.readBytes()
val modifiedBytes = if (entry.name.endsWith(".class")) {
val reader = ClassReader(originalBytes)
val writer = ClassWriter(reader, 0)
reader.accept(ClassTransformer(writer, excludes), 0)
writer.toByteArray()
} else {
originalBytes
}
jarOut.putNextEntry(ZipEntry(entry.name))
jarOut.write(modifiedBytes)
jarOut.closeEntry()
}
}
}
private inline fun Pair<File, File>.useInOut(block: (jarIn: JarInputStream, jarOut: JarOutputStream) -> Unit) =
first.inputStream().nestedUse(::JarInputStream) { jarIn ->
second.outputStream().nestedUse(::JarOutputStream) { jarOut ->
block(jarIn, jarOut)
}
}
private inline fun <T: Closeable, U: Closeable> T.nestedUse(nest: (T) -> U, block: (U) -> Unit) =
use { nest(it).use(block) }
private class ClassTransformer(
inner: ClassVisitor,
private val excludes: Set<String>,
) : ClassVisitor(Opcodes.ASM9, inner) {
private fun excluded(name: String) = excludes.any { name.startsWith(it) }
private fun excluded(type: Type): Boolean = when (type.sort) {
Type.OBJECT, Type.ARRAY -> excluded(type.internalName)
Type.METHOD -> excluded(type.returnType) || type.argumentTypes.any { excluded(it) }
else -> false
}
private fun excluded(type: KmType?) = type != null && excluded(type.classifier)
private fun excluded(classifier: KmClassifier) = when (classifier) {
is KmClassifier.Class -> excluded(classifier.name)
else -> false
}
private fun excluded(params: List<KmValueParameter>) = params.any { excluded(it.type) }
override fun visitAnnotation(descriptor: String, visible: Boolean): AnnotationVisitor {
return if (descriptor == "Lkotlin/Metadata;") {
KotlinMetadataTransformer(super.visitAnnotation(descriptor, visible), descriptor)
} else {
super.visitAnnotation(descriptor, visible)
}
}
override fun visit(
version: Int,
access: Int,
name: String,
signature: String?,
superName: String?,
interfaces: Array<out String>?
) {
super.visit(
version,
access,
name,
signature,
superName?.takeUnless { excluded(it) },
interfaces?.filterNot { excluded(it) }?.toTypedArray(),
)
}
override fun visitMethod(
access: Int,
name: String,
descriptor: String,
signature: String?,
exceptions: Array<out String>?
): MethodVisitor? {
return if (excluded(Type.getMethodType(descriptor)) || exceptions?.any { excluded(it) } == true) {
null
} else {
super.visitMethod(access, name, descriptor, signature, exceptions)
}
}
override fun visitField(
access: Int,
name: String,
descriptor: String,
signature: String?,
value: Any?
): FieldVisitor? {
return if (excluded(Type.getType(descriptor))) {
null
} else {
super.visitField(access, name, descriptor, signature, value)
}
}
inner class KotlinMetadataTransformer(val inner: AnnotationVisitor, desc: String) : AnnotationNode(Opcodes.ASM9, desc) {
override fun visitEnd() {
var metadata = kotlinMetadata ?: return
val extraInt = metadata.header.extraInt
val metadataVersion = compatibleKotlinMetadataVersion(metadata.header.metadataVersion)
when (metadata) {
is KotlinClassMetadata.Class -> {
val cls = metadata.toKmClass()
cls.supertypes.removeIf { excluded(it.classifier) }
cls.properties.removeIf { excluded(it.returnType) || excluded(it.receiverParameterType) }
cls.functions.removeIf { excluded(it.returnType) || excluded(it.receiverParameterType) || excluded(it.valueParameters) }
metadata = KotlinClassMetadata.Class.Writer().apply(cls::accept).write(metadataVersion, extraInt)
}
else -> {}
}
kotlinMetadata = metadata
accept(inner)
}
}
}
companion object {
@JvmStatic
fun Project.registerStripReferencesAttribute(name: String, configure: Parameters.() -> Unit): Attribute<Boolean> {
val attribute = Attribute.of(name, Boolean::class.javaObjectType)
dependencies.registerTransform(StripReferencesTransform::class.java) {
from.attribute(attribute, false)
to.attribute(attribute, true)
parameters(configure)
}
dependencies.artifactTypes.all {
attributes.attribute(attribute, false)
}
return attribute
}
}
}