package org.simpleframework.xml.core;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamClass;
import java.io.Serializable;
import java.lang.reflect.Method;

import junit.framework.TestCase;

public class ObjectStreamClassTest extends TestCase {
   
   public static class Tuple implements Serializable{
      private String name;
      private String value;
      public Tuple(String name, String value) {
         this.name = name;
         this.value = value;
      }
   }
   public void testObjectStreamTest() throws Exception {
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      ObjectOutputStream oout = new ObjectOutputStream(out);
      Tuple tuple = new Tuple("a", "b");
      
      oout.writeObject(tuple);
      byte[] array = out.toByteArray();
      ByteArrayInputStream in = new ByteArrayInputStream(array);
      ObjectInputStream oin = new ObjectInputStream(in);
      
      tuple = (Tuple)oin.readObject();
   }
   
   public void testInstantiation() throws Exception {
      ObjectStreamClass factory = ObjectStreamClass.lookup(Tuple.class);
      Method method = ObjectStreamClass.class.getDeclaredMethod("newInstance");
      
      if(!method.isAccessible()) {
         method.setAccessible(true);
      }
      Tuple tuple = (Tuple)method.invoke(factory);
      
      assertEquals(tuple.name, null);
      assertEquals(tuple.value, null);
   }

}

@@ -268,48 +268,159 @@
 ect.
-Method;%0A%0Aimport junit.framework.TestCase
+Constructor;%0Aimport java.lang.reflect.Method;%0Aimport java.lang.reflect.Modifier;%0A%0Aimport junit.framework.TestCase;%0Aimport sun.reflect.ReflectionFactory
 ;%0A%0Ap
@@ -702,16 +702,223 @@
  %7D%0A   %7D%0A
+   public static class Example %7B%0A      private String name;%0A      private String value;%0A      public Example(String name, String value) %7B%0A         this.name = name;%0A         this.value = value;%0A      %7D%0A   %7D%0A
    publi
@@ -1819,12 +1819,1883 @@
 l);%0A   %7D
+%0A   %0A   public void testInstantiationWithSerialization() throws Exception %7B%0A      Constructor constructor = getSerializableConstructor(Example.class);%0A      %0A      if(!constructor.isAccessible()) %7B%0A         constructor.setAccessible(true);%0A      %7D%0A      Example example = (Example)constructor.newInstance(null);%0A      %0A      assertEquals(example.name, null);%0A      assertEquals(example.value, null);%0A   %7D%0A   %0A   private static Constructor getSerializableConstructor(Class cl) throws Exception %7B%0A      Constructor constructors = ReflectionFactory.class.getDeclaredConstructor();%0A      %0A      if(!constructors.isAccessible()) %7B%0A         constructors.setAccessible(true);%0A      %7D%0A      ReflectionFactory reflFactory = (ReflectionFactory)constructors.newInstance();%0A      Class initCl = Object.class;%0A%0A      try %7B%0A          Constructor cons = initCl.getDeclaredConstructor(new Class%5B0%5D);%0A          int mods = cons.getModifiers();%0A          if ((mods & Modifier.PRIVATE) != 0 %7C%7C%0A         ((mods & (Modifier.PUBLIC %7C Modifier.PROTECTED)) == 0 &&%0A          !packageEquals(cl, initCl)))%0A          %7B%0A         return null;%0A          %7D%0A          cons = reflFactory.newConstructorForSerialization(cl, cons);%0A          cons.setAccessible(true);%0A          return cons;%0A      %7D catch (NoSuchMethodException ex) %7B%0A          return null;%0A      %7D%0A   %7D%0A   %0A   private static boolean packageEquals(Class cl1, Class cl2) %7B%0A      return (cl1.getClassLoader() == cl2.getClassLoader() &&%0A         getPackageName(cl1).equals(getPackageName(cl2)));%0A       %7D%0A%0A       /**%0A        * Returns package name of given class.%0A        */%0A       private static String getPackageName(Class cl) %7B%0A      String s = cl.getName();%0A      int i = s.lastIndexOf('%5B');%0A      if (i %3E= 0) %7B%0A          s = s.substring(i + 2);%0A      %7D%0A      i = s.lastIndexOf('.');%0A      return (i %3E= 0) ? s.substring(0, i) : %22%22;%0A       %7D
 %0A%0A%7D%0A

Update the instantiation