Convert java to kotlin as part of build - Stack Overflow

I have an existing angular project that used .habarta.typescript-generator to convert a java model (use

I have an existing angular project that used .habarta.typescript-generator to convert a java model (used on the server) to a typescript model for use on the angular web client. I am now attempting to move to Compose Multiplatform, and unfortunately, searching here and elsewhere indicates that while on it's own Kotlin can consume java libraries, doing so in Compose Multiplatform will cause iOS and WebASM to fail (very disappointing since I had hoped java interop would simplify my build). The only parts I need to share are specific model classes, so I'm looking for a similar tool to consume a list of either .java or .class files and emit Kotlin classes.

PLEASE DO NOT SUGGEST PASTING anything anywhere. Also I don't want anything that involves a website or an IDE. The server side runs in TomEE and utilizes a lot of java libraries for ORM and Security and will remain java. The goal is to automatically keep the server side java structures and client side Kotlin structures in sync in a way that is long term maintainable. Ideally if a field is added to the class in java, after I run the build, it becomes available in the client app. I'm willing to accept some loss of fidelity such as forcing everything to be nullable in Kotlin.

I did search for typescript -> Kotlin as well which would be less desirable, but workable since I already have a useful typescript representation.

This does not need to be round-tripable, it only needs to work Java -> Kotlin, or Java -> typescript -> Kotlin. The java classes may have some logic in them but if I had to work with a data only representation that is ok. I just don't want to have to make changes in multiple places, or otherwise manually sync the business model across layers.

Does anyone know of a Gradle plugin, or otherwise scriptable automated conversion utility?

I have an existing angular project that used https://plugins.gradle./plugin/cz.habarta.typescript-generator to convert a java model (used on the server) to a typescript model for use on the angular web client. I am now attempting to move to Compose Multiplatform, and unfortunately, searching here and elsewhere indicates that while on it's own Kotlin can consume java libraries, doing so in Compose Multiplatform will cause iOS and WebASM to fail (very disappointing since I had hoped java interop would simplify my build). The only parts I need to share are specific model classes, so I'm looking for a similar tool to consume a list of either .java or .class files and emit Kotlin classes.

PLEASE DO NOT SUGGEST PASTING anything anywhere. Also I don't want anything that involves a website or an IDE. The server side runs in TomEE and utilizes a lot of java libraries for ORM and Security and will remain java. The goal is to automatically keep the server side java structures and client side Kotlin structures in sync in a way that is long term maintainable. Ideally if a field is added to the class in java, after I run the build, it becomes available in the client app. I'm willing to accept some loss of fidelity such as forcing everything to be nullable in Kotlin.

I did search for typescript -> Kotlin as well which would be less desirable, but workable since I already have a useful typescript representation.

This does not need to be round-tripable, it only needs to work Java -> Kotlin, or Java -> typescript -> Kotlin. The java classes may have some logic in them but if I had to work with a data only representation that is ok. I just don't want to have to make changes in multiple places, or otherwise manually sync the business model across layers.

Does anyone know of a Gradle plugin, or otherwise scriptable automated conversion utility?

Share Improve this question asked Mar 24 at 18:56 GusGus 6,8936 gold badges39 silver badges60 bronze badges 3
  • Just to clarify: You say you want to keep the server side in Java. Does this apply to everything on the server side, or would you be ok with converting the models only to Kotlin? This way your server-side code could still remain in Java (Kotlin's Java interop works in both directions), but you could also directly consume the models from all other Kotlin targets like iOS or WASM, you wouldn't need any transformations at all. Would this be an option? – tyg Commented Mar 24 at 19:32
  • The model classes are annotated in several ways leveraged by JPA ORM, custom code and Jackson/JSOG generator and other libraries. I doubt it will be feasible to convert them to kotlin. – Gus Commented Mar 24 at 20:12
  • Also, only 20-30% of the model needs to be available in the UI... – Gus Commented Mar 24 at 20:25
Add a comment  | 

2 Answers 2

Reset to default 0

In principle this is do-able. I don't know of a tool, so here's an approach:

  1. Provide a configuration file listing the package/classname of each of the "root" model objects
  2. Use Java Reflection to work through the objects to discover the member fields and what type they are. Write out the Kotlin classes (in text which then needs compilation). If the source java has Lombok/similar annotations you'll be able to use this to know to use Kotlin data classes (with the gotcha that no inheritance is allowed).
  3. As you go, you track classes already discovered.
  4. You'll need to put some kind of limit to how deep you go - and this will be the most difficult part, I suspect... e.g. stopping when you find classes that have known working equivalents in Kotlin (e.g. Double) or java ones that Kotlin supports on the target platform (HashMap, BigDecimal).
  5. I assume you have already have checked that you have no classes that are not portable... (in others' libraries, etc)

This doesn't handle logic in the model classes. For that, I wonder if you can find out how IntelliJ converts Java to Kotlin. Perhaps they have Open Sourced that.

Google Workspace hit your exact problem while migrating to KMP. The tool they use to convert Java to Kotlin/Native is j2kt. It is developed in the open, but there is no official support for it so you will need to build it from source and somehow integrate it into your pipeline.

I will add that the output is not beautiful, but it might be acceptable as an intermediate step as part of your build process.

Edit: Sorry I missed that your question was also for Kotlin/WASM! j2kt is built on top of j2cl which apparently has a goal to eventually understand Kotlin. This doesn't solve your problem, but it means there is some hope for j2kt's output to hopefully be compatible with translation to js and maybe one day to wasm in the future. My guess is that if you try this out of the box for wasm, you will definitely hit issues with kotlin apis that are not available on the web but if you do this for only pojos maybe you will be ok?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744232837a4564337.html

相关推荐

  • Convert java to kotlin as part of build - Stack Overflow

    I have an existing angular project that used .habarta.typescript-generator to convert a java model (use

    8天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信