Skip to content

Commit

Permalink
Update gradle, kp, kotlin, and ksp (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZacSweers committed Jul 16, 2021
1 parent 98b7eb6 commit a51ea90
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 117 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
matrix:
# No 12 because Kapt
java_version: [ 11, 13, 14, 15, 16 ]
kotlin_version: [ 1.5.0 ]
kotlin_version: [ 1.5.20 ]
ksp_enabled: [ true, false ]
ksp_incremental_enabled: [ true, false ]
exclude:
Expand Down
12 changes: 6 additions & 6 deletions buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ object Dependencies {

const val autoCommon = "com.google.auto:auto-common:1.0"
const val asm = "org.ow2.asm:asm:7.1"
const val ktlintVersion = "0.39.0"
const val ktlintVersion = "0.41.0"

object AutoService {
const val annotations = "com.google.auto.service:auto-service-annotations:1.0"
Expand All @@ -33,7 +33,7 @@ object Dependencies {
}

object Kotlin {
val version = System.getenv()["MOSHIX_KOTLIN"] ?: "1.5.10"
val version = System.getenv()["MOSHIX_KOTLIN"] ?: "1.5.20"
const val dokkaVersion = "1.4.32"
val reflect = "org.jetbrains.kotlin:kotlin-reflect:$version"
const val metadata = "org.jetbrains.kotlinx:kotlinx-metadata-jvm:0.3.0"
Expand All @@ -42,14 +42,14 @@ object Dependencies {
val defaultFreeCompilerArgs = listOf("-Xjsr305=strict", "-progressive")

object Ksp {
const val version = "1.5.10-1.0.0-beta01"
const val version = "1.5.20-1.0.0-beta04"
const val api = "com.google.devtools.ksp:symbol-processing-api:$version"
const val ksp = "com.google.devtools.ksp:symbol-processing:$version"
}
}

object KotlinPoet {
private const val version = "1.8.0"
private const val version = "1.9.0"
const val kotlinPoet = "com.squareup:kotlinpoet:$version"
const val metadata = "com.squareup:kotlinpoet-metadata-specs:$version"
const val metadataSpecs = "com.squareup:kotlinpoet-metadata-specs:$version"
Expand All @@ -76,8 +76,8 @@ object Dependencies {
}

object Testing {
const val compileTesting = "com.github.tschuchortdev:kotlin-compile-testing:1.4.0"
const val kspCompileTesting = "com.github.tschuchortdev:kotlin-compile-testing-ksp:1.4.0"
const val compileTesting = "com.github.tschuchortdev:kotlin-compile-testing:1.4.2"
const val kspCompileTesting = "com.github.tschuchortdev:kotlin-compile-testing-ksp:1.4.2"
const val junit = "junit:junit:4.13.2"
const val truth = "com.google.truth:truth:1.1.2"
}
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
Expand Up @@ -203,134 +203,134 @@ internal class KotlinJsonAdapter<T>(
public class MetadataKotlinJsonAdapterFactory : JsonAdapter.Factory {
override fun create(type: Type, annotations: MutableSet<out Annotation>, moshi: Moshi):
JsonAdapter<*>? {
if (annotations.isNotEmpty()) return null

val rawType = Types.getRawType(type)
if (rawType.isInterface) return null
if (rawType.isEnum) return null
if (!rawType.isAnnotationPresent(KOTLIN_METADATA)) return null
if (Util.isPlatformType(rawType)) return null
try {
val generatedAdapter = generatedAdapter(moshi, type, rawType)
if (generatedAdapter != null) {
return generatedAdapter
}
} catch (e: RuntimeException) {
if (e.cause !is ClassNotFoundException) {
throw e
}
// Fall back to a reflective adapter when the generated adapter is not found.
if (annotations.isNotEmpty()) return null

val rawType = Types.getRawType(type)
if (rawType.isInterface) return null
if (rawType.isEnum) return null
if (!rawType.isAnnotationPresent(KOTLIN_METADATA)) return null
if (Util.isPlatformType(rawType)) return null
try {
val generatedAdapter = generatedAdapter(moshi, type, rawType)
if (generatedAdapter != null) {
return generatedAdapter
}

require(!rawType.isLocalClass) {
"Cannot serialize local class or object expression ${rawType.name}"
} catch (e: RuntimeException) {
if (e.cause !is ClassNotFoundException) {
throw e
}
// Fall back to a reflective adapter when the generated adapter is not found.
}

val kmClass = rawType.header()?.toKmClass() ?: return null
require(!rawType.isLocalClass) {
"Cannot serialize local class or object expression ${rawType.name}"
}

require(!Flag.IS_ABSTRACT(kmClass.flags)) {
"Cannot serialize abstract class ${rawType.name}"
}
require(!Flag.Class.IS_INNER(kmClass.flags)) {
"Cannot serialize inner class ${rawType.name}"
}
require(!Flag.Class.IS_OBJECT(kmClass.flags)) {
"Cannot serialize object declaration ${rawType.name}"
}
require(!Flag.Class.IS_COMPANION_OBJECT(kmClass.flags)) {
"Cannot serialize companion object declaration ${rawType.name}"
}
require(!Flag.IS_SEALED(kmClass.flags)) {
"Cannot reflectively serialize sealed class ${rawType.name}. Please register an adapter."
}
val kmClass = rawType.header()?.toKmClass() ?: return null

val ktConstructor = KtConstructor.primary(rawType, kmClass) ?: return null

// TODO this doesn't cover platform types
val allPropertiesSequence = kmClass.properties.asSequence() +
generateSequence(rawType) { it.superclass }
.mapNotNull { it.header()?.toKmClass() }
.flatMap { it.properties.asSequence() }
.filterNot { Flag.IS_PRIVATE(it.flags) || Flag.IS_PRIVATE_TO_THIS(it.flags) }
.filter { Flag.Property.IS_VAR(it.flags) }

val signatureSearcher = JvmSignatureSearcher(rawType)
val bindingsByName = LinkedHashMap<String, KotlinJsonAdapter.Binding<Any, Any?>>()
val parametersByName = ktConstructor.parameters.associateBy { it.name }

for (property in allPropertiesSequence.distinctBy { it.name }) {
val propertyField = signatureSearcher.field(property)
val ktParameter = parametersByName[property.name]

if (Modifier.isTransient(propertyField?.modifiers ?: 0)) {
ktParameter?.run {
require(declaresDefaultValue) {
"No default value for transient constructor parameter '$name' on type '${rawType.canonicalName}'"
}
}
continue
}
require(!Flag.IS_ABSTRACT(kmClass.flags)) {
"Cannot serialize abstract class ${rawType.name}"
}
require(!Flag.Class.IS_INNER(kmClass.flags)) {
"Cannot serialize inner class ${rawType.name}"
}
require(!Flag.Class.IS_OBJECT(kmClass.flags)) {
"Cannot serialize object declaration ${rawType.name}"
}
require(!Flag.Class.IS_COMPANION_OBJECT(kmClass.flags)) {
"Cannot serialize companion object declaration ${rawType.name}"
}
require(!Flag.IS_SEALED(kmClass.flags)) {
"Cannot reflectively serialize sealed class ${rawType.name}. Please register an adapter."
}

if (ktParameter != null) {
require(ktParameter.km.type valueEquals property.returnType) {
"'${property.name}' has a constructor parameter of type ${ktParameter.km.type?.canonicalName} but a property of type ${property.returnType.canonicalName}."
}
}
val ktConstructor = KtConstructor.primary(rawType, kmClass) ?: return null

if (!Flag.Property.IS_VAR(property.flags) && ktParameter == null) continue
// TODO this doesn't cover platform types
val allPropertiesSequence = kmClass.properties.asSequence() +
generateSequence(rawType) { it.superclass }
.mapNotNull { it.header()?.toKmClass() }
.flatMap { it.properties.asSequence() }
.filterNot { Flag.IS_PRIVATE(it.flags) || Flag.IS_PRIVATE_TO_THIS(it.flags) }
.filter { Flag.Property.IS_VAR(it.flags) }

val getterMethod = signatureSearcher.getter(property)
val setterMethod = signatureSearcher.setter(property)
val annotationsMethod = signatureSearcher.syntheticMethodForAnnotations(property)
val signatureSearcher = JvmSignatureSearcher(rawType)
val bindingsByName = LinkedHashMap<String, KotlinJsonAdapter.Binding<Any, Any?>>()
val parametersByName = ktConstructor.parameters.associateBy { it.name }

val ktProperty = KtProperty(
km = property,
jvmField = propertyField,
jvmGetter = getterMethod,
jvmSetter = setterMethod,
jvmAnnotationsMethod = annotationsMethod,
parameter = ktParameter
)
val allAnnotations = ktProperty.annotations.toMutableList()
val jsonAnnotation = allAnnotations.filterIsInstance<Json>().firstOrNull()

val name = jsonAnnotation?.name ?: property.name
val resolvedPropertyType = resolve(type, rawType, ktProperty.javaType)
val adapter = moshi.adapter<Any>(
resolvedPropertyType,
Util.jsonAnnotations(allAnnotations.toTypedArray()),
property.name
)
for (property in allPropertiesSequence.distinctBy { it.name }) {
val propertyField = signatureSearcher.field(property)
val ktParameter = parametersByName[property.name]

@Suppress("UNCHECKED_CAST")
bindingsByName[property.name] = KotlinJsonAdapter.Binding(
name,
jsonAnnotation?.name ?: name,
adapter,
ktProperty
)
if (Modifier.isTransient(propertyField?.modifiers ?: 0)) {
ktParameter?.run {
require(declaresDefaultValue) {
"No default value for transient constructor parameter '$name' on type '${rawType.canonicalName}'"
}
}
continue
}

val bindings = ArrayList<KotlinJsonAdapter.Binding<Any, Any?>?>()

for (parameter in ktConstructor.parameters) {
val binding = bindingsByName.remove(parameter.name)
require(binding != null || parameter.declaresDefaultValue) {
"No property for required constructor parameter '${parameter.name}' on type '${rawType.canonicalName}'"
if (ktParameter != null) {
require(ktParameter.km.type valueEquals property.returnType) {
"'${property.name}' has a constructor parameter of type ${ktParameter.km.type?.canonicalName} but a property of type ${property.returnType.canonicalName}."
}
bindings += binding
}

var index = bindings.size
for (bindingByName in bindingsByName) {
bindings += bindingByName.value.copy(propertyIndex = index++)
if (!Flag.Property.IS_VAR(property.flags) && ktParameter == null) continue

val getterMethod = signatureSearcher.getter(property)
val setterMethod = signatureSearcher.setter(property)
val annotationsMethod = signatureSearcher.syntheticMethodForAnnotations(property)

val ktProperty = KtProperty(
km = property,
jvmField = propertyField,
jvmGetter = getterMethod,
jvmSetter = setterMethod,
jvmAnnotationsMethod = annotationsMethod,
parameter = ktParameter
)
val allAnnotations = ktProperty.annotations.toMutableList()
val jsonAnnotation = allAnnotations.filterIsInstance<Json>().firstOrNull()

val name = jsonAnnotation?.name ?: property.name
val resolvedPropertyType = resolve(type, rawType, ktProperty.javaType)
val adapter = moshi.adapter<Any>(
resolvedPropertyType,
Util.jsonAnnotations(allAnnotations.toTypedArray()),
property.name
)

@Suppress("UNCHECKED_CAST")
bindingsByName[property.name] = KotlinJsonAdapter.Binding(
name,
jsonAnnotation?.name ?: name,
adapter,
ktProperty
)
}

val bindings = ArrayList<KotlinJsonAdapter.Binding<Any, Any?>?>()

for (parameter in ktConstructor.parameters) {
val binding = bindingsByName.remove(parameter.name)
require(binding != null || parameter.declaresDefaultValue) {
"No property for required constructor parameter '${parameter.name}' on type '${rawType.canonicalName}'"
}
bindings += binding
}

val nonTransientBindings = bindings.filterNotNull()
val options = JsonReader.Options.of(*nonTransientBindings.map { it.name }.toTypedArray())
return KotlinJsonAdapter(ktConstructor, bindings, nonTransientBindings, options).nullSafe()
var index = bindings.size
for (bindingByName in bindingsByName) {
bindings += bindingByName.value.copy(propertyIndex = index++)
}

val nonTransientBindings = bindings.filterNotNull()
val options = JsonReader.Options.of(*nonTransientBindings.map { it.name }.toTypedArray())
return KotlinJsonAdapter(ktConstructor, bindings, nonTransientBindings, options).nullSafe()
}

private infix fun KmType?.valueEquals(other: KmType?): Boolean {
return when {
this === other -> true
Expand Down

0 comments on commit a51ea90

Please sign in to comment.