mirror of
https://github.com/fankes/moshi.git
synced 2025-10-18 23:49:21 +08:00
Update dependency com.pinterest.ktlint:ktlint-cli to v1.2.0 (#1814)
* Update dependency com.pinterest.ktlint:ktlint-cli to v1.2.0 * Appease ktlint --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Jake Wharton <jw@squareup.com>
This commit is contained in:
@@ -200,7 +200,9 @@ public class AdapterGenerator(
|
||||
private fun TypeSpec.createProguardRule(): ProguardConfig {
|
||||
val adapterConstructorParams = when (requireNotNull(primaryConstructor).parameters.size) {
|
||||
1 -> listOf(CN_MOSHI.reflectionName())
|
||||
|
||||
2 -> listOf(CN_MOSHI.reflectionName(), "${CN_TYPE.reflectionName()}[]")
|
||||
|
||||
// Should never happen
|
||||
else -> error("Unexpected number of arguments on primary constructor: $primaryConstructor")
|
||||
}
|
||||
@@ -726,16 +728,26 @@ public data class PreparedAdapter(val spec: FileSpec, val proguardConfig: Progua
|
||||
private fun AsmType.toReflectionString(): String {
|
||||
return when (this) {
|
||||
AsmType.VOID_TYPE -> "void"
|
||||
|
||||
AsmType.BOOLEAN_TYPE -> "boolean"
|
||||
|
||||
AsmType.CHAR_TYPE -> "char"
|
||||
|
||||
AsmType.BYTE_TYPE -> "byte"
|
||||
|
||||
AsmType.SHORT_TYPE -> "short"
|
||||
|
||||
AsmType.INT_TYPE -> "int"
|
||||
|
||||
AsmType.FLOAT_TYPE -> "float"
|
||||
|
||||
AsmType.LONG_TYPE -> "long"
|
||||
|
||||
AsmType.DOUBLE_TYPE -> "double"
|
||||
|
||||
else -> when (sort) {
|
||||
AsmType.ARRAY -> "${elementType.toReflectionString()}[]"
|
||||
|
||||
// Object type
|
||||
else -> className
|
||||
}
|
||||
|
@@ -64,6 +64,7 @@ public data class DelegateKey(
|
||||
|
||||
val (initializerString, args) = when {
|
||||
jsonQualifiers.isEmpty() -> ", %M()" to arrayOf(MemberName("kotlin.collections", "emptySet"))
|
||||
|
||||
else -> {
|
||||
", setOf(%L)" to arrayOf(jsonQualifiers.map { it.asInstantiationExpression() }.joinToCode())
|
||||
}
|
||||
|
@@ -92,10 +92,12 @@ internal abstract class TypeRenderer {
|
||||
target = typeName.inTypes[0]
|
||||
method = "supertypeOf"
|
||||
}
|
||||
|
||||
typeName.outTypes.size == 1 -> {
|
||||
target = typeName.outTypes[0]
|
||||
method = "subtypeOf"
|
||||
}
|
||||
|
||||
else -> throw IllegalArgumentException(
|
||||
"Unrepresentable wildcard type. Cannot have more than one bound: $typeName",
|
||||
)
|
||||
|
@@ -49,7 +49,9 @@ internal fun TypeName.rawType(): ClassName {
|
||||
internal fun TypeName.findRawType(): ClassName? {
|
||||
return when (this) {
|
||||
is ClassName -> this
|
||||
|
||||
is ParameterizedTypeName -> rawType
|
||||
|
||||
is LambdaTypeName -> {
|
||||
var count = parameters.size
|
||||
if (receiver != null) {
|
||||
@@ -62,6 +64,7 @@ internal fun TypeName.findRawType(): ClassName? {
|
||||
}
|
||||
ClassName("kotlin.jvm.functions", functionSimpleName)
|
||||
}
|
||||
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
@@ -104,11 +107,14 @@ internal fun TypeName.asTypeBlock(): CodeBlock {
|
||||
rawType.asTypeBlock()
|
||||
}
|
||||
}
|
||||
|
||||
is TypeVariableName -> {
|
||||
val bound = bounds.firstOrNull() ?: ANY
|
||||
return bound.asTypeBlock()
|
||||
}
|
||||
|
||||
is LambdaTypeName -> return rawType().asTypeBlock()
|
||||
|
||||
is ClassName -> {
|
||||
// Check against the non-nullable version for equality, but we'll keep the nullability in
|
||||
// consideration when creating the CodeBlock if needed.
|
||||
@@ -121,10 +127,13 @@ internal fun TypeName.asTypeBlock(): CodeBlock {
|
||||
CodeBlock.of("%T::class.javaPrimitiveType!!", this)
|
||||
}
|
||||
}
|
||||
|
||||
UNIT, Void::class.asTypeName(), NOTHING -> throw IllegalStateException("Parameter with void, Unit, or Nothing type is illegal")
|
||||
|
||||
else -> CodeBlock.of("%T::class.java", copy(nullable = false))
|
||||
}
|
||||
}
|
||||
|
||||
else -> throw UnsupportedOperationException("Parameter with type '${javaClass.simpleName}' is illegal. Only classes, parameterized types, or type variables are allowed.")
|
||||
}
|
||||
}
|
||||
@@ -138,11 +147,15 @@ internal fun KModifier.checkIsVisibility() {
|
||||
internal fun TypeName.stripTypeVarVariance(resolver: TypeVariableResolver): TypeName {
|
||||
return when (this) {
|
||||
is ClassName -> this
|
||||
|
||||
is ParameterizedTypeName -> {
|
||||
deepCopy { it.stripTypeVarVariance(resolver) }
|
||||
}
|
||||
|
||||
is TypeVariableName -> resolver[name]
|
||||
|
||||
is WildcardTypeName -> deepCopy { it.stripTypeVarVariance(resolver) }
|
||||
|
||||
else -> throw UnsupportedOperationException("Type '${javaClass.simpleName}' is illegal. Only classes, parameterized types, wildcard types, or type variables are allowed.")
|
||||
}
|
||||
}
|
||||
@@ -168,14 +181,17 @@ internal fun WildcardTypeName.deepCopy(transform: (TypeName) -> TypeName): TypeN
|
||||
// Consumer type - single element inTypes, single ANY element outType.
|
||||
return when {
|
||||
this == STAR -> this
|
||||
|
||||
outTypes.isNotEmpty() && inTypes.isEmpty() -> {
|
||||
WildcardTypeName.producerOf(transform(outTypes[0]))
|
||||
.copy(nullable = isNullable, annotations = annotations)
|
||||
}
|
||||
|
||||
inTypes.isNotEmpty() -> {
|
||||
WildcardTypeName.consumerOf(transform(inTypes[0]))
|
||||
.copy(nullable = isNullable, annotations = annotations)
|
||||
}
|
||||
|
||||
else -> throw UnsupportedOperationException("Not possible.")
|
||||
}
|
||||
}
|
||||
|
@@ -44,18 +44,23 @@ private fun TypeName.unwrapTypeAliasInternal(): TypeName? {
|
||||
internal fun TypeName.unwrapTypeAlias(): TypeName {
|
||||
return when (this) {
|
||||
is ClassName -> unwrapTypeAliasInternal() ?: this
|
||||
|
||||
is ParameterizedTypeName -> {
|
||||
unwrapTypeAliasInternal() ?: deepCopy(TypeName::unwrapTypeAlias)
|
||||
}
|
||||
|
||||
is TypeVariableName -> {
|
||||
unwrapTypeAliasInternal() ?: deepCopy(transform = TypeName::unwrapTypeAlias)
|
||||
}
|
||||
|
||||
is WildcardTypeName -> {
|
||||
unwrapTypeAliasInternal() ?: deepCopy(TypeName::unwrapTypeAlias)
|
||||
}
|
||||
|
||||
is LambdaTypeName -> {
|
||||
unwrapTypeAliasInternal() ?: deepCopy(TypeName::unwrapTypeAlias)
|
||||
}
|
||||
|
||||
Dynamic -> throw UnsupportedOperationException("Type '${javaClass.simpleName}' is illegal. Only classes, parameterized types, wildcard types, or type variables are allowed.")
|
||||
}
|
||||
}
|
||||
|
@@ -152,6 +152,7 @@ internal fun targetType(
|
||||
)
|
||||
return null
|
||||
}
|
||||
|
||||
!kmClass.isClass -> {
|
||||
messager.printMessage(
|
||||
ERROR,
|
||||
@@ -160,6 +161,7 @@ internal fun targetType(
|
||||
)
|
||||
return null
|
||||
}
|
||||
|
||||
kmClass.isInner -> {
|
||||
messager.printMessage(
|
||||
ERROR,
|
||||
@@ -168,6 +170,7 @@ internal fun targetType(
|
||||
)
|
||||
return null
|
||||
}
|
||||
|
||||
kmClass.flags.isSealed -> {
|
||||
messager.printMessage(
|
||||
ERROR,
|
||||
@@ -176,6 +179,7 @@ internal fun targetType(
|
||||
)
|
||||
return null
|
||||
}
|
||||
|
||||
kmClass.flags.isAbstract -> {
|
||||
messager.printMessage(
|
||||
ERROR,
|
||||
@@ -184,6 +188,7 @@ internal fun targetType(
|
||||
)
|
||||
return null
|
||||
}
|
||||
|
||||
kmClass.flags.isLocal -> {
|
||||
messager.printMessage(
|
||||
ERROR,
|
||||
@@ -192,6 +197,7 @@ internal fun targetType(
|
||||
)
|
||||
return null
|
||||
}
|
||||
|
||||
!kmClass.flags.isPublic && !kmClass.flags.isInternal -> {
|
||||
messager.printMessage(
|
||||
ERROR,
|
||||
@@ -350,15 +356,18 @@ private fun resolveTypeArgs(
|
||||
|
||||
return when {
|
||||
resolvedType !is TypeVariableName -> resolvedType
|
||||
|
||||
entryStartIndex != 0 -> {
|
||||
// We need to go deeper
|
||||
resolveTypeArgs(targetClass, resolvedType, resolvedTypes, allowedTypeVars, entryStartIndex - 1)
|
||||
}
|
||||
|
||||
resolvedType.copy(nullable = false) in allowedTypeVars -> {
|
||||
// This is a generic type in the top-level declared class. This is fine to leave in because
|
||||
// this will be handled by the `Type` array passed in at runtime.
|
||||
resolvedType
|
||||
}
|
||||
|
||||
else -> error("Could not find $resolvedType in $resolvedTypes. Also not present in allowable top-level type vars $allowedTypeVars")
|
||||
}
|
||||
}
|
||||
|
@@ -76,6 +76,7 @@ private fun addValueToBlock(value: Any, resolver: Resolver, member: CodeBlock.Bu
|
||||
}
|
||||
member.add("⇤⇤)")
|
||||
}
|
||||
|
||||
is KSType -> {
|
||||
val unwrapped = value.unwrapTypeAlias()
|
||||
val isEnum = (unwrapped.declaration as KSClassDeclaration).classKind == ClassKind.ENUM_ENTRY
|
||||
@@ -87,13 +88,16 @@ private fun addValueToBlock(value: Any, resolver: Resolver, member: CodeBlock.Bu
|
||||
member.add("%T::class", unwrapped.toClassName())
|
||||
}
|
||||
}
|
||||
|
||||
is KSName ->
|
||||
member.add(
|
||||
"%T.%L",
|
||||
ClassName.bestGuess(value.getQualifier()),
|
||||
value.getShortName(),
|
||||
)
|
||||
|
||||
is KSAnnotation -> member.add("%L", value.toAnnotationSpec(resolver))
|
||||
|
||||
else -> member.add(memberForValue(value))
|
||||
}
|
||||
}
|
||||
@@ -105,13 +109,21 @@ private fun addValueToBlock(value: Any, resolver: Resolver, member: CodeBlock.Bu
|
||||
*/
|
||||
internal fun memberForValue(value: Any) = when (value) {
|
||||
is Class<*> -> CodeBlock.of("%T::class", value)
|
||||
|
||||
is Enum<*> -> CodeBlock.of("%T.%L", value.javaClass, value.name)
|
||||
|
||||
is String -> CodeBlock.of("%S", value)
|
||||
|
||||
is Float -> CodeBlock.of("%Lf", value)
|
||||
|
||||
is Double -> CodeBlock.of("%L", value)
|
||||
|
||||
is Char -> CodeBlock.of("$value.toChar()")
|
||||
|
||||
is Byte -> CodeBlock.of("$value.toByte()")
|
||||
|
||||
is Short -> CodeBlock.of("$value.toShort()")
|
||||
|
||||
// Int or Boolean
|
||||
else -> CodeBlock.of("%L", value)
|
||||
}
|
||||
|
@@ -80,32 +80,39 @@ private fun KSAnnotation.createInvocationHandler(clazz: Class<*>): InvocationHan
|
||||
}
|
||||
when (val result = argument.value ?: method.defaultValue) {
|
||||
is Proxy -> result
|
||||
|
||||
is List<*> -> {
|
||||
val value = { result.asArray(method) }
|
||||
cache.getOrPut(Pair(method.returnType, result), value)
|
||||
}
|
||||
|
||||
else -> {
|
||||
when {
|
||||
method.returnType.isEnum -> {
|
||||
val value = { result.asEnum(method.returnType) }
|
||||
cache.getOrPut(Pair(method.returnType, result), value)
|
||||
}
|
||||
|
||||
method.returnType.isAnnotation -> {
|
||||
val value = { (result as KSAnnotation).asAnnotation(method.returnType) }
|
||||
cache.getOrPut(Pair(method.returnType, result), value)
|
||||
}
|
||||
|
||||
method.returnType.name == "java.lang.Class" -> {
|
||||
val value = { (result as KSType).asClass() }
|
||||
cache.getOrPut(Pair(method.returnType, result), value)
|
||||
}
|
||||
|
||||
method.returnType.name == "byte" -> {
|
||||
val value = { result.asByte() }
|
||||
cache.getOrPut(Pair(method.returnType, result), value)
|
||||
}
|
||||
|
||||
method.returnType.name == "short" -> {
|
||||
val value = { result.asShort() }
|
||||
cache.getOrPut(Pair(method.returnType, result), value)
|
||||
}
|
||||
|
||||
else -> result // original value
|
||||
}
|
||||
}
|
||||
@@ -129,27 +136,39 @@ private fun KSAnnotation.asAnnotation(
|
||||
private fun List<*>.asArray(method: Method) =
|
||||
when (method.returnType.componentType.name) {
|
||||
"boolean" -> (this as List<Boolean>).toBooleanArray()
|
||||
|
||||
"byte" -> (this as List<Byte>).toByteArray()
|
||||
|
||||
"short" -> (this as List<Short>).toShortArray()
|
||||
|
||||
"char" -> (this as List<Char>).toCharArray()
|
||||
|
||||
"double" -> (this as List<Double>).toDoubleArray()
|
||||
|
||||
"float" -> (this as List<Float>).toFloatArray()
|
||||
|
||||
"int" -> (this as List<Int>).toIntArray()
|
||||
|
||||
"long" -> (this as List<Long>).toLongArray()
|
||||
|
||||
"java.lang.Class" -> (this as List<KSType>).map {
|
||||
Class.forName(it.declaration.qualifiedName!!.asString())
|
||||
}.toTypedArray()
|
||||
|
||||
"java.lang.String" -> (this as List<String>).toTypedArray()
|
||||
|
||||
else -> { // arrays of enums or annotations
|
||||
when {
|
||||
method.returnType.componentType.isEnum -> {
|
||||
this.toArray(method) { result -> result.asEnum(method.returnType.componentType) }
|
||||
}
|
||||
|
||||
method.returnType.componentType.isAnnotation -> {
|
||||
this.toArray(method) { result ->
|
||||
(result as KSAnnotation).asAnnotation(method.returnType.componentType)
|
||||
}
|
||||
}
|
||||
|
||||
else -> throw IllegalStateException("Unable to process type ${method.returnType.componentType.name}")
|
||||
}
|
||||
}
|
||||
|
@@ -668,6 +668,7 @@ class JsonClassCodegenProcessorTest {
|
||||
}
|
||||
""".trimIndent(),
|
||||
)
|
||||
|
||||
"moshi-testPackage.Simple" -> assertThat(generatedFile.readText()).contains(
|
||||
"""
|
||||
-if class testPackage.Simple
|
||||
@@ -678,6 +679,7 @@ class JsonClassCodegenProcessorTest {
|
||||
}
|
||||
""".trimIndent(),
|
||||
)
|
||||
|
||||
"moshi-testPackage.Generic" -> assertThat(generatedFile.readText()).contains(
|
||||
"""
|
||||
-if class testPackage.Generic
|
||||
@@ -688,6 +690,7 @@ class JsonClassCodegenProcessorTest {
|
||||
}
|
||||
""".trimIndent(),
|
||||
)
|
||||
|
||||
"moshi-testPackage.UsingQualifiers" -> {
|
||||
assertThat(generatedFile.readText()).contains(
|
||||
"""
|
||||
@@ -700,6 +703,7 @@ class JsonClassCodegenProcessorTest {
|
||||
""".trimIndent(),
|
||||
)
|
||||
}
|
||||
|
||||
"moshi-testPackage.MixedTypes" -> assertThat(generatedFile.readText()).contains(
|
||||
"""
|
||||
-if class testPackage.MixedTypes
|
||||
@@ -710,6 +714,7 @@ class JsonClassCodegenProcessorTest {
|
||||
}
|
||||
""".trimIndent(),
|
||||
)
|
||||
|
||||
"moshi-testPackage.DefaultParams" -> assertThat(generatedFile.readText()).contains(
|
||||
"""
|
||||
-if class testPackage.DefaultParams
|
||||
@@ -726,6 +731,7 @@ class JsonClassCodegenProcessorTest {
|
||||
}
|
||||
""".trimIndent(),
|
||||
)
|
||||
|
||||
"moshi-testPackage.Complex" -> {
|
||||
assertThat(generatedFile.readText()).contains(
|
||||
"""
|
||||
@@ -744,6 +750,7 @@ class JsonClassCodegenProcessorTest {
|
||||
""".trimIndent(),
|
||||
)
|
||||
}
|
||||
|
||||
"moshi-testPackage.MultipleMasks" -> assertThat(generatedFile.readText()).contains(
|
||||
"""
|
||||
-if class testPackage.MultipleMasks
|
||||
@@ -760,6 +767,7 @@ class JsonClassCodegenProcessorTest {
|
||||
}
|
||||
""".trimIndent(),
|
||||
)
|
||||
|
||||
"moshi-testPackage.NestedType.NestedSimple" -> {
|
||||
assertThat(generatedFile.readText()).contains(
|
||||
"""
|
||||
@@ -772,6 +780,7 @@ class JsonClassCodegenProcessorTest {
|
||||
""".trimIndent(),
|
||||
)
|
||||
}
|
||||
|
||||
else -> error("Unexpected proguard file! ${generatedFile.name}")
|
||||
}
|
||||
}
|
||||
|
@@ -718,6 +718,7 @@ class JsonClassSymbolProcessorTest {
|
||||
}
|
||||
""".trimIndent(),
|
||||
)
|
||||
|
||||
"moshi-testPackage.Simple" -> assertThat(generatedFile.readText()).contains(
|
||||
"""
|
||||
-if class testPackage.Simple
|
||||
@@ -728,6 +729,7 @@ class JsonClassSymbolProcessorTest {
|
||||
}
|
||||
""".trimIndent(),
|
||||
)
|
||||
|
||||
"moshi-testPackage.Generic" -> assertThat(generatedFile.readText()).contains(
|
||||
"""
|
||||
-if class testPackage.Generic
|
||||
@@ -738,6 +740,7 @@ class JsonClassSymbolProcessorTest {
|
||||
}
|
||||
""".trimIndent(),
|
||||
)
|
||||
|
||||
"moshi-testPackage.UsingQualifiers" -> {
|
||||
assertThat(generatedFile.readText()).contains(
|
||||
"""
|
||||
@@ -750,6 +753,7 @@ class JsonClassSymbolProcessorTest {
|
||||
""".trimIndent(),
|
||||
)
|
||||
}
|
||||
|
||||
"moshi-testPackage.MixedTypes" -> assertThat(generatedFile.readText()).contains(
|
||||
"""
|
||||
-if class testPackage.MixedTypes
|
||||
@@ -760,6 +764,7 @@ class JsonClassSymbolProcessorTest {
|
||||
}
|
||||
""".trimIndent(),
|
||||
)
|
||||
|
||||
"moshi-testPackage.DefaultParams" -> assertThat(generatedFile.readText()).contains(
|
||||
"""
|
||||
-if class testPackage.DefaultParams
|
||||
@@ -776,6 +781,7 @@ class JsonClassSymbolProcessorTest {
|
||||
}
|
||||
""".trimIndent(),
|
||||
)
|
||||
|
||||
"moshi-testPackage.Complex" -> {
|
||||
assertThat(generatedFile.readText()).contains(
|
||||
"""
|
||||
@@ -794,6 +800,7 @@ class JsonClassSymbolProcessorTest {
|
||||
""".trimIndent(),
|
||||
)
|
||||
}
|
||||
|
||||
"moshi-testPackage.MultipleMasks" -> assertThat(generatedFile.readText()).contains(
|
||||
"""
|
||||
-if class testPackage.MultipleMasks
|
||||
@@ -810,6 +817,7 @@ class JsonClassSymbolProcessorTest {
|
||||
}
|
||||
""".trimIndent(),
|
||||
)
|
||||
|
||||
"moshi-testPackage.NestedType.NestedSimple" -> {
|
||||
assertThat(generatedFile.readText()).contains(
|
||||
"""
|
||||
@@ -822,6 +830,7 @@ class JsonClassSymbolProcessorTest {
|
||||
""".trimIndent(),
|
||||
)
|
||||
}
|
||||
|
||||
else -> error("Unexpected proguard file! ${generatedFile.name}")
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user