Questions tagged [kotlin-java-interop]

40 questions
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
  • 834
  • 1
  • 7
  • 17
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
  • 1,015
  • 6
  • 13
1
vote
1 answer

Can I add an existing java class to a sealed Kotlin class?

I am using 2 classes to handle error statuses, the Spring's own org.springframework.http.HttpStatus and my custom ErrorStatus: enum class ErrorStatus(val code: Int, val reasonPhrase: String) { ELEMENT_NOT_FOUND(1404, "Element not found"), …
hc0re
  • 1,806
  • 2
  • 26
  • 61
1
vote
1 answer

How to shadow member function with a custom Kotlin extension function? (like Kotlin stdlib does)

I am reading Kotlin in Action 2nd edition. Chapter 3 says: If the class has a member function with the same signature as an extension function, the member function always takes precedence At the same the book demonstrates the CharSequence.split…
Kirill
  • 6,762
  • 4
  • 51
  • 81
1
vote
1 answer

What's the use for Kotlin's @JvmSynthetic on a file target?

The @JvmSynthetic annotation is allowed to be used on a file, but I can't figure out what the purpose of this would be. I was hoping I could hide a file containing a bunch of Kotlin-only extension methods from Java users, but that doesn't seem to be…
1
vote
1 answer

Kotlin noarg plugin not initializing default values

Hello I have the following code(jdk) build.gradle.kts plugins { kotlin("jvm") version "1.7.10" id("org.jetbrains.kotlin.plugin.noarg") version "1.7.10" application } noArg { annotation("com.MyAnnotation") } Classes and…
1
vote
0 answers

Value classes in generics - interoperability with Java/Groovy

I’m struggling with value classes in generics and interoperability with Java or Groovy. Value class are inlined: Inline classes | Kotlin 1 except for generics. Given following value class and interface (Kotlin): @JvmInline value class ValueClass(val…
1
vote
0 answers

Kotlin's "internal" keyword Java interop

I'm trying to figure out what happens with internal classes when seen from Java's perspective. Found this in the docs: Members of internal classes go through name mangling, to make it harder to accidentally use them from Java and to allow…
Grzegorz Piwowarek
  • 13,172
  • 8
  • 62
  • 93
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
  • 3,845
  • 3
  • 22
  • 47
1
vote
1 answer

Deprecated kotlin functions not warned on pure java project

I have Test class which has written purely with kotlin in the library project. class Test{ @Deprecated(message = "Use other function") fun testFunction(id: String): Test { this.testId = id return this } } I've deprecated…
gokhan
  • 627
  • 1
  • 6
  • 16
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…
caeus
  • 3,084
  • 1
  • 22
  • 36
1
vote
1 answer

Can I know when my library is called from Java or Kotlin?

I'm developing a Kotlin library for Android. I want to provide a great developer experience for both Kotlin and Java developers, but because I don't have infinite resources I need to prioritize between those. The library is interacting with a…
PLNech
  • 3,087
  • 1
  • 23
  • 52
0
votes
1 answer

Compiling for both Kotlin/Java and Kotlin/JS?

I'd like to write some of my code in Kotlin such that it can be used by both a JVM-based backend and a JS-based frontend. To do the former (with the new IR complier), the classes have to be annotated with @JsExport. However, if I then try to use…
Brian White
  • 8,332
  • 2
  • 43
  • 67
0
votes
0 answers

IntelliJ does not compile source files, unless I put "Build Project" in Before Launch steps

I have a maven project for Cucumber in Java, where I am mixing Java+Kotlin. The maven build works fine and compiles files as I change them. But when I run a test (*.feature file) from IntelliJ, the files are not compiled. As per the default 'Run…
vikrant21st
  • 93
  • 1
  • 7