Skip to content

Commit

Permalink
Apply KMP plugin only to the kotlinpoet module
Browse files Browse the repository at this point in the history
  • Loading branch information
takahirom committed Aug 5, 2023
1 parent 84560cb commit 305930d
Show file tree
Hide file tree
Showing 125 changed files with 9,820 additions and 115 deletions.
39 changes: 24 additions & 15 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
alias(libs.plugins.kotlin.multiplatform) apply false
alias(libs.plugins.kotlin.jvm) apply false
alias(libs.plugins.ksp) apply false
// alias(libs.plugins.dokka) apply false
alias(libs.plugins.spotless) apply false
Expand All @@ -43,11 +44,6 @@ allprojects {
}

subprojects {
apply(plugin = "org.jetbrains.kotlin.multiplatform")
configure<KotlinMultiplatformExtension> {
jvm {}
}

tasks.withType<KotlinCompile> {
compilerOptions {
jvmTarget.set(JvmTarget.JVM_1_8)
Expand All @@ -62,8 +58,15 @@ subprojects {
if ("test" !in name && buildFile.exists()) {
// apply(plugin = "org.jetbrains.dokka")
apply(plugin = "com.vanniktech.maven.publish")
configure<KotlinProjectExtension> {
explicitApi()
pluginManager.withPlugin("org.jetbrains.kotlin.multiplatform") {
configure<KotlinMultiplatformExtension> {
explicitApi()
}
}
pluginManager.withPlugin("org.jetbrains.kotlin.jvm") {
configure<KotlinProjectExtension> {
explicitApi()
}
}
// Unable to run dokka with single target multiplatform project and
// kotlin 1.9.0
Expand All @@ -78,7 +81,6 @@ subprojects {
// }
// }
}

apply(plugin = "com.diffplug.spotless")
configure<SpotlessExtension> {
kotlin {
Expand Down Expand Up @@ -106,7 +108,7 @@ subprojects {
| * See the License for the specific language governing permissions and
| * limitations under the License.
| */
""".trimMargin()
""".trimMargin(),
)
}
}
Expand All @@ -120,16 +122,23 @@ subprojects {
for (majorVersion in versionsToTest) {
val jdkTest = tasks.register<Test>("testJdk$majorVersion") {
val javaToolchains = project.extensions.getByType(JavaToolchainService::class)
javaLauncher.set(javaToolchains.launcherFor {
languageVersion.set(JavaLanguageVersion.of(majorVersion))
vendor.set(JvmVendorSpec.AZUL)
})
javaLauncher.set(
javaToolchains.launcherFor {
languageVersion.set(JavaLanguageVersion.of(majorVersion))
vendor.set(JvmVendorSpec.AZUL)
},
)

description = "Runs the test suite on JDK $majorVersion"
group = LifecycleBasePlugin.VERIFICATION_GROUP

// Copy inputs from normal Test task.
val testTask = tasks.getByName<Test>("jvmTest")
val testTask =
if (pluginManager.hasPlugin("org.jetbrains.kotlin.multiplatform")) {
tasks.getByName<Test>("jvmTest")
} else {
tasks.getByName<Test>("test")
}
classpath = testTask.classpath
testClassesDirs = testTask.testClassesDirs

Expand All @@ -148,6 +157,6 @@ apiValidation {
nonPublicMarkers += "com.squareup.kotlinpoet.ExperimentalKotlinPoetApi"
ignoredProjects += listOf(
"interop", // Empty middle package
"test-processor" // Test only
"test-processor", // Test only
)
}
27 changes: 10 additions & 17 deletions interop/javapoet/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,19 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
plugins {
kotlin("jvm")
}

project.tasks.withType(org.gradle.jvm.tasks.Jar::class.java) {
tasks.jar {
manifest {
attributes("Automatic-Module-Name" to "com.squareup.kotlinpoet")
attributes("Automatic-Module-Name" to "com.squareup.kotlinpoet.javapoet")
}
}

kotlin {
sourceSets {
val jvmMain by getting {
dependencies {
api(projects.kotlinpoet)
api(libs.javapoet)
}
}
val jvmTest by getting {
dependencies {
implementation(libs.kotlin.junit)
implementation(libs.truth)
}
}
}
dependencies {
api(projects.kotlinpoet)
api(libs.javapoet)
testImplementation(libs.kotlin.junit)
testImplementation(libs.truth)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
* Copyright (C) 2021 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.javapoet

import com.squareup.javapoet.ArrayTypeName
import com.squareup.javapoet.TypeName
import com.squareup.kotlinpoet.ANY
import com.squareup.kotlinpoet.ARRAY
import com.squareup.kotlinpoet.BOOLEAN
import com.squareup.kotlinpoet.BOOLEAN_ARRAY
import com.squareup.kotlinpoet.BYTE
import com.squareup.kotlinpoet.BYTE_ARRAY
import com.squareup.kotlinpoet.CHAR
import com.squareup.kotlinpoet.CHAR_ARRAY
import com.squareup.kotlinpoet.CHAR_SEQUENCE
import com.squareup.kotlinpoet.DOUBLE
import com.squareup.kotlinpoet.DOUBLE_ARRAY
import com.squareup.kotlinpoet.Dynamic
import com.squareup.kotlinpoet.ENUM
import com.squareup.kotlinpoet.FLOAT
import com.squareup.kotlinpoet.FLOAT_ARRAY
import com.squareup.kotlinpoet.INT
import com.squareup.kotlinpoet.INT_ARRAY
import com.squareup.kotlinpoet.LIST
import com.squareup.kotlinpoet.LONG
import com.squareup.kotlinpoet.LONG_ARRAY
import com.squareup.kotlinpoet.LambdaTypeName
import com.squareup.kotlinpoet.MAP
import com.squareup.kotlinpoet.MUTABLE_LIST
import com.squareup.kotlinpoet.MUTABLE_MAP
import com.squareup.kotlinpoet.MUTABLE_SET
import com.squareup.kotlinpoet.SET
import com.squareup.kotlinpoet.SHORT
import com.squareup.kotlinpoet.SHORT_ARRAY
import com.squareup.kotlinpoet.STAR
import com.squareup.kotlinpoet.STRING
import com.squareup.kotlinpoet.U_BYTE
import com.squareup.kotlinpoet.U_BYTE_ARRAY
import com.squareup.kotlinpoet.U_INT
import com.squareup.kotlinpoet.U_INT_ARRAY
import com.squareup.kotlinpoet.U_LONG
import com.squareup.kotlinpoet.U_LONG_ARRAY
import com.squareup.kotlinpoet.U_SHORT
import com.squareup.kotlinpoet.U_SHORT_ARRAY

@KotlinPoetJavaPoetPreview
public fun KClassName.toJClassName(boxIfPrimitive: Boolean = false): JTypeName {
return when (copy(nullable = false)) {
BOOLEAN -> JTypeName.BOOLEAN.boxIfPrimitive(boxIfPrimitive || isNullable)
BYTE, U_BYTE -> JTypeName.BYTE.boxIfPrimitive(boxIfPrimitive || isNullable)
CHAR -> JTypeName.CHAR.boxIfPrimitive(boxIfPrimitive || isNullable)
SHORT, U_SHORT -> JTypeName.SHORT.boxIfPrimitive(boxIfPrimitive || isNullable)
INT, U_INT -> JTypeName.INT.boxIfPrimitive(boxIfPrimitive || isNullable)
LONG, U_LONG -> JTypeName.LONG.boxIfPrimitive(boxIfPrimitive || isNullable)
FLOAT -> JTypeName.FLOAT.boxIfPrimitive(boxIfPrimitive || isNullable)
DOUBLE -> JTypeName.DOUBLE.boxIfPrimitive(boxIfPrimitive || isNullable)
ANY -> JTypeName.OBJECT
CHAR_SEQUENCE -> PoetInterop.CN_JAVA_CHAR_SEQUENCE
STRING -> PoetInterop.CN_JAVA_STRING
LIST, MUTABLE_LIST -> PoetInterop.CN_JAVA_LIST
SET, MUTABLE_SET -> PoetInterop.CN_JAVA_SET
MAP, MUTABLE_MAP -> PoetInterop.CN_JAVA_MAP
BOOLEAN_ARRAY -> ArrayTypeName.of(JTypeName.BOOLEAN)
BYTE_ARRAY, U_BYTE_ARRAY -> ArrayTypeName.of(JTypeName.BYTE)
CHAR_ARRAY -> ArrayTypeName.of(JTypeName.CHAR)
SHORT_ARRAY, U_SHORT_ARRAY -> ArrayTypeName.of(JTypeName.SHORT)
INT_ARRAY, U_INT_ARRAY -> ArrayTypeName.of(JTypeName.INT)
LONG_ARRAY, U_LONG_ARRAY -> ArrayTypeName.of(JTypeName.LONG)
FLOAT_ARRAY -> ArrayTypeName.of(JTypeName.FLOAT)
DOUBLE_ARRAY -> ArrayTypeName.of(JTypeName.DOUBLE)
ENUM -> PoetInterop.CN_JAVA_ENUM
else -> {
if (simpleNames.size == 1) {
JClassName.get(packageName, simpleName)
} else {
JClassName.get(packageName, simpleNames.first(), *simpleNames.drop(1).toTypedArray())
}
}
}
}

@KotlinPoetJavaPoetPreview
public fun KParameterizedTypeName.toJParameterizedOrArrayTypeName(): JTypeName {
return when (rawType) {
ARRAY -> {
val componentType = typeArguments.firstOrNull()?.toJTypeName()
?: throw IllegalStateException("Array with no type! $this")
ArrayTypeName.of(componentType)
}
else -> {
JParameterizedTypeName.get(
rawType.toJClassName() as JClassName,
*typeArguments.map { it.toJTypeName(boxIfPrimitive = true) }.toTypedArray(),
)
}
}
}

@KotlinPoetJavaPoetPreview
public fun KParameterizedTypeName.toJParameterizedTypeName(): JParameterizedTypeName {
check(rawType != ARRAY) {
"Array type! JavaPoet arrays are a custom TypeName. Use this function only for things you know are not arrays"
}
return toJParameterizedOrArrayTypeName() as JParameterizedTypeName
}

@KotlinPoetJavaPoetPreview
public fun KTypeVariableName.toJTypeVariableName(): JTypeVariableName {
return JTypeVariableName.get(name, *bounds.map { it.toJTypeName(boxIfPrimitive = true) }.toTypedArray())
}

@KotlinPoetJavaPoetPreview
public fun KWildcardTypeName.toJWildcardTypeName(): JWildcardTypeName {
return if (this == STAR) {
JWildcardTypeName.subtypeOf(TypeName.OBJECT)
} else if (inTypes.size == 1) {
JWildcardTypeName.supertypeOf(inTypes[0].toJTypeName())
} else {
JWildcardTypeName.subtypeOf(outTypes[0].toJTypeName())
}
}

@KotlinPoetJavaPoetPreview
public fun KTypeName.toJTypeName(boxIfPrimitive: Boolean = false): JTypeName {
return when (this) {
is KClassName -> toJClassName(boxIfPrimitive)
Dynamic -> throw IllegalStateException("Not applicable in Java!")
// TODO should we return a ParameterizedTypeName of the KFunction?
is LambdaTypeName -> throw IllegalStateException("Not applicable in Java!")
is KParameterizedTypeName -> toJParameterizedOrArrayTypeName()
is KTypeVariableName -> toJTypeVariableName()
is KWildcardTypeName -> toJWildcardTypeName()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (C) 2021 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.javapoet

import kotlin.annotation.AnnotationTarget.CLASS
import kotlin.annotation.AnnotationTarget.FUNCTION
import kotlin.annotation.AnnotationTarget.PROPERTY
import kotlin.annotation.AnnotationTarget.TYPEALIAS

/**
* Indicates that a given API is part of the experimental KotlinPoet JavaPoet support and is
* subject to API changes.
*/
@RequiresOptIn
@Retention(AnnotationRetention.BINARY)
@Target(CLASS, FUNCTION, PROPERTY, TYPEALIAS)
public annotation class KotlinPoetJavaPoetPreview
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (C) 2021 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.javapoet

/** Various JavaPoet and KotlinPoet representations of some common types. */
@OptIn(KotlinPoetJavaPoetPreview::class)
internal object PoetInterop {
internal val CN_JAVA_CHAR_SEQUENCE = JClassName.get("java.lang", "CharSequence")
internal val CN_JAVA_STRING = JClassName.get("java.lang", "String")
internal val CN_JAVA_LIST = JClassName.get("java.util", "List")
internal val CN_JAVA_SET = JClassName.get("java.util", "Set")
internal val CN_JAVA_MAP = JClassName.get("java.util", "Map")
internal val CN_JAVA_ENUM = JClassName.get("java.lang", "Enum")
}
Loading

0 comments on commit 305930d

Please sign in to comment.