Suppress common warnings in generated classes (#1028)

* Suppress common warnings in generated classes

Followup from #1023 (Resolves #1023 too). Since we can't do targeted deprecation suppressions, we can just stick with blanket deprecations suppressions for now. This also suppresses a couple other common issues with all our generated adapters.

Have just eyeballed the results for now, can add a proper test as a followup once #1014 is in

* Add redundant projection and re-enable -Werror

* Add tests for deprecation suppression

With -Werror enabled now we can actually check this
This commit is contained in:
Zac Sweers
2019-11-17 09:40:49 -05:00
committed by Jesse Wilson
parent f7dbde2114
commit f49f677bfc
4 changed files with 40 additions and 9 deletions

View File

@@ -57,7 +57,22 @@ internal class AdapterGenerator(
companion object { companion object {
private val INT_TYPE_BLOCK = CodeBlock.of("%T::class.javaPrimitiveType", INT) private val INT_TYPE_BLOCK = CodeBlock.of("%T::class.javaPrimitiveType", INT)
private val DEFAULT_CONSTRUCTOR_MARKER_TYPE_BLOCK = CodeBlock.of("%T.DEFAULT_CONSTRUCTOR_MARKER", Util::class) private val DEFAULT_CONSTRUCTOR_MARKER_TYPE_BLOCK = CodeBlock.of(
"%T.DEFAULT_CONSTRUCTOR_MARKER", Util::class)
private val COMMON_SUPPRESS = AnnotationSpec.builder(Suppress::class)
.addMember("%S, %S, %S, %S",
// https://github.com/square/moshi/issues/1023
"DEPRECATION",
// Because we look it up reflectively
"unused",
// Because we include underscores
"ClassName",
// Because we generate redundant `out` variance for some generics and there's no way
// for us to know when it's redundant.
"REDUNDANT_PROJECTION"
)
.build()
} }
private val nonTransientProperties = propertyList.filterNot { it.isTransient } private val nonTransientProperties = propertyList.filterNot { it.isTransient }
@@ -126,6 +141,7 @@ internal class AdapterGenerator(
private fun generateType(): TypeSpec { private fun generateType(): TypeSpec {
val result = TypeSpec.classBuilder(adapterName) val result = TypeSpec.classBuilder(adapterName)
.addAnnotation(COMMON_SUPPRESS)
result.superclass(jsonAdapterTypeName) result.superclass(jsonAdapterTypeName)
@@ -304,7 +320,8 @@ internal class AdapterGenerator(
if (property.hasConstructorDefault) { if (property.hasConstructorDefault) {
val inverted = (1 shl maskIndex).inv() val inverted = (1 shl maskIndex).inv()
result.addComment("\$mask = \$mask and (1 shl %L).inv()", maskIndex) result.addComment("\$mask = \$mask and (1 shl %L).inv()", maskIndex)
result.addStatement("%1L = %1L and 0x%2L.toInt()", maskNames[maskNameIndex], Integer.toHexString(inverted)) result.addStatement("%1L = %1L and 0x%2L.toInt()", maskNames[maskNameIndex],
Integer.toHexString(inverted))
} else { } else {
// Presence tracker for a mutable property // Presence tracker for a mutable property
result.addStatement("%N = true", property.localIsPresentName) result.addStatement("%N = true", property.localIsPresentName)
@@ -477,6 +494,7 @@ private interface PropertyComponent {
val property: PropertyGenerator val property: PropertyGenerator
val type: TypeName val type: TypeName
} }
private interface ParameterComponent { private interface ParameterComponent {
val parameter: TargetParameter val parameter: TargetParameter
val type: TypeName val type: TypeName
@@ -503,8 +521,8 @@ private sealed class FromJsonComponent {
} }
data class ParameterProperty( data class ParameterProperty(
override val parameter: TargetParameter, override val parameter: TargetParameter,
override val property: PropertyGenerator override val property: PropertyGenerator
) : FromJsonComponent(), ParameterComponent, PropertyComponent { ) : FromJsonComponent(), ParameterComponent, PropertyComponent {
override val type: TypeName = parameter.type override val type: TypeName = parameter.type
} }

View File

@@ -115,11 +115,7 @@
</executions> </executions>
<configuration> <configuration>
<args> <args>
<!-- <arg>-Werror</arg>
Disabled for now because we generate redundant `out` variance for some generics,
but there's no way for us to know when it's redundant.
-->
<!-- <arg>-Werror</arg>-->
<arg>-Xuse-experimental=kotlin.ExperimentalStdlibApi</arg> <arg>-Xuse-experimental=kotlin.ExperimentalStdlibApi</arg>
<arg>-XXLanguage:+InlineClasses</arg> <arg>-XXLanguage:+InlineClasses</arg>
</args> </args>

View File

@@ -513,6 +513,7 @@ class DualKotlinTest(useReflection: Boolean) {
assertThat(result).isEqualTo(instance) assertThat(result).isEqualTo(instance)
} }
@Suppress("REDUNDANT_NULLABLE")
@JsonClass(generateAdapter = true) @JsonClass(generateAdapter = true)
data class TypeAliasNullability( data class TypeAliasNullability(
val aShouldBeNonNull: A, val aShouldBeNonNull: A,

View File

@@ -1169,6 +1169,22 @@ class GeneratedAdaptersTest {
val instance = adapter.fromJson("""{"_links": "link", "_ids": "id" }""")!! val instance = adapter.fromJson("""{"_links": "link", "_ids": "id" }""")!!
assertThat(instance).isEqualTo(ClassWithFieldJson("link").apply { ids = "id" }) assertThat(instance).isEqualTo(ClassWithFieldJson("link").apply { ids = "id" })
} }
/*
* These are a smoke test for https://github.com/square/moshi/issues/1023 to ensure that we
* suppress deprecation warnings for using deprecated properties or classes.
*
* Ideally when stubs are fixed to actually included Deprecated annotations, we could then only
* generate a deprecation suppression as needed and on targeted usages.
* https://youtrack.jetbrains.com/issue/KT-34951
*/
@Deprecated("Deprecated for reasons")
@JsonClass(generateAdapter = true)
data class DeprecatedClass(val foo: String)
@JsonClass(generateAdapter = true)
data class DeprecatedProperty(@Deprecated("Deprecated for reasons") val foo: String)
} }
// Regression test for https://github.com/square/moshi/issues/1022 // Regression test for https://github.com/square/moshi/issues/1022