title: Generics overloading of method - workaround
body: <p>this question was asked many times but I couldn't find elegant workaround for it.
  This example works as desired:</p>
  
  <pre><code>public class RequestWrapper&lt;T&gt; {
      private final T request;
      private final Class&lt;T&gt; type;
  
      public RequestWrapper(T request, Class&lt;T&gt; type) {
          this.request = request;
          this.type = type;
      }
  
      public T getRequest() {
          return request;
      }
  
      public Class&lt;T&gt; getType() {
          return type;
      }
  }
  
  public class Service {
  
      private void invoke(String request) {
          System.out.println("String:" + request);
      }   
  
      private void invoke(Object request) {
          System.out.println("Object:" + request + "," + request.getClass().getSimpleName());
      }   
  
      public static void main(String[] args) {
          RequestWrapper&lt;String&gt; sw = new RequestWrapper&lt;String&gt;("A", String.class);
          RequestWrapper&lt;Integer&gt; iw = new RequestWrapper&lt;Integer&gt;(Integer.valueOf(0), Integer.class);
  
          new Service().invoke(sw.getRequest());
          new Service().invoke(iw.getRequest());
      }
  }
  </code></pre>
  
  <p>But I would need to add one more method to Service class which do something before/after call of invoke method:</p>
  
  <pre><code>public void invoke(RequestWrapper&lt;?&gt; wrapper) {
      try {
          // ...
          invoke(wrapper.getType().cast(wrapper.getRequest()));
          invoke(wrapper.getRequest());
      } catch(Exception e ) {
          // ...
      }
  }
  </code></pre>
  
  <p>then the main method would contain:</p>
  
  <pre><code>new Service().invoke(sw);
  </code></pre>
  
  <p>I understand the reason why the invoke(Object request) is used instead of invoke(String request).
  What would be an elegant solution to call proper invoke method and be able to do some common actions before/after it?</p>
  
  <p><em>To have an interface e.g. Invoker, implement it e.g. StringInvoker, Invoker> and call map.get(wrapper.getType()).invoke(wrapper.getRequest()) is possible solution but I expect something better.</em></p>