title: Casting to a Class which is determined at run-time
body: <p>I have a method <code>fetchObjects(String)</code> that is expected to return an array of <code>Contract</code> business objects. The <code>className</code> parameter tells me what kind of business objects I should return (of course this doesn't make sense in this construed case because I already said I will return <code>Contract</code>s, but it's basically the situation I have in my real scenario). So I get the set of entries from somewhere and load the class of the collection's entries (the type of which is specified by <code>className</code>).</p>
  
  <p>Now I need to construct the array to return, so I use <code>Set</code>'s <code>toArray(T[])</code> method. Using reflection, I build myself an empty Contracts array. <em>But,</em> this gives me a value of static type <code>Object</code>! So next I need to cast it to the appropriate type, which in this case is <code>Contract[]</code> (see "asterisk-underlined" part in the listing below).</p>
  
  <p>My question is: Is there a way, and how, to cast to <code>Contract[]</code> as I do in the listing, <em>but determining the type of the array elements (<code>Contract</code>) only through <code>className</code></em> (or <code>entriesType</code>)? In other words, what I'd like to do is basically casting like this: <code>(entriesType[]) valueWithStaticTypeObject</code>, where entriesType be replaced by the class specified through the <code>classname</code> parameter, i.e. <code>Contract</code>.</p>
  
  <p>Is this somehow inherently impossible, or can it be done somehow? Maybe using generics?</p>
  
  <pre><code>package xx.testcode;
  
  import java.util.HashSet;
  import java.util.Set;
  
  class TypedArrayReflection {
  
      public static void main(String[] args) {
          try {
              Contract[] contracts = fetchObjects("Contract");
              System.out.println(contracts.length);
          } catch (ClassNotFoundException e) {}
      }
  
      static Contract[] fetchObjects(String className) throws ClassNotFoundException {
          Class&lt;?&gt; entriesType = Class.forName("xx.testcode."+className);
          Set&lt;?&gt; entries = ObjectManager.getEntrySet(className); 
          return entries.toArray( 
                  (Contract[]) java.lang.reflect.Array.newInstance(
                  /********/          entriesType, entries.size()) );
      }
  }
  
  class Contract { } // business object
  
  class ObjectManager {
      static Set&lt;?&gt; getEntrySet(String className) {
          if (className.equals("Contract"))
              return new HashSet&lt;Contract&gt;();
          return null; // Error
      }
  }
  </code></pre>
  
  <p>Thanks.</p>
  
  <p><hr>
  <strong>Update:</strong> Using the type-safe method <code>toArray</code>, taken from <a href="http://codeidol.com/java/javagenerics/Reification/The-Principle-of-Truth-in-Advertising/" rel="nofollow noreferrer">CodeIdol</a>, I updated my <code>fetchObjects</code> method thus:</p>
  
  <pre><code>static Contract[] fetchObjects(String className) throws ClassNotFoundException {
      Class&lt;?&gt; entriesType = Class.forName("xx.testcode."+className);
      Set&lt;?&gt; entries = ObjectManager.getEntrySet(className);
      return toArray(entries, entriesType); // compile error
      // -&gt; "method not applicable for (Set&lt;capture#3-of ?&gt;, Class&lt;capture#4-of ?&gt;)"
  }
  
  public static &lt;T&gt; T[] toArray(Collection&lt;T&gt; c, Class&lt;T&gt; k) {
      T[] a = (T[]) java.lang.reflect.Array.newInstance(k, c.size());
      int i = 0;
      for (T x : c)
          a[i++] = x;
      return a;
  }
  </code></pre>
  
  <p>What do I need to do to get rid of the compiler error quoted in the comment? Do I absolutely have to specify <code>Set&lt;Contract&gt;</code> in the return type of my <code>getEntrySet</code> method so that this can work? Thanks for any pointers.</p>