Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WELD-2786 OM configurator without overriden notify method should respect Reception settings #2961

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import jakarta.enterprise.inject.spi.ObserverMethod;
import jakarta.enterprise.inject.spi.configurator.ObserverMethodConfigurator;

import org.jboss.weld.event.ObserverMethodImpl;
import org.jboss.weld.event.SyntheticObserverMethod;
import org.jboss.weld.logging.EventLogger;
import org.jboss.weld.resolution.CovariantTypes;
Expand Down Expand Up @@ -82,7 +83,11 @@ public ObserverMethodConfiguratorImpl(Extension extension) {
public ObserverMethodConfiguratorImpl(ObserverMethod<T> observerMethod, Extension extension) {
this(extension);
read(observerMethod);
notifyWith(e -> observerMethod.notify(e));
if (observerMethod instanceof ObserverMethodImpl<?, ?>) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't very good as it relies on our impl of OM underneath while technically the impl can be arbitrary.
However, I don't think we can achieve this with any other setup anyway as the public API for ObserverMethod doesn't allow us to setReception(). Thoughts or ideas @mkouba?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that it's OK because it should only work for configurators which are based on non-synthetic observer methods, i.e. observer methods backed by a Java method of a class bean which is always a Weld implementation of OM.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i.e. observer methods backed by a Java method of a class bean which is always a Weld implementation of OM.

That is a good point, thank you.

notifyWith(e -> ((ObserverMethodImpl) observerMethod).notify(e.getEvent(), this.reception));
} else {
notifyWith(e -> observerMethod.notify(e));
}
}

@Override
Expand Down
12 changes: 10 additions & 2 deletions impl/src/main/java/org/jboss/weld/event/ObserverMethodImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -295,15 +295,23 @@ public void initialize(EnhancedAnnotatedMethod<T, ? super X> annotated) {

@Override
public void notify(final T event) {
sendEvent(event);
sendEvent(event, getReception());
}

/**
* Used as default notify method from ObserverMethodConfiguratorImpl.
* This allows Weld to respect {@link Reception} set in the configurator.
*/
public void notify(final T event, Reception reception) {
sendEvent(event, reception);
}

/**
* Invokes the observer method immediately passing the event.
*
* @param event The event to notify observer with
*/
protected void sendEvent(final T event) {
public void sendEvent(final T event, Reception reception) {
if (isStatic) {
sendEvent(event, null, null);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.jboss.weld.tests.observers.extension.configure;

// event payload
public class Foo {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.jboss.weld.tests.observers.extension.configure;

import jakarta.enterprise.event.Observes;
import jakarta.enterprise.event.Reception;
import jakarta.enterprise.inject.spi.Extension;
import jakarta.enterprise.inject.spi.ProcessObserverMethod;

public class MyExtension implements Extension {

public void pom(@Observes ProcessObserverMethod<Foo, ObservingBean> pom) {
pom.configureObserverMethod().reception(Reception.IF_EXISTS);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.jboss.weld.tests.observers.extension.configure;

import jakarta.enterprise.event.Event;
import jakarta.enterprise.inject.spi.Extension;
import jakarta.inject.Inject;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.BeanArchive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.jboss.weld.test.util.Utils;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(Arquillian.class)
public class ObserverReceptionConfigurationTest {

@Deployment
public static JavaArchive getDeployment() {
return ShrinkWrap
.create(BeanArchive.class, Utils.getDeploymentNameAsHash(ObserverReceptionConfigurationTest.class))
.addPackage(ObserverReceptionConfigurationTest.class.getPackage())
.addAsServiceProvider(Extension.class, MyExtension.class);
}

@Inject
ObservingBean bean;

@Inject
Event<Foo> fooEvent;

@Test
public void testObserverReceptionChanged() {
// assert initial state
Assert.assertEquals(0, ObservingBean.timesObserved);

// fire event and assert again, should not be notified
fooEvent.fire(new Foo());
Assert.assertEquals(0, ObservingBean.timesObserved);

// trigger bean creation and repeat the test
bean.ping();
fooEvent.fire(new Foo());
Assert.assertEquals(1, ObservingBean.timesObserved);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.jboss.weld.tests.observers.extension.configure;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.Observes;

@ApplicationScoped
public class ObservingBean {

public static int timesObserved = 0;

public void observe(@Observes Foo foo) {
timesObserved++;
}

public void ping() {
// no-op but forces bean init when invoked
}
}
Loading