All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.likethesalad.dagger.remapper.plugin.DaggerRemapper.kt Maven / Gradle / Ivy

package com.likethesalad.dagger.remapper.plugin

import dagger.internal.Factory
import net.bytebuddy.asm.AsmVisitorWrapper
import net.bytebuddy.build.Plugin
import net.bytebuddy.description.field.FieldDescription
import net.bytebuddy.description.field.FieldList
import net.bytebuddy.description.method.MethodList
import net.bytebuddy.description.type.TypeDescription
import net.bytebuddy.dynamic.ClassFileLocator
import net.bytebuddy.dynamic.DynamicType.Builder
import net.bytebuddy.implementation.Implementation
import net.bytebuddy.jar.asm.ClassVisitor
import net.bytebuddy.jar.asm.commons.ClassRemapper
import net.bytebuddy.jar.asm.commons.SimpleRemapper
import net.bytebuddy.pool.TypePool
import javax.inject.Provider

class DaggerRemapper : Plugin {

    override fun apply(
        builder: Builder<*>,
        typeDescription: TypeDescription,
        classFileLocator: ClassFileLocator
    ): Builder<*> {
        return builder.visit(object : AsmVisitorWrapper.AbstractBase() {

            override fun wrap(
                instrumentedType: TypeDescription,
                classVisitor: ClassVisitor,
                implementationContext: Implementation.Context,
                typePool: TypePool,
                fields: FieldList,
                methods: MethodList<*>,
                writerFlags: Int,
                readerFlags: Int
            ): ClassVisitor {
                return ClassRemapper(
                    classVisitor, SimpleRemapper(
                        "dagger/internal/Preconditions",
                        "com/likethesalad/dagger/tools/Preconditions"
                    )
                )
            }
        })
    }

    override fun matches(target: TypeDescription?): Boolean {
        return isDaggerFactory(target) || isJavaxProvider(target)
    }

    private fun isDaggerFactory(target: TypeDescription?): Boolean {
        return target?.isAssignableTo(Factory::class.java) == true
    }

    private fun isJavaxProvider(target: TypeDescription?): Boolean {
        return target?.isAssignableTo(Provider::class.java) == true
    }

    override fun close() {
        // Nothing to do
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy