Motivation:
In our Android project we have many verifications like str != null && !str.isEmpty()
, so I decided to refactor them to a helper method.
For a moment I use following class as a helper:
public class StringUtil {
public static boolean isNullOrEmpty(@Nullable String str) {
return str == null || str.isEmpty();
}
}
Problem:
We already have a string's helper class, written in Kotlin (say, String.kt). So, this is not clear to have two helpers (one in Java and one in Kotlin).
What I tried:
Naive approach to copy-past isNullOrEmpty()
inside String.kt do not successed, because $reciever
is null, so it crashed.
Secondly, I tried to used Kotlin native isNullOrEmpty()
from kotlin.text (https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/is-null-or-empty.html):
public inline fun CharSequence?.isNullOrEmpty(): Boolean
but I cannot figure out how to call it from Java. This page (https://kotlinlang.org/docs/reference/java-to-kotlin-interop.html) do not provide any suggestions.
The problem is not about Accessing Kotlin extension functions from Java. My extension is perfectly visibly, but it crash because of null-receivier. As I mentioned below, question is more about accessing native library code, not my own extension.
Any help please ?