mirror of
https://github.com/apple/pkl.git
synced 2026-06-17 19:09:41 +02:00
codegen-java: Change condition for generating equals/hashCode/toString/with/Serializable (#710)
Motivation:
- Currently, the condition for generating equals/hashCode/toString methods is "generated class declares properties".
As a result, classes that don't declare properties inherit these methods from their generated superclasses.
However, generated `equals` and `toString` methods aren't designed to be inherited
and will either fail or produce wrong results when called for a subclass.
- Currently, the condition for generating `with` methods is "class is not abstract".
However, it isn't useful to generate `with` methods for non-instantiable non-abstract classes.
- Currently, the condition for making classes serializable is "class is not a module class".
However, it isn't useful to make non-instantiable non-module classes serializable,
and it is useful to make instantiable module classes serializable.
Changes:
- Change condition for generating equals/hashCode/toString/with/Serializable to "class is instantiable".
This is a breaking change.
(A generated class is instantiable, i.e., declares a public constructor,
if it is neither abstract nor stateless. This behavior remains unchanged for now.)
- Overhaul JavaCodeGeneratorTest
- introduce classes JavaSourceCode and JavaSourceCodeAssert
- change assertions to use JavaSourceCodeAssert via `assertThat(javaCode)`
- use parameterized test instead of loop
- use explicit trimIndent() and trimMargin() for multiline string literals
- IntelliJ editor desperately wants to insert trimIndent()
- can potentially be exploited by kotlinc and ktfmt
Result:
- Fixes all motivating issues.
- Fixes #706.
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -32,13 +32,23 @@ public final class Mod {
|
||||
public long getOne() {
|
||||
return one;
|
||||
}
|
||||
}
|
||||
|
||||
public static class None extends Foo {
|
||||
public None(@Named("one") long one) {
|
||||
super(one);
|
||||
}
|
||||
|
||||
public None withOne(long one) {
|
||||
return new None(one);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) return true;
|
||||
if (obj == null) return false;
|
||||
if (this.getClass() != obj.getClass()) return false;
|
||||
Foo other = (Foo) obj;
|
||||
None other = (None) obj;
|
||||
if (!Objects.equals(this.one, other.one)) return false;
|
||||
return true;
|
||||
}
|
||||
@@ -53,23 +63,13 @@ public final class Mod {
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder builder = new StringBuilder(100);
|
||||
builder.append(Foo.class.getSimpleName()).append(" {");
|
||||
builder.append(None.class.getSimpleName()).append(" {");
|
||||
appendProperty(builder, "one", this.one);
|
||||
builder.append("\n}");
|
||||
return builder.toString();
|
||||
}
|
||||
}
|
||||
|
||||
public static class None extends Foo {
|
||||
public None(@Named("one") long one) {
|
||||
super(one);
|
||||
}
|
||||
|
||||
public None withOne(long one) {
|
||||
return new None(one);
|
||||
}
|
||||
}
|
||||
|
||||
public static class Bar extends None {
|
||||
protected final String two;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user