https://kotlinlang.org logo
#getting-started
Title
# getting-started
h

hfhbd

08/11/2022, 12:53 PM
I want to generate Kotlin code from another source. At the moment I have my own representation and use KotlinPoet. This works fine, but I want to improve the code, eg
!(foo == "FOO")
should refactored into this
foo != "FOO"
. Is it possible to use the Kotlin plugin/frontend and its Kotlin fixup automatically? (I know this would require mapping my internal representation to Kotlin frontend code) Or do you know some other library which has basic optimizations? Alternative I have to write these Kotlin optimization by myself
c

Chris Lee

08/11/2022, 1:07 PM
generally generated code isn’t checked-in/maintained or otherwise needed to be viewed by people (transient; generated as part of build process, for example), making stylistic optimizations redundant. Is that the case here?
h

hfhbd

08/11/2022, 1:17 PM
Yes, the converted code should be generated once as a base for further development. And I want to make this initial code to look good. I already have some optimizations, but I thought I can reuse other optimizations like the one from IntelliJ. Or other libs, if some exists.
c

Chris Lee

08/11/2022, 1:24 PM
post-generation one could use IntelliJ’s Analyze->Inspect Code to flag issues, which can be selectively bulk-fixed from the analysis results.
h

hfhbd

08/11/2022, 1:28 PM
Yeah, thats true, but only manually.
c

Chris Lee

08/11/2022, 1:29 PM
sure, but that is a small investment for a one-time cleanup relative to writing all kinds of generation-optimizations. For any given inspection, right-click and fix all violations across the code base.
3 Views