Skip to content

Commit

Permalink
allow use inject instead of provides
Browse files Browse the repository at this point in the history
  • Loading branch information
h908714124 committed Dec 24, 2023
1 parent 8fc97ab commit a7fb94c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public Collection<ParameterBinding> parameterBindings() {
List<ExecutableElement> methods = methodsIn(componentElement().element().getEnclosedElements());
Map<Key, InjectBinding> result = new LinkedHashMap<>();
for (ExecutableElement method : methods) {
if (method.getAnnotation(Provides.class) == null) {
if (method.getAnnotation(Provides.class) == null && !tool().hasInjectAnnotation(method)) {
continue; // ignore
}
Key key = keyCache().getKey(method);
Expand All @@ -139,7 +139,7 @@ public Collection<ParameterBinding> parameterBindings() {
if (method.getModifiers().contains(Modifier.STATIC)) {
continue; // ignore
}
if (method.getAnnotation(Provides.class) != null) {
if (method.getAnnotation(Provides.class) != null || tool().hasInjectAnnotation(method)) {
continue; // ignore
}
if (!method.getParameters().isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.jbock.simple.processor.validation;

import io.jbock.simple.Component;
import io.jbock.simple.Inject;
import io.jbock.simple.processor.binding.InjectBindingScanner;
import io.jbock.simple.processor.util.TypeTool;
Expand Down Expand Up @@ -43,11 +44,16 @@ public void validateStaticMethod(ExecutableElement method) {
if (method.getReturnType().getKind() == TypeKind.VOID) {
throw new ValidationFailure("The factory method may not return void", method);
}
if (!isSibling(method)) {
if (!isSibling(method) && !isInComponent(method)) {
throw new ValidationFailure("The factory method must return the type of its enclosing class", method);
}
}

private boolean isInComponent(ExecutableElement method) {
TypeElement typeElement = Visitors.TYPE_ELEMENT_VISITOR.visit(method.getEnclosingElement());
return typeElement.getAnnotation(Component.class) != null;
}

private boolean isSibling(ExecutableElement method) {
TypeElement typeElement = Visitors.TYPE_ELEMENT_VISITOR.visit(method.getEnclosingElement());
List<TypeElement> hierarchyRt = tool.types().asElement(method.getReturnType())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void providesString() {
" interface AComponent {",
" A getA();",
"",
" @Provides static String getString(B b) { return null; }",
" @Inject static String getString(B b) { return null; }",
" }",
"}");

Expand Down Expand Up @@ -64,7 +64,6 @@ void providesFunction() {
"",
"import io.jbock.simple.Component;",
"import io.jbock.simple.Inject;",
"import io.jbock.simple.Provides;",
"import java.util.function.Function;",
"import java.util.Map;",
"",
Expand All @@ -81,7 +80,7 @@ void providesFunction() {
" interface AComponent {",
" A getA();",
"",
" @Provides static Map<String, Function<String, Integer>> provideMyMap(B b) { return null; }",
" @Inject static Map<String, Function<String, Integer>> provideMyMap(B b) { return null; }",
" }",
"}");

Expand Down Expand Up @@ -113,7 +112,6 @@ void nonstaticProvidesMethod() {
"",
"import io.jbock.simple.Component;",
"import io.jbock.simple.Inject;",
"import io.jbock.simple.Provides;",
"",
"final class TestClass {",
" static class A {",
Expand All @@ -128,12 +126,12 @@ void nonstaticProvidesMethod() {
" interface AComponent {",
" A getA();",
"",
" @Provides default String getString() { return null; }",
" @Inject default String getString() { return null; }",
" }",
"}");

Compilation compilation = simpleCompiler().compile(component);
assertThat(compilation).failed();
assertThat(compilation).hadErrorContaining("The @Provides method must be static");
assertThat(compilation).hadErrorContaining("The factory method must be static");
}
}
9 changes: 2 additions & 7 deletions simple-component/src/main/java/io/jbock/simple/Provides.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,8 @@
import static java.lang.annotation.RetentionPolicy.SOURCE;

/**
* Annotates <em>static</em> methods of a {@linkplain Component component}
* to create a provider method binding.
*
* <p>The method's return type is bound to its returned value.
*
* <p>The {@linkplain Component component}
* implementation will pass dependencies to the method as parameters.
* This is an alternative to the {@code @Inject} annotation
* which can only be used on static methods directly in the component.
*/
@Target(METHOD)
@Retention(SOURCE)
Expand Down

0 comments on commit a7fb94c

Please sign in to comment.