Skip to content

Commit

Permalink
Update kotlin monorepo (#1428)
Browse files Browse the repository at this point in the history
* Update kotlin monorepo

* KSP

* Fix opt ins in ElementsClassInspector

* Update to AbstractTypeVisitor8

We are jvm target 8 now

* More opt-ins

* Update some deprecated KM APIs

* JDK 19

* Update kotlinOptions -> compilerOptions

* Add missing jvmTarget

* Use modern javacompile release property

* Update another SimpleAnnotationValueVisitor8 location

* Update another SimpleTypeVisitor8 location

* Gate extra JDK tests on CI

* Spotless

* Update kotlin-compile-testing

* Remove requiresoptin

Co-authored-by: Jake Wharton <jw@squareup.com>

* Use extension

* Enable full test logging

* Add test logging in the right place

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Jake Wharton <jw@squareup.com>
Co-authored-by: Zac Sweers <zac.sweers@gmail.com>
Co-authored-by: Zac Sweers <pandanomic@gmail.com>
  • Loading branch information
4 people committed Feb 23, 2023
1 parent ffd97f5 commit 89d1dde
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 61 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Expand Up @@ -20,7 +20,7 @@ jobs:
uses: actions/setup-java@v3
with:
distribution: 'zulu'
java-version: 18
java-version: 19

- name: Test
run: ./gradlew build
Expand All @@ -37,7 +37,7 @@ jobs:
uses: actions/setup-java@v3
with:
distribution: 'zulu'
java-version: 18
java-version: 19

- name: Prep docs
run: ./gradlew dokkaHtml
Expand Down Expand Up @@ -66,7 +66,7 @@ jobs:
uses: actions/setup-java@v3
with:
distribution: 'zulu'
java-version: 18
java-version: 19

- name: Upload Artifacts
run: ./gradlew publish
Expand Down
53 changes: 31 additions & 22 deletions build.gradle.kts
Expand Up @@ -14,7 +14,9 @@
* limitations under the License.
*/
import com.diffplug.gradle.spotless.SpotlessExtension
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.jetbrains.dokka.gradle.DokkaTask
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

Expand All @@ -38,14 +40,13 @@ allprojects {

subprojects {
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs += listOf("-opt-in=kotlin.RequiresOptIn")
compilerOptions {
jvmTarget.set(JvmTarget.JVM_1_8)
}
}
// Ensure "org.gradle.jvm.version" is set to "8" in Gradle metadata.
tasks.withType<JavaCompile> {
sourceCompatibility = JavaVersion.VERSION_1_8.toString()
targetCompatibility = JavaVersion.VERSION_1_8.toString()
options.release.set(8)
}

apply(plugin = "org.jetbrains.kotlin.jvm")
Expand Down Expand Up @@ -98,26 +99,34 @@ subprojects {
}
}

// Copied from https://github.com/square/retrofit/blob/master/retrofit/build.gradle#L28.
// Create a test task for each supported JDK.
for (majorVersion in 8..18) {
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)
})
// Only enable the extra toolchain tests on CI. Otherwise local development is broken on Apple Silicon macs
// because there are no matching toolchains for several older JDK versions.
if ("CI" in System.getenv()) {
// Copied from https://github.com/square/retrofit/blob/master/retrofit/build.gradle#L28.
// Create a test task for each supported JDK.
for (majorVersion in 8..19) {
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)
})

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

// Copy inputs from normal Test task.
val testTask = tasks.getByName<Test>("test")
classpath = testTask.classpath
testClassesDirs = testTask.testClassesDirs
}
tasks.named("check").configure {
dependsOn(jdkTest)
// Copy inputs from normal Test task.
val testTask = tasks.getByName<Test>("test")
classpath = testTask.classpath
testClassesDirs = testTask.testClassesDirs

testLogging {
exceptionFormat = TestExceptionFormat.FULL
}
}
tasks.named("check").configure {
dependsOn(jdkTest)
}
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions gradle/libs.versions.toml
Expand Up @@ -13,9 +13,9 @@
# limitations under the License.

[versions]
kotlin = "1.7.22"
kct = "1.4.9"
ksp = "1.7.22-1.0.8"
kotlin = "1.8.0"
kct = "0.2.1"
ksp = "1.8.0-1.0.9"
ktlint = "0.48.2"

[plugins]
Expand All @@ -38,7 +38,7 @@ kotlin-compilerEmbeddable = { module = "org.jetbrains.kotlin:kotlin-compiler-emb
kotlin-annotationProcessingEmbeddable = { module = "org.jetbrains.kotlin:kotlin-annotation-processing-embeddable", version.ref = "kotlin" }
kotlin-reflect = { module = "org.jetbrains.kotlin:kotlin-reflect", version.ref = "kotlin" }
kotlin-junit = { module = "org.jetbrains.kotlin:kotlin-test-junit", version.ref = "kotlin" }
kotlin-metadata = { module = "org.jetbrains.kotlinx:kotlinx-metadata-jvm", version = "0.5.0" }
kotlin-metadata = { module = "org.jetbrains.kotlinx:kotlinx-metadata-jvm", version = "0.6.0" }

This comment has been minimized.

Copy link
@kmayoral

kmayoral Mar 7, 2023

would love to have a release out to support kotlin 1.8 metadata 🙇


ksp = { module = "com.google.devtools.ksp:symbol-processing", version.ref = "ksp" }
ksp-api = { module = "com.google.devtools.ksp:symbol-processing-api", version.ref = "ksp" }
Expand All @@ -47,5 +47,5 @@ truth = { module = "com.google.truth:truth", version = "1.1.3" }
compileTesting = { module = "com.google.testing.compile:compile-testing", version = "0.21.0" }
jimfs = { module = "com.google.jimfs:jimfs", version = "1.2" }
ecj = { module = "org.eclipse.jdt.core.compiler:ecj", version = "4.6.1" }
kotlinCompileTesting = { module = "com.github.tschuchortdev:kotlin-compile-testing", version.ref = "kct" }
kotlinCompileTesting-ksp = { module = "com.github.tschuchortdev:kotlin-compile-testing-ksp", version.ref = "kct" }
kotlinCompileTesting = { module = "dev.zacsweers.kctfork:core", version.ref = "kct" }
kotlinCompileTesting-ksp = { module = "dev.zacsweers.kctfork:ksp", version.ref = "kct" }
5 changes: 3 additions & 2 deletions interop/kotlinx-metadata/build.gradle.kts
Expand Up @@ -21,10 +21,11 @@ tasks.jar {
}

tasks.compileTestKotlin {
kotlinOptions {
freeCompilerArgs = listOf(
compilerOptions {
freeCompilerArgs.addAll(
"-Xjvm-default=all",
"-opt-in=com.squareup.kotlinpoet.metadata.KotlinPoetMetadataPreview",
"-opt-in=org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi",
)
}
}
Expand Down
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/
@file:JvmName("KotlinPoetMetadata")
@file:Suppress("unused")

package com.squareup.kotlinpoet.metadata

Expand All @@ -24,8 +23,8 @@ import kotlin.annotation.AnnotationTarget.FUNCTION
import kotlin.annotation.AnnotationTarget.PROPERTY
import kotlin.reflect.KClass
import kotlinx.metadata.KmClass
import kotlinx.metadata.jvm.KotlinClassHeader
import kotlinx.metadata.jvm.KotlinClassMetadata
import kotlinx.metadata.jvm.Metadata

/**
* Indicates that a given API is part of the experimental KotlinPoet metadata support. This exists
Expand Down Expand Up @@ -99,8 +98,8 @@ private inline fun readMetadata(lookup: ((Class<Metadata>) -> Metadata?)): Metad
}
}

private fun Metadata.asClassHeader(): KotlinClassHeader {
return KotlinClassHeader(
private fun Metadata.asClassHeader(): Metadata {
return Metadata(
kind = kind,
metadataVersion = metadataVersion,
data1 = data1,
Expand Down
Expand Up @@ -24,6 +24,7 @@ import com.squareup.kotlinpoet.AnnotationSpec
import com.squareup.kotlinpoet.AnnotationSpec.UseSiteTarget.FILE
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.CodeBlock
import com.squareup.kotlinpoet.DelicateKotlinPoetApi
import com.squareup.kotlinpoet.TypeName
import com.squareup.kotlinpoet.asClassName
import com.squareup.kotlinpoet.asTypeName
Expand Down Expand Up @@ -167,8 +168,8 @@ public class ElementsClassInspector private constructor(
}
}

@OptIn(DelicateKotlinPoetApi::class)
private fun VariableElement.annotationSpecs(): List<AnnotationSpec> {
@Suppress("DEPRECATION")
return filterOutNullabilityAnnotations(
annotationMirrors.map { AnnotationSpec.get(it) },
)
Expand All @@ -185,15 +186,15 @@ public class ElementsClassInspector private constructor(
}
}

@OptIn(DelicateKotlinPoetApi::class)
private fun ExecutableElement.annotationSpecs(): List<AnnotationSpec> {
@Suppress("DEPRECATION")
return filterOutNullabilityAnnotations(
annotationMirrors.map { AnnotationSpec.get(it) },
)
}

@OptIn(DelicateKotlinPoetApi::class)
private fun ExecutableElement.exceptionTypeNames(): List<TypeName> {
@Suppress("DEPRECATION")
return thrownTypes.map { it.asTypeName() }
}

Expand Down Expand Up @@ -303,6 +304,7 @@ public class ElementsClassInspector private constructor(
}
}

@OptIn(DelicateKotlinPoetApi::class)
override fun containerData(
declarationContainer: KmDeclarationContainer,
className: ClassName,
Expand Down Expand Up @@ -507,7 +509,6 @@ public class ElementsClassInspector private constructor(
className = className,
annotations = if (declarationContainer.flags.hasAnnotations) {
ClassInspectorUtil.createAnnotations {
@Suppress("DEPRECATION")
addAll(typeElement.annotationMirrors.map { AnnotationSpec.get(it) })
}
} else {
Expand Down
Expand Up @@ -25,6 +25,7 @@ import javax.lang.model.type.ArrayType
import javax.lang.model.type.DeclaredType
import javax.lang.model.type.ErrorType
import javax.lang.model.type.ExecutableType
import javax.lang.model.type.IntersectionType
import javax.lang.model.type.NoType
import javax.lang.model.type.NullType
import javax.lang.model.type.PrimitiveType
Expand All @@ -38,8 +39,9 @@ import javax.lang.model.type.TypeKind.LONG
import javax.lang.model.type.TypeKind.SHORT
import javax.lang.model.type.TypeMirror
import javax.lang.model.type.TypeVariable
import javax.lang.model.type.UnionType
import javax.lang.model.type.WildcardType
import javax.lang.model.util.AbstractTypeVisitor6
import javax.lang.model.util.AbstractTypeVisitor8
import javax.lang.model.util.Types
import kotlinx.metadata.jvm.JvmFieldSignature
import kotlinx.metadata.jvm.JvmMethodSignature
Expand Down Expand Up @@ -171,7 +173,7 @@ internal fun VariableElement.jvmFieldSignature(types: Types): String {
*
* For reference, see the [JVM specification, section 4.3](https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.3).
*/
internal object JvmDescriptorTypeVisitor : AbstractTypeVisitor6<String, Types>() {
internal object JvmDescriptorTypeVisitor : AbstractTypeVisitor8<String, Types>() {
override fun visitNoType(t: NoType, types: Types): String = t.descriptor
override fun visitDeclared(t: DeclaredType, types: Types): String = t.descriptor
override fun visitPrimitive(t: PrimitiveType, types: Types): String = t.descriptor
Expand All @@ -181,14 +183,13 @@ internal object JvmDescriptorTypeVisitor : AbstractTypeVisitor6<String, Types>()
override fun visitExecutable(t: ExecutableType, types: Types): String = t.descriptor(types)
override fun visitTypeVariable(t: TypeVariable, types: Types): String = t.descriptor(types)

override fun visitNull(t: NullType, types: Types): String = visitUnknown(
t,
types,
)
override fun visitError(t: ErrorType, types: Types): String = visitUnknown(
t,
types,
)
override fun visitNull(t: NullType, types: Types): String = visitUnknown(t, types)

override fun visitError(t: ErrorType, types: Types): String = visitUnknown(t, types)

override fun visitUnion(t: UnionType, types: Types): String = visitUnknown(t, types)

override fun visitIntersection(t: IntersectionType, types: Types): String = visitUnknown(t, types)

override fun visitUnknown(t: TypeMirror, types: Types): String = error("Unsupported type $t")
}
Expand Up @@ -19,6 +19,7 @@ import com.squareup.kotlinpoet.AnnotationSpec
import com.squareup.kotlinpoet.AnnotationSpec.UseSiteTarget.FILE
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.CodeBlock
import com.squareup.kotlinpoet.DelicateKotlinPoetApi
import com.squareup.kotlinpoet.TypeName
import com.squareup.kotlinpoet.asTypeName
import com.squareup.kotlinpoet.metadata.KotlinPoetMetadataPreview
Expand Down Expand Up @@ -171,12 +172,14 @@ public class ReflectiveClassInspector private constructor(
}
}

@OptIn(DelicateKotlinPoetApi::class)
private fun Field.annotationSpecs(): List<AnnotationSpec> {
return filterOutNullabilityAnnotations(
declaredAnnotations.orEmpty().map { AnnotationSpec.get(it, includeDefaultValues = true) },
)
}

@OptIn(DelicateKotlinPoetApi::class)
private fun Constructor<*>.annotationSpecs(): List<AnnotationSpec> {
return filterOutNullabilityAnnotations(
declaredAnnotations.orEmpty().map { AnnotationSpec.get(it, true) },
Expand Down Expand Up @@ -205,12 +208,14 @@ public class ReflectiveClassInspector private constructor(
return jvmMethodModifiers
}

@OptIn(DelicateKotlinPoetApi::class)
private fun Method.annotationSpecs(): List<AnnotationSpec> {
return filterOutNullabilityAnnotations(
declaredAnnotations.orEmpty().map { AnnotationSpec.get(it, includeDefaultValues = true) },
)
}

@OptIn(DelicateKotlinPoetApi::class)
private fun Parameter.annotationSpecs(): List<AnnotationSpec> {
return filterOutNullabilityAnnotations(
declaredAnnotations.map { AnnotationSpec.get(it, includeDefaultValues = true) },
Expand Down Expand Up @@ -285,6 +290,7 @@ public class ReflectiveClassInspector private constructor(
return lookupClass(className)?.lookupMethod(methodSignature) != null
}

@OptIn(DelicateKotlinPoetApi::class)
override fun containerData(
declarationContainer: KmDeclarationContainer,
className: ClassName,
Expand Down
Expand Up @@ -22,6 +22,7 @@ import com.squareup.kotlinpoet.AnnotationSpec
import com.squareup.kotlinpoet.AnnotationSpec.UseSiteTarget
import com.squareup.kotlinpoet.AnnotationSpec.UseSiteTarget.FILE
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.DelicateKotlinPoetApi
import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.FunSpec.Builder
Expand Down Expand Up @@ -140,13 +141,14 @@ public fun KClass<*>.toTypeSpec(
): TypeSpec = java.toTypeSpec(classInspector)

/** @return a [TypeSpec] ABI representation of this [KClass]. */
@OptIn(DelicateKotlinPoetApi::class)
@KotlinPoetMetadataPreview
public fun Class<*>.toTypeSpec(
classInspector: ClassInspector? = null,
): TypeSpec = toKmClass().toTypeSpec(classInspector, asClassName())

/** @return a [TypeSpec] ABI representation of this [TypeElement]. */
@Suppress("DEPRECATION")
@OptIn(DelicateKotlinPoetApi::class)
@KotlinPoetMetadataPreview
public fun TypeElement.toTypeSpec(
classInspector: ClassInspector? = null,
Expand Down Expand Up @@ -627,7 +629,7 @@ private fun KmValueParameter.toParameterSpec(
typeParamResolver: TypeParameterResolver,
annotations: Collection<AnnotationSpec>,
): ParameterSpec {
val paramType = varargElementType ?: type ?: throw IllegalStateException("No argument type!")
val paramType = varargElementType ?: type
return ParameterSpec.builder(name, paramType.toTypeName(typeParamResolver))
.apply {
addAnnotations(annotations)
Expand Down Expand Up @@ -825,7 +827,7 @@ private fun propertyAccessor(
.filterNot { it == FINAL && !isOverride }
.filterNot { it == OPEN && isOverride }
val propertyAccessorFlags = flags.propertyAccessorFlags
return if (visibility != PUBLIC || modalities.isNotEmpty() || propertyAccessorFlags.isNotEmpty()) {
return if (modalities.isNotEmpty() || propertyAccessorFlags.isNotEmpty()) {
functionBuilder
.apply {
addModifiers(visibility)
Expand Down
7 changes: 6 additions & 1 deletion interop/ksp/test-processor/build.gradle.kts
Expand Up @@ -13,11 +13,16 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

plugins {
id("com.google.devtools.ksp")
}

tasks.compileTestKotlin {
compilerOptions {
freeCompilerArgs.add("-opt-in=org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi")
}
}

dependencies {
implementation(project(":kotlinpoet"))
implementation(project(":interop:ksp"))
Expand Down

0 comments on commit 89d1dde

Please sign in to comment.