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 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. <para><!-- FIXME AV -->As per AspectJ 1.5.0 M3 aspects (code style or annotation style) are
  36. reweavable by default, and weaved classes may be as well in 1.5.0 final.</para>
  37. </sect2>
  38. </sect1>
  39. <sect1 id="ltw-rules">
  40. <title>Load-time Weaving Requirements</title>
  41. <para> All load-time weaving is done in the context of a class loader, and hence the set of
  42. aspects used for weaving and the types that can be woven are affected by the class
  43. loader delegation model. This ensures that LTW complies with the Java 2 security model.
  44. The following rules govern the interaction of load-time weaving with class loading: </para>
  45. <orderedlist>
  46. <listitem> All aspects to be used for weaving must be defined to the weaver before any
  47. types to be woven are loaded.</listitem>
  48. <listitem> All abstract and concrete aspects visible to the weaver
  49. are available for extending (abstract aspects) and using for weaving.
  50. A visible aspect is one defined by the
  51. weaving class loader or one of its parent class loaders.</listitem>
  52. <listitem>A class loader may only weave classes that it defines. It may not weave
  53. classes loaded by a delegate or parent class loader.</listitem>
  54. </orderedlist>
  55. </sect1>
  56. <sect1 id="ltw-configuration">
  57. <title>Configuration</title>
  58. <para>AspectJ 5 supports a number of mechanisms designed to make load-time weaving as
  59. easy to use as possibe. The load-time weaving mechanism is chosen through JVM startup options.
  60. Configuration files determine the set of aspects to be used for weaving and which
  61. types will be woven. Additional diagnostic options allow the user to debug the configuration and
  62. weaving process. </para>
  63. <sect2>
  64. <title>Enabling Load-time Weaving</title>
  65. <para> AspectJ 5 supports several different ways of enabling load-time weaving for
  66. an application: agents, a command-line launch script, and a set of interfaces for
  67. integration of AspectJ load-time weaving in custom environments. </para>
  68. <variablelist>
  69. <varlistentry>
  70. <term>Agents</term>
  71. <listitem>
  72. <para>AspectJ 5 ships with a number of load-time weaving agents that
  73. enable load-time weaving. These agents and their configuration
  74. are execution environment dependent. Configuration for the supported environments is discussed
  75. later in this chapter.</para>
  76. <para>
  77. Using Java 5 JVMTI you can specify the <literal>-javaagent:pathto/aspectjweaver.jar</literal> option
  78. to the JVM.</para><para>
  79. Using BEA JRockit and Java 1.3/1.4, the very same behavior can be obtained using BEA JRockit JMAPI features with
  80. the <literal>-Xmanagement:class=org.aspectj.weaver.loadtime.JRockitAgent</literal>
  81. </para>
  82. </listitem>
  83. </varlistentry>
  84. <!-- FIXME: must be made consistent (aop.xml , CL hierarchy etc) -->
  85. <!-- <varlistentry>-->
  86. <!-- <term>Command line</term>-->
  87. <!-- <listitem>-->
  88. <!-- <para> AspectJ includes a script "aj" that allows programs executed at-->
  89. <!-- the command line to take advantage of load-time weaving. -->
  90. <!-- The script is customized when AspectJ is installed depending on the chosen -->
  91. <!-- JDK. For example, for JDK 1.4 the script uses the-->
  92. <!-- <literal>-Djava.system.class.loader</literal> system property to replace-->
  93. <!-- the system class loader with a weaving class loader allowing classes -->
  94. <!-- loaded from the CLASSPATH to be woven. -->
  95. <!-- For JDK 1.5 the JVMTI weaving agent is used allowing classes loaded by all-->
  96. <!-- class loaders to be woven. Versions of the JDK prior to 1.3 are not-->
  97. <!-- supported by the "aj" mechanism. </para>-->
  98. <!-- </listitem>-->
  99. <!-- </varlistentry>-->
  100. <varlistentry>
  101. <term>Custom Integration</term>
  102. <listitem>
  103. <para> A public interface is provided to allow a user written class loader
  104. to instantiate a weaver and weave classes after loading and before
  105. defining them in the JVM. This enables load-time weaving to be supported in
  106. environments where no weaving agent is available. It also allows the
  107. user to explicity restrict by class loader which classes can be woven.</para>
  108. </listitem>
  109. </varlistentry>
  110. </variablelist>
  111. </sect2>
  112. <sect2>
  113. <title>Configuring Load-time Weaving with aop.xml files</title>
  114. <para>The weaver is configured using one or more <literal>META-INF/aop.xml</literal>
  115. files located on the class loader search path. Each file may define a list of
  116. concrete aspects to be used for weaving, type patterns describing which types
  117. should woven, and a set of options to be passed to the weaver. In addition AspectJ 5
  118. supports the definition of concrete aspects in XML. Aspects defined in this way
  119. must extend an abstract aspect visible to the weaver. The abstract aspect
  120. may define abstract pointcuts (but not abstract
  121. methods). The following example shows a simple aop.xml file: </para>
  122. <programlisting><![CDATA[
  123. <aspectj>
  124. <aspects>
  125. <!-- declare two existing aspects to the weaver -->
  126. <aspect name="com.MyAspect"/>
  127. <aspect name="com.MyAspect.Inner"/>
  128. <!-- define a concrete aspect inline -->
  129. <concrete-aspect name="com.xyz.tracing.MyTracing" extends="tracing.AbstractTracing">
  130. <pointcut name="tracingScope" expression="within(org.maw.*)"/>
  131. </concrete-aspect>
  132. <!-- Of the set of aspects known to the weaver, use aspects matching
  133. the type pattern "com..*" for weaving. -->
  134. <include within="com..*"/>
  135. <!-- Do not use any aspects with the @CoolAspect annotation for weaving -->
  136. <exclude within="@CoolAspect *"/>
  137. </aspects>
  138. <weaver options="-verbose -XlazyTjp">
  139. <!-- Weave types that are within the javax.* or org.aspectj.*
  140. packages. Also weave all types in the foo package that do
  141. not have the @NoWeave annotation. -->
  142. <include within="javax.*"/>
  143. <include within="org.aspectj.*"/>
  144. <include within="(!@NoWeave foo.*) AND foo.*"/>
  145. <dump within="somepack.*"/><!-- will dump weaved classes to the "./_ajdump" folder on disk (for diagnostic purpose) -->
  146. </weaver>
  147. </aspectj>
  148. ]]></programlisting>
  149. <para>
  150. An aop.xml file contains two key sections: "aspects" defines one
  151. or more aspects to the weaver and controls which aspects are to be
  152. used in the weaving process; "weaver" defines weaver options and which
  153. types should be woven.
  154. </para>
  155. <para>
  156. The simplest way to define an aspect to the weaver is to
  157. specify the fully-qualified name of the aspect type in an aspect element.
  158. You can also
  159. declare (and define to the weaver) aspects inline in the aop.xml file.
  160. This is done using the "concrete-aspect" element. A concrete-aspect
  161. declaration must provide a pointcut definition for every abstract
  162. pointcut in the abstract aspect it extends. This mechanism is a
  163. useful way of externalizing configuration for infrastructure and
  164. auxiliary aspects where the pointcut definitions themselves can be
  165. considered part of the configuration of the service.
  166. </para>
  167. <para>
  168. <emphasis>
  169. Note: concrete-aspect is not available in AspectJ 1.5 M3.
  170. </emphasis>
  171. </para>
  172. <para>
  173. The aspects element may optionally contain one or more include and
  174. exclude elements (by default, all defined aspects are used for weaving).
  175. Specifying include or exclude elements restricts the set of defined
  176. aspects to be used for weaving to those that are matched by an include
  177. pattern, but not by an exclude pattern. The 'within' attribute accepts
  178. a type pattern of the same form as a within pcd, except that &amp;&amp;
  179. and || are replaced by 'AND' and 'OR'.
  180. </para>
  181. <para>
  182. The weaver element is used to pass options to the weaver and to specify
  183. the set of types that should be woven. If no include elements are specified
  184. then all types seen by the weaver will be woven.
  185. </para>
  186. <para> When several configuration files are visible from a given weaving class loader
  187. their contents are conceptually merged (this applies to both aop.xml files
  188. and to aop.properties files as described in the next section).
  189. The files are merged in the order they are
  190. found on the search path (regular <literal>getResourceAsStream</literal> lookup)
  191. according to the following rules: </para>
  192. <itemizedlist>
  193. <!-- FIXME AV - looks like we can refine conf in a child CL - not good -->
  194. <listitem> The set of available aspects is the set of all
  195. declared and defined aspects (<literal>aspect</literal> and
  196. <literal>concrete-aspect</literal> elements of the <literal>aspects</literal>
  197. section).</listitem>
  198. <listitem>The set of aspects used for weaving is the subset of the available
  199. aspects that are matched by at least one include statement and are not matched
  200. by any exclude statements. If there are no include statements then all non-excluded
  201. aspects are included.</listitem>
  202. <listitem> The set of types to be woven are those types matched by at
  203. least one weaver <literal>include</literal> element and not matched by any
  204. weaver <literal>exclude</literal> element. If there are no weaver include
  205. statements then all non-excluded types are included.</listitem>
  206. <listitem> The weaver options are derived by taking the union of the
  207. options specified in each of the weaver options attribute specifications. Where an
  208. option takes a value e.g. <literal>-warn:none</literal> the most recently defined value
  209. will be used.</listitem>
  210. </itemizedlist>
  211. <para>It is not an error for the same aspect to be defined to the weaver in
  212. more than one visible <literal>META-INF/aop.xml</literal> file.
  213. However, if a declarative concrete aspect
  214. is declared in more than aop.xml file then an error will be issued.
  215. A concrete aspect
  216. defined in this way will be used to weave types loaded by the
  217. class loader that loaded the aop.xml file in which it was defined.
  218. </para>
  219. <para> A <literal>META-INF/aop.xml</literal> file will automatically be generated when
  220. using the <literal>-outjar</literal> option of the AspectJ compiler.
  221. It will simply contain a (possibly empty) set of aspect elements, one for
  222. each concrete aspect included in the JAR. </para>
  223. </sect2>
  224. <!-- TODO someone implement that -->
  225. <!--
  226. <sect2>
  227. <title>Configuring Load-time Weaving with Properties Files</title>
  228. <para> For memory constrained environments or those without support for XML a simple
  229. Java Properties file can be used to configure LTW. Just like XML files,
  230. <literal>META-INF/aop.properties</literal> files are loaded from the class loader
  231. search path. Everything that can be configured through XML can be configured using a
  232. Properties file, with the exception of declarative concrete aspects. For example: </para>
  233. <programlisting><![CDATA[
  234. aspects.names=com.MyAspect,com.MyAspect.Inner
  235. aspects.include=com..*
  236. aspects.exclude=@CoolAspect
  237. weaver.options=-verbose -XlazyTjp
  238. weaver.include=javax.* OR org.aspectj.*
  239. ]]></programlisting>
  240. </sect2>
  241. -->
  242. <sect2>
  243. <title>Weaver Options</title>
  244. <para> The table below lists the AspectJ options supported by LTW. All other options
  245. will be ignored and a warning issued. </para>
  246. <informaltable>
  247. <tgroup cols="2">
  248. <thead>
  249. <row>
  250. <entry>Option</entry>
  251. <entry>Purpose</entry>
  252. </row>
  253. </thead>
  254. <tbody>
  255. <row>
  256. <entry>
  257. <literal>-verbose</literal>
  258. </entry>
  259. <entry>Issue informational messages about the weaving process. If ever you need to have information
  260. when the load time weaving engine is bootstrapped (hence its logger as per <literal>-XmessageHandlerClass:...</literal> not ready yet),
  261. you can use the option <literal>-Daj.weaving.verbose=true</literal> on the JVM startup command line. Messages will then be printed
  262. on stderr as long as the message handler class is not ready.
  263. </entry>
  264. </row>
  265. <row>
  266. <entry>
  267. <literal>-1.5</literal>
  268. </entry>
  269. <entry>Run the weaver in 1.5 mode (supports autoboxing in
  270. join point matching)</entry>
  271. </row>
  272. <row>
  273. <entry>
  274. <literal>-XlazyTjp</literal>
  275. </entry>
  276. <entry>Performance optimization for aspects making use
  277. of thisJoinPoint (non-static parts)</entry>
  278. </row>
  279. <row>
  280. <entry>
  281. <literal>-Xlintfile:pathToAResource</literal>
  282. </entry>
  283. <entry>Configure lint messages as specified in the given resource (visible from this aop.xml file' classloader)</entry>
  284. </row>
  285. <row>
  286. <entry>
  287. <literal>-Xlint:default, -Xlint:ignore, ...</literal>
  288. </entry>
  289. <entry>Configure lint messages, refer to documentation for meaningfull values</entry>
  290. </row>
  291. <row>
  292. <entry>
  293. <literal>-nowarn, -warn:none</literal>
  294. </entry>
  295. <entry>Suppress warning messages</entry>
  296. </row>
  297. <row>
  298. <entry>
  299. <literal>-proceedOnError</literal>
  300. </entry>
  301. <entry>Continue weaving even if errors occur (for example,
  302. "... already woven" errors)</entry>
  303. </row>
  304. <row>
  305. <entry>
  306. <literal>-Xreweavable</literal>
  307. </entry>
  308. <entry>Produce class files that can subsequently be rewoven</entry>
  309. </row>
  310. <row>
  311. <entry>
  312. <literal>-XnoInline</literal>
  313. </entry>
  314. <entry>Don't inline around advice.</entry>
  315. </row>
  316. <row>
  317. <entry>
  318. <literal>-showWeaveInfo</literal>
  319. </entry>
  320. <entry>Issue informational messages whenever the weaver touches a class file</entry>
  321. </row>
  322. <row>
  323. <entry>
  324. <literal>-XmessageHandlerClass:...</literal>
  325. </entry>
  326. <entry>Provide alternative output destination to stdout/stderr for all weaver messages.
  327. The given value must be the full qualified class name of a class that implements
  328. <literal>org.aspectj.bridge.IMessageHandler</literal>
  329. and that is visible from where the <literal>aop.xml</literal> is packed.
  330. If more than one such options are used,
  331. the first occurence only is taken into account.
  332. You must also be very cautious about using a custom handler since it is likely that it will be invoked
  333. (as well as all its third parties) while the weaving is done, which means that f.e. it cannot be weaved
  334. by the aspects that are configured within the same deployment unit.
  335. </entry>
  336. </row>
  337. </tbody>
  338. </tgroup>
  339. </informaltable>
  340. </sect2>
  341. </sect1>
  342. <sect1 id="ltw-specialcases">
  343. <title>Special cases</title>
  344. <para>
  345. Those classes are not exposed to the LTW infrastructure, no matter
  346. the configuration of the <literal>aop.xml</literal> file(s):
  347. <itemizedlist>
  348. <listitem>All <literal>org.aspectj.*</literal> classes (and subpackages) - as those are needed by the infrastructure itself</listitem>
  349. <listitem>All <literal>java.*</literal> and <literal>javax.*</literal> classes (and subpackages)</listitem>
  350. <listitem>All <literal>sun.reflect.*</literal> classes - as those are JDK specific classes used when reflective calls occurs</listitem>
  351. </itemizedlist>
  352. </para>
  353. <para>
  354. Despite these restrictions, it is perfectly possible to match call join points for calls to these types providing the calling
  355. class is exposed to the weaver. Subtypes of these excluded types that are exposed to the weaver may of course be woven.
  356. </para>
  357. <para>
  358. Note that dynamic proxy representations are exposed to the LTW infrastructure and are not considered
  359. a special case.
  360. </para>
  361. </sect1>
  362. <sect1 id="ltw-packaging">
  363. <title>Runtime Requirements for Load-time Weaving</title>
  364. <para> To use LTW the <literal>aspectjweaver.jar</literal> library must be added to the
  365. classpath. This contains the AspectJ 5 runtime, weaver, weaving class loader and
  366. weaving agents. It also contains the DTD for parsing XML weaving configuration files. </para>
  367. </sect1>
  368. <sect1 id="ltw-agents">
  369. <title>Supported Agents</title>
  370. <sect2>
  371. <title>JVMTI</title>
  372. <para> When using Java 5 the JVMTI agent can be used by starting the JVM with the
  373. following option (adapt according to the path to aspectjweaver.jar): </para>
  374. <programlisting><![CDATA[
  375. -javaagent:pathto/aspectjweaver.jar
  376. ]]></programlisting>
  377. </sect2>
  378. <sect2>
  379. <title>JRockit with Java 1.3/1.4 (use JVMTI on Java 5)</title>
  380. <para> The JRockit agent is configured with the following JVM option: </para>
  381. <programlisting><![CDATA[
  382. -Xmanagement:class=org.aspectj.weaver.loadtime.JRockitAgent
  383. ]]></programlisting>
  384. </sect2>
  385. </sect1>
  386. </chapter>