talira

Objenesis

Create Java objects without calling their constructors

open sourceApache-2.0free

Objenesis is a small Java library that instantiates a class without calling its constructor, for classes whose constructors need arguments, have side effects or throw. Serialization, remoting and persistence libraries, proxy and AOP libraries and mock-object frameworks use it, and it can be embedded inside another library. It tries several strategies depending on the JVM, its vendor and the security manager, and ships a TCK to check what works in a given environment.

Problems it solves

  • create an instance of a Java class that has no no-arg constructor without calling its constructor
  • my mocking or serialization library needs to instantiate objects while skipping constructor side effects
  • restore Java objects from storage without running their constructor code

Features

  • instantiates objects without invoking a constructor
  • standard and serialization-compatible instantiators
  • picks a strategy per JVM, vendor and security manager
  • a TCK to test which classes can be instantiated in your environment
  • can be embedded in another library

Limitations

  • On recent Java versions it relies on sun.misc.Unsafe and reflective access, so modular applications may need --add-modules jdk.unsupported or --add-opens flags.1