Skip to content

Commit 6f7d1b5

Browse files
committed
Make it possible to inject context to beans
1 parent 807092a commit 6f7d1b5

File tree

8 files changed

+26
-10
lines changed

8 files changed

+26
-10
lines changed

src/integrationTest/groovy/annotated/InjectMultipleDependenciesSpec.groovy

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ class InjectMultipleDependenciesSpec extends Specification {
4747
then:
4848
ContextException e = thrown(ContextException)
4949
e.message == "Could not create bean: Baz"
50-
e.cause.message == "Beans not found for type: Foo"
50+
e.cause.message == "Could not create bean from constructor: public annotated.samples.optional_multiple_deps.Baz(java.util.List)"
51+
e.cause.cause.message == "Beans not found for type: Foo"
5152
}
5253

5354
def "should fail injecting named list of beans"() {
@@ -59,6 +60,7 @@ class InjectMultipleDependenciesSpec extends Specification {
5960
then:
6061
ContextException e = thrown(ContextException)
6162
e.message == "Could not create bean: Bar"
62-
e.cause.message.startsWith("Detected named @Dependency for a list of dependencies")
63+
e.cause.message == "Could not create bean from constructor: public annotated.samples.named_multiple_deps.Bar(java.util.List)"
64+
e.cause.cause.message.startsWith("Detected named @Dependency for a list of dependencies")
6365
}
6466
}

src/main/java/com/coditory/quark/context/BeanFinalizer.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ private BeanFinalizer() {
1818
}
1919

2020
static void closeBean(Object bean, BeanDescriptor<?> descriptor, ResolutionContext context) {
21+
if (bean instanceof Context) {
22+
return;
23+
}
2124
if (bean instanceof Closeable) {
2225
closeBean((Closeable) bean, descriptor);
2326
}

src/main/java/com/coditory/quark/context/BeanHolder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import static com.coditory.quark.context.BeanFinalizer.closeBean;
1212
import static com.coditory.quark.context.BeanInitializer.initializeBean;
1313
import static com.coditory.quark.context.BeanPostInitializer.postInitializeBean;
14-
import static com.coditory.quark.context.ResolutionPath.emptyResolutionPath;
1514
import static java.util.Objects.requireNonNull;
1615
import static java.util.stream.Collectors.toSet;
1716

@@ -145,3 +144,4 @@ public String toString() {
145144
return "BeanHolder{" + descriptor.toShortString() + '}';
146145
}
147146
}
147+

src/main/java/com/coditory/quark/context/ConstructorBasedBeanCreator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ static <T> ConstructorBasedBeanCreator<T> fromConstructor(Class<T> type) {
2424
.map(c -> (Constructor<T>) c)
2525
.toList();
2626
if (annotated.size() == 1) {
27-
constructor = annotated.get(0);
27+
constructor = annotated.getFirst();
2828
} else if (annotated.size() > 1) {
2929
throw new ContextException("Expected single constructor annotated with @Inject in class: "
3030
+ type.getCanonicalName() + ". Got: " + annotated.size());
@@ -48,11 +48,11 @@ static <T> ConstructorBasedBeanCreator<T> fromConstructor(Class<T> type) {
4848
@NotNull
4949
@Override
5050
public T create(@NotNull ResolutionContext context) {
51-
Object[] args = resolveArguments(constructor, context);
5251
try {
52+
Object[] args = resolveArguments(constructor, context);
5353
return constructor.newInstance(args);
5454
} catch (Exception e) {
55-
throw new ContextException("Could no create bean from constructor: " + constructor, e);
55+
throw new ContextException("Could not create bean from constructor: " + constructor, e);
5656
}
5757
}
5858

src/main/java/com/coditory/quark/context/Context.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ private Context(String name, Map<BeanDescriptor<?>, List<BeanHolder<?>>> beanHol
9898
// register self
9999
BeanDescriptor<Context> descriptor = descriptor(Context.class);
100100
BeanHolder<Context> holder = holder(descriptor, (ResolutionContext r) -> this, true);
101-
holder.setEventEmitter(eventBus);
101+
holder.setEventEmitter(new DummyEventEmitter());
102102
beanHolders.put(descriptor, List.of(holder));
103103
// index beans
104104
this.beanHolders = beanHolders;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.coditory.quark.context;
2+
3+
import com.coditory.quark.eventbus.EventEmitter;
4+
import org.jetbrains.annotations.NotNull;
5+
6+
class DummyEventEmitter implements EventEmitter {
7+
@Override
8+
public void emit(@NotNull Object event) {
9+
// deliberately empty
10+
}
11+
}

src/main/java/com/coditory/quark/context/MethodBasedBeanCreator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ public MethodBasedBeanCreator(BeanHolder<?> holder, Method method) {
2929
@NotNull
3030
@Override
3131
public T create(@NotNull ResolutionContext context) {
32-
Object object = holder.get(context);
33-
Object[] args = resolveArguments(method, context);
3432
try {
33+
Object object = holder.get(context);
34+
Object[] args = resolveArguments(method, context);
3535
return (T) method.invoke(object, args);
3636
} catch (Exception e) {
3737
throw new ContextException("Could not create bean from method: " + simplifyMethodName(method), e);

src/test/groovy/com/coditory/quark/context/BeanDependenciesSpec.groovy renamed to src/test/groovy/com/coditory/quark/context/ContextDependencySpec.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package com.coditory.quark.context
22

33
import spock.lang.Specification
44

5-
class BeanDependenciesSpec extends Specification {
5+
class ContextDependencySpec extends Specification {
66
def "should register bean with dependency on the dependency context"() {
77
given:
88
Context context = Context.builder()

0 commit comments

Comments
 (0)