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

java-agent.mddocs/

Java Agent Integration

The AspectJ Weaver provides Java agent capabilities for transparent load-time weaving using the standard Java instrumentation API. This allows aspects to be applied to classes as they are loaded by the JVM without requiring code changes.

Core Agent Class

Agent

Main entry point for Java agent functionality.

public class Agent {
    public static void premain(String options, Instrumentation instrumentation);
    public static void agentmain(String options, Instrumentation instrumentation);
    public static Instrumentation getInstrumentation();
}

premain

JSR-163 preMain agent entry method called when using -javaagent parameter.

public static void premain(String options, Instrumentation instrumentation)

Parameters:

  • options - Agent options string (can be null)
  • instrumentation - JVM instrumentation instance

Usage:

java -javaagent:aspectjweaver.jar MyApplication
java -javaagent:aspectjweaver.jar=options MyApplication

agentmain

Agent attachment entry method for dynamic agent loading via VirtualMachine.loadAgent.

public static void agentmain(String options, Instrumentation instrumentation)

Parameters:

  • options - Agent options string (can be null)
  • instrumentation - JVM instrumentation instance

getInstrumentation

Returns the system-level Instrumentation instance.

public static Instrumentation getInstrumentation()

Returns: The Instrumentation instance provided by the JVM

Throws: UnsupportedOperationException if the agent was not started via -javaagent or attached via VirtualMachine.loadAgent

Class File Transformation

ClassPreProcessorAgentAdapter

Adapter implementing ClassFileTransformer for Java agent integration.

public class ClassPreProcessorAgentAdapter implements ClassFileTransformer {
    public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, 
        ProtectionDomain protectionDomain, byte[] bytes) throws IllegalClassFormatException;
}

transform

Transforms class bytes using the AspectJ weaver.

public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, 
    ProtectionDomain protectionDomain, byte[] bytes) throws IllegalClassFormatException

Parameters:

  • loader - The defining class loader
  • className - Name of class being loaded (in internal form, e.g., "com/example/MyClass")
  • classBeingRedefined - Set when hotswap is being attempted
  • protectionDomain - Protection domain for the class being loaded
  • bytes - Original class bytes before weaving

Returns: Woven class bytes or null if no transformation applied

throws: IllegalClassFormatException if class format is invalid

Usage Examples

Basic Agent Usage

# Basic load-time weaving
java -javaagent:aspectjweaver.jar com.example.MyApp

# With custom options
java -javaagent:aspectjweaver.jar=verbose com.example.MyApp

Programmatic Agent Check

import org.aspectj.weaver.loadtime.Agent;
import java.lang.instrument.Instrumentation;

public class MyApplication {
    public static void main(String[] args) {
        try {
            Instrumentation inst = Agent.getInstrumentation();
            System.out.println("AspectJ agent is active");
            System.out.println("Can redefine classes: " + inst.isRedefineClassesSupported());
        } catch (UnsupportedOperationException e) {
            System.out.println("AspectJ agent not active: " + e.getMessage());
        }
    }
}

Dynamic Agent Attachment

import com.sun.tools.attach.VirtualMachine;
import java.lang.management.ManagementFactory;

public class AgentAttacher {
    public static void attachAgent() throws Exception {
        String pid = ManagementFactory.getRuntimeMXBean().getName().split("@")[0];
        VirtualMachine vm = VirtualMachine.attach(pid);
        vm.loadAgent("/path/to/aspectjweaver.jar");
        vm.detach();
    }
}

Configuration

The agent supports various configuration options through system properties and aop.xml files:

System Properties

  • aj.weaving.verbose: Enable verbose weaving output
  • org.aspectj.weaver.showWeaveInfo: Show weave information
  • org.aspectj.tracing.messages: Enable message tracing

AOP Configuration

Create META-INF/aop.xml in your classpath:

<?xml version="1.0" encoding="UTF-8"?>
<aspectj>
    <weaver options="-verbose -showWeaveInfo">
        <include within="com.example..*"/>
    </weaver>
    <aspects>
        <aspect name="com.example.MyAspect"/>
    </aspects>
</aspectj>

Troubleshooting

Common Issues

  1. Agent Not Loading: Ensure the aspectjweaver.jar path is correct
  2. No Weaving Occurring: Check that aspects are on the classpath and properly configured
  3. Class Redefinition Errors: Some classes cannot be redefined after initial loading

Debugging

Enable verbose output to see what the agent is doing:

java -javaagent:aspectjweaver.jar -Daj.weaving.verbose=true MyApp

Error Handling

public class AgentErrorHandler {
    public static void checkAgentStatus() {
        try {
            Instrumentation inst = Agent.getInstrumentation();
            if (!inst.isRedefineClassesSupported()) {
                System.err.println("Warning: Class redefinition not supported");
            }
        } catch (UnsupportedOperationException e) {
            System.err.println("AspectJ agent not initialized: " + e.getMessage());
            // Handle non-agent execution
        }
    }
}

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