title: Passing generic event to Guava EventBus?
body: <p>I've become very fond of Google Gauva's EventBus, so much that I want to include it in one of my Swing <a href="http://tomstechnicalblog.blogspot.com/2015/01/gridbagbuilder-wrapping-gridbaglayout.html" rel="noreferrer">GridBagBuilder</a> API's. The goal is to take a Swing component and do something with it in an arbitrary event, and subscribe this to an EventBus. The problem is I think the reflection operations done by the EventBus are not liking my generics for any arbitrary event type. </p>
  
  <p>Essentially, the method accepts a BiConsumer where C is a Swing Component and E is an arbitrary event type to subscribe to EventBus. </p>
  
  <pre><code>public &lt;E&gt; void subscribe(EventBus eventBus, BiConsumer&lt;C,E&gt; consumer) { 
      eventBus.register(new Object() { 
          @Subscribe
          public void processEvent(E event) { 
              try { 
              consumer.accept(comp, event);
              } catch (Exception e) { 
                  e.printStackTrace();
              }
          }
     });
  }
  </code></pre>
  
  <p>The event bus seems to be working but I keep getting bizarre <code>ClassCastException</code> errors. Is there any way I can make this work? Or is what I'm trying to achieve a lost cause?</p>
  
  <p>UPDATE: Here is an SSCCE. It breaks when there are multiple event types, and somewhere the generics and internal reflection mechanisms get messed up and it can't distinguish one event type from another. </p>
  
  <pre><code>import java.awt.Component;
  import java.util.function.BiConsumer;
  
  import javax.swing.JButton;
  
  import com.google.common.eventbus.EventBus;
  import com.google.common.eventbus.Subscribe;
  
  public class EventBusTest&lt;C extends Component&gt; {
      private final C comp;
  
      public static void main(String[] args) {
          final EventBus eventBus = new EventBus();
  
          EventBusTest&lt;JButton&gt; test = new EventBusTest&lt;&gt;(new JButton("Hello"));
          test.subscribe(eventBus, (JButton c, SomeEvent e) -&gt; System.out.println("Hello"));
          test.subscribe(eventBus, (JButton c, SomeOtherEvent e) -&gt; System.out.println("World"));
  
          eventBus.post(new SomeEvent());
      }
  
      private EventBusTest(C comp) { 
          this.comp = comp;
      }
      public&lt;E&gt; void subscribe(EventBus eventBus, BiConsumer&lt;C,E&gt; consumer) { 
          eventBus.register(new Object() { 
              @Subscribe
              public void processEvent(E event) { 
                  try { 
                  consumer.accept(comp, event);
                  } catch (Exception e) { 
                      e.printStackTrace();
                  }
              }
         });
      }
  
      private static final class SomeEvent {}
      private static final class SomeOtherEvent {}
  }
  </code></pre>
  
  <p>And here is the console print...</p>
  
  <pre><code>java.lang.ClassCastException: com.swa.rm.pricing.EventBusTest$SomeEvent cannot be cast to com.swa.rm.pricing.EventBusTest$SomeOtherEvent
      at com.swa.rm.pricing.EventBusTest$$Lambda$14/28594521.accept(Unknown Source)
      at com.swa.rm.pricing.EventBusTest$1.processEvent(EventBusTest.java:32)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      at java.lang.reflect.Method.invoke(Unknown Source)
      at com.google.common.eventbus.EventSubscriber.handleEvent(EventSubscriber.java:74)
      at com.google.common.eventbus.SynchronizedEventSubscriber.handleEvent(SynchronizedEventSubscriber.java:47)
      at com.google.common.eventbus.EventBus.dispatch(EventBus.java:322)
      at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:304)
      at com.google.common.eventbus.EventBus.post(EventBus.java:275)
      at com.swa.rm.pricing.EventBusTest.main(EventBusTest.java:21)
  Hello
  </code></pre>