Conversation
Nstd
commented
Aug 6, 2021
- delegate code builder interface, make it easier to be implemented.
- bugfix: when enable [Replace constructor parameters by member variables], the annotation not go to next line.
2. bugfix: when enable [Replace constructor parameters by member variables], the annotation not go to next line.
| append("(").append("\n") | ||
| append(primaryConstructorPropertiesCode) | ||
| append(")") | ||
| fun genPrimaryConstructor(clazz: DataClass): String { |
There was a problem hiding this comment.
Why change to use data class object as input params?
There was a problem hiding this comment.
Why change to use data class object as input params?
Let's talk about the former DataClass.genPrimaryConstructor().
If we use it as extension function, it will be replaced by kotlinDataClassCodeBuilder [abbr Delegator] (the delegator in BaseDataClassCodeBuilder [abbr Base]) .
When we use DataClassCodeBuilderForNoConstructorMemberFields class, the genPrimaryConstructorProperties() method in it will not be invoked. It'is because the Delegator in Base invoke it DataClass.genPrimaryConstructor() method (the executor of genPrimaryConstructor() method is Delagator). And the genPrimaryConstructorProperties() method in DataClass.genPrimaryConstructor() who's executor is same as Delegator.
Let's show an example,
//kotlin
interface A {
fun B.callIn()
fun B.callOut() {
callIn()
}
fun fun(B b) {
b.callOut()
}
}
class C: A {
fun B.callIn() { print("C callIn") }
}
class D(val delegator: A): A by delegator {
fun B.callIn() { print("D callIn") }
}//java
interface A {
void callIn(B b);
void callOut(B b);
public static final class DefaultImpls {
public static void callOut(A $this, B b) {
$this.callIn(b);
}
public static void fun(A $this, B b) {
$this.callOut(b);
}
}
}
class C implements A {
public void callIn(B b) { System.out.print("C callIn"); }
public void callOut(B b) {
DefaultImpls.callOut(this, b);
}
public void fun(B b) {
DefaultImpls.fun(this, b);
}
}
class D implements A {
A delegator;
public D(A delegator) { this.delegator = delegator; }
public void callIn(B b) { System.out.print("B callIn"); }
public void callOut(B b) {
this.delegator.callOut(b); //the executor is delegator
}
public void fun(B b) {
DefaultImpls.fun(this, b); //invoke this.callOut(b), but the delegator intercept callOut() method
}
}