Questions tagged [kotlin-java-interop]

40 questions
8
votes
1 answer

How do you make Kotlin static variables and functions for Java?

Since Google made Kotlin a first class language for Android, there has been an increase in questions relating to how to perform certain things in Kotlin, "Java-esque" style. The most common ones are how to make static variables in Kotlin. So how do…
Pablo Baxter
  • 2,144
  • 1
  • 17
  • 36
8
votes
2 answers

Kotlin methods accepting inline classes as parameters - how access from Java?

Let's say I have: inline class Email(value: String) and fun something(email: Email) now if I want to call something() from Java I can't. Because any method that is accepting an inline class as a parameter is "mangled" (more about this here:…
WindRider
  • 11,958
  • 6
  • 50
  • 57
6
votes
1 answer

Benefit of specifying -jvm-target / jvmTarget version other than 1.8

As of Kotlin 1.6.0, for Kotlin/JVM projects one may specify the -jvm-target version option up to Java 17, see general and Gradle plugin documentation. What are the benefits of doing so? I couldn't find much on the benefits of specifying something…
Endzeit
  • 4,810
  • 5
  • 29
  • 52
6
votes
1 answer

Hiding Kotlin object's INSTANCE field from Java (and Kotlin)

Short question Can I modify the visibility of a Kotlin object's INSTANCE (for Java interop) to internal or lower? Long question I'm writing a library and I want to have an API file / class, written in Kotlin, that exposes a function to be called…
Erik
  • 4,305
  • 3
  • 36
  • 54
6
votes
1 answer

Access kotlin public field from Java directly without getter

Below is a example of a pattern from Android (Just an example, not interested in android specifics): /*Im a kotlin file*/ class ListItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { val text: = itemView.my_view } Then the…
Adam
  • 2,845
  • 2
  • 32
  • 46
4
votes
4 answers

use static Java methods as static (or singleton) properties in Kotlin

My SDK exposes a Java interface that has only static methods, e.g. public interface MyDevice { public static void setLocation(Location location) {…} public static Location getLocation() {…} } In Java app that uses the SDK, my customers can use…
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
4
votes
0 answers

Kotlin compiler issue with overriding of Java final function in Kotlin

I’m dealing with following issue with Kotlin/Java Compiler. Imagine following scenario: let First be a Java class with a final function and Second be a Kotlin class extending First with a function of the same name like the final function in First…
jBegera
  • 405
  • 2
  • 12
3
votes
2 answers

Ensure that a Stream is closed after converting to Sequence (or: why do "use" and "finally" not work in a sequence)

I have some Java code which makes heavy use of the Stream API. It is critical that these streams are closed when we are finished consuming them, but we are struggling to come up with a robust solution. I had an idea: this is already a mixed Java +…
Jake
  • 321
  • 3
  • 12
3
votes
2 answers

Kotlin Problem Delegating to Map with DefaultValue - Language Bug?

In the following code, where MyMap trivially implements Map by delegation to impl: foo@host:/tmp$ cat Foo.kt class MyMap (val impl : Map ) : Map by impl { fun myGetValue (k: K) = impl.getValue(k) } fun main() { val my_map =…
user2297550
  • 3,142
  • 3
  • 28
  • 39
3
votes
1 answer

Can I use property syntax for a Java setter without a corresponding getter?

I have a Java class with a setter, but no getter for a property. class Person { private String name; public void setName(String name) { this.name = name; } } I'd like to use .name= to assign to it from Kotlin fun example() { val p = Person() …
Ryan Haining
  • 35,360
  • 15
  • 114
  • 174
3
votes
1 answer

How to get reference to static class of Java super class in Kotlin

I've got sample third party Java code: public class ApiClass extends PackagePrivateClass { } abstract class PackagePrivateClass { public static class StaticClass { } } So only ApiClass and StaticClass are public. In Java I can get…
ZZ 5
  • 1,744
  • 26
  • 41
3
votes
1 answer

Override Java interface with only getters in Kotlin

I have this interface in Java: public interface ErrorInfo { String getCode(); String getMessage(); } And I want it to override in Kotlin code. I can't seem to do it. Here's what I tried: Here, I get an error that ' overrides…
sweet suman
  • 1,031
  • 8
  • 18
3
votes
1 answer

Why does generated getter method have dollar signs in it?

I'm in the process of converting my codebase from Java to Kotlin, and we're converting a few classes at a time. During this process today, I noticed a really weird interop issue that I'm hoping there's a way around. I had a package protected Java…
user1902853
  • 399
  • 1
  • 12
2
votes
1 answer

"Parameter specified as non-null is null" when using GestureDetector.SimpleOnGestureListener() onScroll() function

I am using the GestureDetector.SimpleOnGestureListener() to react to UserGestures and expand a BottomSheet. The problem is that a small percentage of the users experience the following crash. ( stacktrace stripped to make the question more…
2
votes
1 answer

Scoping Java static methods in Kotlin

There's have a Java library's class which has aVeryLongNameThatIDontWantToTypeEveryTime. This class has a few static methods with generic names: get(), abs() etc. Now I need to construct complicated calls with them in my kotlin code like this…
Atemu
  • 295
  • 3
  • 12
1
2 3