or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

caching.mdindex.mdjava-agent.mdmulti-classloader.mdprogrammatic-weaving.md
tile.json

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

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/org.aspectj/aspectjweaver@1.9.x

To install, run

npx @tessl/cli install tessl/maven-org-aspectj--aspectjweaver@1.9.0

index.mddocs/

AspectJ Weaver

The AspectJ Weaver applies aspects to Java classes and supports load-time weaving (LTW) during class loading. It provides both Java agent capabilities for transparent weaving and programmatic APIs for embedding weaving into applications. The weaver includes a comprehensive caching system for improved performance and supports multi-classloader and OSGi environments.

Package Information

  • Package Name: org.aspectj:aspectjweaver
  • Package Type: maven
  • Language: Java
  • Installation:
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.9.24</version>
    </dependency>

Core Imports

import org.aspectj.weaver.loadtime.Agent;
import org.aspectj.weaver.loadtime.Aj;
import org.aspectj.weaver.tools.WeavingAdaptor;
import org.aspectj.weaver.tools.cache.WeavedClassCache;

Basic Usage

Java Agent Usage

Run your application with the AspectJ weaver as a Java agent:

java -javaagent:aspectjweaver.jar MyApplication

The agent automatically initializes and applies load-time weaving to classes as they are loaded.

Programmatic Usage

import org.aspectj.weaver.tools.WeavingAdaptor;
import java.net.URL;

// Create weaving adaptor
WeavingAdaptor adaptor = new WeavingAdaptor(classLoader);

// Weave a class
byte[] originalBytes = getClassBytes(className);
byte[] wovenBytes = adaptor.weaveClass(className, originalBytes);

// Use the woven bytes
defineClass(className, wovenBytes);

Architecture

The AspectJ Weaver is organized into several key components:

  • Agent System: Java agent entry point for transparent load-time weaving
  • Core Weaving: Programmatic weaving APIs and class transformation
  • Caching System: Performance optimization through class caching
  • Context Management: Multi-classloader and OSGi environment support

Capabilities

Java Agent Integration

Entry point for using AspectJ weaver as a Java agent with -javaagent parameter.

public static void premain(String options, Instrumentation instrumentation);
public static void agentmain(String options, Instrumentation instrumentation);
public static Instrumentation getInstrumentation();

Java Agent Integration

Programmatic Weaving

Core APIs for embedding AspectJ weaving capabilities into applications.

public WeavingAdaptor(WeavingClassLoader loader);
public byte[] weaveClass(String name, byte[] bytes) throws IOException;
public byte[] weaveClass(String name, byte[] bytes, boolean mustWeave) throws IOException;

Programmatic Weaving

Class Caching

High-performance caching system for weaved and generated classes across JVM restarts.

public static WeavedClassCache createCache(ClassLoader loader, List<String> aspects, 
    GeneratedClassHandler handler, IMessageHandler messageHandler);
public void put(CachedClassReference ref, byte[] classBytes, byte[] weavedBytes);
public CachedClassEntry get(CachedClassReference ref, byte[] classBytes);

Class Caching

Multi-Classloader Support

Support for complex classloader hierarchies, OSGi environments, and multi-module applications.

public interface IWeavingContext {
    Enumeration<URL> getResources(String name) throws IOException;
    String getClassLoaderName();
    boolean isLocallyDefined(String classname);
}

Multi-Classloader Support

Types

Core Interfaces

public interface ClassPreProcessor {
    void initialize();
    byte[] preProcess(String className, byte[] bytes, ClassLoader classLoader, 
        ProtectionDomain protectionDomain);
    void prepareForRedefinition(ClassLoader loader, String className);
}

public interface IClassFileProvider {
    Iterator<UnwovenClassFile> getClassFileIterator();
    IWeaveRequestor getRequestor();
    boolean isApplyAtAspectJMungersOnly();
}

Cache Types

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

public interface CacheKeyResolver {
    CachedClassReference generatedKey(String className);
    CachedClassReference weavedKey(String className, byte[] original_bytes);
    String keyToClass(String key);
}

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

Message Handling Types

public interface IMessageHandler {
    boolean handleMessage(IMessage message) throws AbortException;
    boolean isIgnoring(IMessage.Kind kind);
    void dontIgnore(IMessage.Kind kind);
    void ignore(IMessage.Kind kind);
}

public class AbortException extends RuntimeException {
    // Exception thrown to abort weaving operations
}

public interface IMessageHolder {
    boolean hasAnyMessage(IMessage.Kind kind, boolean orGreater);
    int numMessages(IMessage.Kind kind, boolean orGreater);
    IMessage[] getMessages(IMessage.Kind kind, boolean orGreater);
}

public interface IMessage {
    enum Kind { ABORT, ERROR, WARNING, INFO, DEBUG, TASKTAG, USAGE, WEAVEINFO }
    Kind getKind();
    String getMessage();
    ISourceLocation getSourceLocation();
    Throwable getThrown();
}

public interface ISourceLocation {
    java.io.File getSourceFile();
    int getLine();
    int getColumn();
    String getContext();
}

Data Types

public class CachedClassReference {
    // Reference to a cached class with key information
}

public class CachedClassEntry {
    // Represents a cached class entry with type information
}

public class CacheStatistics {
    // Statistics tracking for cache operations
}

public interface GeneratedClassHandler {
    void acceptClass(String name, byte[] originalBytes, byte[] wovenBytes);
}

public class Definition {
    // Represents weaving definition configuration from aop.xml files
}

Error Handling

The AspectJ Weaver throws several types of exceptions:

  • IllegalClassFormatException: When class transformation fails
  • IOException: During file operations and weaving
  • UnsupportedOperationException: When agent is not properly initialized
  • ExceptionInInitializerError: During ClassPreProcessorAgentAdapter initialization

Example error handling:

try {
    byte[] wovenBytes = adaptor.weaveClass(className, originalBytes);
} catch (IOException e) {
    // Handle weaving failure
    System.err.println("Failed to weave class " + className + ": " + e.getMessage());
}