title: Java reflection: What does my Collection contain?
body: <p>I've defined a method in a class:</p>
  
  <pre><code>public void setCollection(Collection&lt;MyClass&gt;);
  </code></pre>
  
  <p>and in another class</p>
  
  <pre><code>public void setCollection(Collection&lt;OtherClass&gt;);
  </code></pre>
  
  <p>(and really, lots of similar classes)</p>
  
  <p>All are in classes with the same superclass, and I have a method in a support-class where I want to call this method and set it with items of the correct class type. Now, I can get that I'm setting a Collection by doing</p>
  
  <pre><code>Method setter = ...;
  Class&lt;?&gt; paramClass = setter.getParameterTypes()[0]; // Is Collection in this case
  if(paramClass.equals(Collection.class)) {
    HashSet col = new HashSet();
    // fill the set with something
    setter.invoke(this, col);
  }
  </code></pre>
  
  <p>But how do I figure out what class the objects in this collection should belong to?</p>
  
  <p>Cheers</p>
  
  <p>Nik</p>