Questions tagged [kotlin-java-interop]

24 questions
7
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,475
  • 15
  • 31
5
votes
1 answer

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
  • 10,514
  • 5
  • 46
  • 55
4
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
  • 3,314
  • 27
  • 45
4
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,329
  • 2
  • 20
  • 37
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
  • 30,835
  • 10
  • 95
  • 145
3
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
  • 52,705
  • 8
  • 94
  • 269
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,103
  • 17
  • 34
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
  • 951
  • 7
  • 18
3
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
  • 356
  • 1
  • 12
2
votes
0 answers

How to reference a public field of java.awt.Dimension from Kotlin

Is it possible in Kotlin to directly access a public backing-field declared in a Java class, if that class happens to have the corresponding getter for the field? For example, JDK's java.awt.Dimension has the fields width and height, which are…
ysakhno
  • 594
  • 1
  • 4
  • 15
2
votes
0 answers

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…
Shift Delete
  • 381
  • 1
  • 10
2
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
  • 319
  • 1
  • 9
1
vote
1 answer

How can an exhaustive when throw NoWhenBranchMatchedException?

I observe a LiveData with an exhaustive when statement, how can it throw a NoWhenBranchMatchedException at runtime? Wasn't exhaustiveness checked for at compile time? enum class MyEnum { Foo, Bar } liveData.observe(viewLifecycleOwner) {…
1
vote
1 answer

Passing a Map from Java to Kotlin does not compile when updating the map

I have a Java function that has a Map) It seems that the map is immutable and I can't do:…
Jim
  • 2,341
  • 1
  • 11
  • 23
1
vote
0 answers

How to create a Kotlin list from Java?

How to access methods such as listOf, mapOf and setOf from Java. I'm in a project that doesn't use Kotlin but we want to use the effectively read-only Kotlin collections. Why? Because even when using immutable lists in Java, they have mutator…
1
2