title: Is retrieving the generic parameter of a class using the reflection method guaranteed to work (in my case)?
body: <p>Alright, so I'm trying to design an event system for myself, and this is what I have so far for an event handler (Event is some empty class):</p>
  
  <pre><code>public interface EventHandler
  {
      public Class&lt;? extends Event&gt; type();
  
      public void handle(Event event);
  }
  </code></pre>
  
  <p>The goal is that an EventHandler can only handle one type of event (at this point).
  But, I wonder if I could reformat it like this:</p>
  
  <pre><code>public interface EventHandler &lt;T extends Event&gt;
  {
      public void handle(T event);
  }
  </code></pre>
  
  <p>This would help me in two ways:</p>
  
  <ul>
  <li>The need for <code>type()</code> is gone. </li>
  <li>I wouldn't have to cast while handling events.</li>
  </ul>
  
  <p>Based on this question: <a href="https://stackoverflow.com/questions/11164756/reflection-for-class-of-generic-parameter-in-java">Reflection for Class of generic parameter in Java?</a>, I can get <code>T</code> by this method:</p>
  
  <pre><code>ParameterizedType t = (ParameterizedType) MyClass.class.getGenericSuperclass(); //OtherClass&lt;String&gt;
  Class&lt;?&gt; clazz = (Class&lt;?&gt;) t.getActualTypeArguments()[0]; // Class&lt;String&gt;
  </code></pre>
  
  <p>I am aware that Type Erasure is a factor when dealing with stuff like this, but what I don't understand is when it would apply to my use of <code>EventHandler&lt;T&gt;</code>, or pretty much at all to be honest. For example, the only way I would use <code>EventHandler&lt;T&gt;</code> is:</p>
  
  <pre><code>public class TestEvent extends Event {}
  
  public class TestEventHandler implements EventHandler&lt;TestEvent&gt;
  {
      @Override
      public void handle(TestEvent event)
      {
          System.out.println("Event Handled");
      }
  }
  </code></pre>
  
  <p>(my Events would be in their own package, and the handlers would be in another)</p>
  
  <p>The soon-to-be event bus would register this handler by class and get the generic Event type of it to use as a key in a map of Event classes and EventHandlers.</p>
  
  <p>Anyways, any clarification would be appreciated. If I could only get the generic type like half the time or something, it wouldn't work out, and I'd probably have to do a version without generic types. Feel free to criticize how EventHandler is formatted to begin with in your answer if you want.</p>