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-36 remove mock logic from builder and factory #40

Merged
merged 1 commit into from
Dec 11, 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 @@ -21,7 +21,6 @@
import java.util.stream.Collectors;

import static javax.lang.model.element.Modifier.FINAL;
import static javax.lang.model.element.Modifier.PRIVATE;
import static javax.lang.model.element.Modifier.PROTECTED;
import static javax.lang.model.element.Modifier.PUBLIC;
import static javax.lang.model.element.Modifier.STATIC;
Expand All @@ -41,49 +40,29 @@ private BuilderImpl(
this.names = names;
}

TypeSpec generate(BuilderElement builder, MockBuilder mockBuilder, MockBuilder2 mockBuilder2) {
if (component.mockBuilder()) {
return generateMock(builder, mockBuilder, mockBuilder2);
} else {
return generateNoMock(builder);
}
}

private TypeSpec generateMock(BuilderElement builder, MockBuilder mockBuilder, MockBuilder2 mockBuilder2) {
TypeSpec generate(BuilderElement builder, MockBuilder2 mockBuilder2) {
TypeMirror builderType = builder.element().asType();
TypeSpec.Builder spec = TypeSpec.classBuilder(builder.generatedClass());
FieldSpec mockBuilderField = FieldSpec.builder(mockBuilder.getClassName(), "mockBuilder", FINAL).build();
spec.addField(mockBuilderField);
ParameterSpec mockBuilderParam = ParameterSpec.builder(mockBuilder.getClassName(), "mockBuilder").build();
spec.addMethod(MethodSpec.constructorBuilder()
.addParameter(mockBuilderParam)
.addStatement("this.$N = $N", mockBuilderField, mockBuilderParam)
.build());
spec.addMethod(generateBuildMethod(builder, mockBuilderField));
spec.addMethod(generateWithMocksMethod(mockBuilder2));
spec.addFields(fields());
spec.addMethods(setterMethods(builder));
if (component.mockBuilder()) {
spec.addMethod(generateWithMocksMethod(mockBuilder2));
}
spec.addModifiers(PUBLIC, STATIC, FINAL);
spec.addSuperinterface(builder.element().asType());
spec.addSuperinterface(builderType);
spec.addMethod(generateBuildMethod(builder));
return spec.build();
}

private MethodSpec generateBuildMethod(BuilderElement builder, FieldSpec mockBuilderField) {
private MethodSpec generateBuildMethod(BuilderElement builder) {
MethodSpec.Builder buildMethod = MethodSpec.methodBuilder(builder.buildMethod().getSimpleName().toString());
for (NamedBinding namedBinding : sorted.values()) {
Binding b = namedBinding.binding();
if (b instanceof ParameterBinding) {
continue;
}
Key key = b.key();
CodeBlock invocation = b.invocation(names, true, sorted);
CodeBlock invocation = b.invocation(names);
ParameterSpec param = names.apply(key);
if (!key.typeName().isPrimitive()) {
buildMethod.addStatement("$1T $2N = this.$3N != null && this.$3N.$2N != null ? this.$3N.$2N : $4L",
key.typeName(), param, mockBuilderField, invocation);
} else {
FieldSpec auxField = FieldSpec.builder(TypeName.BOOLEAN, namedBinding.auxName(), PRIVATE).build();
buildMethod.addStatement("$1T $2N = this.$3N != null && this.$3N.$4N ? this.$3N.$2N : $5L",
key.typeName(), param, mockBuilderField, auxField, invocation);
if (!(b instanceof ParameterBinding)) {
buildMethod.addStatement("$T $N = $L", key.typeName(), param, invocation);
}
}
buildMethod.addAnnotation(Override.class);
Expand Down Expand Up @@ -115,33 +94,6 @@ private MethodSpec generateWithMocksMethod(MockBuilder2 mockBuilder2) {
return method.build();
}

private TypeSpec generateNoMock(BuilderElement builder) {
TypeMirror builderType = builder.element().asType();
TypeSpec.Builder spec = TypeSpec.classBuilder(builder.generatedClass());
MethodSpec.Builder buildMethod = MethodSpec.methodBuilder(builder.buildMethod().getSimpleName().toString());
for (NamedBinding namedBinding : sorted.values()) {
Binding b = namedBinding.binding();
Key key = b.key();
CodeBlock invocation = b.invocation(names);
ParameterSpec param = names.apply(key);
if (!(b instanceof ParameterBinding)) {
buildMethod.addStatement("$T $N = $L", key.typeName(), param, invocation);
}
}
spec.addFields(fields());
spec.addMethods(setterMethods(builder));
spec.addModifiers(PUBLIC, STATIC, FINAL);
spec.addSuperinterface(builderType);
buildMethod.addAnnotation(Override.class);
buildMethod.addModifiers(builder.buildMethod().getModifiers().stream()
.filter(m -> m == PUBLIC || m == PROTECTED).collect(Collectors.toList()));
buildMethod.returns(TypeName.get(component.element().asType()));
buildMethod.addStatement("return new $T($L)", component.generatedClass(), constructorParameters().stream()
.collect(CodeBlock.joining(", ")));
spec.addMethod(buildMethod.build());
return spec.build();
}

private List<FieldSpec> fields() {
List<FieldSpec> result = new ArrayList<>();
for (NamedBinding namedBinding : sorted.values()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import io.jbock.simple.processor.binding.DependencyRequest;
import io.jbock.simple.processor.binding.FactoryElement;
import io.jbock.simple.processor.binding.Key;
import io.jbock.simple.processor.binding.ParameterBinding;

import javax.annotation.processing.Generated;
import javax.lang.model.element.Modifier;
Expand All @@ -39,7 +40,6 @@ public class ComponentImpl {
private final ComponentElement component;
private final Map<Key, NamedBinding> sorted;
private final Function<Key, ParameterSpec> names;
private final MockBuilder mockBuilder;
private final MockBuilder2 mockBuilder2;
private final BuilderImpl builderImpl;
private final FactoryImpl factoryImpl;
Expand All @@ -49,14 +49,12 @@ private ComponentImpl(
ComponentElement component,
Map<Key, NamedBinding> sorted,
Function<Key, ParameterSpec> names,
MockBuilder mockBuilder,
MockBuilder2 mockBuilder2,
BuilderImpl builderImpl,
FactoryImpl factoryImpl) {
this.component = component;
this.sorted = sorted;
this.names = names;
this.mockBuilder = mockBuilder;
this.modifiers = component.element().getModifiers().stream()
.filter(m -> m == PUBLIC).toArray(Modifier[]::new);
this.mockBuilder2 = mockBuilder2;
Expand All @@ -80,11 +78,14 @@ TypeSpec generate() {
}
component.factoryElement().ifPresent(factory -> {
spec.addMethod(generateFactoryMethod(factory));
spec.addType(factoryImpl.generate(factory, mockBuilder, mockBuilder2));
spec.addType(factoryImpl.generate(factory));
if (component.mockBuilder()) {
spec.addMethod(generateMockBuilderMethodFactory());
}
});
component.builderElement().ifPresent(builder -> {
spec.addMethod(generateBuilderMethod(builder));
spec.addType(builderImpl.generate(builder, mockBuilder, mockBuilder2));
spec.addType(builderImpl.generate(builder, mockBuilder2));
});
if (component.factoryElement().isEmpty() && component.builderElement().isEmpty()) {
spec.addMethod(generateCreateMethod());
Expand All @@ -93,7 +94,6 @@ TypeSpec generate() {
}
}
if (component.mockBuilder()) {
spec.addType(mockBuilder.generate());
spec.addType(mockBuilder2.generate());
}
spec.addAnnotation(AnnotationSpec.builder(Generated.class)
Expand All @@ -115,11 +115,7 @@ private MethodSpec generateFactoryMethod(FactoryElement factory) {
.addModifiers(STATIC)
.addModifiers(modifiers)
.returns(factory.generatedClass());
if (component.mockBuilder()) {
spec.addStatement("return new $T(null)", factory.generatedClass());
} else {
spec.addStatement("return new $T()", factory.generatedClass());
}
spec.addStatement("return new $T()", factory.generatedClass());
return spec.build();
}

Expand All @@ -128,11 +124,7 @@ private MethodSpec generateBuilderMethod(BuilderElement builder) {
.addModifiers(STATIC)
.addModifiers(modifiers)
.returns(builder.generatedClass());
if (component.mockBuilder()) {
spec.addStatement("return new $T(null)", builder.generatedClass());
} else {
spec.addStatement("return new $T()", builder.generatedClass());
}
spec.addStatement("return new $T()", builder.generatedClass());
return spec.build();
}

Expand Down Expand Up @@ -171,6 +163,27 @@ MethodSpec generateMockBuilderMethod() {
return method.build();
}

MethodSpec generateMockBuilderMethodFactory() {
MethodSpec.Builder method = MethodSpec.methodBuilder(MOCK_BUILDER_METHOD);
List<CodeBlock> constructorParameters = new ArrayList<>();
for (NamedBinding namedBinding : sorted.values()) {
Binding b = namedBinding.binding();
if (!(b instanceof ParameterBinding)) {
continue;
}
ParameterSpec param = names.apply(b.key());
constructorParameters.add(CodeBlock.of("$N", param));
}
if (component.publicMockBuilder()) {
method.addModifiers(PUBLIC);
}
method.addParameters(factoryImpl.parameters());
method.returns(mockBuilder2.getClassName());
method.addStatement("return new $T($L)", mockBuilder2.getClassName(),
constructorParameters.stream().collect(CodeBlock.joining(", ")));
return method.build();
}

private List<FieldSpec> getFields() {
List<FieldSpec> fields = new ArrayList<>();
for (NamedBinding namedBinding : sorted.values()) {
Expand Down Expand Up @@ -199,21 +212,18 @@ private MethodSpec generateAllParametersConstructor() {

public static final class Factory {
private final ComponentElement component;
private final MockBuilder.Factory mockBuilderFactory;
private final MockBuilder2.Factory mockBuilder2Factory;
private final MockBuilder2.Factory mockBuilderFactory;
private final BuilderImpl.Factory builderImplFactory;
private final FactoryImpl.Factory factoryImplFactory;

@Inject
public Factory(
ComponentElement component,
MockBuilder.Factory mockBuilderFactory,
MockBuilder2.Factory mockBuilder2Factory,
MockBuilder2.Factory mockBuilderFactory,
BuilderImpl.Factory builderImplFactory,
FactoryImpl.Factory factoryImplFactory) {
this.component = component;
this.mockBuilderFactory = mockBuilderFactory;
this.mockBuilder2Factory = mockBuilder2Factory;
this.builderImplFactory = builderImplFactory;
this.factoryImplFactory = factoryImplFactory;
}
Expand All @@ -226,7 +236,6 @@ ComponentImpl create(
sorted,
names,
mockBuilderFactory.create(sorted, names),
mockBuilder2Factory.create(sorted, names),
builderImplFactory.create(sorted, names),
factoryImplFactory.create(sorted, names));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.jbock.simple.processor.writing;

import io.jbock.javapoet.CodeBlock;
import io.jbock.javapoet.FieldSpec;
import io.jbock.javapoet.MethodSpec;
import io.jbock.javapoet.ParameterSpec;
import io.jbock.javapoet.TypeName;
Expand Down Expand Up @@ -41,82 +40,7 @@ private FactoryImpl(
this.names = names;
}

TypeSpec generate(FactoryElement factory, MockBuilder mockBuilder, MockBuilder2 mockBuilder2) {
if (component.mockBuilder()) {
return generateMock(factory, mockBuilder, mockBuilder2);
} else {
return generateNoMock(factory);
}
}

private TypeSpec generateMock(FactoryElement factory, MockBuilder mockBuilder, MockBuilder2 mockBuilder2) {
TypeSpec.Builder spec = TypeSpec.classBuilder(factory.generatedClass());
FieldSpec mockBuilderField = FieldSpec.builder(mockBuilder.getClassName(), "mockBuilder", FINAL).build();
spec.addField(mockBuilderField);
ParameterSpec mockBuilderParam = ParameterSpec.builder(mockBuilder.getClassName(), "mockBuilder").build();
spec.addMethod(MethodSpec.constructorBuilder()
.addParameter(mockBuilderParam)
.addStatement("this.$N = $N", mockBuilderField, mockBuilderParam)
.build());
ExecutableElement abstractMethod = factory.singleAbstractMethod();
spec.addMethod(generateBuildMethod(abstractMethod, mockBuilderField));
spec.addMethod(generateWithMocksMethod(mockBuilder2));
spec.addModifiers(PUBLIC, STATIC, FINAL);
spec.addSuperinterface(factory.element().asType());
return spec.build();
}

private MethodSpec generateBuildMethod(ExecutableElement abstractMethod, FieldSpec mockBuilderField) {
MethodSpec.Builder method = MethodSpec.methodBuilder(abstractMethod.getSimpleName().toString());
method.addAnnotation(Override.class);
method.addModifiers(abstractMethod.getModifiers().stream()
.filter(m -> m == PUBLIC || m == PROTECTED).collect(Collectors.toList()));
method.returns(TypeName.get(component.element().asType()));
for (NamedBinding namedBinding : sorted.values()) {
Binding b = namedBinding.binding();
if (b instanceof ParameterBinding) {
continue;
}
Key key = b.key();
CodeBlock invocation = b.invocation(names);
ParameterSpec param = names.apply(key);
if (!key.typeName().isPrimitive()) {
method.addStatement("$1T $2N = this.$3N != null && this.$3N.$2N != null ? this.$3N.$2N : $4L",
key.typeName(), param, mockBuilderField, invocation);
} else {
FieldSpec auxField = FieldSpec.builder(TypeName.BOOLEAN, namedBinding.auxName(), PRIVATE).build();
method.addStatement("$1T $2N = this.$3N != null && this.$3N.$4N ? this.$3N.$2N : $5L",
key.typeName(), param, mockBuilderField, auxField, invocation);
}
}
method.addParameters(parameters());
method.addStatement("return new $T($L)", component.generatedClass(), constructorParameters().stream()
.collect(CodeBlock.joining(", ")));
return method.build();
}

private MethodSpec generateWithMocksMethod(MockBuilder2 mockBuilder2) {
MethodSpec.Builder method = MethodSpec.methodBuilder("withMocks");
List<CodeBlock> constructorParameters = new ArrayList<>();
for (NamedBinding namedBinding : sorted.values()) {
Binding b = namedBinding.binding();
if (!(b instanceof ParameterBinding)) {
continue;
}
ParameterSpec param = names.apply(b.key());
constructorParameters.add(CodeBlock.of("$N", param));
}
if (component.publicMockBuilder()) {
method.addModifiers(PUBLIC);
}
method.addParameters(parameters());
method.returns(mockBuilder2.getClassName());
method.addStatement("return new $T($L)", mockBuilder2.getClassName(),
constructorParameters.stream().collect(CodeBlock.joining(", ")));
return method.build();
}

private TypeSpec generateNoMock(FactoryElement factory) {
TypeSpec generate(FactoryElement factory) {
TypeSpec.Builder spec = TypeSpec.classBuilder(factory.generatedClass());
spec.addModifiers(PRIVATE, STATIC, FINAL);
spec.addSuperinterface(factory.element().asType());
Expand Down Expand Up @@ -155,7 +79,7 @@ private List<CodeBlock> constructorParameters() {
return result;
}

private List<ParameterSpec> parameters() {
List<ParameterSpec> parameters() {
List<ParameterSpec> result = new ArrayList<>();
for (NamedBinding namedBinding : sorted.values()) {
Binding b = namedBinding.binding();
Expand Down
Loading
Loading