Objenesis Documentation

repository·master·Indexed 20 days ago

https://github.com/easymock/objenesis

A 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).

Tokens
1.9K
Snippets
5
Records
11
Agent score
71%

What's inside Objenesis

  1. Understand Objenesis support on Google App Engine (GAE)

    master

    Due to platform constraints, Objenesis has only partial support on Google App Engine (GAE). Only serializable objects can be instantiated, meaning the Serializing instantiator is the primary way to achieve correct behavior.

    Key Differences on GAE:

    • readResolve: If implemented by the class, readResolve will be called on GAE. This does not happen on other platforms.
    • Standard Instantiator Behavior: The Standard instantiator 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 typeObjenesis serializerObjenesis std
    Constructor throwing exceptionN/An
    Constructor throwing exception (serializable)YY
    Constructor with argumentsN/An
    Constructor with arguments (serializable)YY
    Constructor with mandatory argumentsN/An
    Constructor with mandatory arguments (serializable)YY
    Default package constructorN/AY
    Default package constructor (serializable)YY
    Default private constructorN/AY
    Default private constructor (serializable)YY
    Default protected constructorN/AY
    Default protected constructor (serializable)YY
    Default public constructorN/AY
    Default public constructor (serializable)YY
    No constructorN/AY
    No constructor (serializable)YY
    Serializable replacing with another classYY
    Serializable resolving to another classnn
    Serializable with ancestor throwing exceptionN/An

    Note: 'Y' indicates the class was instantiated without exception, but a constructor may still have been called.

  2. How instantiator caching works in Objenesis

    master

    When 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 synchronized implementation 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.

  3. Choosing an instantiation strategy

    master

    Objenesis 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:

    1. Standard Strategy: The default approach. It automatically deduces the best instantiator for your specific platform.
    2. 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.
    3. Custom Strategy: Use this if you want to avoid reflection entirely by providing a custom instantiator that always uses the new keyword.
  4. Update Objenesis versions

    master

    To update the project versions across all modules, use the versions:set command with the all profile. 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 -Pall
  5. Build Objenesis with Maven

    master

    You 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 install

    Full Build

    To create source and javadoc JARs and run Spotbugs, use the full profile: mvn install -Pfull

    # Basic build
    mvn install
    
    # Full build (includes javadoc, source jars, and spotbugs)
    mvn install -Pfull
  6. Verify Reproducible Builds

    master

    Objenesis 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
  7. Run Android TCK

    master

    To run the Android TCK, you must first set up the Android environment.

    Prerequisites

    MacOs / *nix

    1. Install the Android SDK: brew cask install android-sdk
    2. Install platform-tools and build-tools via sdkmanager: sdkmanager "platform-tools" "build-tools"
    3. Set the ANDROID_HOME environment variable: export ANDROID_HOME=$(realpath $(echo "$(dirname $(readlink $(which sdkmanager)))/../.."))

    Windows

    1. Install Android Studio.
    2. Launch Android Studio and install the SDK and emulator.
    3. Add the path used to install the SDK to your ANDROID_HOME environment variable.

    Execution

    1. Configure and launch a device (real or simulated). Note: Use API 26. Higher versions may require a signature that is not yet supported.
    2. If using a real device, activate debug mode.
    3. Run the build: mvn package -Pandroid
  8. Configure Sonatype Maven Repository deployment

    master

    To 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.

  9. Check supported JVMs and Objenesis versions

    master

    Objenesis 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).