Skip to content

Instantly share code, notes, and snippets.

@olbpetersson
Created February 10, 2016 20:19
Show Gist options
  • Save olbpetersson/305276d7c4fec847a413 to your computer and use it in GitHub Desktop.
Save olbpetersson/305276d7c4fec847a413 to your computer and use it in GitHub Desktop.
package se.olapetersson.automagic;
import javax.ejb.Stateless;
import javax.enterprise.event.Event;
import javax.enterprise.event.Observes;
import javax.inject.Inject;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
@Stateless
public class Example {
@Inject
Event<UnaryOperator> unaryEvent;
@Inject
Event<Supplier> supplierEvent;
@Inject
Event<Supplier<String>> stringSupplierEvent;
/*
Why are the first three observers (the first two events) working but not the last
typed one?
*/
public void fireEvent() {
UnaryOperator unaryOperator = (x) -> x + " consumed this unary";
unaryEvent.fire(unaryOperator);
Supplier supplier = () -> "Yes I am observing ALL";
supplierEvent.fire(supplier);
Supplier<String> stringSupplier = () -> "Yes I am observing STRINGS";
stringSupplierEvent.fire(stringSupplier);
}
public void observeEvent(@Observes UnaryOperator unaryOperator) {
System.out.println(unaryOperator.apply("observerEvent"));
}
public void secondObserver(@Observes UnaryOperator unaryOperator) {
System.out.println(unaryOperator.apply("secondObserver"));
}
public void consumeSupplier(@Observes Supplier supplier) {
System.out.println(supplier.get() + " IN GENERAL");
}
public void consumeStringSupplier(@Observes Supplier<String> supplier) {
System.out.println(supplier.get());
}
}
@antoinesd
Copy link

Because you discovered a bug in Weld ;) Just filled a ticket thanks to you: https://issues.jboss.org/browse/WELD-2108
This is linked to lambdas limitations http://jdk8-dev.openjdk.java.narkive.com/bSPiKxap/jdk-8-lambda-reflection-issues
We'll probably find a work around as described in the ticket.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment