codegen-kotlin: Don't generate equals/hashCode/toString/copy/:Serializable for abstract classes (#714)

* codegen-kotlin: Don't generate equals/hashCode/toString/copy/:Serializable for abstract classes

* Polish KotlinCodeGeneratorTest

- use `assertContains()` instead of `assertThat().contains()`
- remove redundant `trimIndent()` and `trimMargin()` calls
- rename `javaCode` to `kotlinCode`

* Overhaul KotlinCodeGeneratorTest

- introduce classes KotlinSourceCode and KotlinSourceCodeAssert
- change assertions to use KotlinSourceCodeAssert via `assertThat(kotlinCode)`
- use explicit trimIndent() and trimMargin() for multiline string literals
  - IntelliJ editor desperately wants to insert trimIndent()
  - can be used by kotlinc and ktfmt
- format code in the exact way it falls out of the IntelliJ editor
  (successfully destroyed by ktfmt)
This commit is contained in:
translatenix
2024-10-23 21:31:08 -07:00
committed by GitHub
parent f9fe226eba
commit 730257861f
2 changed files with 853 additions and 771 deletions

View File

@@ -485,12 +485,13 @@ class KotlinCodeGenerator(
builder.addProperty(generateProperty(name, property))
}
generateCopyMethods(builder)
builder
.addFunction(generateEqualsMethod())
.addFunction(generateHashCodeMethod())
.addFunction(generateToStringMethod())
if (!pClass.isAbstract) {
generateCopyMethods(builder)
builder
.addFunction(generateEqualsMethod())
.addFunction(generateHashCodeMethod())
.addFunction(generateToStringMethod())
}
return builder
}
@@ -530,7 +531,7 @@ class KotlinCodeGenerator(
}
private fun TypeSpec.Builder.ensureSerializable(): TypeSpec.Builder {
if (!options.implementSerializable) {
if (!options.implementSerializable || modifiers.contains(KModifier.ABSTRACT)) {
return this
}