This is an automated email from the ASF dual-hosted git repository.
rmannibucau pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/openwebbeans.git
The following commit(s) were added to refs/heads/master by this push:
new e43981b OWB-1302 ensure produceWith works in bean configurator
e43981b is described below
commit e43981be78f21a3250a50e1fcfaf3107d8444075
Author: Romain Manni-Bucau <[email protected]>
AuthorDate: Mon Nov 18 16:40:48 2019 +0100
OWB-1302 ensure produceWith works in bean configurator
---
.../configurator/BeanConfiguratorImpl.java | 17 ++++++++++++
.../test/portable/events/PortableAddBeanTest.java | 31 ++++++++++++++++++++++
2 files changed, 48 insertions(+)
diff --git
a/webbeans-impl/src/main/java/org/apache/webbeans/configurator/BeanConfiguratorImpl.java
b/webbeans-impl/src/main/java/org/apache/webbeans/configurator/BeanConfiguratorImpl.java
index 18cacbf..644e949 100644
---
a/webbeans-impl/src/main/java/org/apache/webbeans/configurator/BeanConfiguratorImpl.java
+++
b/webbeans-impl/src/main/java/org/apache/webbeans/configurator/BeanConfiguratorImpl.java
@@ -49,7 +49,9 @@ import org.apache.webbeans.component.OwbBean;
import org.apache.webbeans.component.WebBeansType;
import org.apache.webbeans.component.creation.BeanAttributesBuilder;
import org.apache.webbeans.config.WebBeansContext;
+import org.apache.webbeans.context.creational.CreationalContextImpl;
import org.apache.webbeans.exception.WebBeansConfigurationException;
+import org.apache.webbeans.inject.instance.InstanceImpl;
import org.apache.webbeans.util.AnnotationUtil;
import org.apache.webbeans.util.GenericsUtil;
@@ -486,6 +488,10 @@ public class BeanConfiguratorImpl<T> implements
BeanConfigurator<T>
@Override
public T create(CreationalContext<T> context)
{
+ if (produceWithCallback != null)
+ {
+ return produceWithCallback.apply(createInstance(context));
+ }
return (T) createWithCallback.apply(context);
}
@@ -516,6 +522,10 @@ public class BeanConfiguratorImpl<T> implements
BeanConfigurator<T>
@Override
public void destroy(T instance, CreationalContext<T> context)
{
+ if (disposeWithCallback != null)
+ {
+ disposeWithCallback.accept(instance, createInstance(context));
+ }
if (destroyWithCallback != null)
{
destroyWithCallback.accept(instance, context);
@@ -564,6 +574,13 @@ public class BeanConfiguratorImpl<T> implements
BeanConfigurator<T>
return webBeansContext;
}
+ private InstanceImpl<Object> createInstance(final CreationalContext<T>
context)
+ {
+ return new InstanceImpl<>(
+ Object.class,
CreationalContextImpl.class.cast(context).getInjectionPoint(),
+ webBeansContext, getQualifiers().toArray(new
Annotation[0]));
+ }
+
public String toString()
{
StringBuilder builder = new StringBuilder();
diff --git
a/webbeans-impl/src/test/java/org/apache/webbeans/test/portable/events/PortableAddBeanTest.java
b/webbeans-impl/src/test/java/org/apache/webbeans/test/portable/events/PortableAddBeanTest.java
index b9715cb..ba70b67 100644
---
a/webbeans-impl/src/test/java/org/apache/webbeans/test/portable/events/PortableAddBeanTest.java
+++
b/webbeans-impl/src/test/java/org/apache/webbeans/test/portable/events/PortableAddBeanTest.java
@@ -18,9 +18,18 @@
*/
package org.apache.webbeans.test.portable.events;
+import static java.util.Collections.emptyList;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertSame;
+
import java.util.ArrayList;
import java.util.Collection;
+import javax.enterprise.context.Dependent;
+import javax.enterprise.event.Observes;
+import javax.enterprise.inject.spi.AfterBeanDiscovery;
+import javax.enterprise.inject.spi.Extension;
+
import org.junit.Assert;
import org.apache.webbeans.test.AbstractUnitTest;
@@ -47,4 +56,26 @@ public class PortableAddBeanTest extends AbstractUnitTest
shutDownContainer();
}
+ @Test
+ public void testAddBeanExtensionWithProduceInsteadOfCreateCallback()
+ {
+ final MyBean myBean = new MyBean();
+ addExtension(new Extension()
+ {
+ public void addBean(@Observes AfterBeanDiscovery event)
+ {
+ event.addBean()
+ .scope(Dependent.class)
+ .addTypes(MyBean.class, Object.class)
+ .beanClass(MyBean.class)
+ .produceWith(i -> myBean);
+ }
+ });
+ startContainer(emptyList(), emptyList());
+ assertSame(myBean, getInstance(MyBean.class));
+ }
+
+ public static class MyBean
+ {
+ }
}