aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/shot/InjectionApplicator.kt
blob: 82b05a6705a73d01fc617a6436c49aeaa46a954f (plain)
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
package moe.nea.shot

import org.objectweb.asm.*

class InjectionApplicator(
    val injections: ClassShots,
    writer: ClassWriter
) : ClassVisitor(Opcodes.ASM9, writer) {

    override fun visit(
        version: Int,
        access: Int,
        name: String?,
        signature: String?,
        superName: String?,
        interfaces: Array<out String>?
    ) {
        super.visit(version, access, name, signature, superName, interfaces)
        for (annotation in injections.annotations) {
            super.visitAnnotation(annotation.jvmRef, false).visitEnd()
        }
    }

    override fun visitField(
        access: Int,
        name: String,
        descriptor: String?,
        signature: String?,
        value: Any?
    ): FieldVisitor {
        val fieldVisitor = super.visitField(access, name, descriptor, signature, value)
        val fieldShots = injections.fieldShots[FieldRef(name)] ?: return fieldVisitor
        for (annotation in fieldShots.annotations) {
            fieldVisitor.visitAnnotation(annotation.jvmRef, false)
                .visitEnd()
        }
        return fieldVisitor
    }

    override fun visitMethod(
        access: Int,
        name: String,
        descriptor: String,
        signature: String?,
        exceptions: Array<out String>?
    ): MethodVisitor {
        val parentVisitor = super.visitMethod(access, name, descriptor, signature, exceptions)
        val methodShots = injections.methodShots[MethodRef(name, descriptor.replaceAfter(")", ""))] ?: return parentVisitor
        for (injection in methodShots.annotations) {
            parentVisitor.visitAnnotation(injection.jvmRef, false)
                .visitEnd()
        }
        var maxParameterCount = 0
        for ((parameter, annotations) in methodShots.parameterAnnotations) {
            maxParameterCount = maxOf(maxParameterCount, parameter + 1)
            for (annotation in annotations) {
                parentVisitor
                    .visitParameterAnnotation(parameter, annotation.jvmRef, false)
                    .visitEnd()
            }
        }
        return object : MethodVisitor(Opcodes.ASM8, parentVisitor) {
            override fun visitAnnotableParameterCount(parameterCount: Int, visible: Boolean) {
                super.visitAnnotableParameterCount(
                    if (visible) parameterCount else maxOf(parameterCount, maxParameterCount), visible)
            }
        }
    }
}