google / dagger

A fast dependency injector for Android and Java.
https://dagger.dev
Apache License 2.0
17.36k stars 2k forks source link

Extension methods in the Dagger-Spi library cannot be referenced normally #4102

Closed zhangkevd closed 9 months ago

zhangkevd commented 9 months ago
import dagger.spi.shaded.androidx.room.compiler.codegen.XClassName.toJavaPoet

import dagger.spi.shaded.androidx.room.compiler.processing.XTypeElement

class MyClass {

    companion object {

        fun create(xTypeElement: XTypeElement) {

            xTypeElement.asClassName().toJavaPoet()

        }

    }

}

toJavaPoet() function cannot be referenced

dependencies {
    implementation "com.google.dagger:dagger-spi:2.48.1"
    implementation "com.google.guava:guava:32.1.3-jre"
    implementation "com.google.devtools.ksp:symbol-processing-api:1.9.10-1.0.13"
}

the extension methods:

image

demo:

CustomDagger.zip

wanyingd1996 commented 9 months ago

Hi, the shaded APIs are generated for compatibility, and should not be used. We are working on hiding those classes now. We don't want to expose XProcessing types, those are implementation details of Dagger ksp, and may be removed in future. Please directly use javac() or ksp() element exposed from Dagger model types instead.

zhangkevd commented 9 months ago

@wanyingd1996 sorry,I don't understand

Please directly use javac() or ksp() element exposed from Dagger model types instead.

where is it?

You mean not to reference anything in dagger-spi, but I need to extend the content of the compiler, such as customizing something like ViewModel. I tried to imitate the implementation of hilt\android\processor\internale\viewmodel, but encountered this difficulty. I hope that in subsequent hilt updates, I can successfully synchronize related logic. Are there any aspects that I need to know, or are there any similar examples?

wanyingd1996 commented 9 months ago

I thought you were just trying to use dagger spi API, but seems you are trying to contribute to dagger code, https://github.com/google/dagger/blob/master/java/dagger/spi/model/DaggerElement.java#L31.

such as customizing something like ViewModel. I tried to imitate the implementation of hilt\android\processor\internale\viewmodel

If you are trying to contribute to dagger code, you should reference androidx.room.compiler.processing.XElement directly instead of dagger.spi.shaded.androidx.room.compiler.processing.XElement, the later one is generating for shading purpose, https://medium.com/@akhaku/java-class-shadowing-and-shading-9439b0eacb13