Created
February 10, 2016 20:19
-
-
Save olbpetersson/305276d7c4fec847a413 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.