From 31d393c26ce168ec82c1015ce88070e90bbc9d81 Mon Sep 17 00:00:00 2001 From: Danil Pavlov Date: Thu, 4 Dec 2025 14:17:27 +0100 Subject: [PATCH 1/2] update: warning suppression on Any --- docs/topics/jvm/java-interop.md | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/docs/topics/jvm/java-interop.md b/docs/topics/jvm/java-interop.md index 10091d5754f..aa22f349f1c 100644 --- a/docs/topics/jvm/java-interop.md +++ b/docs/topics/jvm/java-interop.md @@ -687,10 +687,18 @@ so to make other members of `java.lang.Object` available, Kotlin uses [extension ### wait()/notify() Methods `wait()` and `notify()` are not available on references of type `Any`. Their usage is generally discouraged in -favor of `java.util.concurrent`. If you really need to call these methods, you can cast to `java.lang.Object`: +favor of `java.util.concurrent`. + +If you have to call these methods, cast to `java.lang.Object` and suppress the warning: ```kotlin -(foo as java.lang.Object).wait() +fun main() { + val any = Any() + synchronized(any) { + @Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN") + (any as java.lang.Object).wait() + } +} ``` ### getClass() From 630bb7c653100b3c046462cd524ace8378de78c3 Mon Sep 17 00:00:00 2001 From: Danil Pavlov Date: Tue, 9 Dec 2025 13:45:38 +0100 Subject: [PATCH 2/2] fix: real-life example --- docs/topics/jvm/java-interop.md | 38 ++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/docs/topics/jvm/java-interop.md b/docs/topics/jvm/java-interop.md index aa22f349f1c..19d12427ce6 100644 --- a/docs/topics/jvm/java-interop.md +++ b/docs/topics/jvm/java-interop.md @@ -692,11 +692,39 @@ favor of `java.util.concurrent`. If you have to call these methods, cast to `java.lang.Object` and suppress the warning: ```kotlin -fun main() { - val any = Any() - synchronized(any) { - @Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN") - (any as java.lang.Object).wait() +import java.util.LinkedList + +class SimpleBlockingQueue(private val capacity: Int) { + private val queue = LinkedList() + + // java.lang.Object is used specifically to access wait() and notify() + // In Kotlin, the standard 'Any' type does not expose these methods. + @Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN") + private val lock = Object() + + fun put(item: T) { + synchronized(lock) { + while (queue.size >= capacity) { + lock.wait() + } + queue.add(item) + println("Produced: $item") + + lock.notifyAll() + } + } + + fun take(): T { + synchronized(lock) { + while (queue.isEmpty()) { + lock.wait() + } + val item = queue.removeFirst() + println("Consumed: $item") + + lock.notifyAll() + return item + } } } ```