Skip to content

Commit

Permalink
refactoring, remove InjectBinding Factory
Browse files Browse the repository at this point in the history
  • Loading branch information
h908714124 committed Dec 24, 2023
1 parent 4f447a7 commit 6c91be6
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public final class ComponentElement {

private final TypeElement element;
private final KeyFactory keyFactory;
private final InjectBinding.Factory injectBindingFactory;

private final Supplier<ClassName> generatedClass = memoize(() -> {
ClassName className = ClassName.get(element());
Expand Down Expand Up @@ -74,7 +73,7 @@ public final class ComponentElement {
continue; // ignore
}
Key key = keyFactory().getKey(method);
InjectBinding b = injectBindingFactory().create(method);
InjectBinding b = keyFactory().createBinding(method);
result.put(key, b);
}
return result;
Expand Down Expand Up @@ -125,11 +124,9 @@ public final class ComponentElement {
@Inject
public ComponentElement(
TypeElement element,
KeyFactory keyFactory,
InjectBinding.Factory injectBindingFactory) {
KeyFactory keyFactory) {
this.element = element;
this.keyFactory = keyFactory;
this.injectBindingFactory = injectBindingFactory;
}

public TypeElement element() {
Expand Down Expand Up @@ -164,10 +161,6 @@ private KeyFactory keyFactory() {
return keyFactory;
}

private InjectBinding.Factory injectBindingFactory() {
return injectBindingFactory;
}

public Optional<Binding> parameterBinding(Key key) {
return Optional.ofNullable(parameterBindings.get().get(key));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
import io.jbock.javapoet.ParameterSpec;
import io.jbock.javapoet.ParameterizedTypeName;
import io.jbock.javapoet.TypeName;
import io.jbock.simple.Inject;
import io.jbock.simple.Provides;
import io.jbock.simple.processor.util.ValidationFailure;
import io.jbock.simple.processor.util.Visitors;
import io.jbock.simple.processor.writing.NamedBinding;

Expand Down Expand Up @@ -105,7 +103,7 @@ private static String lowerFirst(String s) {
.map(parameter -> new DependencyRequest(keyFactory().getKey(parameter), parameter, element()))
.collect(Collectors.toList()));

private InjectBinding(
InjectBinding(
Key key,
KeyFactory keyFactory,
ExecutableElement bindingElement) {
Expand Down Expand Up @@ -159,23 +157,4 @@ public String toString() {
private KeyFactory keyFactory() {
return keyFactory;
}

public static final class Factory {
private final KeyFactory keyFactory;

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

InjectBinding create(ExecutableElement m) {
Key key = keyFactory.getKey(m);
if (m.getKind() == ElementKind.CONSTRUCTOR) {
if (key.qualifier().isPresent()) {
throw new ValidationFailure("Constructors can't have qualifiers", m);
}
}
return new InjectBinding(key, keyFactory, m);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ public final class InjectBindingFactory implements ClearableCache {
private final Map<TypeElement, Map<Key, InjectBinding>> injectBindingCache = new HashMap<>();

private final TypeTool tool;
private final InjectBinding.Factory injectBindingFactory;
private final KeyFactory keyFactory;
private final InjectBindingScanner injectBindingScanner;

@Inject
public InjectBindingFactory(
TypeTool tool,
InjectBinding.Factory injectBindingFactory,
KeyFactory keyFactory,
InjectBindingScanner injectBindingScanner) {
this.tool = tool;
this.injectBindingFactory = injectBindingFactory;
this.keyFactory = keyFactory;
this.injectBindingScanner = injectBindingScanner;
}

Expand All @@ -40,7 +40,7 @@ private Map<Key, InjectBinding> injectBindingsMiss(TypeElement typeElement) {
Map<Key, InjectBinding> result = new LinkedHashMap<>();
List<ExecutableElement> methods = injectBindingScanner.scan(typeElement);
for (ExecutableElement method : methods) {
InjectBinding b = injectBindingFactory.create(method);
InjectBinding b = keyFactory.createBinding(method);
result.put(b.key(), b);
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
Expand Down Expand Up @@ -62,7 +63,7 @@ public Key getKey(ExecutableElement element) {
return key;
}

public Key getKey(VariableElement parameter) {
Key getKey(VariableElement parameter) {
Key key = keyCache.get(parameter);
if (key != null) {
return key;
Expand All @@ -78,6 +79,16 @@ private boolean hasQualifierAnnotation(AnnotationMirror mirror) {
return tool.hasQualifierAnnotation(element);
}

InjectBinding createBinding(ExecutableElement m) {
Key key = getKey(m);
if (m.getKind() == ElementKind.CONSTRUCTOR) {
if (key.qualifier().isPresent()) {
throw new ValidationFailure("Constructors can't have qualifiers", m);
}
}
return new InjectBinding(key, this, m);
}

TypeTool tool() {
return tool;
}
Expand Down

0 comments on commit 6c91be6

Please sign in to comment.