Karim Houari
11/29/2022, 11:05 AMclass Foo {
val abc = "abc"
fun hello() = "Hello World"
}
and then wanted to create the following, but not modifying Foo class
class Bar {
fun hello(name: String) = "Hello $name"
}
Resulting in a Foo.class and a Bar.class
If there are no examples, any advice would be great please.raulraja
11/29/2022, 11:40 AMBar
or it's members in other parts of your code in your local module then you will need to do more than just an IR transformation.
If you are not planning to use the generated declaration Bar
in your local module you can use an IR transformation.raulraja
11/29/2022, 11:40 AMraulraja
11/29/2022, 11:41 AMraulraja
11/29/2022, 11:42 AMorg.jetbrains.kotlin.backend.common.extensions.IrGenerationExtension
and register that in your pluginraulraja
11/29/2022, 11:42 AMfun generate(moduleFragment: IrModuleFragment, pluginContext: IrPluginContext)
raulraja
11/29/2022, 11:43 AMorg.jetbrains.kotlin.ir.visitors.IrElementTransformer
to transform every declaration and expressionraulraja
11/29/2022, 11:45 AMraulraja
11/29/2022, 11:49 AMKarim Houari
11/29/2022, 11:53 AMBar
into a jar and use in another project.Karim Houari
11/29/2022, 11:55 AMorg.jetbrains.kotlin.ir.visitors.IrElementTransformer
which you mention, could you generate a new IrClass (e.g. Bar
in the example) using that?dmitriy.novozhilov
11/29/2022, 12:44 PMKarim Houari
11/29/2022, 12:57 PMKarim Houari
11/29/2022, 12:59 PMNoArg
and annotated Foo, would you expect it to work? Also, if necessary I would be fine for the IDE not to resolve the generated class, but would a class in another module compile successfully referencing the generated class?dmitriy.novozhilov
11/29/2022, 1:05 PMdmitriy.novozhilov
11/29/2022, 1:08 PMNoArg
because of how is check if @NoArg
annotation applicable to to some class. It checks if at least one of two things is true:
1. Parent class has constructor without arguments
2. Parent class is annotated with @NoArg
So since generated constructor is invisible for frontend, changing @NoArg
retention fixes this particular issueKarim Houari
11/29/2022, 1:51 PMdmitriy.novozhilov
11/29/2022, 1:54 PMSyntheticResolveExtension
. But for K2 you need to use FirDeclarationGenerationExtension
You can check how it's done for both frontends for kotlinx.serialization
(.k1
and .k2
directories respectively)dmitriy.novozhilov
11/29/2022, 1:56 PMKarim Houari
11/29/2022, 1:57 PM