Objenesis Documentation
repository·master·Indexed 20 days ago
https://github.com/easymock/objenesisA specialized Java library used to instantiate objects without calling their constructors, essential for mocking and proxying frameworks. It supports a wide range of JVMs including Sun/Oracle Hotspot, OpenJDK, Android, and others. The library provides multiple instantiation strategies (Standard, Single, and Custom) and supports various Java versions: 1.x (Java 1.3-1.7), 2.x (Java 1.5-10), and 3.x (Java 1.8-16).
What's inside Objenesis
- Objenesis is a library designed to bypass constructors when creating objects on any JVM. This allows for object instantiation without triggering the logic typically contained within a class's constructor.
Understand Objenesis support on Google App Engine (GAE)
masterDue to platform constraints, Objenesis has only partial support on Google App Engine (GAE). Only serializable objects can be instantiated, meaning the
Serializinginstantiator is the primary way to achieve correct behavior.Key Differences on GAE:
readResolve: If implemented by the class,readResolvewill be called on GAE. This does not happen on other platforms.- Standard Instantiator Behavior: The
Standardinstantiator on GAE is not ideal as it calls constructors, which partially defeats the purpose of Objenesis. It behaves as follows:- If the class is serializable: It calls the constructor from the first non-serializable parent.
- If the class is not serializable: It calls the default constructor.
Compatibility Matrix for GAE:
Class type Objenesis serializer Objenesis std Constructor throwing exception N/A n Constructor throwing exception (serializable) Y Y Constructor with arguments N/A n Constructor with arguments (serializable) Y Y Constructor with mandatory arguments N/A n Constructor with mandatory arguments (serializable) Y Y Default package constructor N/A Y Default package constructor (serializable) Y Y Default private constructor N/A Y Default private constructor (serializable) Y Y Default protected constructor N/A Y Default protected constructor (serializable) Y Y Default public constructor N/A Y Default public constructor (serializable) Y Y No constructor N/A Y No constructor (serializable) Y Y Serializable replacing with another class Y Y Serializable resolving to another class n n Serializable with ancestor throwing exception N/A n Note: 'Y' indicates the class was instantiated without exception, but a constructor may still have been called.
How instantiator caching works in Objenesis
masterWhen client code requests an instantiator for a specific class, Objenesis can either create a new one or return a cached instance. Instantiators are thread-safe, so using cached instances is safe for concurrent usage.
Historically, Objenesis used a
synchronizedimplementation for caching (Java 1.3 style), but modern versions use concurrent collections (Java 5+ style) to improve performance. Using a cache significantly reduces the time required to obtain an instantiator compared to creating one without a cache.Choosing an instantiation strategy
masterObjenesis provides different strategies for selecting how objects are instantiated. While performance differences between these strategies are generally minimal in practice, you can choose based on your requirements:
- Standard Strategy: The default approach. It automatically deduces the best instantiator for your specific platform.
- Single Strategy: Use this if you already know your platform and want to force the use of a single instantiator type that relies on reflection.
- Custom Strategy: Use this if you want to avoid reflection entirely by providing a custom instantiator that always uses the
newkeyword.
Update Objenesis versions
masterTo update the project versions across all modules, use the
versions:setcommand with theallprofile. After verifying the changes, you can either commit them or revert them.# Set new version mvn versions:set -DnewVersion=X.Y -Pall # If successful, commit the changes mvn versions:commit -Pall # If something is wrong, revert the changes mvn versions:revert -PallBuild Objenesis with Maven
masterYou can build the project using Maven with different profiles depending on the depth of the build required.
Basic Compilation
To perform a basic compilation of the application, run:
mvn installFull Build
To create source and javadoc JARs and run Spotbugs, use the
fullprofile:mvn install -Pfull# Basic build mvn install # Full build (includes javadoc, source jars, and spotbugs) mvn install -PfullRun Objenesis Benchmarks
masterTo run the project's benchmarks, package the project with the
benchmarkprofile, navigate to the benchmark directory, and execute the launch script.mvn package -Pbenchmark cd benchmark ./launch.shVerify Reproducible Builds
masterObjenesis follows guidelines to ensure that builds are reproducible (the same source always produces the same binary result). Use these commands to verify compatibility and integrity:
- Check plugin compatibility:
mvn artifact:check-buildplan -Pfull,all - Build and install:
mvn clean install -Pfull,all - Compare the built artifact with the installed one:
mvn clean verify artifact:compare -Pfull,all
# Check plugin compatibility mvn artifact:check-buildplan -Pfull,all # Build and install mvn clean install -Pfull,all # Compare artifact with installed one mvn clean verify artifact:compare -Pfull,all- Check plugin compatibility:
Run Android TCK
masterTo run the Android TCK, you must first set up the Android environment.
Prerequisites
MacOs / *nix
- Install the Android SDK:
brew cask install android-sdk - Install
platform-toolsandbuild-toolsviasdkmanager:sdkmanager "platform-tools" "build-tools" - Set the
ANDROID_HOMEenvironment variable:export ANDROID_HOME=$(realpath $(echo "$(dirname $(readlink $(which sdkmanager)))/../.."))
Windows
- Install Android Studio.
- Launch Android Studio and install the SDK and emulator.
- Add the path used to install the SDK to your
ANDROID_HOMEenvironment variable.
Execution
- Configure and launch a device (real or simulated). Note: Use API 26. Higher versions may require a signature that is not yet supported.
- If using a real device, activate debug mode.
- Run the build:
mvn package -Pandroid
- Install the Android SDK:
Configure Sonatype Maven Repository deployment
masterTo deploy to the Sonatype Maven repository, add the following server configuration to your
settings.xml:<servers> <server> <id>ossrh</id> <username>sonatypeuser</username> <password>sonatypepassword</password> </server> </servers>You must also follow the official instructions to create a PGP key to sign the deployed items.
Check supported JVMs and Objenesis versions
masterObjenesis supports a wide range of JVMs, including Sun/Oracle Hotspot, OpenJDK, Android, and various specialized JDKs like Azul Zulu and Zing. Compatibility depends on the version of Objenesis you are using:
- Objenesis 1.x: Supports Java 1.3 to 1.7
- Objenesis 2.x: Supports Java 1.5 to 10
- Objenesis 3.x: Supports Java 1.8 to 16
Specific JVM Support Details:
- Android: API level 8 to 30 (Serialization support requires Honeycomb or higher).
- Azul Zulu: Tested on 1.8.0_45-b14.
- Azul Zing JDK 1.8: Tested on 1.8.0-zing_16.10.1.0-b2.
- IKVM: Tested on 8.1.5717.
- BEA JRockit R27, R28: Supported by Objenesis versions < 3.
- GCJ: Version 3.4.4 (tested on Windows/Cygwin).
- PTC Perc: Tested on version 5.0.0667 (Note: No serialization support).