Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the kotlinpoet module multi-platform and add source set configuration for the JS and wasmJs platforms #1959

Open
wants to merge 38 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
ae89e37
Apply the JS and WasmJs source sets to the main module and move the c…
ForteScarlet Aug 10, 2024
a46a5a7
Migrating the Util.kt to common
ForteScarlet Aug 10, 2024
4d24217
Migrating JvmDefaultWithCompatibility to common
ForteScarlet Aug 10, 2024
52068af
Creating type aliases for JVM platform compatibility
ForteScarlet Aug 10, 2024
02569d1
Migrate types that have no or little use for platform content, such a…
ForteScarlet Aug 10, 2024
92495f2
Migrate NameAllocator to common and add a new CodePoint implementatio…
ForteScarlet Aug 10, 2024
511e8e8
Add the yarn.lock and apply spotless in stages
ForteScarlet Aug 10, 2024
6a9198d
Migrate OriginatingElementsHolder.kt to common
ForteScarlet Aug 10, 2024
43197b0
Migrate Taggable.kt to common
ForteScarlet Aug 10, 2024
5259b9c
Migrate TypeName, WildcardTypeName, ParameterizedTypeName, ClassName,…
ForteScarlet Aug 10, 2024
11522f1
Migrate Documentable to common
ForteScarlet Aug 10, 2024
8c8ddeb
Migrate ContextReceivable and Dynamic to common
ForteScarlet Aug 10, 2024
e31e57c
Migrate MemberName to common
ForteScarlet Aug 10, 2024
462aa07
Migrate TypeAliasTag to common
ForteScarlet Aug 10, 2024
80fb80d
Migrate LineWrapper to common
ForteScarlet Aug 10, 2024
28248a9
Migrate ParameterSpec to common
ForteScarlet Aug 10, 2024
c3719ec
Migrate TypeAliasSpec ad TypeVariableName to common.
ForteScarlet Aug 10, 2024
3dfd12f
Migrate LambdaTypeName to common.
ForteScarlet Aug 10, 2024
d5be4e7
Migrate FunSpec and PropertySpec to common.
ForteScarlet Aug 10, 2024
16a0cf0
Migrate MemberSpecHolder to common.
ForteScarlet Aug 10, 2024
4725895
Migrate TypeSpec and TypeSpecHolder to common.
ForteScarlet Aug 10, 2024
e158bba
Migrate FileSpec to common.
ForteScarlet Aug 10, 2024
3151780
Migrate CodeWriter to common.
ForteScarlet Aug 10, 2024
63440e7
Migrate JvmAnnotations.kt to common.
ForteScarlet Aug 10, 2024
12b6818
Restore functions in Taggable that waited for type migration before t…
ForteScarlet Aug 10, 2024
e44b6fb
Restoring a Temporary TODO in AnnotationSpec
ForteScarlet Aug 10, 2024
0c04c8d
Complementary TypeName.kt missing platform implementation TypeName.no…
ForteScarlet Aug 10, 2024
4e062d9
Apply apiDump
ForteScarlet Aug 10, 2024
97755a2
Fix some of the noisy changes.
ForteScarlet Aug 10, 2024
7afe913
Create an expected typealias Closeable for java.io.Closeable
ForteScarlet Aug 10, 2024
dcfab3f
Adjust and optimize the structure of CodePoint and the logic of some …
ForteScarlet Aug 10, 2024
ca71f03
Edit changelog.md
ForteScarlet Aug 10, 2024
9133127
Try to edit `String.isIdentifier` in `Util.kt` to make it usable on t…
ForteScarlet Aug 26, 2024
36c8e23
Fix some blank lines, typos and indentation issues
ForteScarlet Aug 29, 2024
9343ea2
Remove the browser's tests configuration
ForteScarlet Aug 29, 2024
71a69af
Move `JvmTypeElement.asClassName` to JVM platform
ForteScarlet Aug 29, 2024
8eed20b
Clean up the standalone `Closeable` type and implement `kotlin.AutoCl…
ForteScarlet Aug 29, 2024
584fe49
Remove the browser's tests configuration for js platform
ForteScarlet Sep 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Change Log
* Fix: Preserve nullability in `KSType.toClassName()`. (#1956)
* New: Add `KSTypeAlias.toClassName()`. (#1956)
* New: Add `KSType.toClassNameOrNull()`. (#1956)
* New: Make the kotlinpoet module multi-platform and add source set configuration for the JS and wasmJs platforms. (#1959)

## Version 1.18.1

Expand Down
2,010 changes: 2,010 additions & 0 deletions kotlin-js-store/yarn.lock

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions kotlinpoet/api/kotlinpoet.api
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,9 @@ public final class com/squareup/kotlinpoet/Import : java/lang/Comparable {
public fun toString ()Ljava/lang/String;
}

public abstract interface annotation class com/squareup/kotlinpoet/JvmTypeAliasKotlinPoetApi : java/lang/annotation/Annotation {
}

public final class com/squareup/kotlinpoet/KModifier : java/lang/Enum {
public static final field ABSTRACT Lcom/squareup/kotlinpoet/KModifier;
public static final field ACTUAL Lcom/squareup/kotlinpoet/KModifier;
Expand Down Expand Up @@ -1253,6 +1256,15 @@ 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/JvmClasses {
public static final fun getKotlin (Ljava/lang/Class;)Lkotlin/reflect/KClass;
public static final fun typeName (Ljava/lang/reflect/Type;)Ljava/lang/String;
}

public final class com/squareup/kotlinpoet/jvm/alias/JvmPath_jvmKt {
public static final fun toJvmFile (Ljava/nio/file/Path;)Ljava/io/File;
}

public final class com/squareup/kotlinpoet/tags/TypeAliasTag {
public fun <init> (Lcom/squareup/kotlinpoet/TypeName;)V
public final fun getAbbreviatedType ()Lcom/squareup/kotlinpoet/TypeName;
Expand Down
54 changes: 47 additions & 7 deletions kotlinpoet/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl

plugins {
kotlin("multiplatform")
Expand All @@ -23,11 +24,12 @@ spotless {
kotlin {
targetExclude(
// Non-Square licensed files
"src/commonMain/kotlin/com/squareup/kotlinpoet/ClassName.kt",
"src/commonTest/kotlin/com/squareup/kotlinpoet/AbstractTypesTest.kt",
"src/commonTest/kotlin/com/squareup/kotlinpoet/ClassNameTest.kt",
"src/commonTest/kotlin/com/squareup/kotlinpoet/TypesEclipseTest.kt",
"src/commonTest/kotlin/com/squareup/kotlinpoet/TypesTest.kt",
"src/*Main/kotlin/com/squareup/kotlinpoet/ClassName.kt",
"src/*Main/kotlin/com/squareup/kotlinpoet/ClassName.*.kt",
Egorand marked this conversation as resolved.
Show resolved Hide resolved
"src/*Test/kotlin/com/squareup/kotlinpoet/AbstractTypesTest.kt",
"src/*Test/kotlin/com/squareup/kotlinpoet/ClassNameTest.kt",
"src/*Test/kotlin/com/squareup/kotlinpoet/TypesEclipseTest.kt",
"src/*Test/kotlin/com/squareup/kotlinpoet/TypesTest.kt",
)
}
}
Expand All @@ -37,19 +39,46 @@ kotlin {
withJava()
}

js {
nodejs {
testTask {
useMocha()
}
}
binaries.library()
}

@OptIn(ExperimentalWasmDsl::class)
wasmJs {
nodejs {
testTask {
useMocha()
}
}
binaries.library()
}

@OptIn(ExperimentalKotlinGradlePluginApi::class)
compilerOptions {
allWarningsAsErrors.set(true)
optIn.add("com.squareup.kotlinpoet.DelicateKotlinPoetApi")
freeCompilerArgs.add("-Xexpect-actual-classes")
Egorand marked this conversation as resolved.
Show resolved Hide resolved
}

sourceSets {
val commonMain by getting {
commonMain {
dependencies {
implementation(libs.kotlin.reflect)
}
}
val commonTest by getting {

commonTest {
dependencies {
implementation(kotlin("test"))
}
}

jvmTest {
dependencies {
implementation(libs.kotlin.junit)
implementation(libs.truth)
Expand All @@ -62,6 +91,17 @@ kotlin {
implementation(libs.kotlin.compilerEmbeddable)
}
}

val nonJvmMain by creating {
dependsOn(commonMain.get())
}

jsMain {
dependsOn(nonJvmMain)
}
wasmJsMain {
dependsOn(nonJvmMain)
}
}
}

Expand Down
1 change: 1 addition & 0 deletions kotlinpoet/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ POM_ARTIFACT_ID=kotlinpoet
POM_NAME=KotlinPoet
POM_DESCRIPTION=Use beautiful Kotlin code to generate beautiful Kotlin code.
POM_PACKAGING=jar
kotlin.mpp.applyDefaultHierarchyTemplate=false
Egorand marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.squareup.kotlinpoet

import com.squareup.kotlinpoet.jvm.alias.JvmClass
import kotlin.reflect.KClass

/** A spec or type which can be annotated. */
Expand Down Expand Up @@ -42,7 +43,7 @@ public interface Annotatable {
message = "Java reflection APIs don't give complete information on Kotlin types. Consider " +
"using the kotlinpoet-metadata APIs instead.",
)
public fun addAnnotation(annotation: Class<*>): T = addAnnotation(annotation.asClassName())
public fun addAnnotation(annotation: JvmClass<*>): T = addAnnotation(annotation.asClassName())

public fun addAnnotation(annotation: KClass<*>): T = addAnnotation(annotation.asClassName())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@file:JvmName("AnnotationSpecs")
@file:JvmMultifileClass

package com.squareup.kotlinpoet

import java.lang.reflect.Array
import java.util.Objects
import javax.lang.model.element.AnnotationMirror
import javax.lang.model.element.AnnotationValue
import javax.lang.model.element.TypeElement
import javax.lang.model.element.VariableElement
import javax.lang.model.type.TypeMirror
import javax.lang.model.util.SimpleAnnotationValueVisitor8
import com.squareup.kotlinpoet.jvm.alias.JvmAnnotationMirror
import com.squareup.kotlinpoet.jvm.alias.JvmClass
import kotlin.LazyThreadSafetyMode.NONE
import kotlin.jvm.JvmMultifileClass
import kotlin.jvm.JvmName
import kotlin.jvm.JvmOverloads
import kotlin.jvm.JvmStatic
import kotlin.reflect.KClass

/** A generated annotation on a declaration. */
Expand Down Expand Up @@ -98,7 +99,7 @@ public class AnnotationSpec private constructor(
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other == null) return false
if (javaClass != other.javaClass) return false
if (this::class != other::class) return false
return toString() == other.toString()
}

Expand Down Expand Up @@ -146,8 +147,8 @@ public class AnnotationSpec private constructor(
* `%L` for other types.
*/
internal fun memberForValue(value: Any) = when (value) {
is Class<*> -> CodeBlock.of("%T::class", value)
is Enum<*> -> CodeBlock.of("%T.%L", value.javaClass, value.name)
is JvmClass<*> -> CodeBlock.of("%T::class", value)
is Enum<*> -> resolveEnumValueCodeBlock(value)
is String -> CodeBlock.of("%S", value)
is Float -> CodeBlock.of("%Lf", value)
is Char -> CodeBlock.of("'%L'", characterLiteralWithoutSingleQuotes(value))
Expand All @@ -156,37 +157,6 @@ public class AnnotationSpec private constructor(
}
}

/**
* Annotation value visitor adding members to the given builder instance.
*/
@OptIn(DelicateKotlinPoetApi::class)
private class Visitor(
val builder: CodeBlock.Builder,
) : SimpleAnnotationValueVisitor8<CodeBlock.Builder, String>(builder) {

override fun defaultAction(o: Any, name: String) =
builder.add(Builder.memberForValue(o))

override fun visitAnnotation(a: AnnotationMirror, name: String) =
builder.add("%L", get(a))

override fun visitEnumConstant(c: VariableElement, name: String) =
builder.add("%T.%L", c.asType().asTypeName(), c.simpleName)

override fun visitType(t: TypeMirror, name: String) =
builder.add("%T::class", t.asTypeName())

override fun visitArray(values: List<AnnotationValue>, name: String): CodeBlock.Builder {
builder.add("arrayOf(⇥⇥")
values.forEachIndexed { index, value ->
if (index > 0) builder.add(", ")
value.accept(this, name)
}
builder.add("⇤⇤)")
return builder
}
}

public companion object {
@DelicateKotlinPoetApi(
message = "Java reflection APIs don't give complete information on Kotlin types. Consider " +
Expand All @@ -198,63 +168,16 @@ public class AnnotationSpec private constructor(
annotation: Annotation,
includeDefaultValues: Boolean = false,
): AnnotationSpec {
try {
@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN")
val javaAnnotation = annotation as java.lang.annotation.Annotation
val builder = builder(javaAnnotation.annotationType())
.tag(annotation)
val methods = annotation.annotationType().declaredMethods.sortedBy { it.name }
for (method in methods) {
val value = method.invoke(annotation)
if (!includeDefaultValues) {
if (Objects.deepEquals(value, method.defaultValue)) {
continue
}
}
val member = CodeBlock.builder()
member.add("%L = ", method.name)
if (value.javaClass.isArray) {
member.add("arrayOf(⇥⇥")
for (i in 0..<Array.getLength(value)) {
if (i > 0) member.add(", ")
member.add(Builder.memberForValue(Array.get(value, i)))
}
member.add("⇤⇤)")
builder.addMember(member.build())
continue
}
if (value is Annotation) {
member.add("%L", get(value))
builder.addMember(member.build())
continue
}
member.add("%L", Builder.memberForValue(value))
builder.addMember(member.build())
}
return builder.build()
} catch (e: Exception) {
throw RuntimeException("Reflecting $annotation failed!", e)
}
return doGet(annotation, includeDefaultValues)
}

@DelicateKotlinPoetApi(
message = "Mirror APIs don't give complete information on Kotlin types. Consider using" +
" the kotlinpoet-metadata APIs instead.",
)
@JvmStatic
public fun get(annotation: AnnotationMirror): AnnotationSpec {
val element = annotation.annotationType.asElement() as TypeElement
val builder = builder(element.asClassName()).tag(annotation)
for (executableElement in annotation.elementValues.keys) {
val member = CodeBlock.builder()
val visitor = Visitor(member)
val name = executableElement.simpleName.toString()
member.add("%L = ", name)
val value = annotation.elementValues[executableElement]!!
value.accept(visitor, name)
builder.addMember(member.build())
}
return builder.build()
public fun get(annotation: JvmAnnotationMirror): AnnotationSpec {
return doGet(annotation)
}

@JvmStatic public fun builder(type: ClassName): Builder = Builder(type)
Expand All @@ -266,10 +189,19 @@ public class AnnotationSpec private constructor(
"using the kotlinpoet-metadata APIs instead.",
)
@JvmStatic
public fun builder(type: Class<out Annotation>): Builder =
public fun builder(type: JvmClass<out Annotation>): Builder =
builder(type.asClassName())

@JvmStatic public fun builder(type: KClass<out Annotation>): Builder =
builder(type.asClassName())
}
}

internal expect fun resolveEnumValueCodeBlock(value: Enum<*>): CodeBlock

internal expect fun doGet(
annotation: Annotation,
includeDefaultValues: Boolean,
): AnnotationSpec

internal expect fun doGet(annotation: JvmAnnotationMirror): AnnotationSpec
Loading