Skip to content

optimize code builder & bugfix: when enable [Replace constructor parameters by member variables], the annotation not go to next line.#373

Merged
wuseal merged 2 commits intomasterfrom
optimize/reduce-the-sub-class-complexity-of-code-builder
Aug 7, 2021
Merged

optimize code builder & bugfix: when enable [Replace constructor parameters by member variables], the annotation not go to next line.#373
wuseal merged 2 commits intomasterfrom
optimize/reduce-the-sub-class-complexity-of-code-builder

Conversation

@Nstd
Copy link
Copy Markdown
Collaborator

@Nstd Nstd commented Aug 6, 2021

  1. delegate code builder interface, make it easier to be implemented.
  2. 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.
@Nstd Nstd requested a review from wuseal August 6, 2021 09:15
append("(").append("\n")
append(primaryConstructorPropertiesCode)
append(")")
fun genPrimaryConstructor(clazz: DataClass): String {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why change to use data class object as input params?

Copy link
Copy Markdown
Collaborator Author

@Nstd Nstd Aug 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
    }
}

Copy link
Copy Markdown
Collaborator Author

@Nstd Nstd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's more clear.

@wuseal wuseal self-requested a review August 7, 2021 14:37
Copy link
Copy Markdown
Owner

@wuseal wuseal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@wuseal wuseal changed the title optimize code builder & bugfix optimize code builder & bugfix: when enable [Replace constructor parameters by member variables], the annotation not go to next line. Aug 7, 2021
@wuseal wuseal merged commit 6d5c9aa into master Aug 7, 2021
wuseal added a commit that referenced this pull request Sep 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants