title: Java IllegalCastException when casting object returned from API call
body: <p>I am trying to integrate a public google calendar into my android application. To do this without the complicated oauth2 flow, I used a firebase cloud function to perform the API call. However, the json returned to my application has the java.lang Object type. I tried casting this to the google calendar Events type and I receive a IllegalCastException.</p>
  
  <p>What I have tried:
  First I tried to use gson to convert the object to a json string, then use the fromJson function to convert the string back to the Events class. This sorta worked, but I would get an error stating the argument "events.Updated" is type DateTime, but got String... I tried various methods of making sure gson was using the correct date format but in the end I tried something else.</p>
  
  <p>Then I tried to use reflection to convert the object but then I found myself having to use reflection for every sub object and it was getting ugly so I stopped.</p>
  
  <p>Here is what I tried for reflection:</p>
  
  <pre><code>Events placeholderEvents = new Events();
  Class aClass = Events.class;
  for (Field field : aClass.getDeclaredFields()) {
      field.setAccessible(true);
      if (map.containsKey(field.getName())) {
          if (field.getType().toString().equals("class com.google.api.client.util.DateTime")) {
              SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
              Timestamp datetime = new Timestamp(simpleDateFormat.parse((String) map.get(field.getName())).getTime());
              DateTime date = new DateTime(datetime);
              field.set(placeholderEvents, date);
          } else if (field.getType().toString().equals("interface java.util.List")) {
              ArrayList&lt;Object&gt; innerArray = (ArrayList&lt;Object&gt;) map.get(field.getName());
              List&lt;Event&gt; placeholderList = new ArrayList&lt;&gt;();
              Class secondClass = placeholderList.getClass();
              for (Object object : innerArray) {
                  for (Field innerField : secondClass.getDeclaredFields()) {
                      innerField.setAccessible(true);
                      if (innerArray.contains(innerField.getName())) {
                          Log.i("innerfield", "" + innerArray.get(innerArray.indexOf(innerField.getName())));
                          innerField.set(placeholderList, innerArray.get(innerArray.indexOf(innerField.getName())));
                      }
                  }
              }
              field.set(placeholderEvents, placeholderList);
          } else {
              field.set(placeholderEvents, map.get(field.getName()));
          }
      }
  }
  return placeholderEvents;
  </code></pre>
  
  <p>So my question is, how can I cast an Object of which I know the type of its contents at compile time to its type so that I can access the type specific functions? I know it is supposed to be of type Events, but I cannot cast it from Object.  </p>
  
  <p>Is something like this possible?</p>
  
  <pre><code>Object obj = task.getResult().getData();
  Events events = (Events) obj;
  </code></pre>