From c3d69db755e8922fbf9840dd5ea7f89e710fd9d1 Mon Sep 17 00:00:00 2001 From: ForteScarlet Date: Wed, 20 Nov 2024 21:28:53 +0800 Subject: [PATCH 1/4] Migrate OriginatingElementsHolder from JVM to common --- kotlinpoet/api/kotlinpoet.api | 3 ++ .../kotlinpoet/OriginatingElementsHolder.kt | 14 ++++--- .../jvm/alias/JvmDefaultWithCompatibility.kt | 23 +++++++++++ .../kotlinpoet/jvm/alias/JvmElement.kt | 36 ++++++++++++++++ .../jvm/alias/JvmTypeAliasKotlinPoetApi.kt | 29 +++++++++++++ .../alias/JvmDefaultWithCompatibility.jvm.kt | 21 ++++++++++ .../kotlinpoet/jvm/alias/JvmElement.jvm.kt | 41 +++++++++++++++++++ .../kotlinpoet/jvm/alias/JvmElement.nonJvm.kt | 28 +++++++++++++ 8 files changed, 189 insertions(+), 6 deletions(-) rename kotlinpoet/src/{jvmMain => commonMain}/kotlin/com/squareup/kotlinpoet/OriginatingElementsHolder.kt (76%) create mode 100644 kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmDefaultWithCompatibility.kt create mode 100644 kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmElement.kt create mode 100644 kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmTypeAliasKotlinPoetApi.kt create mode 100644 kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmDefaultWithCompatibility.jvm.kt create mode 100644 kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmElement.jvm.kt create mode 100644 kotlinpoet/src/nonJvmMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmElement.nonJvm.kt diff --git a/kotlinpoet/api/kotlinpoet.api b/kotlinpoet/api/kotlinpoet.api index eef2b6132b..8cd66d7043 100644 --- a/kotlinpoet/api/kotlinpoet.api +++ b/kotlinpoet/api/kotlinpoet.api @@ -1253,6 +1253,9 @@ public final class com/squareup/kotlinpoet/jvm/JvmAnnotations { public static final fun volatile (Lcom/squareup/kotlinpoet/PropertySpec$Builder;)Lcom/squareup/kotlinpoet/PropertySpec$Builder; } +public abstract interface annotation class com/squareup/kotlinpoet/jvm/alias/JvmTypeAliasKotlinPoetApi : java/lang/annotation/Annotation { +} + public final class com/squareup/kotlinpoet/tags/TypeAliasTag { public fun (Lcom/squareup/kotlinpoet/TypeName;)V public final fun getAbbreviatedType ()Lcom/squareup/kotlinpoet/TypeName; diff --git a/kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/OriginatingElementsHolder.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/OriginatingElementsHolder.kt similarity index 76% rename from kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/OriginatingElementsHolder.kt rename to kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/OriginatingElementsHolder.kt index 4e877742f3..e1816c1cc5 100644 --- a/kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/OriginatingElementsHolder.kt +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/OriginatingElementsHolder.kt @@ -15,24 +15,26 @@ */ package com.squareup.kotlinpoet -import javax.lang.model.element.Element +import com.squareup.kotlinpoet.jvm.alias.JvmDefaultWithCompatibility +import com.squareup.kotlinpoet.jvm.alias.JvmElement +import kotlin.jvm.JvmInline /** A type that can have originating [elements][Element]. */ public interface OriginatingElementsHolder { /** The originating elements of this type. */ - public val originatingElements: List + public val originatingElements: List /** The builder analogue to [OriginatingElementsHolder] types. */ @JvmDefaultWithCompatibility public interface Builder> { /** Mutable map of the current originating elements this builder contains. */ - public val originatingElements: MutableList + public val originatingElements: MutableList /** Adds an [originatingElement] to this type's list of originating elements. */ @Suppress("UNCHECKED_CAST") - public fun addOriginatingElement(originatingElement: Element): T = apply { + public fun addOriginatingElement(originatingElement: JvmElement): T = apply { originatingElements += originatingElement } as T } @@ -41,10 +43,10 @@ public interface OriginatingElementsHolder { internal fun OriginatingElementsHolder.Builder<*>.buildOriginatingElements() = OriginatingElements(originatingElements.toImmutableList()) -internal fun List.buildOriginatingElements() = +internal fun List.buildOriginatingElements() = OriginatingElements(toImmutableList()) @JvmInline internal value class OriginatingElements( - override val originatingElements: List, + override val originatingElements: List, ) : OriginatingElementsHolder diff --git a/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmDefaultWithCompatibility.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmDefaultWithCompatibility.kt new file mode 100644 index 0000000000..ecfea3abf8 --- /dev/null +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmDefaultWithCompatibility.kt @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2024 Square, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.squareup.kotlinpoet.jvm.alias + +/** + * A typealias annotation for `kotlin.jvm.JvmDefaultWithCompatibility`. + */ +@OptIn(ExperimentalMultiplatform::class) +@OptionalExpectation +public expect annotation class JvmDefaultWithCompatibility() diff --git a/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmElement.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmElement.kt new file mode 100644 index 0000000000..5f9e5febee --- /dev/null +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmElement.kt @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2024 Square, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.squareup.kotlinpoet.jvm.alias + +/** + * An expected typealias for `javax.lang.model.element.Element`. + */ +public expect interface JvmElement + +/** + * An expected typealias for `javax.lang.model.element.TypeElement`. + */ +public expect interface JvmTypeElement : JvmElement + +/** + * An expected typealias for `javax.lang.model.element.ExecutableElement`. + */ +public expect interface JvmExecutableElement : JvmElement + +/** + * An expected typealias for `javax.lang.model.element.VariableElement`. + */ +public expect interface JvmVariableElement : JvmElement diff --git a/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmTypeAliasKotlinPoetApi.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmTypeAliasKotlinPoetApi.kt new file mode 100644 index 0000000000..81fa088b04 --- /dev/null +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmTypeAliasKotlinPoetApi.kt @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2024 Square, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.squareup.kotlinpoet.jvm.alias + +/** + * An expected type for JVM to `typealias` only. + */ +@MustBeDocumented +@Retention(AnnotationRetention.BINARY) +@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY, AnnotationTarget.TYPEALIAS) +@RequiresOptIn( + level = RequiresOptIn.Level.ERROR, + message = "An expected type for JVM to `typealias` only." + + " Don't implement or use it in non-JVM platforms.", +) +public annotation class JvmTypeAliasKotlinPoetApi diff --git a/kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmDefaultWithCompatibility.jvm.kt b/kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmDefaultWithCompatibility.jvm.kt new file mode 100644 index 0000000000..ed62336645 --- /dev/null +++ b/kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmDefaultWithCompatibility.jvm.kt @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2024 Square, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.squareup.kotlinpoet.jvm.alias + +/** + * A typealias annotation for [kotlin.jvm.JvmDefaultWithCompatibility]. + */ +public actual typealias JvmDefaultWithCompatibility = kotlin.jvm.JvmDefaultWithCompatibility diff --git a/kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmElement.jvm.kt b/kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmElement.jvm.kt new file mode 100644 index 0000000000..ce35be5b49 --- /dev/null +++ b/kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmElement.jvm.kt @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2024 Square, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.squareup.kotlinpoet.jvm.alias + +import javax.lang.model.element.Element +import javax.lang.model.element.ExecutableElement +import javax.lang.model.element.TypeElement +import javax.lang.model.element.VariableElement + +/** + * Type alias for [Element]. + */ +public actual typealias JvmElement = Element + +/** + * Type alias for [TypeElement]. + */ +public actual typealias JvmTypeElement = TypeElement + +/** + * Type alias for [ExecutableElement]. + */ +public actual typealias JvmExecutableElement = ExecutableElement + +/** + * Type alias for [VariableElement]. + */ +public actual typealias JvmVariableElement = VariableElement diff --git a/kotlinpoet/src/nonJvmMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmElement.nonJvm.kt b/kotlinpoet/src/nonJvmMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmElement.nonJvm.kt new file mode 100644 index 0000000000..4ef6575ff7 --- /dev/null +++ b/kotlinpoet/src/nonJvmMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmElement.nonJvm.kt @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2024 Square, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.squareup.kotlinpoet.jvm.alias + +@JvmTypeAliasKotlinPoetApi +public actual interface JvmElement + +@JvmTypeAliasKotlinPoetApi +public actual interface JvmTypeElement : JvmElement + +@JvmTypeAliasKotlinPoetApi +public actual interface JvmExecutableElement : JvmElement + +@JvmTypeAliasKotlinPoetApi +public actual interface JvmVariableElement : JvmElement From 1cf0fa525135bf3b6fd171d80ccd2f21aab0e662 Mon Sep 17 00:00:00 2001 From: ForteScarlet Date: Mon, 16 Dec 2024 20:15:48 +0800 Subject: [PATCH 2/4] Migrate Taggable from JVM to common --- kotlinpoet/api/kotlinpoet.api | 4 + .../com/squareup/kotlinpoet/Taggable.kt | 86 +++++++++++++++++++ .../squareup/kotlinpoet/jvm/alias/JvmClass.kt | 28 ++++++ .../{Taggable.kt => Taggable.jvm.kt} | 66 -------------- .../kotlinpoet/jvm/alias/JvmClass.jvm.kt | 30 +++++++ .../kotlinpoet/jvm/alias/JvmClass.nonJvm.kt | 26 ++++++ 6 files changed, 174 insertions(+), 66 deletions(-) create mode 100644 kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/Taggable.kt create mode 100644 kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmClass.kt rename kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/{Taggable.kt => Taggable.jvm.kt} (60%) create mode 100644 kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmClass.jvm.kt create mode 100644 kotlinpoet/src/nonJvmMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmClass.nonJvm.kt diff --git a/kotlinpoet/api/kotlinpoet.api b/kotlinpoet/api/kotlinpoet.api index 8cd66d7043..bfe77cac9e 100644 --- a/kotlinpoet/api/kotlinpoet.api +++ b/kotlinpoet/api/kotlinpoet.api @@ -1253,6 +1253,10 @@ public final class com/squareup/kotlinpoet/jvm/JvmAnnotations { public static final fun volatile (Lcom/squareup/kotlinpoet/PropertySpec$Builder;)Lcom/squareup/kotlinpoet/PropertySpec$Builder; } +public final class com/squareup/kotlinpoet/jvm/alias/JvmClass_jvmKt { + public static final fun getKotlin (Ljava/lang/Class;)Lkotlin/reflect/KClass; +} + public abstract interface annotation class com/squareup/kotlinpoet/jvm/alias/JvmTypeAliasKotlinPoetApi : java/lang/annotation/Annotation { } diff --git a/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/Taggable.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/Taggable.kt new file mode 100644 index 0000000000..3cde256423 --- /dev/null +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/Taggable.kt @@ -0,0 +1,86 @@ +/* + * Copyright (C) 2019 Square, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.squareup.kotlinpoet + +import com.squareup.kotlinpoet.jvm.alias.JvmClass +import com.squareup.kotlinpoet.jvm.alias.JvmDefaultWithCompatibility +import com.squareup.kotlinpoet.jvm.alias.kotlin +import kotlin.jvm.JvmInline +import kotlin.reflect.KClass + +/** A type that can be tagged with extra metadata of the user's choice. */ +@JvmDefaultWithCompatibility +public interface Taggable { + + /** Returns all tags. */ + public val tags: Map, Any> get() = emptyMap() + + /** Returns the tag attached with [type] as a key, or null if no tag is attached with that key. */ + public fun tag(type: JvmClass): T? = tag(type.kotlin) + + /** Returns the tag attached with [type] as a key, or null if no tag is attached with that key. */ + public fun tag(type: KClass): T? { + @Suppress("UNCHECKED_CAST") + return tags[type] as T? + } + + /** The builder analogue to [Taggable] types. */ + @JvmDefaultWithCompatibility + public interface Builder> { + + /** Mutable map of the current tags this builder contains. */ + public val tags: MutableMap, Any> + + /** + * Attaches [tag] to the request using [type] as a key. Tags can be read from a + * request using [Taggable.tag]. Use `null` to remove any existing tag assigned for + * [type]. + * + * Use this API to attach originating elements, debugging, or other application data to a spec + * so that you may read it in other APIs or callbacks. + */ + public fun tag(type: JvmClass<*>, tag: Any?): T = tag(type.kotlin, tag) + + /** + * Attaches [tag] to the request using [type] as a key. Tags can be read from a + * request using [Taggable.tag]. Use `null` to remove any existing tag assigned for + * [type]. + * + * Use this API to attach originating elements, debugging, or other application data to a spec + * so that you may read it in other APIs or callbacks. + */ + @Suppress("UNCHECKED_CAST") + public fun tag(type: KClass<*>, tag: Any?): T = apply { + if (tag == null) { + this.tags.remove(type) + } else { + this.tags[type] = tag + } + } as T + } +} + +/** Returns the tag attached with [T] as a key, or null if no tag is attached with that key. */ +public inline fun Taggable.tag(): T? = tag(T::class) + +internal fun Taggable.Builder<*>.buildTagMap(): TagMap = TagMap(tags) + +@JvmInline +internal value class TagMap private constructor(override val tags: Map, Any>) : Taggable { + companion object { + operator fun invoke(tags: Map, Any>): TagMap = TagMap(tags.toImmutableMap()) + } +} diff --git a/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmClass.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmClass.kt new file mode 100644 index 0000000000..518cffe5fe --- /dev/null +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmClass.kt @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2024 Square, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.squareup.kotlinpoet.jvm.alias + +import kotlin.reflect.KClass + +/** + * An expected typealias for `java.lang.Class`. + */ +public expect class JvmClass + +/** + * Convert [JvmClass] to [KClass]. + */ +public expect val JvmClass.kotlin: KClass diff --git a/kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/Taggable.kt b/kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/Taggable.jvm.kt similarity index 60% rename from kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/Taggable.kt rename to kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/Taggable.jvm.kt index f3313aeadc..960f8000e6 100644 --- a/kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/Taggable.kt +++ b/kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/Taggable.jvm.kt @@ -15,63 +15,6 @@ */ package com.squareup.kotlinpoet -import kotlin.reflect.KClass - -/** A type that can be tagged with extra metadata of the user's choice. */ -@JvmDefaultWithCompatibility -public interface Taggable { - - /** Returns all tags. */ - public val tags: Map, Any> get() = emptyMap() - - /** Returns the tag attached with [type] as a key, or null if no tag is attached with that key. */ - public fun tag(type: Class): T? = tag(type.kotlin) - - /** Returns the tag attached with [type] as a key, or null if no tag is attached with that key. */ - public fun tag(type: KClass): T? { - @Suppress("UNCHECKED_CAST") - return tags[type] as T? - } - - /** The builder analogue to [Taggable] types. */ - @JvmDefaultWithCompatibility - public interface Builder> { - - /** Mutable map of the current tags this builder contains. */ - public val tags: MutableMap, Any> - - /** - * Attaches [tag] to the request using [type] as a key. Tags can be read from a - * request using [Taggable.tag]. Use `null` to remove any existing tag assigned for - * [type]. - * - * Use this API to attach originating elements, debugging, or other application data to a spec - * so that you may read it in other APIs or callbacks. - */ - public fun tag(type: Class<*>, tag: Any?): T = tag(type.kotlin, tag) - - /** - * Attaches [tag] to the request using [type] as a key. Tags can be read from a - * request using [Taggable.tag]. Use `null` to remove any existing tag assigned for - * [type]. - * - * Use this API to attach originating elements, debugging, or other application data to a spec - * so that you may read it in other APIs or callbacks. - */ - @Suppress("UNCHECKED_CAST") - public fun tag(type: KClass<*>, tag: Any?): T = apply { - if (tag == null) { - this.tags.remove(type) - } else { - this.tags[type] = tag - } - } as T - } -} - -/** Returns the tag attached with [T] as a key, or null if no tag is attached with that key. */ -public inline fun Taggable.tag(): T? = tag(T::class) - /** * Attaches [tag] to the request using [T] as a key. Tags can be read from a * request using [Taggable.tag]. Use `null` to remove any existing tag assigned for @@ -149,12 +92,3 @@ public inline fun TypeAliasSpec.Builder.tag(tag: T?): TypeAlia */ public inline fun TypeSpec.Builder.tag(tag: T?): TypeSpec.Builder = tag(T::class, tag) - -internal fun Taggable.Builder<*>.buildTagMap(): TagMap = TagMap(tags) - -@JvmInline -internal value class TagMap private constructor(override val tags: Map, Any>) : Taggable { - companion object { - operator fun invoke(tags: Map, Any>): TagMap = TagMap(tags.toImmutableMap()) - } -} diff --git a/kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmClass.jvm.kt b/kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmClass.jvm.kt new file mode 100644 index 0000000000..1c86b7e6a1 --- /dev/null +++ b/kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmClass.jvm.kt @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2024 Square, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.squareup.kotlinpoet.jvm.alias + +import kotlin.jvm.kotlin as jvmKotlin +import kotlin.reflect.KClass + +/** + * An expected typealias for [Class]. + */ +public actual typealias JvmClass = Class + +/** + * Convert [JvmClass] to [KClass]. + */ +public actual val JvmClass.kotlin: KClass + get() = jvmKotlin diff --git a/kotlinpoet/src/nonJvmMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmClass.nonJvm.kt b/kotlinpoet/src/nonJvmMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmClass.nonJvm.kt new file mode 100644 index 0000000000..0aae338445 --- /dev/null +++ b/kotlinpoet/src/nonJvmMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmClass.nonJvm.kt @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2024 Square, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.squareup.kotlinpoet.jvm.alias + +import kotlin.reflect.KClass + +@JvmTypeAliasKotlinPoetApi +public actual class JvmClass<@Suppress("unused") + T : Any,> private constructor() + +@JvmTypeAliasKotlinPoetApi +public actual val JvmClass.kotlin: KClass + get() = throw UnsupportedOperationException() From 2e79139f48af6457b696d013aa1747ac8aa567f2 Mon Sep 17 00:00:00 2001 From: ForteScarlet Date: Mon, 16 Dec 2024 21:15:37 +0800 Subject: [PATCH 3/4] Optimise comments and typography. --- .../com/squareup/kotlinpoet/jvm/alias/JvmClass.kt | 5 +---- .../squareup/kotlinpoet/jvm/alias/JvmClass.jvm.kt | 8 +------- .../jvm/alias/JvmDefaultWithCompatibility.jvm.kt | 3 --- .../squareup/kotlinpoet/jvm/alias/JvmElement.jvm.kt | 12 ------------ .../squareup/kotlinpoet/jvm/alias/JvmClass.nonJvm.kt | 5 ++--- 5 files changed, 4 insertions(+), 29 deletions(-) diff --git a/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmClass.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmClass.kt index 518cffe5fe..8257e0c328 100644 --- a/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmClass.kt +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmClass.kt @@ -22,7 +22,4 @@ import kotlin.reflect.KClass */ public expect class JvmClass -/** - * Convert [JvmClass] to [KClass]. - */ -public expect val JvmClass.kotlin: KClass +internal expect val JvmClass.kotlin: KClass diff --git a/kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmClass.jvm.kt b/kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmClass.jvm.kt index 1c86b7e6a1..bb00d6ab0e 100644 --- a/kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmClass.jvm.kt +++ b/kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmClass.jvm.kt @@ -18,13 +18,7 @@ package com.squareup.kotlinpoet.jvm.alias import kotlin.jvm.kotlin as jvmKotlin import kotlin.reflect.KClass -/** - * An expected typealias for [Class]. - */ public actual typealias JvmClass = Class -/** - * Convert [JvmClass] to [KClass]. - */ -public actual val JvmClass.kotlin: KClass +internal actual val JvmClass.kotlin: KClass get() = jvmKotlin diff --git a/kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmDefaultWithCompatibility.jvm.kt b/kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmDefaultWithCompatibility.jvm.kt index ed62336645..b15b05b9f3 100644 --- a/kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmDefaultWithCompatibility.jvm.kt +++ b/kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmDefaultWithCompatibility.jvm.kt @@ -15,7 +15,4 @@ */ package com.squareup.kotlinpoet.jvm.alias -/** - * A typealias annotation for [kotlin.jvm.JvmDefaultWithCompatibility]. - */ public actual typealias JvmDefaultWithCompatibility = kotlin.jvm.JvmDefaultWithCompatibility diff --git a/kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmElement.jvm.kt b/kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmElement.jvm.kt index ce35be5b49..2dd85ba0df 100644 --- a/kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmElement.jvm.kt +++ b/kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmElement.jvm.kt @@ -20,22 +20,10 @@ import javax.lang.model.element.ExecutableElement import javax.lang.model.element.TypeElement import javax.lang.model.element.VariableElement -/** - * Type alias for [Element]. - */ public actual typealias JvmElement = Element -/** - * Type alias for [TypeElement]. - */ public actual typealias JvmTypeElement = TypeElement -/** - * Type alias for [ExecutableElement]. - */ public actual typealias JvmExecutableElement = ExecutableElement -/** - * Type alias for [VariableElement]. - */ public actual typealias JvmVariableElement = VariableElement diff --git a/kotlinpoet/src/nonJvmMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmClass.nonJvm.kt b/kotlinpoet/src/nonJvmMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmClass.nonJvm.kt index 0aae338445..7d1e53811d 100644 --- a/kotlinpoet/src/nonJvmMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmClass.nonJvm.kt +++ b/kotlinpoet/src/nonJvmMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmClass.nonJvm.kt @@ -18,9 +18,8 @@ package com.squareup.kotlinpoet.jvm.alias import kotlin.reflect.KClass @JvmTypeAliasKotlinPoetApi -public actual class JvmClass<@Suppress("unused") - T : Any,> private constructor() +public actual class JvmClass private constructor() @JvmTypeAliasKotlinPoetApi -public actual val JvmClass.kotlin: KClass +internal actual val JvmClass.kotlin: KClass get() = throw UnsupportedOperationException() From 47baf7b3a6c2ad948fbf0c8159a89d5f6ff1ce3a Mon Sep 17 00:00:00 2001 From: ForteScarlet Date: Mon, 16 Dec 2024 21:17:47 +0800 Subject: [PATCH 4/4] Rename JvmTypeAliasKotlinPoetApi to JvmOnlyKotlinPoetApi --- kotlinpoet/api/kotlinpoet.api | 6 +----- ...mTypeAliasKotlinPoetApi.kt => JvmOnlyKotlinPoetApi.kt} | 2 +- .../com/squareup/kotlinpoet/jvm/alias/JvmClass.nonJvm.kt | 4 ++-- .../squareup/kotlinpoet/jvm/alias/JvmElement.nonJvm.kt | 8 ++++---- 4 files changed, 8 insertions(+), 12 deletions(-) rename kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/jvm/alias/{JvmTypeAliasKotlinPoetApi.kt => JvmOnlyKotlinPoetApi.kt} (95%) diff --git a/kotlinpoet/api/kotlinpoet.api b/kotlinpoet/api/kotlinpoet.api index bfe77cac9e..3a07f05740 100644 --- a/kotlinpoet/api/kotlinpoet.api +++ b/kotlinpoet/api/kotlinpoet.api @@ -1253,11 +1253,7 @@ public final class com/squareup/kotlinpoet/jvm/JvmAnnotations { public static final fun volatile (Lcom/squareup/kotlinpoet/PropertySpec$Builder;)Lcom/squareup/kotlinpoet/PropertySpec$Builder; } -public final class com/squareup/kotlinpoet/jvm/alias/JvmClass_jvmKt { - public static final fun getKotlin (Ljava/lang/Class;)Lkotlin/reflect/KClass; -} - -public abstract interface annotation class com/squareup/kotlinpoet/jvm/alias/JvmTypeAliasKotlinPoetApi : java/lang/annotation/Annotation { +public abstract interface annotation class com/squareup/kotlinpoet/jvm/alias/JvmOnlyKotlinPoetApi : java/lang/annotation/Annotation { } public final class com/squareup/kotlinpoet/tags/TypeAliasTag { diff --git a/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmTypeAliasKotlinPoetApi.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmOnlyKotlinPoetApi.kt similarity index 95% rename from kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmTypeAliasKotlinPoetApi.kt rename to kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmOnlyKotlinPoetApi.kt index 81fa088b04..d0171b3dc1 100644 --- a/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmTypeAliasKotlinPoetApi.kt +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmOnlyKotlinPoetApi.kt @@ -26,4 +26,4 @@ package com.squareup.kotlinpoet.jvm.alias message = "An expected type for JVM to `typealias` only." + " Don't implement or use it in non-JVM platforms.", ) -public annotation class JvmTypeAliasKotlinPoetApi +public annotation class JvmOnlyKotlinPoetApi diff --git a/kotlinpoet/src/nonJvmMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmClass.nonJvm.kt b/kotlinpoet/src/nonJvmMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmClass.nonJvm.kt index 7d1e53811d..242aa4db85 100644 --- a/kotlinpoet/src/nonJvmMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmClass.nonJvm.kt +++ b/kotlinpoet/src/nonJvmMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmClass.nonJvm.kt @@ -17,9 +17,9 @@ package com.squareup.kotlinpoet.jvm.alias import kotlin.reflect.KClass -@JvmTypeAliasKotlinPoetApi +@JvmOnlyKotlinPoetApi public actual class JvmClass private constructor() -@JvmTypeAliasKotlinPoetApi +@JvmOnlyKotlinPoetApi internal actual val JvmClass.kotlin: KClass get() = throw UnsupportedOperationException() diff --git a/kotlinpoet/src/nonJvmMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmElement.nonJvm.kt b/kotlinpoet/src/nonJvmMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmElement.nonJvm.kt index 4ef6575ff7..8638d54950 100644 --- a/kotlinpoet/src/nonJvmMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmElement.nonJvm.kt +++ b/kotlinpoet/src/nonJvmMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmElement.nonJvm.kt @@ -15,14 +15,14 @@ */ package com.squareup.kotlinpoet.jvm.alias -@JvmTypeAliasKotlinPoetApi +@JvmOnlyKotlinPoetApi public actual interface JvmElement -@JvmTypeAliasKotlinPoetApi +@JvmOnlyKotlinPoetApi public actual interface JvmTypeElement : JvmElement -@JvmTypeAliasKotlinPoetApi +@JvmOnlyKotlinPoetApi public actual interface JvmExecutableElement : JvmElement -@JvmTypeAliasKotlinPoetApi +@JvmOnlyKotlinPoetApi public actual interface JvmVariableElement : JvmElement