Skip to content

Commit

Permalink
ISSUES-4 create method invoke all param constructor (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
h908714124 authored Nov 28, 2023
1 parent f818156 commit 1f74588
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,7 @@ TypeSpec generate() {
spec.addType(createBuilderImpl(builder));
});
if (component.factoryElement().isEmpty() && component.builderElement().isEmpty()) {
spec.addMethod(MethodSpec.methodBuilder("create")
.addModifiers(STATIC)
.addModifiers(modifiers)
.returns(TypeName.get(component.element().asType()))
.addStatement("return new $T()", component.generatedClass())
.build());
spec.addMethod(generateCreateMethod(modifiers));
}
spec.addAnnotation(AnnotationSpec.builder(Generated.class)
.addMember("value", CodeBlock.of("$S", SimpleComponentProcessor.class.getCanonicalName()))
Expand All @@ -103,6 +98,29 @@ TypeSpec generate() {
return spec.build();
}

private MethodSpec generateCreateMethod(Modifier[] modifiers) {
List<CodeBlock> constructorParameters = new ArrayList<>();
MethodSpec.Builder method = MethodSpec.methodBuilder("create");
for (NamedBinding namedBinding : sorted.values()) {
Binding b = namedBinding.binding();
Key key = b.key();
CodeBlock invocation = b.invocation(names);
ParameterSpec param = names.apply(key);
if (namedBinding.isComponentRequest()) {
constructorParameters.add(CodeBlock.of("$N", names.apply(key)));
}
method.addStatement("$T $N = $L", key.typeName(), param, invocation);
}
return method
.addModifiers(STATIC)
.addModifiers(modifiers)
.returns(TypeName.get(component.element().asType()))
.addStatement("return new $T($L)",
component.generatedClass(),
constructorParameters.stream().collect(CodeBlock.joining(", ")))
.build();
}

private MethodSpec generateConstructor() {
MethodSpec.Builder constructor = MethodSpec.constructorBuilder().addModifiers(PRIVATE);
for (NamedBinding namedBinding : sorted.values()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ void staticMethodBindings() {
" }",
"",
" public static TestClass.AComponent create() {",
" return new TestClass_AComponent_Impl();",
" TestClass.C c = TestClass.C.createC();",
" TestClass.B b = TestClass.B.createB(c);",
" TestClass.A a = new TestClass.A(b);",
" return new TestClass_AComponent_Impl(a);",
" }",
"}");
}
Expand Down Expand Up @@ -125,7 +128,11 @@ void dependencyDiamond() {
" }",
"",
" static TestClass.AComponent create() {",
" return new TestClass_AComponent_Impl();",
" TestClass.E e = new TestClass.E();",
" TestClass.C c = new TestClass.C(e);",
" TestClass.B b = new TestClass.B(e);",
" TestClass.A a = new TestClass.A(b, c);",
" return new TestClass_AComponent_Impl(a);",
" }",
"}");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ void clashResolvedByQualifiers() {
" }",
"",
" static TestClass.AComponent create() {",
" return new TestClass_AComponent_Impl();",
" TestClass.B b = TestClass.B.create1();",
" TestClass.B b2 = TestClass.B.create2();",
" TestClass.A a = new TestClass.A(b, b2);",
" return new TestClass_AComponent_Impl(a);",
" }",
"}");
}
Expand Down

0 comments on commit 1f74588

Please sign in to comment.