diff --git a/kotlinpoet/api/kotlinpoet.api b/kotlinpoet/api/kotlinpoet.api index eef2b6132b..3a07f05740 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/JvmOnlyKotlinPoetApi : 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/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..8257e0c328 --- /dev/null +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmClass.kt @@ -0,0 +1,25 @@ +/* + * 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 + +internal expect val JvmClass.kotlin: KClass 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/JvmOnlyKotlinPoetApi.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmOnlyKotlinPoetApi.kt new file mode 100644 index 0000000000..d0171b3dc1 --- /dev/null +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmOnlyKotlinPoetApi.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 JvmOnlyKotlinPoetApi 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..bb00d6ab0e --- /dev/null +++ b/kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmClass.jvm.kt @@ -0,0 +1,24 @@ +/* + * 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 + +public actual typealias JvmClass = Class + +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 new file mode 100644 index 0000000000..b15b05b9f3 --- /dev/null +++ b/kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmDefaultWithCompatibility.jvm.kt @@ -0,0 +1,18 @@ +/* + * 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 + +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..2dd85ba0df --- /dev/null +++ b/kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmElement.jvm.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 + +import javax.lang.model.element.Element +import javax.lang.model.element.ExecutableElement +import javax.lang.model.element.TypeElement +import javax.lang.model.element.VariableElement + +public actual typealias JvmElement = Element + +public actual typealias JvmTypeElement = TypeElement + +public actual typealias JvmExecutableElement = ExecutableElement + +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 new file mode 100644 index 0000000000..242aa4db85 --- /dev/null +++ b/kotlinpoet/src/nonJvmMain/kotlin/com/squareup/kotlinpoet/jvm/alias/JvmClass.nonJvm.kt @@ -0,0 +1,25 @@ +/* + * 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 + +@JvmOnlyKotlinPoetApi +public actual class JvmClass private constructor() + +@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 new file mode 100644 index 0000000000..8638d54950 --- /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 + +@JvmOnlyKotlinPoetApi +public actual interface JvmElement + +@JvmOnlyKotlinPoetApi +public actual interface JvmTypeElement : JvmElement + +@JvmOnlyKotlinPoetApi +public actual interface JvmExecutableElement : JvmElement + +@JvmOnlyKotlinPoetApi +public actual interface JvmVariableElement : JvmElement