The AspectJ weaver applies aspects to Java classes and can be used as a Java agent for load-time weaving (LTW).
—
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.
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();
}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 instanceUsage:
java -javaagent:aspectjweaver.jar MyApplication
java -javaagent:aspectjweaver.jar=options MyApplicationAgent 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 instanceReturns 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
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;
}Transforms class bytes using the AspectJ weaver.
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined,
ProtectionDomain protectionDomain, byte[] bytes) throws IllegalClassFormatExceptionParameters:
loader - The defining class loaderclassName - Name of class being loaded (in internal form, e.g., "com/example/MyClass")classBeingRedefined - Set when hotswap is being attemptedprotectionDomain - Protection domain for the class being loadedbytes - Original class bytes before weavingReturns: Woven class bytes or null if no transformation applied
throws: IllegalClassFormatException if class format is invalid
# Basic load-time weaving
java -javaagent:aspectjweaver.jar com.example.MyApp
# With custom options
java -javaagent:aspectjweaver.jar=verbose com.example.MyAppimport 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());
}
}
}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();
}
}The agent supports various configuration options through system properties and aop.xml files:
aj.weaving.verbose: Enable verbose weaving outputorg.aspectj.weaver.showWeaveInfo: Show weave informationorg.aspectj.tracing.messages: Enable message tracingCreate 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>Enable verbose output to see what the agent is doing:
java -javaagent:aspectjweaver.jar -Daj.weaving.verbose=true MyApppublic 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