Sasha Shpota
01/18/2021, 11:32 AMIrElementTransformerVoidWithContext.visitPropertyNew()
and my code looks somewhat like this:
override fun visitPropertyNew(declaration: IrProperty): IrStatement {
declaration.getter?.let {
it.annotations = it.annotations + aNewAnnotationEntry
}
return super.visitPropertyNew(declaration)
}
Problem: This works well for all properties but not for properties of data classes.
Question: How do I add an annotation to a getter of a property of a data class declared in its constructor?Sasha Shpota
01/18/2021, 9:41 PMvisitConstructor
function. Bot how do I access getters and setters from here?
override fun visitConstructor(declaration: IrConstructor): IrStatement {
declaration.valueParameters.forEach { param ->
// how to access getters/setters from here?
}
return super.visitConstructor(declaration)
}
rnett
01/18/2021, 9:43 PMIrElement.dump(true)
)? You can debug it too, and manually inspect each property.Sasha Shpota
01/18/2021, 9:51 PMunless the data class properties don't have gettersMy data class looks like this:
data class User(@SampleAnnotation val name: String)
Maybe try dumping the IR (I am dumping as well as debugging 🙂)? You can debug it too, and manually inspect each property.IrElement.dump(true)
rnett
01/18/2021, 9:51 PMrnett
01/19/2021, 4:02 AMdata class User(@get:SampleAnnotation val name: String)
) and see what the IR dump looks likeSasha Shpota
01/19/2021, 8:24 AMvisitPropertyNew
. I can use use-site targets, but it is not suitable in my case. In a nutshell, I want to achieve an effect that if a property declared in constructor or whenever else has an annotation (without specifing use-site target) I want to modify its getter.
So now I need to figure out, how to check if a property was produced from a constructor parameter of a data class...
Thank you for help @rnett!rnett
01/19/2021, 8:29 AMSasha Shpota
01/19/2021, 8:35 AMYou mean the annotations like from your example are put on the parameter, not the property?Exactly. My logic was relying on the fact that a property has an annotation. It didn't have, so it didn't enter a needed code block.
The serialization plugin detects it somehow I thinkIs this the plugin you are talking about https://github.com/Kotlin/kotlinx.serialization ?
Or look at the initialization, either on the property's field or in an init block (I'm not sure which is generated)Thank you for the suggestion. I am new to compiler plugins and I do not understand it (yet), but now I know the direction to dig in 🙂
rnett
01/20/2021, 4:21 AM@SampleAnnotation
in your sample) only target properties (i.e. @Target(AnnotationTarget.PROPERTY)
). It was required for me to get serializaiton to pick up my @SerialInfo annotation.