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.

tools-intro.xml 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <chapter id="tools-intro"
  2. xreflabel="Introduction to the AspectJ tools">
  3. <title>Introduction to the AspectJ tools</title>
  4. <sect1
  5. id="eclipse-aspectj"
  6. xreflabel="The Eclipse AspectJ implementation">
  7. <title>The Eclipse AspectJ implementation</title>
  8. <para>The <ulink url="../progguide/index.html">AspectJ Programming Guide</ulink>
  9. describes the AspectJ language. This guide describes the AspectJ
  10. tools produced by the AspectJ
  11. team on
  12. <ulink url="http://eclipse.org/aspectj">http://eclipse.org/aspectj</ulink>.
  13. The AspectJ tools include
  14. - ajc, the compiler/weaver;
  15. ajdoc, a documentation tool; ajbrowser, a crosscutting code viewer;
  16. Ant support for ajc; and load-time weaving support.
  17. These tools are delivered in the library folder of the AspectJ tools
  18. installation, mainly in <literal>aspectjtools.jar</literal> (tools) and
  19. <literal>aspectjrt.jar</literal> (runtime).
  20. This guide does not describe the Eclipse AspectJ development tools
  21. (AJDT). That is produced by another team (sharing some members) on
  22. <ulink url="http://eclipse.org/aspectj">http://eclipse.org/ajdt</ulink>.
  23. AJDT is delivered as an Eclipse plugin, incorporating the classes in
  24. the AspectJ tools libraries along with the Eclipse plugin interface
  25. classes.
  26. </para>
  27. <para>
  28. Since AspectJ 1.1, the tools have implemented the AspectJ language
  29. using bytecode weaving, which combines aspects and classes to produce
  30. .class files that run in a Java VM. There are other ways to implement the
  31. language (e.g., compiler preprocessor, VM support); the AspectJ team
  32. has always tried to distinguish the language and the implementation
  33. so other groups could build alternative implementations of AspectJ.
  34. To that end,
  35. <ulink url="../progguide/implementation.html">The AspectJ Programming Guide,
  36. Implementation Notes</ulink> describes how the Java bytecode form affects
  37. language semantics. VM- or source-based implementations may be free
  38. of these limits or impose limits of their own, but most should be
  39. fairly close to what's possible in Java bytecode.
  40. </para>
  41. <para>
  42. Please be careful not to confuse any description of
  43. weaving or of this implementation of the AspectJ language with
  44. the AspectJ language semantics.
  45. If you do, you might find yourself writing code that doesn't work as
  46. expected when you compile or run it on other systems.
  47. More importantly, if you
  48. think about aspects in terms of weaving or of inserting or merging
  49. code, then you can lose many of the design benefits of thinking
  50. about an aspect as a single crosscutting module.
  51. When the text below introduces an implementation detail, it will warn if
  52. users make mistakes by applying it in lieu of the language semantics.
  53. </para>
  54. </sect1>
  55. <!-- graphic for bytecode weaving -->
  56. <sect1
  57. id="bytecode-concepts"
  58. xreflabel="Bytecode weaving, incremental compilation, and memory usage">
  59. <title>Bytecode weaving, incremental compilation, and memory usage</title>
  60. <para>Bytecode weaving takes classes and aspects in .class form
  61. and weaves them together to produce binary-compatible .class files that
  62. run in any Java VM and implement the AspectJ semantics.
  63. This process supports not only the compiler but also IDE's.
  64. The compiler, given an aspect in source form, produces a binary
  65. aspect and runs the weaver. IDE's can get information about
  66. crosscutting in the program by subscribing to information
  67. produced by weaver as a side-effect of weaving.
  68. </para>
  69. <para>Incremental compilation involves recompiling only what is necessary
  70. to bring the binary form of a program up-to-date with the source form
  71. in the shortest time possible.
  72. Incremental weaving supports this by weaving on a per-class basis.
  73. (Some implementations of AOP (including AspectJ 1.0) make use
  74. of whole-program analysis that can't be done in incremental mode.)
  75. Weaving per-class means that if the source for a pure Java class
  76. is updated, only that class needs to be produced. However, if
  77. some crosscutting specification may have been updated, then all
  78. code potentially affected by it may need to be woven. The AspectJ
  79. tools are getting better at minimizing this effect, but it is to
  80. some degree unavoidable due to the crosscutting semantics.
  81. </para>
  82. <para>
  83. Memory usage can seem higher with AspectJ tools.
  84. Some aspects are written to potentially affect many classes, so each
  85. class must be checked during the process of weaving. Programmers can
  86. minimize this by writing the crosscutting specifications as narrowly
  87. as possible while maintaining correctness.
  88. (While it may seem like more memory, the proper comparison
  89. would with with a Java program that had the same crosscutting,
  90. with changes made to each code segment. That would likely require
  91. more memory and more time to recompile than the corresponding
  92. AspectJ program.)
  93. </para>
  94. <sect2
  95. id="classpathInpathAndAspectpath"
  96. xreflabel="Classpath, inpath, and aspectpath">
  97. <title>Classpath, inpath, and aspectpath</title>
  98. <para>AspectJ introduces two new paths for the binary input to the
  99. weaver which you'll find referenced in <xref linkend="ajc-ref"/>,
  100. <xref linkend="antTasks"/>,
  101. and <xref linkend="ltw"/>.
  102. </para>
  103. <para>As in Java, the <literal>classpath</literal> is where the AspectJ
  104. tools resolve types specified in the program. When running an AspectJ
  105. program, the classpath should contain the classes and aspects along with
  106. the AspectJ runtime library, <literal>aspectjrt.jar</literal>.
  107. </para>
  108. <para>
  109. In AspectJ tools, the <literal>aspectpath</literal> is where to find binary
  110. aspects. Like the classpath, it can include archives (.jar and .zip files)
  111. and directories containing .class files in a package layout (since
  112. binary aspects are in .class files). These aspects affect other
  113. classes in exactly the same way as source-level aspects, but are themselves
  114. not affected. When deploying programs, the original aspects must be included
  115. on the runtime classpath.
  116. </para>
  117. <para>
  118. In AspectJ tools, the <literal>inpath</literal> is where to find binary
  119. input - aspects and classes that weave and may be woven.
  120. Like the classpath, it can include archives and class directories.
  121. Like the aspectpath, it can include aspects that affect other classes
  122. and aspects.
  123. However, unlike the aspectpath, an aspect on the inpath may itself be
  124. affected by aspects, as if the source were all compiled together.
  125. When deploying aspects that were put on the inpath, only the woven output
  126. should be on the runtime classpath.
  127. </para>
  128. <para>
  129. Although types in the inpath and the aspectpath need to be resolved by
  130. the AspectJ tools, you usually do not need to place them on the classpath
  131. because this is done automatically by the compiler/weaver. But when using
  132. the <literal>WeavingURLClassLoader</literal>, your code must explicitly add the aspects
  133. to the classpath so they can be resolved (as you'll see in the sample
  134. code and the <literal>aj.bat</literal> script).
  135. </para>
  136. <para>The most common mistake is failing to add
  137. <literal>aspectjrt.jar</literal> to the classpath. Also, when
  138. weaving with binary aspects, users forget to deploy the aspect itself
  139. along with any classes it requires. A more subtle mistake is putting a
  140. binary aspect (BA) on the inpath instead of the aspectpath. In this case
  141. the aspect BA might be affected by an aspect, even itself; this can
  142. cause the program to fail, e.g., when an aspect uses exclusion to
  143. avoid infinite recursion but fails to exclude advice in aspect BA.
  144. </para>
  145. <para>The latter is one of many ways that mistakes in the build process
  146. can affect aspects that are written poorly. Aspects should never
  147. rely on the boundaries of the build specification to narrow the
  148. scope of their crosscutting, since the build can be changed
  149. without notice to the aspect developer. Careful users might even
  150. avoid relying on the implementation scope, to ensure their
  151. AspectJ code will run on other implementations.
  152. </para>
  153. </sect2>
  154. </sect1>
  155. </chapter>