[[ltw]] = Load-Time Weaving [[ltw-introduction]] == Introduction The AspectJ weaver takes class files as input and produces class files as output. The weaving process itself can take place at one of three different times: compile-time, post-compile time, and load-time. The class files produced by the weaving process (and hence the run-time behaviour of an application) are the same regardless of the approach chosen. * Compile-time weaving is the simplest approach. When you have the source code for an application, ajc will compile from source and produce woven class files as output. The invocation of the weaver is integral to the ajc compilation process. The aspects themselves may be in source or binary form. If the aspects are required for the affected classes to compile, then you must weave at compile-time. Aspects are required, e.g., when they add members to a class and other classes being compiled reference the added members. * Post-compile weaving (also sometimes called binary weaving) is used to weave existing class files and JAR files. As with compile-time weaving, the aspects used for weaving may be in source or binary form, and may themselves be woven by aspects. * Load-time weaving (LTW) is simply binary weaving defered until the point that a class loader loads a class file and defines the class to the JVM. To support this, one or more "weaving class loaders", either provided explicitly by the run-time environment or enabled through a "weaving agent" are required. You may also hear the term "run-time weaving". We define this as the weaving of classes that have already been defined to the JVM (without reloading those classes). AspectJ 5 does not provide explicit support for run-time weaving although simple coding patterns can support dynamically enabling and disabling advice in aspects. === Weaving class files more than once As of AspectJ 5 aspects (code style or annotation style) and woven classes are reweavable by default. If you are developing AspectJ applications that are to be used in a load-time weaving environment with an older version of the compiler you need to specify the `-Xreweavable` compiler option when building them. This causes AspectJ to save additional state in the class files that is used to support subsequent reweaving. [[ltw-rules]] == Load-time Weaving Requirements All load-time weaving is done in the context of a class loader, and hence the set of aspects used for weaving and the types that can be woven are affected by the class loader delegation model. This ensures that LTW complies with the Java 2 security model. The following rules govern the interaction of load-time weaving with class loading: [arabic] . All aspects to be used for weaving must be defined to the weaver before any types to be woven are loaded. This avoids types being "missed" by aspects added later, with the result that invariants across types fail. . All aspects visible to the weaver are usable. A visible aspect is one defined by the weaving class loader or one of its parent class loaders. All concrete visible aspects are woven and all abstract visible aspects may be extended. . A class loader may only weave classes that it defines. It may not weave classes loaded by a delegate or parent class loader. [[ltw-configuration]] == Configuration New in AspectJ 5 are a number of mechanisms to make load-time weaving easy to use. The load-time weaving mechanism is chosen through JVM startup options. Configuration files determine the set of aspects to be used for weaving and which types will be woven. Additional diagnostic options allow the user to debug the configuration and weaving process. === Enabling Load-time Weaving AspectJ 5 supports several ways of enabling load-time weaving for an application: agents, a command-line launch script, and a set of interfaces for integration of AspectJ load-time weaving in custom environments. Agents:: AspectJ 5 ships with a load-time weaving agent that enables load-time weaving. This agent and its configuration is execution environment dependent. Configuration for the supported environments is discussed later in this chapter. + Using Java 5 JVMTI you can specify the `-javaagent:pathto/aspectjweaver.jar` option to the JVM. + Since AspectJ 1.9.7, the obsolete Oracle/BEA JRockit agent is no longer part of AspectJ. JRockit JDK never supported Java versions higher than 1.6. Several JRockit JVM features are now part of HotSpot and tools like Mission Control available for OpenJDK and Oracle JDK. Command-line wrapper scripts `aj`:: The `aj` command runs Java programs in Java 1.4 or later by setting up `WeavingURLClassLoader` as the system class loader. For more information, see xref:aj.adoc#aj[`aj`, the AspectJ load-time weaving launcher]. + The `aj5` command runs Java programs in Java 5 by using the `-javaagent:pathto/aspectjweaver.jar` option described above. For more information, see xref:aj.adoc#aj[`aj`, the AspectJ load-time weaving launcher]. Custom class loader:: A public interface is provided to allow a user written class loader to instantiate a weaver and weave classes after loading and before defining them in the JVM. This enables load-time weaving to be supported in environments where no weaving agent is available. It also allows the user to explicitly restrict by class loader which classes can be woven. For more information, see xref:aj.adoc#aj[`aj`, the AspectJ load-time weaving launcher] and the API documentation and source for `WeavingURLClassLoader` and `WeavingAdapter`. [[configuring-load-time-weaving-with-aopxml-files]] === Configuring Load-time Weaving with aop.xml files The weaver is configured using one or more `META-INF/aop.xml` files located on the class loader search path. Each file may declare a list of aspects to be used for weaving, type patterns describing which types should woven, and a set of options to be passed to the weaver. In addition AspectJ 5 supports the definition of concrete aspects in XML. Aspects defined in this way must extend an abstract aspect visible to the weaver. The abstract aspect may define abstract pointcuts (but not abstract methods). The following example shows a simple aop.xml file: [source, xml] ....