Skip to content

Commit

Permalink
Merge pull request #5 from jbock-java/4-dumb-constructor
Browse files Browse the repository at this point in the history
ISSUES-4 dumb constructor
  • Loading branch information
h908714124 authored Nov 28, 2023
2 parents 30440c6 + 20a1e5d commit 0471110
Showing 1 changed file with 26 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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<ParameterBinding> parameterBindings = component.parameterBindings();
TypeSpec.Builder spec = TypeSpec.classBuilder(factory.generatedClass());
Expand Down

0 comments on commit 0471110

Please sign in to comment.