Replacing Unsafe.allocateInstance

Henri Tremblay henri at tremblay.pro
Sat Jun 27 00:54:22 UTC 2015


Hi,

I've started to follow the discussion about the Unsafe replacement.

I would like to help if that is possible. I'm in fact only really
interested in one method: allocateInstance. I've been implementing this
magical method by myself since 2003 when I had the idea to "invent" class
mocking. One of the requirement was to be able to bypass a constructor.

This problematic was later solved by Objenesis (of which I am one of the 3
creators) which basically implements allocateInstance for all JVMs.

It used to be done with ReflectionFactory.newConstructorForSerialization.

Objenesis can now also use allocateInstance but in fact it is not the
favored option on Hotspot.

My latest benchmarks were showing it much slower than
ReflectionFactory.newConstructorForSerialization. I must confess I haven't
had the time to investigate correctly to see where the difference is coming
from. And haven't try with JDK9 yet.

The benchmark is here:
https://github.com/easymock/objenesis/blob/master/Benchmarks.md

So, my goals are:

   1. Have a nice and standard way to allocate instances (without calling a
   constructor). Which would make Objenesis obsolete and my life easier
   2. Make it as fast or faster than anything else
   3. Ideally have only one official way to create an object like that

The problems to solve seem to be:

   1. Find a place to put the new method in the official java.* API
   2. Make it fast

Please tell me if anything seems worthy or it's just useless, annoying or
already solved.

Thanks,
Henri
<https://docs.google.com/document/d/1GDm_cAxYInmoHMor-AkStzWvwE9pw6tnz_CebJQxuUE/edit?usp=gmail>


More information about the jdk9-dev mailing list