JDK9 and Objenesis

Henri Tremblay henri.tremblay at gmail.com
Fri Jan 13 15:41:22 UTC 2017


Hi,

I did a release of Objenesis yesterday to provide Java 9 (151 jigsaw)
support. It has been on and off so far but the serialization part seemed to
be permanently broken so I fixed it.

For those who don't know, Objenesis a library dedicated to create objects
without calling a constructor. A hidden and forbidden feature of Java that
allowed mocks (EasyMock, Mockito) and proxies (Spring, JBoss) to thrive.

Objenesis has many instantiator and I have a little test to see which are
working on a given JVM. I tried to instantiate a JDK class (ArrayList) and
a class in my project.

There are 3 types of instantiator:

   - *Standard:* Just create the class without any constructor
   - *Serializable:* Creates an object like Java serialization will do
   (calling readObject and so on)
   - *Not compliant:* Instantiate a class but not respecting the rules of
   the 2 above (for instance, a constructor might be called). Those are used
   for test and as a sad fallback

Here are the current results so you can check if everything is as expected:

   - *AccessibleInstantiator (NOT_COMPLIANT):* Still working! Just turn
   accessible the default constructor. I was surprised it worked
   - *MagicInstantiator(STANDARD):* Not working anymore! This one is
   generating and instantiator that extends MagicAccessorImpl to be allowed to
   call `new Object()` on a class without the class loader complaining
   - *ObjectStreamClassInstantiator(SERIALIZATION):* Not working anymore! This
   is the one that was used so far. Because it's the fastest one for
   serialization. It doesn't work because setAccessible on
   ObjectStreamClass.newInstance doesn't work anymore. My personal take is
   that this method is so useful for serialization framework that it should
   have been public for years
   - *SunReflectionFactoryInstantiator (STANDARD):* Still working! This is
   the current default for hotspot and openjdk. It uses
   sun.reflect.ReflectionFactory*SunReflectionFactorySerializationInstantiator
   (SERIALIZATION): *Still working! This is the new default for
   serialization on Java 9. It wasn't the default so far for performance
   reasons
   - *UnsafeFactoryInstantiator (STANDARD):* Still working! Call
   Unsafe.allocateInstance. Happily still working. It is not the default for
   Hotspot because it is slower than SunReflectionFactoryInstantiator

So the good news are that it is still working without reinventing the wheel.

Henri


More information about the jdk9-dev mailing list