< prev index next >

src/java.base/share/classes/java/io/ObjectInputStream.java

Print this page

        

@@ -374,10 +374,12 @@
         vlist = null;
         serialFilter = ObjectInputFilter.Config.getSerialFilter();
         enableOverride = true;
     }
 
+    private ClassLoader cachedLoader = null;
+    
     /**
      * Read an object from the ObjectInputStream.  The class of the object, the
      * signature of the class, and the values of the non-transient and
      * non-static fields of the class and all of its supertypes are read.
      * Default deserializing for a class can be overridden using the writeObject

@@ -415,10 +417,18 @@
     {
         if (enableOverride) {
             return readObjectOverride();
         }
 
+        ClassLoader prevClassLoader = null;
+        boolean cached = false;
+        if (curContext == null) {
+            prevClassLoader = cachedLoader;
+            cachedLoader = latestUserDefinedLoader();
+            cached = true;
+        }
+
         // if nested read, passHandle contains handle of enclosing object
         int outerHandle = passHandle;
         try {
             Object obj = readObject0(false);
             handles.markDependency(outerHandle, passHandle);

@@ -431,10 +441,13 @@
                 freeze();
             }
             return obj;
         } finally {
             passHandle = outerHandle;
+            if (cached) {
+                cachedLoader = prevClassLoader;
+            }
             if (closed && depth == 0) {
                 clear();
             }
         }
     }

@@ -510,10 +523,17 @@
      * @throws  OptionalDataException if primitive data is next in stream
      * @throws  IOException if an I/O error occurs during deserialization
      * @since   1.4
      */
     public Object readUnshared() throws IOException, ClassNotFoundException {
+        ClassLoader prevClassLoader = null;
+        boolean cached = false;
+        if (curContext == null) {
+            prevClassLoader = cachedLoader;
+            cachedLoader = latestUserDefinedLoader();
+            cached = true;
+        }
         // if nested read, passHandle contains handle of enclosing object
         int outerHandle = passHandle;
         try {
             Object obj = readObject0(true);
             handles.markDependency(outerHandle, passHandle);

@@ -526,10 +546,13 @@
                 freeze();
             }
             return obj;
         } finally {
             passHandle = outerHandle;
+            if (cached) {
+                cachedLoader = prevClassLoader;
+            }
             if (closed && depth == 0) {
                 clear();
             }
         }
     }

@@ -681,10 +704,13 @@
     protected Class<?> resolveClass(ObjectStreamClass desc)
         throws IOException, ClassNotFoundException
     {
         String name = desc.getName();
         try {
+            if (cachedLoader != null)
+                return Class.forName(name, false, cachedLoader);
+            else
             return Class.forName(name, false, latestUserDefinedLoader());
         } catch (ClassNotFoundException ex) {
             Class<?> cl = primClasses.get(name);
             if (cl != null) {
                 return cl;
< prev index next >