Skip to content

Commit

Permalink
ISSUES-4 factory use mockBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
h908714124 committed Nov 30, 2023
1 parent b9a62fb commit 0597cb3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,32 +62,39 @@ ClassName getClassName() {
}

private MethodSpec buildMethod() {
List<CodeBlock> constructorParameters = new ArrayList<>();
MethodSpec.Builder method = MethodSpec.methodBuilder("build");
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)));
}
if (b instanceof ParameterBinding) {
method.addParameter(names.apply(b.key()));
} else if (!key.typeName().isPrimitive()) {
method.addStatement("$1T $2N = this.$2N != null ? this.$2N : $3L", key.typeName(), param, invocation);
} else {
FieldSpec auxField = FieldSpec.builder(TypeName.BOOLEAN, namedBinding.auxName(), PRIVATE).build();
method.addStatement("$1T $2N = this.$3N ? this.$2N : $4L", key.typeName(), param, auxField, invocation);
method.addModifiers(modifiers);
component.factoryElement().ifPresent(factory -> {
method.returns(TypeName.get(factory.element().asType()));
method.addStatement("return new $T(this)", factory.generatedClass());
});
component.builderElement().ifPresent(builder -> {
method.returns(TypeName.get(builder.element().asType()));
method.addStatement("return new $T(this)", builder.generatedClass());
});
if (component.factoryElement().isEmpty() && component.builderElement().isEmpty()) {
method.returns(TypeName.get(component.element().asType()));
List<CodeBlock> constructorParameters = new ArrayList<>();
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)));
}
if (!key.typeName().isPrimitive()) {
method.addStatement("$1T $2N = this.$2N != null ? this.$2N : $3L", key.typeName(), param, invocation);
} else {
FieldSpec auxField = FieldSpec.builder(TypeName.BOOLEAN, namedBinding.auxName(), PRIVATE).build();
method.addStatement("$1T $2N = this.$3N ? this.$2N : $4L", key.typeName(), param, auxField, invocation);
}
}
method.addStatement("return new $T($L)",
component.generatedClass(),
constructorParameters.stream().collect(CodeBlock.joining(", ")));
}
return method
.addModifiers(modifiers)
.returns(TypeName.get(component.element().asType()))
.addStatement("return new $T($L)",
component.generatedClass(),
constructorParameters.stream().collect(CodeBlock.joining(", ")))
.build();
return method.build();
}

private List<FieldSpec> getFields() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ void qualifiedIdentity() {
" }",
"",
" static final class MockBuilder {",
" TestClass.AComponent build(String a, String b) {",
" return new TestClass_AComponent_Impl(a);",
" TestClass.AComponent.Factory build() {",
" return new Factory_Impl(this);",
" }",
" }",
"}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,8 @@ void providesPrimitive() {
" private boolean b_isSet;",
" private TestClass.A a;",
"",
" TestClass.AComponent build(int i) {",
" int b = this.b_isSet ? this.b : TestClass.AComponent.getB(i);",
" TestClass.A a = this.a != null ? this.a : new TestClass.A(b);",
" return new TestClass_AComponent_Impl(a);",
" TestClass.AComponent.Factory build() {",
" return new Factory_Impl(this);",
" }",
"",
" void b(int b) {",
Expand Down

0 comments on commit 0597cb3

Please sign in to comment.