title: Passing generic classes and objects to a Java method
body: <p>I want to replace the following with generics:</p>
  
  <ul>
  <li><code>for (Post post : postResponse.getResults()) {</code>, where <code>Post</code> can be any POJO.</li>
  <li><code>List&lt;Post&gt; posts = postResponse.getResults();</code> where <code>List&lt;Post&gt;</code> can be a list of anything I pass into it.</li>
  </ul>
  
  <p>What would my method call and method body look like?</p>
  
  <p><strong>Different examples of method calls:</strong></p>
  
  <pre><code>retrieveData(mCardAdapter, new Post(), Post.class);
  retrieveData(mCardAdapter, new Contact(), Contact.class);
  retrieveData(mCardAdapter, new Product(), Product.class);
  retrieveData(mCardAdapter, new Booking(), Booking.class);
  </code></pre>
  
  <p><strong>Method:</strong></p>
  
  <pre><code>private void retrieveData(final CardAdapter mCardAdapter, final Object&lt;T&gt; postObject, Class&lt;T&gt; postClass) {
          RetrofitService service = ServiceFactory.createRetrofitService(RetrofitService.class, RetrofitService.SERVICE_ENDPOINT);
          service.getPosts()
                  .subscribeOn(Schedulers.newThread())
                  .observeOn(AndroidSchedulers.mainThread())
                  .subscribe(new Subscriber&lt;PostResponse&gt;() {
                      @Override
                      public final void onCompleted() {
                          setRefreshingFalse();
                          Log.e("RetrofitService", "Retrofit Request Completed!");
                      }
  
                      @Override
                      public final void onError(Throwable e) {
                          setRefreshingFalse();
                          Log.e("RetrofitService", e.getMessage());
                      }
  
                      @Override
                      public final void onNext(PostResponse postResponse) {
                          if (postResponse != null) {
  
                              Log.e("RetrofitService", "Returned objects: " + postResponse.getResults());
  
                              for (postObject post : postResponse.getResults()) {
                                  Log.e("RetrofitService", post.getObjectId() + ": " + post.getText());
                              }
  
                              setRefreshingFalse();
  
                              mCardAdapter.clear();
                              List&lt;postClass&gt; posts = postResponse.getResults();
                              mCardAdapter.addData(posts);
  
                          } else {
                              Log.e("RetrofitService", "Object returned is null.");
                          }
  
                      }
  
                  });
      }
  </code></pre>
  
  <p>I'm getting <code>Unknown class: 'tClass'</code> and <code>Unknown class: 'postClass'</code>. Obviously this is not the way to do it so perhaps treat what I've shown above as pseduo-code. Does it makes sense what I'm trying to? I really want to generify this <code>retrieveData</code> method so that I can be used to query differences classes.</p>
  
  <p>To help with understanding. <strong>What I want to avoid:</strong></p>
  
  <pre><code>retrievePosts(mCardAdapter);
  retrieveUsers(mCardAdapter);
  
  private void retrievePosts(final CardAdapter mCardAdapter) {
          RetrofitService service = ServiceFactory.createRetrofitService(RetrofitService.class, RetrofitService.SERVICE_ENDPOINT);
          service.getPosts()
                  .subscribeOn(Schedulers.newThread())
                  .observeOn(AndroidSchedulers.mainThread())
                  .subscribe(new Subscriber&lt;PostResponse&gt;() {
                      @Override
                      public final void onCompleted() {
                          setRefreshingFalse();
                          Log.e("RetrofitService", "Retrofit Request Completed!");
                      }
  
                      @Override
                      public final void onError(Throwable e) {
                          setRefreshingFalse();
                          Log.e("RetrofitService", e.getMessage());
                      }
  
                      @Override
                      public final void onNext(PostResponse postResponse) {
                          if (postResponse != null) {
                              Log.e("RetrofitService", "Returned objects: " + postResponse.getResults());
  
                              for (Post post : postResponse.getResults()) {
                                  Log.e("RetrofitService", post.getObjectId() + ": " + post.getText());
                              }
  
                              /*for (Post post : postResponse.getResults()) {
                                  mCardAdapter.addData(post);
                              }*/
  
                              setRefreshingFalse();
  
                              mCardAdapter.clear();
                              List&lt;Post&gt; posts = postResponse.getResults();
                              mCardAdapter.addData(posts);
  
                          } else {
                              Log.e("RetrofitService", "Object returned is null.");
                          }
  
                      }
  
                  });
      }
  
      private void retrieveUsers(final CardAdapter mCardAdapter) {
          RetrofitService service = ServiceFactory.createRetrofitService(RetrofitService.class, RetrofitService.SERVICE_ENDPOINT);
          service.getUsers()
                  .subscribeOn(Schedulers.newThread())
                  .observeOn(AndroidSchedulers.mainThread())
                  .subscribe(new Subscriber&lt;PostResponse&gt;() {
                      @Override
                      public final void onCompleted() {
                          setRefreshingFalse();
                          Log.e("RetrofitService", "Retrofit Request Completed!");
                      }
  
                      @Override
                      public final void onError(Throwable e) {
                          setRefreshingFalse();
                          Log.e("RetrofitService", e.getMessage());
                      }
  
                      @Override
                      public final void onNext(PostResponse postResponse) {
                          if (postResponse != null) {
                              Log.e("RetrofitService", "Returned objects: " + postResponse.getResults());
  
                              for (User user : userResponse.getResults()) {
                                  Log.e("RetrofitService", user.getObjectId() + ": " + user.getText());
                              }
  
                              /*for (Post post : postResponse.getResults()) {
                                  mCardAdapter.addData(post);
                              }*/
  
                              setRefreshingFalse();
  
                              mCardAdapter.clear();
                              List&lt;User&gt; users = userResponse.getResults();
                              mCardAdapter.addData(users);
  
                          } else {
                              Log.e("RetrofitService", "Object returned is null.");
                          }
  
                      }
  
                  });
      }
  </code></pre>