Skip to content

Commit

Permalink
Update kotlin (and related dependencies) to 2.0.21.
Browse files Browse the repository at this point in the history
This CL:

  * Updates KSP to 2.0.21-1.0.28
  * Updates kotlin-metadata-jvm to 2.0.21
  * Note: due to bazelbuild/rules_kotlin#1176 we can't actually update Kotlin itself to 2.0.21, but somehow that doesn't seem to cause an issue
  * Migrates kotlin-metadata-jvm usages to the new API (the pre-2.0.0 APIs are deprecated).
  * Updates XProcessing and XProcessing Testing jars
  * Adds flags to fall back to Kotlin 1.9 for compiler tests (Working on getting these tests working with K2/KSP2 is in progress).
  * Updated all of our artifact tests to use the same Kotlin and KSP versions.
  * Added `compat_kt_jvm_library` to alias output jars to the expected locations.

Fixes #4525

RELNOTES=Fixes #4525: Update kotlin-jvm-metadata to 2.0.21 to remove dependency on Beta version.
PiperOrigin-RevId: 702486745
  • Loading branch information
bcorso authored and Dagger Team committed Dec 6, 2024
1 parent 09a4246 commit 419563a
Show file tree
Hide file tree
Showing 26 changed files with 378 additions and 600 deletions.
15 changes: 11 additions & 4 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,17 @@ http_archive(

load("@io_bazel_rules_kotlin//kotlin:repositories.bzl", "kotlin_repositories", "kotlinc_version")

KOTLIN_VERSION = "1.9.24"
# TODO: update to Kotlin 2 once rules_kotlin support it.
# See https://github.com/bazelbuild/rules_kotlin/issues/1176
KOTLINC_VERSION = "1.9.24"

# Get from https://github.com/JetBrains/kotlin/releases/
KOTLINC_RELEASE_SHA = "eb7b68e01029fa67bc8d060ee54c12018f2c60ddc438cf21db14517229aa693b"

kotlin_repositories(
compiler_release = kotlinc_version(
release = KOTLIN_VERSION,
release = KOTLINC_VERSION,
# Get from https://github.com/JetBrains/kotlin/releases/
sha256 = KOTLINC_RELEASE_SHA,
),
)
Expand Down Expand Up @@ -203,7 +206,9 @@ GRPC_VERSION = "1.2.0"

INCAP_VERSION = "0.2"

KSP_VERSION = KOTLIN_VERSION + "-1.0.20"
KOTLIN_VERSION = "2.0.21"

KSP_VERSION = KOTLIN_VERSION + "-1.0.28"

MAVEN_VERSION = "3.3.3"

Expand Down Expand Up @@ -245,6 +250,8 @@ maven_install(
"com.google.code.findbugs:jsr305:3.0.1",
"com.google.devtools.ksp:symbol-processing:%s" % KSP_VERSION,
"com.google.devtools.ksp:symbol-processing-api:%s" % KSP_VERSION,
"com.google.devtools.ksp:symbol-processing-common-deps:%s" % KSP_VERSION,
"com.google.devtools.ksp:symbol-processing-aa-embeddable:%s" % KSP_VERSION,
"com.google.errorprone:error_prone_annotation:%s" % ERROR_PRONE_VERSION,
"com.google.errorprone:error_prone_annotations:%s" % ERROR_PRONE_VERSION,
"com.google.errorprone:error_prone_check_api:%s" % ERROR_PRONE_VERSION,
Expand Down Expand Up @@ -290,8 +297,8 @@ maven_install(
"org.jetbrains.kotlin:kotlin-annotation-processing-embeddable:%s" % KOTLIN_VERSION,
"org.jetbrains.kotlin:kotlin-compiler-embeddable:%s" % KOTLIN_VERSION,
"org.jetbrains.kotlin:kotlin-daemon-embeddable:%s" % KOTLIN_VERSION,
"org.jetbrains.kotlin:kotlin-metadata-jvm:%s" % KOTLIN_VERSION,
"org.jetbrains.kotlin:kotlin-stdlib:%s" % KOTLIN_VERSION,
"org.jetbrains.kotlin:kotlin-metadata-jvm:2.0.0-Beta5",
"org.jspecify:jspecify:1.0.0",
"org.mockito:mockito-core:2.28.2",
"org.pantsbuild:jarjar:1.7.2",
Expand Down
4 changes: 2 additions & 2 deletions java/dagger/hilt/android/plugin/build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
buildscript {
ext {
kotlin_version = "1.9.24"
kotlin_version = "2.0.21"
agp_version = System.getenv('AGP_VERSION') ?: "8.1.1"
ksp_version = "$kotlin_version-1.0.20"
ksp_version = "$kotlin_version-1.0.28"
pluginArtifactId = 'hilt-android-gradle-plugin'
pluginId = 'com.google.dagger.hilt.android'
}
Expand Down
23 changes: 16 additions & 7 deletions java/dagger/hilt/android/testing/compile/HiltCompilerTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@

/** {@link Compiler} instances for testing Android Hilt. */
public final class HiltCompilerTests {
private static final ImmutableList<String> DEFAULT_JAVAC_OPTIONS = ImmutableList.of();

private static final ImmutableList<String> DEFAULT_KOTLINC_OPTIONS =
ImmutableList.of(
"-api-version=1.9",
"-language-version=1.9",
"-P", "plugin:org.jetbrains.kotlin.kapt3:correctErrorTypes=true");

/** Returns the {@link XProcessingEnv.Backend} for the given {@link CompilationResultSubject}. */
public static XProcessingEnv.Backend backend(CompilationResultSubject subject) {
return CompilerTests.backend(subject);
Expand Down Expand Up @@ -161,9 +169,8 @@ public static void compileWithKapt(
sources,
/* classpath= */ ImmutableList.of(CompilerTests.compilerDepsJar()),
/* inheritClasspath= */ false,
/* javacArguments= */ ImmutableList.of(),
/* kotlincArguments= */ ImmutableList.of(
),
/* javacArguments= */ DEFAULT_JAVAC_OPTIONS,
/* kotlincArguments= */ DEFAULT_KOTLINC_OPTIONS,
/* kaptProcessors= */ ImmutableList.<Processor>builder()
.addAll(defaultProcessors())
.addAll(additionalProcessors)
Expand Down Expand Up @@ -275,10 +282,12 @@ public void compile(Consumer<CompilationResultSubject> onCompilationResult) {
sources().asList(),
/* classpath= */ ImmutableList.of(CompilerTests.compilerDepsJar()),
/* options= */ processorOptions(),
/* javacArguments= */ javacArguments().asList(),
/* kotlincArguments= */ ImmutableList.of(
"-P",
"plugin:org.jetbrains.kotlin.kapt3:correctErrorTypes=true"),
/* javacArguments= */
ImmutableList.<String>builder()
.addAll(DEFAULT_JAVAC_OPTIONS)
.addAll(javacArguments())
.build(),
/* kotlincArguments= */ DEFAULT_KOTLINC_OPTIONS,
/* config= */ HiltProcessingEnvConfigs.CONFIGS,
/* javacProcessors= */ ImmutableList.<Processor>builder()
.addAll(mergeProcessors(defaultProcessors(), additionalJavacProcessors()))
Expand Down
9 changes: 6 additions & 3 deletions java/dagger/hilt/processor/internal/kotlin/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
# Description:
# Sources related to Kotlin metadata.

load("@rules_java//java:defs.bzl", "java_library")
load("//tools:bazel_compat.bzl", "compat_kt_jvm_library")

package(default_visibility = ["//:src"])

java_library(
compat_kt_jvm_library(
name = "kotlin",
srcs = glob(["*.java"]),
srcs = glob([
"*.java",
"*.kt",
]),
deps = [
"//:dagger_with_compiler",
"//java/dagger/hilt/processor/internal:classnames",
Expand Down
100 changes: 100 additions & 0 deletions java/dagger/hilt/processor/internal/kotlin/ClassMetadata.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright (C) 2023 The Dagger Authors.
*
* 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
*
* http://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 dagger.hilt.processor.internal.kotlin

import androidx.room.compiler.processing.XAnnotation
import androidx.room.compiler.processing.XFieldElement
import androidx.room.compiler.processing.XMethodElement
import androidx.room.compiler.processing.XTypeElement
import kotlin.Metadata
import kotlin.metadata.declaresDefaultValue
import kotlin.metadata.KmClass
import kotlin.metadata.KmConstructor
import kotlin.metadata.KmFunction
import kotlin.metadata.KmProperty
import kotlin.metadata.KmValueParameter
import kotlin.metadata.jvm.KotlinClassMetadata
import kotlin.metadata.jvm.fieldSignature
import kotlin.metadata.jvm.getterSignature
import kotlin.metadata.jvm.signature
import kotlin.metadata.jvm.syntheticMethodForAnnotations

/** Container classes for kotlin metadata types. */
class ClassMetadata private constructor(private val kmClass: KmClass) {
val functionsBySignature = buildList<FunctionMetadata> {
addAll(kmClass.constructors.map { ConstructorMetadata(it) })
addAll(kmClass.functions.map { MethodMetadata(it) })
}.associateBy { it.signature }

val propertiesBySignature =
kmClass.properties
.filter { it.fieldSignature != null }
.map { PropertyMetadata(it) }
.associateBy { it.fieldSignature }

fun constructors(): List<FunctionMetadata> =
functionsBySignature.values.filterIsInstance<ConstructorMetadata>()

companion object {
/** Parse Kotlin class metadata from a given type element. */
@JvmStatic
fun of(typeElement: XTypeElement): ClassMetadata {
val metadataAnnotation = checkNotNull(typeElement.getAnnotation(Metadata::class)).value
return when (val classMetadata = KotlinClassMetadata.readStrict(metadataAnnotation)) {
is KotlinClassMetadata.Class -> ClassMetadata(classMetadata.kmClass)
else -> error("Unsupported metadata type: ${classMetadata}")
}
}
}
}

class ConstructorMetadata(private val kmConstructor: KmConstructor) : FunctionMetadata {
override val name = "<init>"
override val signature = kmConstructor.signature!!.toString()
override val parameters = kmConstructor.valueParameters.map { ParameterMetadata(it) }
}

class MethodMetadata(private val kmFunction: KmFunction) : FunctionMetadata {
override val name = kmFunction.name
override val signature = kmFunction.signature!!.toString()
override val parameters = kmFunction.valueParameters.map { ParameterMetadata(it) }
}

interface FunctionMetadata {
val name: String
val signature: String
val parameters: List<ParameterMetadata>
}

class PropertyMetadata(private val kmProperty: KmProperty) {
val name = kmProperty.name

/** Returns the JVM field descriptor of the backing field of this property. */
val fieldSignature = kmProperty.fieldSignature?.toString()

val getterSignature = kmProperty.getterSignature?.toString()

/** Returns JVM method descriptor of the synthetic method for property annotations. */
val methodForAnnotationsSignature = kmProperty.syntheticMethodForAnnotations?.toString()
}

class ParameterMetadata(private val kmValueParameter: KmValueParameter) {
val name = kmValueParameter.name

fun declaresDefaultValue() = kmValueParameter.declaresDefaultValue
}

Loading

0 comments on commit 419563a

Please sign in to comment.