Skip to content

Commit

Permalink
refactoring, add InjectBindingScanner
Browse files Browse the repository at this point in the history
  • Loading branch information
h908714124 committed Dec 23, 2023
1 parent 1ba9d5c commit e8f4d3c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
import io.jbock.simple.Inject;
import io.jbock.simple.processor.util.ClearableCache;
import io.jbock.simple.processor.util.TypeTool;
import io.jbock.simple.processor.util.Util;

import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import static io.jbock.simple.processor.util.Visitors.EXECUTABLE_ELEMENT_VISITOR;
import static io.jbock.simple.processor.util.Visitors.TYPE_ELEMENT_VISITOR;

public final class InjectBindingFactory implements ClearableCache {
Expand All @@ -21,13 +20,16 @@ public final class InjectBindingFactory implements ClearableCache {

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

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

public Map<Key, InjectBinding> injectBindings(TypeElement typeElement) {
Expand All @@ -36,13 +38,10 @@ public Map<Key, InjectBinding> injectBindings(TypeElement typeElement) {

private Map<Key, InjectBinding> injectBindingsMiss(TypeElement typeElement) {
Map<Key, InjectBinding> result = new LinkedHashMap<>();
for (TypeElement element : Util.getWithEnclosing(typeElement)) {
tool.elements().getAllMembers(element).stream()
.filter(tool::hasInjectAnnotation)
.map(EXECUTABLE_ELEMENT_VISITOR::visit)
.filter(Objects::nonNull)
.map(injectBindingFactory::create)
.forEach(b -> result.put(b.key(), b) /* duplicates handled elsewhere */);
List<ExecutableElement> methods = injectBindingScanner.scan(typeElement);
for (ExecutableElement method : methods) {
InjectBinding b = injectBindingFactory.create(method);
result.put(b.key(), b);
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.jbock.simple.processor.binding;

import io.jbock.simple.Inject;
import io.jbock.simple.processor.util.TypeTool;
import io.jbock.simple.processor.util.Util;

import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static io.jbock.simple.processor.util.Visitors.EXECUTABLE_ELEMENT_VISITOR;

public class InjectBindingScanner {

private final TypeTool tool;

@Inject
public InjectBindingScanner(TypeTool tool) {
this.tool = tool;
}

public List<ExecutableElement> scan(TypeElement typeElement) {
List<ExecutableElement> result = new ArrayList<>();
for (TypeElement element : Util.getWithEnclosing(typeElement)) {
tool.elements().getAllMembers(element).stream()
.filter(tool::hasInjectAnnotation)
.map(EXECUTABLE_ELEMENT_VISITOR::visit)
.filter(Objects::nonNull)
.forEach(result::add);
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package io.jbock.simple.processor.validation;

import io.jbock.simple.Inject;
import io.jbock.simple.processor.binding.InjectBinding;
import io.jbock.simple.processor.binding.InjectBindingFactory;
import io.jbock.simple.processor.binding.Key;
import io.jbock.simple.processor.binding.InjectBindingScanner;
import io.jbock.simple.processor.util.TypeTool;
import io.jbock.simple.processor.util.Util;
import io.jbock.simple.processor.util.ValidationFailure;
Expand All @@ -15,7 +13,6 @@
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeKind;
import java.util.List;
import java.util.Map;

import static io.jbock.simple.processor.util.TypeNames.JAKARTA_INJECT;
import static io.jbock.simple.processor.util.TypeNames.JAVAX_INJECT;
Expand All @@ -24,14 +21,14 @@
public final class InjectBindingValidator {

private final TypeTool tool;
private final InjectBindingFactory injectBindingFactory;
private final InjectBindingScanner injectBindingScanner;

@Inject
public InjectBindingValidator(
TypeTool tool,
InjectBindingFactory injectBindingFactory) {
InjectBindingScanner injectBindingScanner) {
this.tool = tool;
this.injectBindingFactory = injectBindingFactory;
this.injectBindingScanner = injectBindingScanner;
}

public void validateConstructor(ExecutableElement element) {
Expand Down Expand Up @@ -70,15 +67,15 @@ private void validate(ExecutableElement element) {
if (!typeElement.getTypeParameters().isEmpty()) {
throw new ValidationFailure("Type parameters are not allowed on element", typeElement);
}
Map<Key, InjectBinding> m = injectBindingFactory.injectBindings(typeElement);
for (InjectBinding b : m.values()) {
if (b.element().getAnnotationMirrors().stream().filter(mirror -> {
List<ExecutableElement> methods = injectBindingScanner.scan(typeElement);
for (ExecutableElement m : methods) {
if (m.getAnnotationMirrors().stream().filter(mirror -> {
DeclaredType annotationType = mirror.getAnnotationType();
return tool.isSameType(annotationType, JAVAX_INJECT)
|| tool.isSameType(annotationType, JAKARTA_INJECT)
|| tool.isSameType(annotationType, SIMPLE_INJECT);
}).count() >= 2) {
throw new ValidationFailure("Duplicate inject annotation", b.element());
throw new ValidationFailure("Duplicate inject annotation", m);
}
}
}
Expand Down

0 comments on commit e8f4d3c

Please sign in to comment.