Only add constructor property if it will be used (#918)

Before - we would always add the constructor property even if the class had no default constructor parameters. Apparently -Werror doesn't flag this as a warning either, so we didn't catch it before
This commit is contained in:
Zac Sweers
2019-09-12 16:07:41 -04:00
committed by GitHub
parent 71cf745852
commit 489b178a9b

View File

@@ -139,14 +139,13 @@ internal class AdapterGenerator(
} }
result.addProperty(optionsProperty) result.addProperty(optionsProperty)
result.addProperty(constructorProperty)
for (uniqueAdapter in nonTransientProperties.distinctBy { it.delegateKey }) { for (uniqueAdapter in nonTransientProperties.distinctBy { it.delegateKey }) {
result.addProperty(uniqueAdapter.delegateKey.generateProperty( result.addProperty(uniqueAdapter.delegateKey.generateProperty(
nameAllocator, typeRenderer, moshiParam, uniqueAdapter.name)) nameAllocator, typeRenderer, moshiParam, uniqueAdapter.name))
} }
result.addFunction(generateToStringFun()) result.addFunction(generateToStringFun())
result.addFunction(generateFromJsonFun()) result.addFunction(generateFromJsonFun(result))
result.addFunction(generateToJsonFun()) result.addFunction(generateToJsonFun())
return result.build() return result.build()
@@ -172,7 +171,7 @@ internal class AdapterGenerator(
.build() .build()
} }
private fun generateFromJsonFun(): FunSpec { private fun generateFromJsonFun(classBuilder: TypeSpec.Builder): FunSpec {
val result = FunSpec.builder("fromJson") val result = FunSpec.builder("fromJson")
.addModifiers(KModifier.OVERRIDE) .addModifiers(KModifier.OVERRIDE)
.addParameter(readerParam) .addParameter(readerParam)
@@ -240,6 +239,7 @@ internal class AdapterGenerator(
val maskName = nameAllocator.newName("mask") val maskName = nameAllocator.newName("mask")
val localConstructorName = nameAllocator.newName("localConstructor") val localConstructorName = nameAllocator.newName("localConstructor")
if (useDefaultsConstructor) { if (useDefaultsConstructor) {
classBuilder.addProperty(constructorProperty)
// Dynamic default constructor call // Dynamic default constructor call
val booleanArrayBlock = parameterProperties.map { param -> val booleanArrayBlock = parameterProperties.map { param ->
when { when {