Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've been thinking about a generic annotation-driven event framework as well. I like the benefits provided by static typing, but the current interface-driven event model is painful to use (ugly code). Would it be possible to use a custom annotation processor to do some compile-time checking? That might help add some of the missing "safety" that we've all grown used to.</p> <p>A lot of the error checking can also be done at the time that the listeners are "registered" with the event producers. Thus, the application would fail early (when the listeners are registered), possibly even at at startup-time.</p> <p>Here's an example of what the generic framework I've been toying with might look like:</p> <pre><code>public class ExampleProducer { private EventSupport&lt;ActionEvent&gt; eventSupport; public ExampleProducer() { eventSupport = new EventSupport&lt;ActionEvent&gt;(this); } @AddListenersFor(ActionEvent.class) public void addActionListener(Object listener) { eventSupport.addListener(listener); } @RemoveListenersFor(ActionEvent.class) public void removeActionListener(Object listener) { eventSupport.removeListener(listener); } public void buttonClicked() { eventSupport.fire(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Click")); } } </code></pre> <p>The producer uses <code>EventSupport</code>, which uses reflection to invoke the events. As mentioned before, <code>EventSupport</code> could preform some initial checks when the events listeners are registered.</p> <pre><code>public class ExampleListener { private ExampleProducer submitButton; public ExampleListener() { submitButton = new ExampleProducer(); EventSupport.autoRegisterEvents(this); } @HandlesEventFor("submitButton") public void handleSubmitButtonClick(ActionEvent event) { //...some code to handle the event here } } </code></pre> <p>Here, <code>EventSupport</code> has a static method that uses reflection to auto-register the listener with the event producer. This eliminates the need to manually register with the event source. A custom annotation processor could be used to validate that the <code>@HandlesEventFor</code> annotation refers to an actual field of the <code>ExampleListener</code>. The annotation processor could do other checks as well, such as ensuring that the event handler method signature matches up with one of the registration methods on the <code>ExampleProducer</code> (basically, the same check that could be performed at registration-time).</p> <p>What do you think? Is this worth putting some time into fully developing?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload