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:
Zac Sweers
2018-05-16 12:44:07 -07:00
committed by Jesse Wilson
parent d48e3aaa27
commit b956b06f6d
3 changed files with 29 additions and 8 deletions

View File

@@ -49,7 +49,7 @@ internal class AdapterGenerator(
) { ) {
private val className = target.name private val className = target.name
private val isDataClass = target.proto.isDataClass private val isDataClass = target.proto.isDataClass
private val hasCompanionObject = target.hasCompanionObject private val companionObjectName = target.companionObjectName
private val visibility = target.proto.visibility!! private val visibility = target.proto.visibility!!
private val typeVariables = target.typeVariables private val typeVariables = target.typeVariables
@@ -94,8 +94,8 @@ internal class AdapterGenerator(
val result = FileSpec.builder(className.packageName(), adapterName) val result = FileSpec.builder(className.packageName(), adapterName)
result.addComment("Code generated by moshi-kotlin-codegen. Do not edit.") result.addComment("Code generated by moshi-kotlin-codegen. Do not edit.")
if (hasCompanionObject) { companionObjectName?.let {
result.addFunction(generateJsonAdapterFun()) result.addFunction(generateJsonAdapterFun(it))
} }
result.addType(generateType(messager, generatedOption)) result.addType(generateType(messager, generatedOption))
return result.build() return result.build()
@@ -312,7 +312,7 @@ internal class AdapterGenerator(
return result.build() return result.build()
} }
private fun generateJsonAdapterFun(): FunSpec { private fun generateJsonAdapterFun(name: String): FunSpec {
val rawType = when (originalTypeName) { val rawType = when (originalTypeName) {
is TypeVariableName -> throw IllegalArgumentException("Cannot get raw type of TypeVariable!") is TypeVariableName -> throw IllegalArgumentException("Cannot get raw type of TypeVariable!")
is ParameterizedTypeName -> originalTypeName.rawType is ParameterizedTypeName -> originalTypeName.rawType
@@ -320,7 +320,7 @@ internal class AdapterGenerator(
} }
val result = FunSpec.builder("jsonAdapter") val result = FunSpec.builder("jsonAdapter")
.receiver(rawType.nestedClass("Companion")) .receiver(rawType.nestedClass(name))
.returns(jsonAdapterTypeName) .returns(jsonAdapterTypeName)
.addParameter(moshiParam) .addParameter(moshiParam)

View File

@@ -53,10 +53,10 @@ internal data class TargetType(
val element: TypeElement, val element: TypeElement,
val constructor: TargetConstructor, val constructor: TargetConstructor,
val properties: Map<String, TargetProperty>, val properties: Map<String, TargetProperty>,
val typeVariables: List<TypeVariableName> val typeVariables: List<TypeVariableName>,
val companionObjectName: String?
) { ) {
val name = element.className val name = element.className
val hasCompanionObject = proto.hasCompanionObjectName()
companion object { companion object {
private val OBJECT_CLASS = ClassName("java.lang", "Object") private val OBJECT_CLASS = ClassName("java.lang", "Object")
@@ -129,7 +129,12 @@ internal data class TargetType(
properties.putIfAbsent(name, property) 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`. */ /** Returns the properties declared by `typeElement`. */

View File

@@ -786,6 +786,22 @@ class GeneratedAdaptersTest {
@JsonClass(generateAdapter = true) @JsonClass(generateAdapter = true)
class DuplicateValue(var a: Int = -1, var b: Int = -2) class DuplicateValue(var a: Int = -1, var b: Int = -2)
@Test fun companionObjectsTests() {
val moshi = Moshi.Builder().build()
val standardAdapter = CompanionObjectClass.jsonAdapter(moshi)
val customNameAdapter = NamedCompanionObjectClass.jsonAdapter(moshi)
}
@JsonClass(generateAdapter = true)
data class CompanionObjectClass(val foo: String) {
companion object
}
@JsonClass(generateAdapter = true)
data class NamedCompanionObjectClass(val foo: String) {
companion object CustomCompanionObject
}
@JsonQualifier @JsonQualifier
annotation class Uppercase(val inFrench: Boolean, val onSundays: Boolean = false) annotation class Uppercase(val inFrench: Boolean, val onSundays: Boolean = false)