Skip to content

Commit 8ea35dd

Browse files
authored
allow use inject instead of provides (#57)
1 parent 8fc97ab commit 8ea35dd

File tree

4 files changed

+15
-16
lines changed

4 files changed

+15
-16
lines changed

compiler/src/main/java/io/jbock/simple/processor/binding/KeyFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public Collection<ParameterBinding> parameterBindings() {
122122
List<ExecutableElement> methods = methodsIn(componentElement().element().getEnclosedElements());
123123
Map<Key, InjectBinding> result = new LinkedHashMap<>();
124124
for (ExecutableElement method : methods) {
125-
if (method.getAnnotation(Provides.class) == null) {
125+
if (method.getAnnotation(Provides.class) == null && !tool().hasInjectAnnotation(method)) {
126126
continue; // ignore
127127
}
128128
Key key = keyCache().getKey(method);
@@ -139,7 +139,7 @@ public Collection<ParameterBinding> parameterBindings() {
139139
if (method.getModifiers().contains(Modifier.STATIC)) {
140140
continue; // ignore
141141
}
142-
if (method.getAnnotation(Provides.class) != null) {
142+
if (method.getAnnotation(Provides.class) != null || tool().hasInjectAnnotation(method)) {
143143
continue; // ignore
144144
}
145145
if (!method.getParameters().isEmpty()) {

compiler/src/main/java/io/jbock/simple/processor/validation/InjectBindingValidator.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.jbock.simple.processor.validation;
22

3+
import io.jbock.simple.Component;
34
import io.jbock.simple.Inject;
45
import io.jbock.simple.processor.binding.InjectBindingScanner;
56
import io.jbock.simple.processor.util.TypeTool;
@@ -43,11 +44,16 @@ public void validateStaticMethod(ExecutableElement method) {
4344
if (method.getReturnType().getKind() == TypeKind.VOID) {
4445
throw new ValidationFailure("The factory method may not return void", method);
4546
}
46-
if (!isSibling(method)) {
47+
if (!isSibling(method) && !isInComponent(method)) {
4748
throw new ValidationFailure("The factory method must return the type of its enclosing class", method);
4849
}
4950
}
5051

52+
private boolean isInComponent(ExecutableElement method) {
53+
TypeElement typeElement = Visitors.TYPE_ELEMENT_VISITOR.visit(method.getEnclosingElement());
54+
return typeElement.getAnnotation(Component.class) != null;
55+
}
56+
5157
private boolean isSibling(ExecutableElement method) {
5258
TypeElement typeElement = Visitors.TYPE_ELEMENT_VISITOR.visit(method.getEnclosingElement());
5359
List<TypeElement> hierarchyRt = tool.types().asElement(method.getReturnType())

compiler/src/test/java/io/jbock/simple/processor/ProvidesTest.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ void providesString() {
3333
" interface AComponent {",
3434
" A getA();",
3535
"",
36-
" @Provides static String getString(B b) { return null; }",
36+
" @Inject static String getString(B b) { return null; }",
3737
" }",
3838
"}");
3939

@@ -64,7 +64,6 @@ void providesFunction() {
6464
"",
6565
"import io.jbock.simple.Component;",
6666
"import io.jbock.simple.Inject;",
67-
"import io.jbock.simple.Provides;",
6867
"import java.util.function.Function;",
6968
"import java.util.Map;",
7069
"",
@@ -81,7 +80,7 @@ void providesFunction() {
8180
" interface AComponent {",
8281
" A getA();",
8382
"",
84-
" @Provides static Map<String, Function<String, Integer>> provideMyMap(B b) { return null; }",
83+
" @Inject static Map<String, Function<String, Integer>> provideMyMap(B b) { return null; }",
8584
" }",
8685
"}");
8786

@@ -113,7 +112,6 @@ void nonstaticProvidesMethod() {
113112
"",
114113
"import io.jbock.simple.Component;",
115114
"import io.jbock.simple.Inject;",
116-
"import io.jbock.simple.Provides;",
117115
"",
118116
"final class TestClass {",
119117
" static class A {",
@@ -128,12 +126,12 @@ void nonstaticProvidesMethod() {
128126
" interface AComponent {",
129127
" A getA();",
130128
"",
131-
" @Provides default String getString() { return null; }",
129+
" @Inject default String getString() { return null; }",
132130
" }",
133131
"}");
134132

135133
Compilation compilation = simpleCompiler().compile(component);
136134
assertThat(compilation).failed();
137-
assertThat(compilation).hadErrorContaining("The @Provides method must be static");
135+
assertThat(compilation).hadErrorContaining("The factory method must be static");
138136
}
139137
}

simple-component/src/main/java/io/jbock/simple/Provides.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,8 @@
77
import static java.lang.annotation.RetentionPolicy.SOURCE;
88

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

0 commit comments

Comments
 (0)