I had met problem hope this channel can help me ou...
# compiler
j
I had met problem hope this channel can help me out... I am using Kotlin few months ago and I have the kotlinc compiler plugin (using arrow meta library) in place which changes the .class by adding, for example, new properties or new methods etc during kotlin compilation time. for example, the original source Kotlin A.kt is like below
Copy code
@MetaData
data class A (val x: String, val y: String)
after applying compiler plugin, the .class file will be altered to something like below (from source perspective), basically, I will add implicitly MetaData into the primary constructor for all source Kotlin so long as it is with annonation @MetaData in place, plus a new method 
fun getMetaData()
 get generated.
Copy code
data class A(val x: String, val y:String, val myMeta: MetaData) {
       fun getMetaData() {
         //some logic check
         return myMeta
       }
    }
now when it comes to use the new synthetic ".class" file manipulated as below, IntelliJ complains it cannot find resolve A (it has only the constructor with 2 parameters not 3) and cannot resolve the the synthetic method getMetaData() either.
Copy code
val x = A("ba", "fo", MetaData(..))
val y = x.getMetaData()
can somebody shed some light on it? I know lombok seems no problem with it after adding its @Getter annotation for example into Java source code, IntelliJ can recognize its getXXX method (which is generated by lombok). I don't know how to implement the same for my case for kotlin language. please include the detailed steps if possible.
stackoverflow 1
👀 1
r
Hi @jianbing this question is probably better suited in #arrow-meta. In whatever case your issue is a general one for all compiler plugins. New generation in place like the one you are doing is not seen by the IDEA plugin unless you manually spell what you are doing through synthetic descriptors and I found this to not work in many cases either. With the new Frontend IR system that is upcoming this may change since transformations you do over in FIR should be picked up by the IntelliJ IDEA plugin but that is not yet stable. If you want to do what you are doing probably the best solution is to generate extension functions with something like KSP or wait for FIR to be stable.
j
@raulraja, Thank you so much!!! Could you please point me an existing case (or detail me how-to if you can) to do below as you mentioned? I just want to have a try and maybe there is a luck to me...😀 "..New generation in place like the one you are doing is not seen by the IDEA plugin unless you manually spell what you are doing through synthetic descriptors...."
c
@raulraja I’m a little interested in this, but I have a question: is it different from
SyntheticResolveExtension
? I found that
SyntheticResolveExtension
can achieve the similar behavior. Why are there two different extensions?
r
I believe the difference is that
SyntheticResolveExtension
is for members and the other is needed for
scope
resolution but I’m not positive. I could never get either to work without a custom IDEA plugin.
🤞 1
j
@Chachako, Hi Rin, can you shed me some detail light on how to implement, some mock code will be better. 😀 much appreicate in adavnce. "....I found that 
SyntheticResolveExtension
 can achieve the similar behavior...." basically my case is : I added new inner "data class XXX" and other new methods "fun getYYY()" into original ".class" file say A.kt, which is now very sythentic so that IntelliJ IDE cannot do code completion, nor resovle those synthetic symbols when I wrote code say A.XXX(...), a.getYYYY()....
@raulraja, it is too overwhelming to me to implement below SyntheticScope.kt, I cannot understand the APIs, its parameters etc after my best tries, so sad....😅 I event cannot search out a live example from my region (I am from China, cannot access google search). very headaching now...🥶 https://github.com/JetBrains/kotlin/blob/92d200e093c693b3c06e53a39e0b0973b84c7ec5/[…]tors/src/org/jetbrains/kotlin/resolve/scopes/SyntheticScopes.kt
c
@jianbing Yes, you can refer to the example I implemented. It actually implements similar behavior to data class, both implicitly creating some new methods in the class (
copy
,
componentN
). This should be similar to your needs, so you just need to implement the
SyntheticResolveExtension
in the compiler plugin (https://github.com/RinOrz/sweekt/blob/2b680e1042557b7d3dcea752db7db8d095f2c025/plugin/compiler-hosted/src/main/kotlin/com/meowool/sweekt/info/InfoClassSynthetic.kt#L56-L98), and then declare the Kotlin extension point in the IDEA plugin and this is done (https://github.com/RinOrz/sweekt/blob/2b680e1042557b7d3dcea752db7db8d095f2c025/plugin/ide/src/main/resources/META-INF/plugin.xml#L19-L20), but I have to tell you that IDEA does not currently allow us to create the Kotlin extension (https://jetbrains-platform.slack.com/archives/C5U8BM1MK/p1637391960430400), so it will not be able to upload to the plugin marketplace. Good luck 🙌
j
@Chachako, Thanks. My case is something different here. I already massaged the .class with arrow-meta library at very beginning stage (Analyze phase), meaning my synthetic functions getXXX(..) were already added into that ".class". The extenstion point SyntheticResolveExtension seems to be used to generate new synthetic functions/properties/classes on the fly during the Resolution phase (as per my understanding).