This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[patch] merge from classpath: java/io/ObjectInputStream/StreamClass


Hi all,

ok for main if no regressions in mauve?

Andreas

2004-09-09 Jeroen Frijters <jeroen@frijters.net>

	* java/io/ObjectInputStream.java (readObject): Delegate instantation
	of Externalizable classes to ObjectStreamClass.
	* java/io/ObjectStreamClass.java (newInstance): New method to
	instantiate Externalizable (while ignoring the accessibility of the
	constructor).
	(constructor): New field to cache the constructor.
Index: java/io/ObjectStreamClass.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/io/ObjectStreamClass.java,v
retrieving revision 1.22
diff -u -r1.22 ObjectStreamClass.java
--- java/io/ObjectStreamClass.java	23 Jul 2004 22:20:14 -0000	1.22
+++ java/io/ObjectStreamClass.java	9 Sep 2004 20:00:00 -0000
@@ -811,6 +811,54 @@
     return fieldsArray;
   }
 
+  /**
+   * Returns a new instance of the Class this ObjectStreamClass corresponds
+   * to.
+   * Note that this should only be used for Externalizable classes.
+   *
+   * @return A new instance.
+   */
+  Externalizable newInstance() throws InvalidClassException
+  {
+    synchronized(this)
+    {
+	if (constructor == null)
+	{
+	    try
+	    {
+		final Constructor c = clazz.getConstructor(new Class[0]);
+
+		AccessController.doPrivileged(new PrivilegedAction()
+		{
+		    public Object run()
+		    {
+			c.setAccessible(true);
+			return null;
+		    }
+		});
+
+		constructor = c;
+	    }
+	    catch(NoSuchMethodException x)
+	    {
+		throw new InvalidClassException(clazz.getName(),
+		    "No public zero-argument constructor");
+	    }
+	}
+    }
+
+    try
+    {
+	return (Externalizable)constructor.newInstance(null);
+    }
+    catch(Throwable t)
+    {
+	throw (InvalidClassException)
+	    new InvalidClassException(clazz.getName(),
+		     "Unable to instantiate").initCause(t);
+    }
+  }
+
   public static final ObjectStreamField[] NO_FIELDS = {};
 
   private static Hashtable classLookupTable = new Hashtable();
@@ -840,6 +888,7 @@
   boolean realClassIsExternalizable;
   ObjectStreamField[] fieldMapping;
   Class firstNonSerializableParent;
+  private Constructor constructor;  // default constructor for Externalizable
 
   boolean isProxyClass = false;
 
Index: java/io/ObjectInputStream.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/io/ObjectInputStream.java,v
retrieving revision 1.33
diff -u -r1.33 ObjectInputStream.java
--- java/io/ObjectInputStream.java	9 Jul 2004 15:37:31 -0000	1.33
+++ java/io/ObjectInputStream.java	9 Sep 2004 20:00:02 -0000
@@ -291,29 +291,7 @@
 	      
 	      if (osc.realClassIsExternalizable)
 		{
-		  Externalizable obj = null;
-		  
-		  try
-		    {
-		      obj = (Externalizable)clazz.newInstance();
-		    }
-		  catch (InstantiationException e)
-		    {
-		      throw new ClassNotFoundException
-			("Instance of " + clazz + " could not be created");
-		    }
-		  catch (IllegalAccessException e)
-		    {
-		      throw new ClassNotFoundException
-			("Instance of " + clazz + " could not be created because class or "
-			 + "zero-argument constructor is not accessible");
-		    }
-		  catch (NoSuchMethodError e)
-		    {
-		      throw new ClassNotFoundException
-			("Instance of " + clazz
-			 + " could not be created because zero-argument constructor is not defined");
-		    }
+		  Externalizable obj = osc.newInstance();
 		  
 		  int handle = assignNewHandle(obj);
 		  

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]