or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/maven-org-powermock--powermock-classloading-xstream

Deep cloning library for objects across different classloaders using XStream serialization

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/org.powermock/powermock-classloading-xstream@2.0.x

To install, run

npx @tessl/cli install tessl/maven-org-powermock--powermock-classloading-xstream@2.0.0

index.mddocs/

PowerMock Classloading XStream

PowerMock Classloading XStream provides deep cloning capabilities for Java objects using XStream serialization. It enables objects to be deeply cloned across different classloaders, which is essential for PowerMock's advanced mocking features including static method mocking, final class mocking, and bytecode manipulation.

Package Information

  • Package Name: org.powermock:powermock-classloading-xstream
  • Package Type: maven
  • Language: Java
  • Version: 2.0.9
  • License: Apache-2.0
  • Installation: Include in Maven dependencies:
<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-classloading-xstream</artifactId>
    <version>2.0.9</version>
</dependency>

Core Imports

import org.powermock.classloading.DeepCloner;

Basic Usage

import org.powermock.classloading.DeepCloner;
import java.net.URLClassLoader;

// Create a deep cloner with the current context classloader
DeepCloner cloner = new DeepCloner();

// Or specify a specific classloader
ClassLoader customClassLoader = new URLClassLoader(urls);
DeepCloner customCloner = new DeepCloner(customClassLoader);

// Clone an object
MyObject original = new MyObject("example");
MyObject cloned = cloner.clone(original);

// The cloned object is a deep copy
assert original != cloned;
assert original.equals(cloned); // Assuming equals() is properly implemented

Capabilities

Object Deep Cloning

Creates complete deep copies of Java objects using XStream serialization, with support for cross-classloader cloning.

public class DeepCloner implements DeepClonerSPI {
    
    // Create deep cloner using current context classloader
    public DeepCloner();
    
    // Create deep cloner using specified classloader
    public DeepCloner(ClassLoader classLoader);
    
    // Clone an object with full deep copy semantics
    public <T> T clone(T objectToClone);
}

Constructor Details:

  • DeepCloner(): Creates a deep cloner using the current thread's context ClassLoader (Thread.currentThread().getContextClassLoader())
  • DeepCloner(ClassLoader classLoader): Creates a deep cloner using the specified ClassLoader for loading cloned classes

Method Details:

  • clone(T objectToClone):
    • Creates a deep clone of the supplied object using XStream XML serialization
    • Returns the same type as the input object
    • Supports cross-classloader cloning when a specific ClassLoader was provided
    • Handles complex object graphs including collections, arrays, and nested objects
    • Thread Safety: Not thread-safe (XStream instance is not thread-safe)

Usage Examples

Basic Cloning:

DeepCloner cloner = new DeepCloner();
String original = "Hello World";
String cloned = cloner.clone(original);

Collection Cloning:

import java.util.Arrays;
import java.util.List;

DeepCloner cloner = new DeepCloner();
List<String> originalList = Arrays.asList("a", "b", "c");
List<String> clonedList = cloner.clone(originalList);

Cross-ClassLoader Cloning:

import java.net.URL;
import java.net.URLClassLoader;

URLClassLoader targetClassLoader = new URLClassLoader(new URL[]{jarUrl});
DeepCloner cloner = new DeepCloner(targetClassLoader);
MyObject cloned = cloner.clone(originalObject);
// cloned object's class is loaded by targetClassLoader

Types

DeepClonerSPI Interface

public interface DeepClonerSPI {
    <T> T clone(T objectToClone);
}

The DeepClonerSPI interface from the org.powermock.classloading.spi package defines the contract that all deep cloners must implement.

Dependencies

This package requires the following dependencies:

  • XStream 1.4.10: Core XML serialization engine
  • powermock-classloading-base: Base classloading utilities and SPI interfaces

Security Considerations

The DeepCloner disables XStream's security restrictions to allow unrestricted type access, which is intended for testing scenarios where maximum flexibility is required. This configuration allows serialization of any type but should be used carefully in production environments.

Error Handling

The DeepCloner relies on XStream's exception handling for serialization errors. Common exceptions that may be thrown include:

  • Serialization errors for non-serializable objects
  • ClassNotFoundException when target classloader cannot load required classes
  • XStream configuration errors for complex object graphs

Implementation Notes

  • Uses XStream's toXML() and fromXML() methods for serialization/deserialization
  • Configured to omit the classloader field from SingleClassloaderExecutor instances during serialization
  • Security is disabled with allowTypesByRegExp(new String[]{".*"}) for maximum compatibility