You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ltw.xml 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <chapter id="ltw" xreflabel="Load-Time Weaving">
  2. <title>Load-Time Weaving</title>
  3. <sect1 id="ltw-introduction">
  4. <title>Introduction</title>
  5. <para> The AspectJ 5 weaver takes class files as input and produces class files as output.
  6. The weaving process itself can take place at one of three different times: compile-time,
  7. post-compile time, and load-time. The class files produced by the weaving process (and
  8. hence the run-time behaviour of an application) are the same regardless of the approach
  9. chosen. </para>
  10. <itemizedlist>
  11. <listitem> Compile-time weaving is the simplest approach. When you have the source code
  12. for an application, ajc will compile from source and produce woven class files as
  13. output. The invocation of the weaver is integral to the ajc compilation process. The
  14. aspects themselves may be in source or binary form. </listitem>
  15. <listitem> Post-compile weaving (also sometimes called binary weaving) is used to weave
  16. existing class files and JAR files. As with compile-time weaving,
  17. the aspects used for weaving may be in source or binary form. </listitem>
  18. <listitem> Load-time weaving (LTW) is simply binary weaving defered until the point that
  19. a class loader loads a class file and defines the class to the JVM. To support this,
  20. one or more "weaving class loaders", either provided explicitly by the run-time
  21. environment or enabled through a "weaving agent" are required. </listitem>
  22. </itemizedlist>
  23. <para> You may also hear the term "run-time weaving". We define this as the weaving of
  24. classes that have already been defined to the JVM (without reloading those
  25. classes). AspectJ 5 does not provide explicit support for run-time weaving although
  26. simple coding patterns can support dynamically enabling and disabling advice in aspects. </para>
  27. <sect2>
  28. <title>Weaving class files more than once</title>
  29. <para> By default a class file that has been woven by the AspectJ compiler cannot
  30. subsequently be rewoven (passed as input to the weaver). If you are developing
  31. AspectJ applications that are to be used in a load-time weaving environment, you
  32. need to specify the <literal>-Xreweavable</literal> compiler option when building
  33. them. This causes AspectJ to save additional state in the class files that is used
  34. to support subsequent reweaving. </para>
  35. </sect2>
  36. </sect1>
  37. <sect1 id="ltw-rules">
  38. <title>Load-time Weaving Requirements</title>
  39. <para> All load-time weaving is done in the context of a class loader, and hence the set of
  40. aspects used for weaving and the types that can be woven are affected by the class
  41. loader delegation model. This ensures that LTW complies with the Java 2 security model.
  42. The following rules govern the interaction of load-time weaving with class loading: </para>
  43. <orderedlist>
  44. <listitem> All aspects to be used for weaving must be defined to the weaver before any
  45. types to be woven are loaded.</listitem>
  46. <listitem> All abstract and concrete aspects visible to the weaver
  47. are available for extending (abstract aspects) and using for weaving.
  48. A visible aspect is one defined by the
  49. weaving class loader or one of its parent class loaders.</listitem>
  50. <listitem>A class loader may only weave classes that it defines. It may not weave
  51. classes loaded by a delegate or parent class loader.</listitem>
  52. </orderedlist>
  53. </sect1>
  54. <sect1 id="ltw-configuration">
  55. <title>Configuration</title>
  56. <para>AspectJ 5 supports a number of mechanisms designed to make load-time weaving as
  57. easy to use as possibe. The load-time weaving mechanism is chosen through JVM startup options.
  58. Configuration files determine the set of aspects to be used for weaving and which
  59. types will be woven. Additional diagnostic options allow the user to debug the configuration and
  60. weaving process. </para>
  61. <sect2>
  62. <title>Enabling Load-time Weaving</title>
  63. <para> AspectJ 5 supports three different ways of enabling load-time weaving for
  64. an application: agents, a command-line launch script, and a set of interfaces for
  65. integration of AspectJ load-time weaving in custom environments. </para>
  66. <variablelist>
  67. <varlistentry>
  68. <term>Agents</term>
  69. <listitem>
  70. <para>AspectJ 5 ships with a number of load-time weaving agents that
  71. enable load-time weaving. These agents and their configuration
  72. are execution environment dependent.
  73. Using Java 5 for example, you can specify the "-javaagent" option
  74. to the JVM. Configuration for the supported environments is discussed
  75. later in this chapter. AspectJ 5
  76. has several agents including those that use JVMTI, and the
  77. JRockit MAPI. </para>
  78. </listitem>
  79. </varlistentry>
  80. <varlistentry>
  81. <term>Command line</term>
  82. <listitem>
  83. <para> AspectJ includes a script "aj" that allows programs executed at
  84. the command line to take advantage of load-time weaving.
  85. The script is customized when AspectJ is installed depending on the chosen
  86. JDK. For example, for JDK 1.4 the script uses the
  87. <literal>-Djava.system.class.loader</literal> system property to replace
  88. the system class loader with a weaving class loader allowing classes
  89. loaded from the CLASSPATH to be woven.
  90. For JDK 1.5 the JVMTI weaving agent is used allowing classes loaded by all
  91. class loaders to be woven. Versions of the JDK prior to 1.3 are not
  92. supported by the "aj" mechanism. </para>
  93. </listitem>
  94. </varlistentry>
  95. <varlistentry>
  96. <term>Custom Integration</term>
  97. <listitem>
  98. <para> A public interface is provided to allow a user written class loader
  99. to instantiate a weaver and weave classes after loading and before
  100. defining them in the JVM. This enables load-time weaving to be supported in
  101. environments where no weaving agent is available. It also allows the
  102. user to explicity restrict by class loader which classes can be woven. </para>
  103. </listitem>
  104. </varlistentry>
  105. </variablelist>
  106. </sect2>
  107. <sect2>
  108. <title>Configuring Load-time Weaving with aop.xml files</title>
  109. <para>The weaver is configured using one or more <literal>META-INF/aop.xml</literal>
  110. files located on the class loader search path. Each file may define a list of
  111. concrete aspects to be used for weaving, type patterns describing which types
  112. should woven, and a set of options to be passed to the weaver. In addition AspectJ 5
  113. supports the definition of concrete aspects in XML. Aspects defined in this way
  114. must extend an abstract aspect visible to the weaver. The abstract aspect
  115. may define abstract pointcuts (but not abstract
  116. methods). The following example shows a simple aop.xml file: </para>
  117. <programlisting><![CDATA[
  118. <aspectj>
  119. <aspects>
  120. <!-- declare two existing aspects to the weaver -->
  121. <aspect name="com.MyAspect"/>
  122. <aspect name="com.MyAspect.Inner"/>
  123. <!-- define a concrete aspect inline -->
  124. <concrete-aspect name="com.xyz.tracing.MyTracing" extends="tracing.AbstractTracing">
  125. <pointcut name="tracingScope" expression="within(org.maw.*)"/>
  126. </concrete-aspect>
  127. <!-- Of the set of aspects known to the weaver, use aspects matching
  128. the type pattern "com..*" for weaving. -->
  129. <include within="com..*"/>
  130. <!-- Do not use any aspects with the @CoolAspect annotation for weaving -->
  131. <exclude within="@CoolAspect *"/>
  132. </aspects>
  133. <weaver options="-verbose -XlazyTjp">
  134. <!-- Weave types that are within the javax.* or org.aspectj.*
  135. packages. Also weave all types in the foo package that do
  136. not have the @NoWeave annotation. -->
  137. <include within="javax.*"/>
  138. <include within="org.aspectj.*"/>
  139. <include within="(!@NoWeave foo.*) AND foo.*"/>
  140. </weaver>
  141. </aspectj>
  142. ]]></programlisting>
  143. <para>
  144. An aop.xml file contains two key sections: "aspects" defines one
  145. or more aspects to the weaver and controls which aspects are to be
  146. used in the weaving process; "weaver" defines weaver options and which
  147. types should be woven.
  148. </para>
  149. <para>
  150. The simplest way to define an aspect to the weaver is to
  151. specify the fully-qualified name of the aspect type in an aspect element.
  152. You can also
  153. declare (and define to the weaver) aspects inline in the aop.xml file.
  154. This is done using the "concrete-aspect" element. A concrete-aspect
  155. declaration must provide a pointcut definition for every abstract
  156. pointcut in the abstract aspect it extends. This mechanism is a
  157. useful way of externalizing configuration for infrastructure and
  158. auxiliary aspects where the pointcut definitions themselves can be
  159. considered part of the configuration of the service.
  160. </para>
  161. <para>
  162. The aspects element may optionally contain one or more include and
  163. exclude elements (by default, all defined aspects are used for weaving).
  164. Specifying include or exclude elements restricts the set of defined
  165. aspects to be used for weaving to those that are matched by an include
  166. pattern, but not by an exclude pattern. The 'within' attribute accepts
  167. a type pattern of the same form as a within pcd, except that &amp;&amp;
  168. and || are replaced by 'AND' and 'OR'.
  169. </para>
  170. <para>
  171. The weaver element is used to pass options to the weaver and to specify
  172. the set of types that should be woven. If no include elements are specified
  173. then all types seen by the weaver will be woven.
  174. </para>
  175. <para> When several configuration files are visible from a given weaving class loader
  176. their contents are conceptually merged (this applies to both aop.xml files
  177. and to aop.properties files as described in the next section).
  178. The files are merged in the order they are
  179. found on the search path (regular <literal>getResourceAsStream</literal> lookup)
  180. according to the following rules: </para>
  181. <itemizedlist>
  182. <listitem> The set of available aspects is the set of all
  183. declared and defined aspects (<literal>aspect</literal> and
  184. <literal>concrete-aspect</literal> elements of the <literal>aspects</literal>
  185. section).</listitem>
  186. <listitem>The set of aspects used for weaving is the subset of the available
  187. aspects that are matched by at least one include statement and are not matched
  188. by any exclude statements. If there are no include statements then all non-excluded
  189. aspects are included.</listitem>
  190. <listitem> The set of types to be woven are those types matched by at
  191. least one weaver <literal>include</literal> element and not matched by any
  192. weaver <literal>exclude</literal> element. If there are no weaver include
  193. statements then all non-excluded types are included.</listitem>
  194. <listitem> The weaver options are derived by taking the union of the
  195. options specified in each of the weaver options attribute specifications. Where an
  196. option takes a value e.g. <literal>-warn:none</literal> the most recently defined value
  197. will be used.</listitem>
  198. </itemizedlist>
  199. <para>It is not an error for the same aspect to be defined to the weaver in
  200. more than one visible <literal>META-INF/aop.xml</literal> file.
  201. However, if a declarative concrete aspect
  202. is declared in more than aop.xml file then an error will be issued.
  203. A concrete aspect
  204. defined in this way will be used to weave types loaded by the
  205. class loader that loaded the aop.xml file in which it was defined.
  206. </para>
  207. <para> A <literal>META-INF/aop.xml</literal> file will automatically be generated when
  208. using the <literal>-outjar</literal> option of the AspectJ compiler.
  209. It will simply contain a (possibly empty) set of aspect elements, one for
  210. each concrete aspect included in the JAR. </para>
  211. </sect2>
  212. <sect2>
  213. <title>Configuring Load-time Weaving with Properties Files</title>
  214. <para> For memory constrained environments or those without support for XML a simple
  215. Java Properties file can be used to configure LTW. Just like XML files,
  216. <literal>META-INF/aop.properties</literal> files are loaded from the class loader
  217. search path. Everything that can be configured through XML can be configured using a
  218. Properties file, with the exception of declarative concrete aspects. For example: </para>
  219. <programlisting><![CDATA[
  220. aspects.names=com.MyAspect,com.MyAspect.Inner
  221. aspects.include=com..*
  222. aspects.exclude=@CoolAspect
  223. weaver.options=-verbose -XlazyTjp
  224. weaver.include=javax.* OR org.aspectj.*
  225. ]]></programlisting>
  226. </sect2>
  227. <sect2>
  228. <title>Weaver Options</title>
  229. <para> The table below lists the AspectJ options supported by LTW. All other options
  230. will be ignored and a warning issued. </para>
  231. <informaltable>
  232. <tgroup cols="2">
  233. <thead>
  234. <row>
  235. <entry>Option</entry>
  236. <entry>Purpose</entry>
  237. </row>
  238. </thead>
  239. <tbody>
  240. <row>
  241. <entry>
  242. <literal>-1.5</literal>
  243. </entry>
  244. <entry>Run the weaver in 1.5 mode (supports autoboxing in
  245. join point matching)</entry>
  246. </row>
  247. <row>
  248. <entry>
  249. <literal>-XlazyTjp</literal>
  250. </entry>
  251. <entry>Performance optimization for aspects making use
  252. of thisJoinPoint (non-static parts)</entry>
  253. </row>
  254. <row>
  255. <entry>
  256. <literal>-nowarn, -warn:none</literal>
  257. </entry>
  258. <entry>Suppress warning messages</entry>
  259. </row>
  260. <row>
  261. <entry>
  262. <literal>-proceedOnError</literal>
  263. </entry>
  264. <entry>Continue weaving even if errors occur (for example,
  265. "... already woven" errors)</entry>
  266. </row>
  267. <row>
  268. <entry>
  269. <literal>-verbose</literal>
  270. </entry>
  271. <entry>Issue informational messages about the weaving process</entry>
  272. </row>
  273. <row>
  274. <entry>
  275. <literal>-Xreweavable</literal>
  276. </entry>
  277. <entry>Produce class files that can subsequently be rewoven</entry>
  278. </row>
  279. <row>
  280. <entry>
  281. <literal>-Xnoinline</literal>
  282. </entry>
  283. <entry>Don't inline around advice.</entry>
  284. </row>
  285. <row>
  286. <entry>
  287. <literal>-showWeaveInfo</literal>
  288. </entry>
  289. <entry>Issue informational messages whenever the weaver touches a class file</entry>
  290. </row>
  291. <row>
  292. <entry>
  293. <literal>-XmessageHolderClass</literal>
  294. </entry>
  295. <entry>Provide alternative output destination to stderr for all weaver messages</entry>
  296. </row>
  297. </tbody>
  298. </tgroup>
  299. </informaltable>
  300. </sect2>
  301. </sect1>
  302. <sect1 id="ltw-packaging">
  303. <title>Runtime Requirements for Load-time Weaving</title>
  304. <para> To use LTW the <literal>aspectjweaver.jar</literal> library must be added to the
  305. classpath. This contains the AspectJ 5 runtime, weaver, weaving class loader and
  306. weaving agents. It also contains the DTD for parsing XML weaving configuration files. </para>
  307. </sect1>
  308. <sect1 id="ltw-agents">
  309. <title>Supported Agents</title>
  310. <sect2>
  311. <title>JVMTI</title>
  312. <para> When using JDK 1.5 the JVMTI agent can be used by starting the JVM with the
  313. following option: </para>
  314. <programlisting><![CDATA[
  315. -javaagent=aspectjweaver.jar
  316. ]]></programlisting>
  317. </sect2>
  318. <sect2>
  319. <title>JRockit</title>
  320. <para> The JRockit agent is configured with the following JVM option: </para>
  321. <programlisting><![CDATA[
  322. -Xmanagement:class=org.aspectj.weaver.tools.JRockitWeavingAgent
  323. ]]></programlisting>
  324. </sect2>
  325. </sect1>
  326. </chapter>