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 mockbuilder instance in builder #13

Merged
merged 1 commit into from
Nov 30, 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 @@ -41,9 +41,18 @@ private BuilderImpl(
this.names = names;
}

TypeSpec generate(BuilderElement builder) {
TypeSpec generate(BuilderElement builder, MockBuilder mockBuilder) {
TypeMirror builderType = builder.element().asType();
TypeSpec.Builder spec = TypeSpec.classBuilder(builder.generatedClass());
if (!component.omitMockBuilder()) {
FieldSpec field = FieldSpec.builder(mockBuilder.getClassName(), "mockBuilder", FINAL).build();
spec.addField(field);
ParameterSpec param = ParameterSpec.builder(mockBuilder.getClassName(), "mockBuilder").build();
spec.addMethod(MethodSpec.constructorBuilder()
.addParameter(param)
.addStatement("this.$N = $N", field, param)
.build());
}
MethodSpec.Builder buildMethod = MethodSpec.methodBuilder(builder.buildMethod().getSimpleName().toString());
List<CodeBlock> constructorParameters = new ArrayList<>();
for (NamedBinding namedBinding : sorted.values()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
import io.jbock.simple.Inject;
import io.jbock.simple.processor.SimpleComponentProcessor;
import io.jbock.simple.processor.binding.Binding;
import io.jbock.simple.processor.binding.BuilderElement;
import io.jbock.simple.processor.binding.ComponentElement;
import io.jbock.simple.processor.binding.DependencyRequest;
import io.jbock.simple.processor.binding.FactoryElement;
import io.jbock.simple.processor.binding.Key;

import javax.annotation.processing.Generated;
Expand Down Expand Up @@ -61,6 +63,7 @@ private ComponentImpl(
TypeSpec generate() {
TypeSpec.Builder spec = TypeSpec.classBuilder(component.generatedClass())
.addModifiers(modifiers)
.addModifiers(FINAL)
.addSuperinterface(component.element().asType());
spec.addFields(getFields());
for (DependencyRequest r : component.requests()) {
Expand All @@ -72,42 +75,46 @@ TypeSpec generate() {
spec.addMethod(method.build());
}
component.factoryElement().ifPresent(factory -> {
spec.addMethod(MethodSpec.methodBuilder(FACTORY_METHOD)
.addModifiers(STATIC)
.addModifiers(modifiers)
.returns(TypeName.get(factory.element().asType()))
.addStatement("return new $T()", factory.generatedClass())
.build());
spec.addMethod(generateFactoryMethod(factory));
spec.addType(factoryImpl.generate(factory));
});
component.builderElement().ifPresent(builder -> {
spec.addMethod(MethodSpec.methodBuilder(BUILDER_METHOD)
.addModifiers(STATIC)
.addModifiers(modifiers)
.returns(TypeName.get(builder.element().asType()))
.addStatement("return new $T()", builder.generatedClass())
.build());
spec.addType(builderImpl.generate(builder));
spec.addMethod(generateBuilderMethod(builder));
spec.addType(builderImpl.generate(builder, mockBuilder));
});
if (component.factoryElement().isEmpty() && component.builderElement().isEmpty()) {
spec.addMethod(generateCreateMethod());
if (!component.omitMockBuilder()) {
spec.addMethod(generateMockBuilderMethod());
}
}
if (!component.omitMockBuilder()) {
spec.addMethod(generateMockBuilderMethod());
spec.addType(mockBuilder.generate());
}
spec.addAnnotation(AnnotationSpec.builder(Generated.class)
.addMember("value", CodeBlock.of("$S", SimpleComponentProcessor.class.getCanonicalName()))
.addMember("comments", CodeBlock.of("$S", "https://github.com/jbock-java/simple-component"))
.build());
spec.addModifiers(FINAL);
spec.addMethod(generateAllParametersConstructor());
spec.addOriginatingElement(component.element());
return spec.build();
}

private MethodSpec generateFactoryMethod(FactoryElement factory) {
return MethodSpec.methodBuilder(FACTORY_METHOD)
.addModifiers(STATIC)
.addModifiers(modifiers)
.returns(TypeName.get(factory.element().asType()))
.addStatement("return new $T()", factory.generatedClass())
.build();
}

private MethodSpec generateBuilderMethod(BuilderElement builder) {
return MethodSpec.methodBuilder(BUILDER_METHOD)
.addModifiers(STATIC)
.addModifiers(modifiers)
.addStatement("return new $T(null)", builder.generatedClass())
.returns(TypeName.get(builder.element().asType())).build();
}

private MethodSpec generateCreateMethod() {
List<CodeBlock> constructorParameters = new ArrayList<>();
MethodSpec.Builder method = MethodSpec.methodBuilder(CREATE_METHOD);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public TypeSpec generate(List<Binding> bindings) {

private Map<Key, NamedBinding> addNames(List<Binding> bindings) {
UniqueNameSet uniqueNameSet = new UniqueNameSet();
uniqueNameSet.claim("mockBuilder");
uniqueNameSet.claim("build");
Map<Key, NamedBinding> result = new LinkedHashMap<>();
for (Binding b : bindings) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,7 @@ TypeSpec generate() {
}

private MethodSpec generateConstructor() {
MethodSpec.Builder constructor = MethodSpec.constructorBuilder().addModifiers(PRIVATE);
for (NamedBinding namedBinding : sorted.values()) {
if (!(namedBinding.binding() instanceof ParameterBinding)) {
continue;
}
ParameterSpec param = names.apply(namedBinding.binding().key());
constructor.addParameter(param);
constructor.addStatement("this.$1N = $1N", param);
}
return constructor.build();
return MethodSpec.constructorBuilder().addModifiers(PRIVATE).build();
}

ClassName getClassName() {
Expand Down Expand Up @@ -102,11 +93,10 @@ private MethodSpec buildMethod() {
private List<FieldSpec> getFields() {
List<FieldSpec> fields = new ArrayList<>();
for (NamedBinding namedBinding : sorted.values()) {
TypeName type = namedBinding.binding().key().typeName();
if (namedBinding.binding() instanceof ParameterBinding) {
fields.add(FieldSpec.builder(type, namedBinding.name(), PRIVATE, FINAL).build());
continue;
}
TypeName type = namedBinding.binding().key().typeName();
FieldSpec field = FieldSpec.builder(type, namedBinding.name(), PRIVATE).build();
fields.add(field);
if (namedBinding.binding().key().typeName().isPrimitive()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void noParameters() {
" }",
"",
" static TestClass.AComponent.Builder builder() {",
" return new Builder_Impl();",
" return new Builder_Impl(null);",
" }",
"",
" private static final class Builder_Impl implements TestClass.AComponent.Builder {",
Expand Down Expand Up @@ -107,7 +107,7 @@ void builderParameterIdentity() {
" }",
"",
" public static TestClass.AComponent.Builder builder() {",
" return new Builder_Impl();",
" return new Builder_Impl(null);",
" }",
"",
" private static final class Builder_Impl implements TestClass.AComponent.Builder {",
Expand Down Expand Up @@ -172,7 +172,7 @@ void builderParameter() {
" }",
"",
" public static TestClass.AComponent.Builder builder() {",
" return new Builder_Impl();",
" return new Builder_Impl(null);",
" }",
"",
" private static final class Builder_Impl implements TestClass.AComponent.Builder {",
Expand Down
Loading