<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">In brainstorming mode about fun that could be made possible with Java 8 and Java EE.<br class=""><br class="">Question in my mind is: is there some way we could make it possible for Lambdas or Method Refs to be CDI beans?<br class=""><br class="">It goes against the grain obviously as CDI creation is very much a “Don’t call us, we’ll call you” kind of thing. &nbsp;The VM dynamically creates a wrapper object around the Lambda or method reference and it implements the given interface.<br class=""><br class="">To make it work, there would need to be some non-producer method way of saying “put this thing in the context with these qualifiers”.<br class=""><br class="">Imagine a method somewhere that would allow you to:<br class=""><br class=""> &nbsp;&nbsp;&nbsp;public &lt;T&gt; void addObserver(java.util.function.Consumer&lt;T&gt; observer, Annotation... qualifiers);<br class=""><br class=""><br class="">Then you could take advantage as follows:<br class=""><br class=""> &nbsp;&nbsp;&nbsp;final List&lt;URI&gt; uris = new ArrayList&lt;&gt;();<br class=""> &nbsp;&nbsp;&nbsp;// @Observes URI<br class=""> &nbsp;&nbsp;&nbsp;addObserver((Consumer&lt;URI&gt;) uris::add);<br class=""><br class=""> &nbsp;&nbsp;&nbsp;// @Observes Thread<br class=""> &nbsp;&nbsp;&nbsp;addObserver(Runtime.getRuntime()::addShutdownHook);<br class=""><br class=""> &nbsp;&nbsp;&nbsp;// @Observes Runnable<br class=""> &nbsp;&nbsp;&nbsp;addObserver((Consumer&lt;Runnable&gt;) Executors.newFixedThreadPool(3)::submit);<br class=""><br class=""> &nbsp;&nbsp;&nbsp;// @Observes URI<br class=""> &nbsp;&nbsp;&nbsp;addObserver((Consumer&lt;URI&gt;) System.out::println, new AnnotationLiteral&lt;Fine&gt;() {<br class=""> &nbsp;&nbsp;&nbsp;});<br class=""><br class=""> &nbsp;&nbsp;&nbsp;// @Observes Handler<br class=""> &nbsp;&nbsp;&nbsp;final Logger logger = Logger.getLogger("somewhere");<br class=""> &nbsp;&nbsp;&nbsp;addObserver(logger::addHandler); // add handlers via event<br class=""><br class=""> &nbsp;&nbsp;&nbsp;// @Observes @Fine String<br class=""> &nbsp;&nbsp;&nbsp;addObserver((Consumer&lt;String&gt;) logger::fine, new AnnotationLiteral&lt;Fine&gt;() {});<br class=""> &nbsp;&nbsp;&nbsp;}<br class=""><br class=""><br class=""><br class="">-David<br class=""><br class=""><div apple-content-edited="true" class="">
<div style="color: rgb(0, 0, 0); font-family: Helvetica; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; " class=""><br class="Apple-interchange-newline"><br class=""></div><div style="color: rgb(0, 0, 0); font-family: Helvetica;  font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; " class="">--&nbsp;</div><div style="color: rgb(0, 0, 0); font-family: Helvetica;  font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; " class="">David Blevins<br class=""><a href="http://twitter.com/dblevins" class="">http://twitter.com/dblevins</a><br class="">http://www.tomitribe.com<br class=""></div>
</div>
<br class=""></body></html>