How can I stop IntelliJ IDEA from turning methods into properties when Kotlinizing a Java file? - kotlin

In IntelliJ IDEA I can right-click on a Java file and convert it to Kotlin.
It mostly goes well, but this irks me: When the method name starts with is, in Kotlin it comes out as a property instead of a function.
Is there a setting I can change to get Kotlin to convert these methods to functions like all the other methods?

As of Kotlin IDEA plugin 1.4.0, every getter-like method that starts with get or is is always converted into a Kotlin property. This behavior cannot be overridden.
The problem is described in the issue https://youtrack.jetbrains.com/issue/KT-36826, you can vote for it and discuss further on YouTrack.

Related

Kotlin constructor reference in Intellij

It is not really a Kotlin question, maybe an Intellij question, I don't know.
Assume we have a data class
data class Person(val name: String = "untitled", val age: Int = 20)
And we have a function
fun factory(cstr: ()->Person) : Person {
return cstr()
}
Then we can invoke factory(::Person) and obtain an instance of Person class with default constructor parameters.
The fun factory can be invoked successfully wherever. But in IntelliJ I get a red underline error
Look like the IDE failed to recognize that there is a default constructor.
However, If I change the code like that, the error goes away. Everything runs perfectly and no error is shown in the IDE.
I am using IntelliJ 2020.2 and Kotlin 1.4.10.
Maybe it is about some IntelliJ inspection rules, but I cannot find one related.
Further, it is a piece of old code that showed no error before (maybe 5 months ago). I am not sure what has bee change since then caused the error.
So the problem is why is Intellij show error for lambda version and not for KFunction version?
I assume invoking factory like this works fine:
factory(() -> Person())
Then it seems your version of the Kotlin IDE plugin does not handle constructor references well in this case. The compiler does, since the code works. If the IDE plugin version is the latest, please file a bug at https://youtrack.jetbrains.com/issues/KT
I reproduced the problem with old inference enabled in the build.gradle file: freeCompilerArgs += ["-XXLanguage:-NewInference"]. Please make sure you're using new (default) type inference in Kotlin 1.4+. So, remove the compiler argument, that should fix the IDE highlighting after Gradle project reimport into IDEA.

Java method not available in Kotlin?

I am using the ejml-library (written in Java) in a Kotlin project. I imported the library (everything seems to work fine) in IntelliJ. However, some methods which should be available (e.g. the inherited getDDRM() method of the class SimpleMatrix) are not recognized and I can't use them. Whats very weird is that the very same procedure used with Scala (also using IntelliJ) works. That is, in Scala I can access the method - with Kotlin I can't.
It would be great if someone could shed some light on this.
update:
the getDDRM() method is part of the abstract class "Class SimpleBase<T extends SimpleBase>" and has the following signature: "public DMatrixRMaj getDDRM()". In my code I call this method on an instance of class SimpleMatrix which is a concrete class inheriting the SimpleBaseClass –
I should also note that I rebuild it with gradle and the issue still persists.
IMPORTANT: I should add that I can access other methods defined in the very same class. For instance I can access the method getMatrix() which is just another method of the very same (abstract) class. In fact, IntelliJ's method completion shows me a whole list of methods - but the getDDRM() is missing. I really don't get the cause of this problem.
Update 2:
If it is of any help: When I do not use gradle but instead open a Kotlinproject in IntelliJ and add the libary jars manually then everything works fine. Can anyone explain this?
Thanks in advance!

Why Kotlin blindly change internal classes into public in JVM?

As you know the private classes in Kotlin change to package-private under the hood and internals changed to the public.
unfortunately, this can lead to the known problem here.
if the compiler sees the usage of Kotlin internal classes when it wants to change it to the byte code, it can choose package-private for internal kotlin classes that didn't use outside of the package and choose public for others, so we can handle above problem on our own.
Or they can define another annotation such as #JvmPackagePrivate before internal classes to tell the compiler we want a package-private class in java.
Or they can do both.
The question is, why they don't solve this obvious problem with such an obvious solution?
Are they have another approach to solve this?
I just got acquainted with the Kotlin, so I think that I cant create lib for java with kotlin because when I create internal concrete classes, all client can see them outside of the library and its serious problem with kotlin. why they can't see this obvious problem??????
I want to mention that none of the answers in here solve this problem because of #JvmSynthetic and #JvmName just target the fun in kotlin, not classes and at the end they both visible even if they change the name of classes.
at last kotlin claims that it is completely interoperable with java but I think it's not right. better to say that it is 99 percent interoperable with java :)

Calling java code that doesn't accept null from kotlin

IDEA Community 2017.1.2, JRE 1.8, Kotlin 1.1.2-2
I have Java methods, located in libGdx that don't have any annotations regarding their nullability, e.g.:
public void render (final RenderableProvider renderableProvider) {
renderableProvider.getRenderables(renderables, renderablesPool);
as we can see, argument can't be null. However, since nothing tells that it's not-null argument, Kotlin will happily pass null in RenderableProvider?. How do I tell Kotlin to check during compile-time that I should be passing RenderableProvider and not RenderableProvider??
I've read about external annotations, however there is no "Specify Custom Kotlin Signature" and if I annotate renderableProvider as #NotNull nothing changes - kotlin still allows null.
I even tried to replace org.jetbrains.annotations.NotNull with javax.annotation.Nonnull in XML manually, but it makes no difference - code compiles and crashes with NPE.
External annotations are no longer supported. You'll either have to fork libgdx and annotate the methods there or live with this issue, unfortunately.
You could wrap it in an extension function and then only use that for rendering:
fun RenderClass.renderSafe(renderProvider: RenderableProvider) =
this.render(renderProvider)
Now you can't pass null.

Does kotlin support making a class implementing an interface outside of its definition file?

I see kotlin.List and kotlin.MutableList is implemented by java.util.ArrayList. But where did kotlin put this trick? Compiler or somewhere in stdlib?
If kotlin supports making a class implementing an interface outside of its definition file like the ArrayList case, it will be fascinated.
No, it is not supported.
You are right, that is only a compiler trick. There are lots of magic applied to the collections to make them right. Fortunately it is not available to the devs. Special paragraph in the docs: https://kotlinlang.org/docs/reference/java-interop.html#mapped-types