The AspectJ weaver applies aspects to Java classes and can be used as a Java agent for load-time weaving (LTW).
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
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.
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.24</version>
</dependency>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;Run your application with the AspectJ weaver as a Java agent:
java -javaagent:aspectjweaver.jar MyApplicationThe agent automatically initializes and applies load-time weaving to classes as they are loaded.
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);The AspectJ Weaver is organized into several key components:
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();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;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);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);
}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();
}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);
}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();
}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
}The AspectJ Weaver throws several types of exceptions:
IllegalClassFormatException: When class transformation failsIOException: During file operations and weavingUnsupportedOperationException: When agent is not properly initializedExceptionInInitializerError: During ClassPreProcessorAgentAdapter initializationExample 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());
}