title: How do you obtain a class definition byte[] for array classes?
body: <p>I'm trying to understand how the Java class loader works, specifically the <code>defineClass(String,byte[],int,int)</code> method.  Let's say I want to load the class definition for <code>NonIntrinsicType</code>.  I know I can find the *.class file for <code>NonIntrinsicType</code>, serialize its contents into a <code>byte[]</code> called <code>bytes</code>, and then call <code>defineClass(null,bytes,0,bytes.length)</code>.</p>
  
  <p>The weird situation I'm having trouble understanding is, how can I get a <code>byte[]</code> for the class definition of something like <code>[LNonIntrinsicType</code> - in other words, if my application requests the class definition for an instance of <code>NonIntrinsicType[]</code>, I don't have a *.class file that corresponds to this class type.  How would I obtain a <code>byte[]</code> for this type's definition?</p>
  
  <p>You're probably wondering why I want to do this.  I'm basically trying to create a network class loader, with a "Local" JVM that has class definitions on its class path, and a "Remote" JVM that does not have the aforementioned class definitions on its class path.  "Remote" is encountering an instance of <code>NonIntrinsicType[]</code> and requests a definition from "Local" for that class type, but my "Local" JVM can't find a class file for <code>NonIntrinsicType[]</code>, so it fails to provde a class definition <code>byte[]</code> to "Remote".</p>
  
  <p><strong>Added 9/7/2010</strong></p>
  
  <p>One thing I should have mentioned...  I am using <code>Socket</code> and <code>ObjectInputStream</code> to receive messages sent to "Remote" by "Local".  I've created a custom extension of <code>ObjectInputStream</code> on the "Remote" side and overrode <code>protected Class&lt;?&gt; resolveClass(ObjectStreamClass)</code>.  This is the first notification my "Remote" JVM gets when an incoming message contains instances of classes that have yet to be loaded.  I have another socket that I use to pass class definitions around with, so when the first socket receives an instance of a class that hasn't been loaded, it requests a definition for that class from the "Local" JVM via the second socket.</p>
  
  <p>The problem happens when my custom <code>ObjectInputStream.resolveClass(ObjectStreamClass)</code> gets passed an <code>ObjectStreamClass</code> that returns a class name for an array type (the name looks like <code>[Lsome.ClassName;</code>).  I can easily parse out the base-level type <code>some.ClassName</code> and load it first - this is done in the call to <code>NetworkClassLoader.getInstance.loadClass()</code>, where I get a <code>byte[]</code> for the base-level class and then use it to call <code>defineClass()</code> inside that method call.</p>
  
  <p>I understand now that you probably can't get a <code>byte[]</code> for an array type's class definition, so that leaves me trying to figure out how to find the <code>Class&lt;?&gt;</code> instance for the array type and return it.  Here's the code for my <code>resolveClass</code> method in my custom <code>ObjectInputStream</code> on the "Remote" JVM's message-receiving <code>Socket</code>:</p>
  
  <pre><code>@Override
  protected Class&lt;?&gt; resolveClass(ObjectStreamClass osc) 
          throws IOException, ClassNotFoundException{
      try {
          return super.resolveClass(osc);
      } catch (Exception e){}
  
      String name = osc.getName();
      boolean array = name.contains("[L");
  
      if(array) name = name.replace("[L","").
              replace("[","").replace(";","");
  
      Class&lt;?&gt; c = NetworkClassLoader.getInstance().loadClass(name);
      if(!array) return c;
  
      //If it's an array type, how do I obtain its corresponding
      //Class&lt;?&gt; and return it here?
  
      throw new ClassNotFoundException();
  }
  </code></pre>