diff --git a/compiler/src/main/java/io/jbock/simple/processor/binding/KeyFactory.java b/compiler/src/main/java/io/jbock/simple/processor/binding/KeyFactory.java index 87144fe..aead4e2 100644 --- a/compiler/src/main/java/io/jbock/simple/processor/binding/KeyFactory.java +++ b/compiler/src/main/java/io/jbock/simple/processor/binding/KeyFactory.java @@ -122,7 +122,7 @@ public Collection parameterBindings() { List methods = methodsIn(componentElement().element().getEnclosedElements()); Map 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); @@ -139,7 +139,7 @@ public Collection 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()) { diff --git a/compiler/src/main/java/io/jbock/simple/processor/validation/InjectBindingValidator.java b/compiler/src/main/java/io/jbock/simple/processor/validation/InjectBindingValidator.java index cee7c4b..f4de321 100644 --- a/compiler/src/main/java/io/jbock/simple/processor/validation/InjectBindingValidator.java +++ b/compiler/src/main/java/io/jbock/simple/processor/validation/InjectBindingValidator.java @@ -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; @@ -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 hierarchyRt = tool.types().asElement(method.getReturnType()) diff --git a/compiler/src/test/java/io/jbock/simple/processor/ProvidesTest.java b/compiler/src/test/java/io/jbock/simple/processor/ProvidesTest.java index 9f03f5c..3b8f588 100644 --- a/compiler/src/test/java/io/jbock/simple/processor/ProvidesTest.java +++ b/compiler/src/test/java/io/jbock/simple/processor/ProvidesTest.java @@ -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; }", " }", "}"); @@ -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;", "", @@ -81,7 +80,7 @@ void providesFunction() { " interface AComponent {", " A getA();", "", - " @Provides static Map> provideMyMap(B b) { return null; }", + " @Inject static Map> provideMyMap(B b) { return null; }", " }", "}"); @@ -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 {", @@ -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"); } } \ No newline at end of file diff --git a/simple-component/src/main/java/io/jbock/simple/Provides.java b/simple-component/src/main/java/io/jbock/simple/Provides.java index ad62b87..936a3e3 100644 --- a/simple-component/src/main/java/io/jbock/simple/Provides.java +++ b/simple-component/src/main/java/io/jbock/simple/Provides.java @@ -7,13 +7,8 @@ import static java.lang.annotation.RetentionPolicy.SOURCE; /** - * Annotates static methods of a {@linkplain Component component} - * to create a provider method binding. - * - *

The method's return type is bound to its returned value. - * - *

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)