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

refactoring, make context component injectable #54

Merged
merged 1 commit into from
Dec 24, 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 @@ -9,12 +9,14 @@
import io.jbock.simple.processor.util.TypeTool;
import io.jbock.simple.processor.writing.Generator;

import javax.lang.model.element.TypeElement;

@Component
public interface ContextComponent {

@Component.Builder
interface Builder {
Builder componentElement(ComponentElement componentElement);
Builder typeElement(TypeElement typeElement);

Builder tool(TypeTool tool);

Expand All @@ -25,6 +27,8 @@ interface Builder {
ContextComponent build();
}

ComponentElement componentElement();

Generator generator();

TopologicalSorter topologicalSorter();
Expand All @@ -44,9 +48,9 @@ public Factory(
this.keyFactory = keyFactory;
}

public ContextComponent create(ComponentElement component) {
public ContextComponent create(TypeElement typeElement) {
return ContextComponent_Impl.builder()
.componentElement(component)
.typeElement(typeElement)
.tool(tool)
.injectBindingFactory(injectBindingFactory)
.keyFactory(keyFactory)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ public final class ComponentElement {
return result;
});

private ComponentElement(
@Inject
public ComponentElement(
TypeElement element,
KeyFactory keyFactory,
InjectBinding.Factory injectBindingFactory) {
Expand Down Expand Up @@ -190,20 +191,4 @@ public boolean mockBuilder() {
}
return annotation.mockBuilder();
}

public static final class Factory {
private final KeyFactory keyFactory;
private final InjectBinding.Factory injectBindingFactory;

@Inject
public Factory(KeyFactory keyFactory, InjectBinding.Factory injectBindingFactory) {
this.keyFactory = keyFactory;
this.injectBindingFactory = injectBindingFactory;
}

public ComponentElement create(
TypeElement element) {
return new ComponentElement(element, keyFactory, injectBindingFactory);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public class ComponentStep implements Step {
private final TypeElementValidator typeElementValidator;
private final ExecutableElementValidator executableElementValidator;
private final SpecWriter specWriter;
private final ComponentElement.Factory componentElementFactory;
private final ContextComponent.Factory contextComponentFactory;

@Inject
Expand All @@ -42,14 +41,12 @@ public ComponentStep(
TypeElementValidator typeElementValidator,
ExecutableElementValidator executableElementValidator,
SpecWriter specWriter,
ComponentElement.Factory componentElementFactory,
ContextComponent.Factory contextComponentFactory) {
this.messager = messager;
this.tool = tool;
this.typeElementValidator = typeElementValidator;
this.executableElementValidator = executableElementValidator;
this.specWriter = specWriter;
this.componentElementFactory = componentElementFactory;
this.contextComponentFactory = contextComponentFactory;
}

Expand All @@ -74,7 +71,8 @@ public Set<? extends Element> process(Map<String, Set<Element>> elementsByAnnota

private void process(TypeElement typeElement) {
typeElementValidator.validate(typeElement);
ComponentElement component = componentElementFactory.create(typeElement);
ContextComponent context = contextComponentFactory.create(typeElement);
ComponentElement component = context.componentElement();
component.factoryElement().ifPresent(factory -> {
ExecutableElement method = factory.singleAbstractMethod();
if (!tool.types().isSameType(method.getReturnType(), typeElement.asType())) {
Expand All @@ -86,9 +84,8 @@ private void process(TypeElement typeElement) {
executableElementValidator.validate(m);
}
}
ContextComponent componentComponent = contextComponentFactory.create(component);
Generator generator = componentComponent.generator();
List<Binding> bindings = componentComponent.topologicalSorter().sortedBindings();
Generator generator = context.generator();
List<Binding> bindings = context.topologicalSorter().sortedBindings();
TypeSpec typeSpec = generator.generate(bindings);
specWriter.write(component.generatedClass(), typeSpec);
}
Expand Down
Loading