mirror of
https://github.com/fankes/moshi.git
synced 2025-10-19 16:09:21 +08:00
Fix companion object names not being resolved (#549)
* Fix companion object names not being resolved This slipped through the cracks before the release Fixes #546 * Add braces on the else clause for symmetry
This commit is contained in:
@@ -49,7 +49,7 @@ internal class AdapterGenerator(
|
||||
) {
|
||||
private val className = target.name
|
||||
private val isDataClass = target.proto.isDataClass
|
||||
private val hasCompanionObject = target.hasCompanionObject
|
||||
private val companionObjectName = target.companionObjectName
|
||||
private val visibility = target.proto.visibility!!
|
||||
private val typeVariables = target.typeVariables
|
||||
|
||||
@@ -94,8 +94,8 @@ internal class AdapterGenerator(
|
||||
|
||||
val result = FileSpec.builder(className.packageName(), adapterName)
|
||||
result.addComment("Code generated by moshi-kotlin-codegen. Do not edit.")
|
||||
if (hasCompanionObject) {
|
||||
result.addFunction(generateJsonAdapterFun())
|
||||
companionObjectName?.let {
|
||||
result.addFunction(generateJsonAdapterFun(it))
|
||||
}
|
||||
result.addType(generateType(messager, generatedOption))
|
||||
return result.build()
|
||||
@@ -312,7 +312,7 @@ internal class AdapterGenerator(
|
||||
return result.build()
|
||||
}
|
||||
|
||||
private fun generateJsonAdapterFun(): FunSpec {
|
||||
private fun generateJsonAdapterFun(name: String): FunSpec {
|
||||
val rawType = when (originalTypeName) {
|
||||
is TypeVariableName -> throw IllegalArgumentException("Cannot get raw type of TypeVariable!")
|
||||
is ParameterizedTypeName -> originalTypeName.rawType
|
||||
@@ -320,7 +320,7 @@ internal class AdapterGenerator(
|
||||
}
|
||||
|
||||
val result = FunSpec.builder("jsonAdapter")
|
||||
.receiver(rawType.nestedClass("Companion"))
|
||||
.receiver(rawType.nestedClass(name))
|
||||
.returns(jsonAdapterTypeName)
|
||||
.addParameter(moshiParam)
|
||||
|
||||
|
@@ -53,10 +53,10 @@ internal data class TargetType(
|
||||
val element: TypeElement,
|
||||
val constructor: TargetConstructor,
|
||||
val properties: Map<String, TargetProperty>,
|
||||
val typeVariables: List<TypeVariableName>
|
||||
val typeVariables: List<TypeVariableName>,
|
||||
val companionObjectName: String?
|
||||
) {
|
||||
val name = element.className
|
||||
val hasCompanionObject = proto.hasCompanionObjectName()
|
||||
|
||||
companion object {
|
||||
private val OBJECT_CLASS = ClassName("java.lang", "Object")
|
||||
@@ -129,7 +129,12 @@ internal data class TargetType(
|
||||
properties.putIfAbsent(name, property)
|
||||
}
|
||||
}
|
||||
return TargetType(proto, element, constructor, properties, typeVariables)
|
||||
val companionObjectName = if (proto.hasCompanionObjectName()) {
|
||||
typeMetadata.data.nameResolver.getQualifiedClassName(proto.companionObjectName)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
return TargetType(proto, element, constructor, properties, typeVariables, companionObjectName)
|
||||
}
|
||||
|
||||
/** Returns the properties declared by `typeElement`. */
|
||||
|
Reference in New Issue
Block a user