title: Checking if Object is instance of List<Object>
body: <p>I have an <code>Object</code> that sometimes contains a <code>List&lt;Object&gt;</code>. I want to check it with <code>instanceof</code>, and if it is, then add some elements to it.</p>
  
  <pre><code>void add(Object toAdd) {
      Object obj = getValue();
      if (obj instanceof List&lt;?&gt;) {
          List&lt;?&gt; list = obj;
          if (list instanceof List&lt;Object&gt;) {    // Error
              ((List&lt;Object&gt;) list).add(toAdd);
          } else {
              List&lt;Object&gt; newList = new ArrayList&lt;Object&gt;(list);
              newList.add(toAdd);
              setValue(newList);
          }
          return;
      }
      throw new SomeException();
  }
  </code></pre>
  
  <p>And it says I can't check if it is <code>instanceof List&lt;Object&gt;</code> because java doesn't care and erased the type in <code>&lt;&gt;</code>.
  Does this mean I have to create new ArrayList every time? Or is there a way to check that, eg. with reflection?</p>