codeconsole opened a new pull request, #3197:
URL: https://github.com/apache/groovy/pull/3197

   `withObjectInputStream(ClassLoader)` and `newObjectInputStream(ClassLoader)` 
fail with `ClassNotFoundException: int` for any stream that holds a primitive 
`Class`. That includes `[int.class]` and a dehydrated closure such as `{ int x 
-> x }.dehydrate()`, whose `parameterTypes` holds `int.class`. The InputStream, 
File and Path variants all fail, and so does the `clone()` generated by 
`@AutoClone(style=SERIALIZATION)`. The same stream reads fine through 
`withObjectInputStream` without a class loader.
   
   ```groovy
   def bytes = new ByteArrayOutputStream()
   bytes.withObjectOutputStream { it.writeObject([int.class]) }
   new 
ByteArrayInputStream(bytes.toByteArray()).withObjectInputStream(getClass().classLoader)
 { it.readObject() }
   // java.lang.ClassNotFoundException: int
   ```
   
   Seen in 4.0.33, 5.0.8 and 6.0.0 too.
   
   `IOGroovyMethods.newObjectInputStream(InputStream, ClassLoader)` overrides 
`ObjectInputStream.resolveClass` with `Class.forName`, which cannot load 
primitive types. All the class-loader-aware variants go through this override. 
The JDK's `resolveClass` falls back to a table of primitive types (`boolean` 
through `double`, plus `void`) when `Class.forName` fails, and the override now 
does the same. The given class loader is still tried first; a class it cannot 
see still fails with `ClassNotFoundException`, since the override does not fall 
back to `super.resolveClass`, which would resolve through a different loader.
   
   ## Tests
   
   These fail without the change:
   - `BinaryStreamsTest`: 
`testWithObjectStreamAndClassLoaderReadsPrimitiveClasses`, 
`testWithObjectStreamAndClassLoaderReadsDehydratedClosureWithPrimitiveParameters`
 and `testWithObjectStreamAndClassLoaderStillResolvesClassesThroughThatLoader`.
   - `CanonicalComponentsTransformTest`: 
`testCloningWithSerializablePrimitiveClassField` and 
`testCloningWithSerializablePrimitiveClassFieldCompileStatic`.
   - 
`NioExtensionsTest.testWithObjectInputStreamAndClassLoaderReadsPrimitiveClasses`.
   
   `IOGroovyMethodsTest`, `BinaryStreamsTest`, 
`CanonicalComponentsTransformTest`, `AutoCloneTransformTest`, 
`StreamClassloaderInScriptTest`, `Groovy11272`, 
`ClosureSerializationCycleTest`, `SerializableCompatibilityTest`, 
`ClosureTest`, `RecordTest`, `NamedValueTest`, `IntRangeTest` and 
`org.apache.groovy.nio.*` pass. The Checkstyle and CodeNarc gates pass.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to