Hey! Can I generate code (do transformations) aft...
# arrow-meta
a
Hey! Can I generate code (do transformations) after the analysis phase completes? I have this snippet, in which,
generateAllViolations
returns some code (String) that needs to be generated and that is referenced and used by other parts of the user's codebase.
Copy code
internal fun Meta.violationsGenerator(): AnalysisHandler = analysis(
    doAnalysis = Noop.nullable7<AnalysisResult>(),
    analysisCompleted = { _, _, bindingTrace, _ ->
        // this returns the code to generate
        val (location, code) = generateAllViolations(bindingTrace)
        null
    }
)
r
Hi @Ahmed Mourad!, yes Transform.newSources takes your generated files and returns AnalisisResult.retryWithAdditionalRoots which includes the new generated files. you could look at its impl in meta which does that on
analysysCompleted
or change your code to depend instead on a quote for the File that returns the Files you want to generate to Transform.newSources
💯 1
which allows you to pass new files to get re-analized. This is exactly what Transform.newSources does and what Kapt does to generate new sources
💯 1
a
Awesome! Is it possible to add functions or classes to existing files or classes instead of creating new files?
r
yes, but if you do that it won’t work in IDEA because the IDEA doc won’t be in sync with codegen so highly unrecommended. That’s the other kind of transforms and they will eventually go away for that reason. We have not found a good solution for that
💯 1
It requires IDEA plugins additionally and it’s too much to maintain the many use cases of in place transformations, specially when FIR may provide this or similar down the road and multiphased
💯 1
a
I see, doesn't seem to be worth it until it's provided by the FIR. Thanks a lot :)