Jakub Ledwon
08/08/2023, 7:51 PMTest
class which contains hi
method and I annotated it with @MyTest
. In my plugin I tried to rename this method to new_hi
. I used kotlin-compile-testing to test it (in compiler plugin module) and I can see that the method name has changed. But when I add Test().new_hi()
inside my test the test fails with Unresolved reference: new_hi
. I also did the same thing in other module that is using the compiler plugin and came across the same problem. Moreover when I inspected build/classes…/Test
I can see that there is still old name for hi
method. Two questions regarding to this:
• How can I make my new_hi
available in source files that use the plugin (it doesn’t have to have autocompletion and can be red in IDE, but just so it compiles and works)?
• What am I missing and what is happening behind the scenes that causes build/classes…/Test
to remain unchanged and tests to fail?
I’m attaching my test code in 🧵Jakub Ledwon
08/08/2023, 7:52 PM@Test
fun test() {
val file = SourceFile.kotlin(
"main.kt", """
import com.example.MyTest
@MyTest
class Test {
fun hi () {
}
}
fun main() {
Test().new_hi() // this fails unless changed to Test().hi()
}
""".trimIndent()
)
val result = KotlinCompilation().apply {
sources = listOf(file)
useIR = true
compilerPluginRegistrars = listOf(MyTestCompilerPluginRegistrar())
inheritClassPath = true
}.compile()
val javaCode = result.javaCode("Test") // this has new_hi() as I expected
assertEquals(KotlinCompilation.ExitCode.OK, result.exitCode)
}
javaCode
extension is based on TestUtils from debuglog repositoryshikasd
08/08/2023, 8:32 PMSyntheticResolveExtension
or FirDeclarationGenerationExtension
) to make it available for other modules.
For IDE support, you'll have to create IDE plugin, I believe, that adds your compiler plugin to Kotlin resolution in IDE.Jakub Ledwon
08/08/2023, 9:30 PMSyntheticResolveExtension
in which I overrode generateSyntheticMethods
to generate new method called helloWorld
for classes with @MyTest
. I also created IrGenerationExtension
which uses visitFunction
to set body of my newly created function. Again it works fine and in test I can see that this class is available in javaCode
, but when I try to call it using Test().helloWorld()
I get Unresolved reference: helloWorld
againJakub Ledwon
08/08/2023, 9:32 PM