This plugin will automatically and implicitly add open to some specific Spring classes. Luckily, there is yet another easy solution. And what if other classes needed to be subclassed? Do I needed to repeat this try and error forever? This was annoying: I should had go over all the Kotlin entities and add open to their declaration. Apparently, Spring needs to subclass your classes to do its magic. They cannot be subclassed unless you mark them explicitely as open. I instantaneously knew the cause: classes in Kotlin are final by default. In the logs, there were errors like this: Cannot subclass final class gr.Tenant I deployed the artifact to do some real test in the dev environment and… the app did not start. However, for simple classes, it works fine.Īfter I converted 4/5 classes, I try to compile again. The built-in conversion tool is very good, even if the result is not really Kotlin-idiomatic. The first thing I did was to convert my Spring entities classes into Kotlin classes (and finally getting rid of all the annoying setter and getter). I tried again to compile, this time, everything worked. maven-compiler-plugin 3.5.1 default-compile none default-testCompile none java-compile compile compile java-test-compile test-compile testCompile KOTLIN TO SWIFT CONVERTER CODEFor your convenience, I copy the code below. The good news is the solution was easy to find: I went to the official Kotlin documentation and copied the maven-compiler-plugin plugin configuration from the example code an pasted in my pom.xml. Somehow, the automation tool is broken and produces a Maven configuration that cannot compile Kotlin and Java code at the same time. At least in Maven and with my version of IntelliJ (2020.1). Unfortunately this automatic conversion does not work. That was easy: I eagerly clicked Build and… I clicked, IntelliJ started doing its magic and, after a while, Maven was configured for Kotlin. You go in “Tool -> Kotlin -> Configure Kotlin in Project.” It was worth a try. Looking around, I found that IntelliJ contains a tool to add Kotlin’s support in your current project. I knew it is possible, but I still felt fear and excitement. Let’s begin by converting one classĪt this point, I had a Java Spring codebase and I needed to configure my build system ( Maven) to compile a Java/Kotlin hybrid Spring codebase. If you add to this the fact that the main Kotlin design principle is “painless Java interoperability”, you already know the best way to proceed. They are less verbose and they are widely supported. Kotlin, on the other hand, has been designed with this modern principle in mind and nullable/non-nullalbe types flow straightforwardly into the code. It is not completely integrated into the language and legacy libraries, and converting an old-style Java code into an Optional-Java code pollute your code of a lot of ugly if (foo.isPresent()) checks. The feature is a life-saver, but, unfortunately, it is still a bit clunky. If you know Kotlin, Rust, Swift, or any other language designed in this decade, you know what I am talking about. Optional types are not an exoteric feature anymore. If you have Optional foo, you cannot use it where a String is expected: you will first need to unwrap it and, as a consequence, check if the content of foo is not null. Optional, in short, wraps around a nullable variable providing type-safe access to that value. The addition of Stream was excellent but the introduction of the Optional type is probably the best feature of any modern Java implementation. After years and years of stagnation, Java is now a not-unbearable language to work with. In recent years, Java got much better at almost everything. Do your research first! Talk with who is in charge. If you are not, it is always a bad idea to switch technology on an impulse. Therefore, I can assume the risk of playing and spending time messing with the core application of our business. At work I am the guy in charge of deciding the technical stack.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |