CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-aspectj--aspectjweaver

The AspectJ weaver applies aspects to Java classes and can be used as a Java agent for load-time weaving (LTW).

Pending
Overview
Eval results
Files

caching.mddocs/

Class Caching

The AspectJ weaver provides a comprehensive caching system for weaved and generated classes to improve performance across JVM restarts. The caching system stores the results of weaving operations and can significantly reduce startup time for applications with many classes.

Core Cache Classes

WeavedClassCache

Main cache management class that handles weaved and generated classes.

public class WeavedClassCache {
    public static WeavedClassCache createCache(ClassLoader loader, List<String> aspects, 
        GeneratedClassHandler existingClassHandler, IMessageHandler messageHandler);
    public static void setDefaultCacheFactory(CacheFactory factory);
    public static boolean isEnabled();
    public static List<WeavedClassCache> getCaches();
    public CachedClassReference createGeneratedCacheKey(String className);
    public CachedClassReference createCacheKey(String className, byte[] originalBytes);
    public GeneratedClassHandler getCachingClassHandler();
    public void put(CachedClassReference ref, byte[] classBytes, byte[] weavedBytes);
    public CachedClassEntry get(CachedClassReference ref, byte[] classBytes);
    public void ignore(CachedClassReference ref, byte[] classBytes);
    public void remove(CachedClassReference ref);
    public void clear();
    public CacheStatistics getStats();
    public String getName();
}

Static Methods

public static WeavedClassCache createCache(ClassLoader loader, List<String> aspects, 
    GeneratedClassHandler existingClassHandler, IMessageHandler messageHandler)

Creates a cache instance for the specified classloader and aspects.

Parameters:

  • loader - ClassLoader to create cache for
  • aspects - List of aspect names that will be applied
  • existingClassHandler - Handler for generated classes (can be null)
  • messageHandler - Handler for cache messages (can be null)

Returns: WeavedClassCache instance for the classloader

public static void setDefaultCacheFactory(CacheFactory factory)

Sets a custom cache factory for creating cache implementations.

Parameters:

  • factory - Custom cache factory implementation
public static boolean isEnabled()

Returns whether caching is enabled system-wide.

Returns: True if caching is enabled

public static List<WeavedClassCache> getCaches()

Returns all active cache instances.

Returns: List of all WeavedClassCache instances

Instance Methods

public CachedClassReference createGeneratedCacheKey(String className)

Creates a cache key for a generated class.

Parameters:

  • className - Name of the generated class

Returns: CachedClassReference for the generated class

public CachedClassReference createCacheKey(String className, byte[] originalBytes)

Creates a cache key for a weaved class.

Parameters:

  • className - Name of the class
  • originalBytes - Original class bytes before weaving

Returns: CachedClassReference for the class

public void put(CachedClassReference ref, byte[] classBytes, byte[] weavedBytes)

Stores weaved class in the cache.

Parameters:

  • ref - Cache reference for the class
  • classBytes - Original class bytes
  • weavedBytes - Weaved class bytes
public CachedClassEntry get(CachedClassReference ref, byte[] classBytes)

Retrieves a cached class entry.

Parameters:

  • ref - Cache reference for the class
  • classBytes - Original class bytes for validation

Returns: CachedClassEntry if found, null otherwise

public void ignore(CachedClassReference ref, byte[] classBytes)

Marks a class to be ignored by the cache.

Parameters:

  • ref - Cache reference for the class
  • classBytes - Class bytes
public void remove(CachedClassReference ref)

Removes a class from the cache.

Parameters:

  • ref - Cache reference to remove
public void clear()

Clears all entries from the cache.

public CacheStatistics getStats()

Returns cache performance statistics.

Returns: CacheStatistics object with performance metrics

public String getName()

Returns the cache name.

Returns: String name identifying the cache

Cache Factory Interface

CacheFactory

Interface for creating custom cache implementations.

public interface CacheFactory {
    CacheKeyResolver createResolver();
    CacheBacking createBacking(String scope);
}

createResolver

Creates a cache key resolver.

CacheKeyResolver createResolver()

Returns: CacheKeyResolver implementation

createBacking

Creates a cache backing store.

CacheBacking createBacking(String scope)

Parameters:

  • scope - Scope identifier for the cache backing

Returns: CacheBacking implementation

DefaultCacheFactory

Default implementation of CacheFactory.

public class DefaultCacheFactory implements CacheFactory {
    public CacheKeyResolver createResolver();
    public CacheBacking createBacking(String scope);
}

SimpleCacheFactory

Utility class for creating simple file-based caches, used internally by the weaver.

public class SimpleCacheFactory {
    public static final String CACHE_ENABLED_PROPERTY = "aj.weaving.cache.enabled";
    public static final String CACHE_DIR = "aj.weaving.cache.dir";
    public static final String CACHE_IMPL = "aj.weaving.cache.impl";
    public static SimpleCache createSimpleCache();
    public static boolean isEnabled();
}

createSimpleCache

Creates a simple cache instance based on system properties.

public static SimpleCache createSimpleCache()

Returns: SimpleCache instance if caching is enabled, null otherwise

isEnabled

Returns whether simple caching is enabled via system properties.

public static boolean isEnabled()

Returns: True if simple caching is enabled

Cache Key Resolution

CacheKeyResolver

Interface for generating and resolving cache keys.

public interface CacheKeyResolver {
    CachedClassReference generatedKey(String className);
    CachedClassReference weavedKey(String className, byte[] original_bytes);
    String keyToClass(String key);
    String createClassLoaderScope(ClassLoader loader, List<String> aspects);
    String getGeneratedRegex();
    String getWeavedRegex();
}

generatedKey

Generates a cache key for a generated class.

CachedClassReference generatedKey(String className)

Parameters:

  • className - Name of the generated class

Returns: CachedClassReference for the generated class

weavedKey

Generates a cache key for a weaved class.

CachedClassReference weavedKey(String className, byte[] original_bytes)

Parameters:

  • className - Name of the class
  • original_bytes - Original class bytes

Returns: CachedClassReference for the weaved class

keyToClass

Converts a cache key back to a class name.

String keyToClass(String key)

Parameters:

  • key - Cache key string

Returns: Class name corresponding to the key

createClassLoaderScope

Creates a scope identifier for a classloader and aspects.

String createClassLoaderScope(ClassLoader loader, List<String> aspects)

Parameters:

  • loader - ClassLoader instance
  • aspects - List of aspect names

Returns: Scope identifier string

DefaultCacheKeyResolver

Default implementation of CacheKeyResolver.

public class DefaultCacheKeyResolver implements CacheKeyResolver {
    // Implementation of all CacheKeyResolver methods
}

Cache Backing Store

CacheBacking

Interface for cache storage backend.

public interface CacheBacking {
    String[] getKeys(String regex);
    void remove(CachedClassReference ref);
    void clear();
    CachedClassEntry get(CachedClassReference ref, byte[] originalBytes);
    void put(CachedClassEntry entry, byte[] originalBytes);
}

getKeys

Returns all cache keys matching a regex pattern.

String[] getKeys(String regex)

Parameters:

  • regex - Regular expression pattern to match

Returns: Array of matching cache keys

get

Retrieves a cache entry.

CachedClassEntry get(CachedClassReference ref, byte[] originalBytes)

Parameters:

  • ref - Cache reference
  • originalBytes - Original class bytes for validation

Returns: CachedClassEntry if found, null otherwise

put

Stores a cache entry.

void put(CachedClassEntry entry, byte[] originalBytes)

Parameters:

  • entry - Cache entry to store
  • originalBytes - Original class bytes

File-Based Cache Implementations

DefaultFileCacheBacking

Default file-based cache implementation.

public class DefaultFileCacheBacking extends AbstractFileCacheBacking {
    // File-based cache storage
}

FlatFileCacheBacking

Flat file cache implementation storing each class in a separate file.

public class FlatFileCacheBacking extends AbstractIndexedFileCacheBacking {
    // Flat file storage implementation
}

ZippedFileCacheBacking

Zipped file cache implementation for compressed storage.

public class ZippedFileCacheBacking extends AbstractIndexedFileCacheBacking {
    // Zipped file storage implementation
}

AsynchronousFileCacheBacking

Asynchronous file cache for non-blocking cache operations.

public class AsynchronousFileCacheBacking extends AbstractFileCacheBacking {
    // Asynchronous file operations
}

Data Types

CachedClassReference

Reference to a cached class with key information.

public class CachedClassReference {
    // Methods for accessing cache key information
}

CachedClassEntry

Represents a cached class entry with type information.

public class CachedClassEntry {
    // Methods for accessing cached class data
}

CacheStatistics

Statistics tracking for cache operations.

public class CacheStatistics {
    // Methods for accessing cache performance metrics
}

Usage Examples

Basic Cache Usage

import org.aspectj.weaver.tools.cache.WeavedClassCache;
import java.util.Arrays;
import java.util.List;

public class BasicCacheExample {
    public static void main(String[] args) {
        ClassLoader loader = MyClass.class.getClassLoader();
        List<String> aspects = Arrays.asList("com.example.LoggingAspect", "com.example.SecurityAspect");
        
        // Create cache for classloader
        WeavedClassCache cache = WeavedClassCache.createCache(loader, aspects, null, null);
        
        // Create cache key
        byte[] originalBytes = getOriginalClassBytes("com.example.MyClass");
        CachedClassReference ref = cache.createCacheKey("com.example.MyClass", originalBytes);
        
        // Check if class is cached
        CachedClassEntry entry = cache.get(ref, originalBytes);
        if (entry != null) {
            System.out.println("Class found in cache");
            byte[] cachedBytes = entry.getBytes();
            // Use cached bytes
        } else {
            System.out.println("Class not in cache, need to weave");
            // Weave class and store in cache
            byte[] wovenBytes = weaveClass("com.example.MyClass", originalBytes);
            cache.put(ref, originalBytes, wovenBytes);
        }
        
        // Get cache statistics
        CacheStatistics stats = cache.getStats();
        System.out.println("Cache hits: " + stats.getHits());
        System.out.println("Cache misses: " + stats.getMisses());
    }
    
    private static byte[] getOriginalClassBytes(String className) {
        // Implementation to read original class bytes
        return new byte[0];
    }
    
    private static byte[] weaveClass(String className, byte[] originalBytes) {
        // Implementation to weave class
        return originalBytes;
    }
}

Custom Cache Factory

import org.aspectj.weaver.tools.cache.*;

public class CustomCacheExample {
    public static void setupCustomCache() {
        // Create custom cache factory
        CacheFactory customFactory = new CacheFactory() {
            @Override
            public CacheKeyResolver createResolver() {
                return new CustomCacheKeyResolver();
            }
            
            @Override
            public CacheBacking createBacking(String scope) {
                return new CustomCacheBacking(scope);
            }
        };
        
        // Set as default factory
        WeavedClassCache.setDefaultCacheFactory(customFactory);
        
        // Subsequent cache creation will use custom factory
        WeavedClassCache cache = WeavedClassCache.createCache(
            classLoader, aspects, null, null);
    }
    
    private static class CustomCacheKeyResolver implements CacheKeyResolver {
        @Override
        public CachedClassReference generatedKey(String className) {
            // Custom key generation logic
            return new CachedClassReference();
        }
        
        @Override
        public CachedClassReference weavedKey(String className, byte[] original_bytes) {
            // Custom weaved key generation
            return new CachedClassReference();
        }
        
        // Implement other methods...
    }
    
    private static class CustomCacheBacking implements CacheBacking {
        private final String scope;
        
        public CustomCacheBacking(String scope) {
            this.scope = scope;
        }
        
        @Override
        public CachedClassEntry get(CachedClassReference ref, byte[] originalBytes) {
            // Custom retrieval logic
            return null;
        }
        
        @Override
        public void put(CachedClassEntry entry, byte[] originalBytes) {
            // Custom storage logic
        }
        
        // Implement other methods...
    }
}

Cache Management

import org.aspectj.weaver.tools.cache.WeavedClassCache;

public class CacheManagement {
    public static void manageCaches() {
        // Check if caching is enabled
        if (WeavedClassCache.isEnabled()) {
            System.out.println("Caching is enabled");
            
            // Get all active caches
            List<WeavedClassCache> caches = WeavedClassCache.getCaches();
            System.out.println("Active caches: " + caches.size());
            
            // Print cache statistics
            for (WeavedClassCache cache : caches) {
                System.out.println("Cache: " + cache.getName());
                CacheStatistics stats = cache.getStats();
                System.out.println("  Hits: " + stats.getHits());
                System.out.println("  Misses: " + stats.getMisses());
                System.out.println("  Hit ratio: " + stats.getHitRatio());
            }
        } else {
            System.out.println("Caching is disabled");
        }
    }
    
    public static void clearAllCaches() {
        List<WeavedClassCache> caches = WeavedClassCache.getCaches();
        for (WeavedClassCache cache : caches) {
            cache.clear();
            System.out.println("Cleared cache: " + cache.getName());
        }
    }
}

Configuration

System Properties

Enable caching:

System.setProperty(WeavedClassCache.WEAVED_CLASS_CACHE_ENABLED, "true");

Set cache implementation:

System.setProperty(WeavedClassCache.CACHE_IMPL, "custom.CacheFactory");

Cache Directory

By default, caches are stored in the system temporary directory. Configure with:

System.setProperty("aj.cache.dir", "/path/to/cache/directory");

Performance Considerations

  1. Cache Location: Use fast storage (SSD) for cache directory
  2. Cache Size: Monitor cache size and implement cleanup strategies
  3. Concurrent Access: Cache implementations handle concurrent access
  4. Memory Usage: Large caches may impact memory usage

Troubleshooting

Common Issues

  1. Cache Not Working: Check that caching is enabled and cache directory is writable
  2. Performance Issues: Monitor cache hit ratios and consider cache size limits
  3. Disk Space: Implement cache cleanup for long-running applications

Debug Output

// Enable cache debugging
System.setProperty("aj.cache.debug", "true");

Install with Tessl CLI

npx tessl i tessl/maven-org-aspectj--aspectjweaver

docs

caching.md

index.md

java-agent.md

multi-classloader.md

programmatic-weaving.md

tile.json