forked from square/dagger
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update kotlin (and related dependencies) to 2.0.21.
This CL also updates kotlin-metadata-jvm to 2.0.21 which requires migrating, since all of the deprecated methods Dagger/Hilt previously referenced were removed in v2.0.0. 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
Showing
17 changed files
with
247 additions
and
504 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
java/dagger/hilt/processor/internal/kotlin/ClassMetadata.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* 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) }) | ||
}.associate { it.signature to it } | ||
|
||
val propertiesBySignature = | ||
kmClass.properties | ||
.map { PropertyMetadata(it) } | ||
.associate { it.fieldSignature to it } | ||
.filterKeys { it != null } | ||
|
||
fun constructors(): List<FunctionMetadata> = | ||
functionsBySignature.values | ||
.filter { it is ConstructorMetadata } | ||
.toList() | ||
|
||
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 | ||
} | ||
|
Oops, something went wrong.