From 20a1e5d76046b4d910609ef6a53b67cf7e4aa4bd Mon Sep 17 00:00:00 2001 From: jbock Date: Tue, 28 Nov 2023 12:35:37 +0100 Subject: [PATCH] ISSUES-4 dumb constructor --- .../processor/writing/ComponentImpl.java | 41 ++++++++++++------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/compiler/src/main/java/io/jbock/simple/processor/writing/ComponentImpl.java b/compiler/src/main/java/io/jbock/simple/processor/writing/ComponentImpl.java index 2afae01..cfc9545 100644 --- a/compiler/src/main/java/io/jbock/simple/processor/writing/ComponentImpl.java +++ b/compiler/src/main/java/io/jbock/simple/processor/writing/ComponentImpl.java @@ -53,22 +53,13 @@ TypeSpec generate() { TypeSpec.Builder spec = TypeSpec.classBuilder(component.generatedClass()) .addModifiers(modifiers) .addSuperinterface(component.element().asType()); - MethodSpec.Builder constructor = MethodSpec.constructorBuilder().addModifiers(PRIVATE); + MethodSpec constructor = generateConstructor(); for (NamedBinding namedBinding : sorted.values()) { - Binding b = namedBinding.binding(); - Key key = b.key(); - String name = namedBinding.name(); - if (namedBinding.isComponentRequest()) { - FieldSpec field = FieldSpec.builder(key.typeName(), name, PRIVATE, FINAL).build(); - spec.addField(field); - constructor.addStatement("this.$N = $L", field, b.invocation(names)); - } else if (!(b instanceof ParameterBinding)) { - ParameterSpec param = names.apply(key); - constructor.addStatement("$T $N = $L", b.key().typeName(), param, b.invocation(names)); - } - if (b instanceof ParameterBinding) { - constructor.addParameter(names.apply(key)); + if (!namedBinding.isComponentRequest()) { + continue; } + TypeName type = namedBinding.binding().key().typeName(); + spec.addField(FieldSpec.builder(type, namedBinding.name(), PRIVATE, FINAL).build()); } for (DependencyRequest r : component.requests()) { MethodSpec.Builder method = MethodSpec.methodBuilder(r.requestingElement().getSimpleName().toString()); @@ -109,11 +100,31 @@ TypeSpec generate() { .addMember("comments", CodeBlock.of("$S", "https://github.com/jbock-java/simple-component")) .build()); spec.addModifiers(FINAL); - spec.addMethod(constructor.build()); + spec.addMethod(constructor); spec.addOriginatingElement(component.element()); return spec.build(); } + private MethodSpec generateConstructor() { + MethodSpec.Builder constructor = MethodSpec.constructorBuilder().addModifiers(PRIVATE); + for (NamedBinding namedBinding : sorted.values()) { + Binding b = namedBinding.binding(); + Key key = b.key(); + String name = namedBinding.name(); + if (namedBinding.isComponentRequest()) { + FieldSpec field = FieldSpec.builder(key.typeName(), name, PRIVATE, FINAL).build(); + constructor.addStatement("this.$N = $L", field, b.invocation(names)); + } else if (!(b instanceof ParameterBinding)) { + ParameterSpec param = names.apply(key); + constructor.addStatement("$T $N = $L", b.key().typeName(), param, b.invocation(names)); + } + if (b instanceof ParameterBinding) { + constructor.addParameter(names.apply(key)); + } + } + return constructor.build(); + } + private TypeSpec createFactoryImpl(FactoryElement factory) { Collection parameterBindings = component.parameterBindings(); TypeSpec.Builder spec = TypeSpec.classBuilder(factory.generatedClass());