Will Kotlin compile .java file? And could you put some support for Java static functions?


#1

Hallo,

I am facing a problem with using Kotlin alongside with Java in a TestNG + Maven project. My Kotlin file needs some dependencies which will be auto generated during compile time, but for some stupid reasons, it seems that Kotlin compiles first and at that time, the supposed-to-be generated dependencies, which are in Java, have not been generated yet and it leads to the error of not being able to access the classes and asked me to check if it is missing or conflicting. When I used Java, everything works perfectly fine.

During my development, I also find it really annoying to deal with the problem of configuring Java and Kotlin in Maven. Will Kotlinc be able to compile Java as well?

One more problem, why can’t there be like a static annotation for java function without using the companion object?

Thank you for your time and consideration. I am looking forward to seeing Kotlin better.


#2

You need to tell Maven that the Kotlin compile should happen after the code generation. How you do that, I don’t know. I tend to use Gradle these days, and have some generation steps (Swagger, WSDL2Java) and it’s a case of defining the appropriate task dependencies. Then everything works as expected.

As far as static, Kotlin is making the separation between Class methods and Object methods explicit. This was an explicit design decision. Java has this separation, BUT the only indicator is a ‘static’ keyword, which has it’s own issues.

And as you use Kotlin more, and get more comfortable with the features it has, you’ll find very few companion objects. Extension functions, package functions all make companion objects unnecessary.

The only general exception is for Java interop where a ‘static’ is required, hence the @JvmStatic annotation.

Hope the helps some.