From 5d989661f3e9eccd88cf0745b14ef5403dd47c64 Mon Sep 17 00:00:00 2001 From: mikr Date: Tue, 23 Apr 2019 09:09:01 +0200 Subject: [PATCH] BAEL-2580 JVM Platform Annotations in Kotlin --- .../com/baeldung/jvmannotations/Document.kt | 31 +++++++++ .../baeldung/jvmannotations/HtmlDocument.java | 9 +++ .../com/baeldung/jvmannotations/Message.kt | 66 +++++++++++++++++++ .../jvmannotations/MessageConverter.kt | 6 ++ 4 files changed, 112 insertions(+) create mode 100644 core-kotlin/src/main/kotlin/com/baeldung/jvmannotations/Document.kt create mode 100644 core-kotlin/src/main/kotlin/com/baeldung/jvmannotations/HtmlDocument.java create mode 100644 core-kotlin/src/main/kotlin/com/baeldung/jvmannotations/Message.kt create mode 100644 core-kotlin/src/main/kotlin/com/baeldung/jvmannotations/MessageConverter.kt diff --git a/core-kotlin/src/main/kotlin/com/baeldung/jvmannotations/Document.kt b/core-kotlin/src/main/kotlin/com/baeldung/jvmannotations/Document.kt new file mode 100644 index 0000000000..3f9922b88b --- /dev/null +++ b/core-kotlin/src/main/kotlin/com/baeldung/jvmannotations/Document.kt @@ -0,0 +1,31 @@ +package com.baeldung.jvmannotations + +import java.util.* + +interface Document { + + @JvmDefault + fun getType() = "document" +} + +class TextDocument : Document { + override fun getType() = "text" + + fun transformList(list : List) : List { + return list.filter { n -> n.toInt() > 1 } + } + + fun transformListInverseWildcards(list : List<@JvmSuppressWildcards Number>) : List<@JvmWildcard Number> { + return list.filter { n -> n.toInt() > 1 } + } + + var list : List<@JvmWildcard Any> = ArrayList() +} + +class XmlDocument(d : Document) : Document by d + +fun main() { + val myDocument = TextDocument() + val myTextDocument = XmlDocument(myDocument) + println("${myDocument.getType()} ${myTextDocument.getType()}") +} diff --git a/core-kotlin/src/main/kotlin/com/baeldung/jvmannotations/HtmlDocument.java b/core-kotlin/src/main/kotlin/com/baeldung/jvmannotations/HtmlDocument.java new file mode 100644 index 0000000000..feb71772cb --- /dev/null +++ b/core-kotlin/src/main/kotlin/com/baeldung/jvmannotations/HtmlDocument.java @@ -0,0 +1,9 @@ +package com.baeldung.jvmannotations; + +public class HtmlDocument implements Document { + + @Override + public String getType() { + return "HTML"; + } +} \ No newline at end of file diff --git a/core-kotlin/src/main/kotlin/com/baeldung/jvmannotations/Message.kt b/core-kotlin/src/main/kotlin/com/baeldung/jvmannotations/Message.kt new file mode 100644 index 0000000000..80180bd924 --- /dev/null +++ b/core-kotlin/src/main/kotlin/com/baeldung/jvmannotations/Message.kt @@ -0,0 +1,66 @@ +@file:JvmName("MessageHelper") +@file:JvmMultifileClass //used +package com.baeldung.jvmannotations + +import java.util.* + +@JvmName("getMyUsername") +fun getMyName() : String { + return "myUserId" +} + +object MessageBroker { + @JvmStatic + var totalMessagesSent = 0 + + const val maxMessageLength = 0 + + @JvmStatic + fun clearAllMessages() { + } + + @JvmStatic + @JvmOverloads + @Throws(Exception::class) + fun findMessages(sender : String, type : String = "text", maxResults : Int = 10) : List { + if(sender.isEmpty()) { + throw Exception() + } + return ArrayList() + } +} + +class Message { + + // this would cause a compilation error since sender is immutable + // @set:JvmName("setSender") + val sender = "myself" + + // this works as name is overridden + @JvmName("getSenderName") + fun getSender() : String = "from:$sender" + + @get:JvmName("getReceiverName") + @set:JvmName("setReceiverName") + var receiver : String = "" + + @get:JvmName("getContent") + @set:JvmName("setContent") + var text = "" + + // generates a warning + @get:JvmName("getId") + private val id = 0 + + @get:JvmName("hasAttachment") + var hasAttachment = true + + var isEncrypted = true + + fun setReceivers(receiverNames : List) { + } + + @JvmName("setReceiverIds") + fun setReceivers(receiverNames : List) { + } +} \ No newline at end of file diff --git a/core-kotlin/src/main/kotlin/com/baeldung/jvmannotations/MessageConverter.kt b/core-kotlin/src/main/kotlin/com/baeldung/jvmannotations/MessageConverter.kt new file mode 100644 index 0000000000..3b19b12e10 --- /dev/null +++ b/core-kotlin/src/main/kotlin/com/baeldung/jvmannotations/MessageConverter.kt @@ -0,0 +1,6 @@ +@file:JvmMultifileClass +@file:JvmName("MessageHelper") //applies to all top level functions / variables / constants +package com.baeldung.jvmannotations + +fun convert(message: Message) { +}