Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ISSUES-4 create method invoke all param constructor #7

Merged
merged 1 commit into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading