title: Java gson.fromJson() to POJO using Reflection causes java.lang.ClassCastException
body: <p>I am having a bit of a nightmarish issue parsing some JSON using Gson.</p>
  
  <p>The Json structure I am trying to parse looks like this:</p>
  
  <pre><code>{ "user_id" : "833", "message" : "User created and registered" }
  </code></pre>
  
  <p>When I parse the Json using the following code:</p>
  
  <pre><code>Type response = gson.fromJson(jsonString, new TypeToken&lt;RestResponse&gt;(){}.getType());
  </code></pre>
  
  <p>I receive the following error:</p>
  
  <p>java.lang.ClassCastException: ... RestResponse cannot be cast to java.lang.reflect.Type</p>
  
  <p>I am parsing into the following POJO:</p>
  
  <pre><code>import java.lang.reflect.Type;
  
  public class RestResponse {
  
      private void RestResponse() {
          // Constructor required for GSON
      }
  
      private StatusCode statusCode;
      private String content;
      private Object data;
      private Type output;
      private String user_id;
      private String message;
      private String error;
  
      public String getUserID() {
          return this.user_id;
      }
      public void setUserID(String user_id) {
          this.user_id = user_id;
      }
      public String getMessage() { return this.message; }
      public void setMessage(String message) {
          this.message = message;
      }
      public String getError() { return this.error; }
      public void setError(String error) { this.error = error; }
      public StatusCode getStatusCode() {
          return statusCode;
      }
      public void setStatusCode(StatusCode statusCode) {
          this.statusCode = statusCode;
      }
      public final String getContent() {
          return content;
      }
      public final void setContent(final String content) {
          this.content = content;
      }
      public final Object getData() {
          return data;
      }
      public final void setData(final Object data) {
          this.data = data;
      }
      public final Type getOutput() {
          return output;
      }
      public final void setOutput(final Type output) {
          this.output = output;
      }
      @Override
      public final String toString() {
          return String.format("%s", this.content);
      }
  }
  </code></pre>
  
  <p>The reason I am returning a Type as opposed to the RestResponse object is because I am calling a callback depending upon the type of response Type. I then process the code like so:</p>
  
  <pre><code>if (statusCode == StatusCode.SUCCESS) {
      if (requestCode == AppConstants.REQUEST_REGISTRATION) {
          if (data instanceof RestResponse) {
              RestResponse restResponse       = (RestResponse) data;
              AppDelegate delegate            = (AppDelegate) getApplicationContext();
              delegate.name                   = nameText.getText().toString();
              delegate.email                  = emailText.getText().toString();
              delegate.mobile                 = validatedFormattedNum;
              delegate.referredID             = "";
              delegate.isRegistered           = 1;
              delegate.uid                    = restResponse.getUserID();
              delegate.setInMemory();
          } else {
              if(data instanceof ApiErrorResponse) {
                  ApiErrorResponse apiErrorResponse = (ApiErrorResponse) data;
                  ...
                  ...
                  ...
  </code></pre>
  
  <p>A stack trace is here should anybody like to see:</p>
  
  <pre><code>E/com.sourgun.http.AppParser: General exception encountered in parse()    com.sourgun.http.RestResponse cannot be cast to java.lang.reflect.Type
  04-22 12:51:13.748 31779-31779/? W/System.err: java.lang.ClassCastException: com.sourgun.http.RestResponse cannot be cast to java.lang.reflect.Type
  04-22 12:51:13.748 31779-31779/? W/System.err:     at com.sourgun.http.AppParser.parse(AppParser.java:37)
  04-22 12:51:13.749 31779-31779/? W/System.err:     at com.sourgun.http.RestController.onResponse(RestController.java:85)
  04-22 12:51:13.750 31779-31779/? W/System.err:     at com.sourgun.http.AsyncTaskExecutor.onPostExecute(AsyncTaskExecutor.java:66)
  04-22 12:51:13.750 31779-31779/? W/System.err:     at com.sourgun.http.AsyncTaskExecutor.onPostExecute(AsyncTaskExecutor.java:14)
  04-22 12:51:13.751 31779-31779/? W/System.err:     at android.os.AsyncTask.finish(AsyncTask.java:651)
  04-22 12:51:13.751 31779-31779/? W/System.err:     at android.os.AsyncTask.-wrap1(AsyncTask.java)
  04-22 12:51:13.752 31779-31779/? W/System.err:     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
  04-22 12:51:13.753 31779-31779/? W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
  04-22 12:51:13.753 31779-31779/? W/System.err:     at android.os.Looper.loop(Looper.java:148)
  04-22 12:51:13.754 31779-31779/? W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5422)
  04-22 12:51:13.754 31779-31779/? W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
  04-22 12:51:13.755 31779-31779/? W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
  04-22 12:51:13.755 31779-31779/? W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
  </code></pre>