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.

ajc.xml 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. <refentry id="ajc-ref" xreflabel="The ajc Command-line Reference">
  2. <refnamediv>
  3. <refname>ajc</refname>
  4. <refpurpose>compiler and bytecode weaver for the AspectJ and Java languages</refpurpose>
  5. </refnamediv>
  6. <refsynopsisdiv>
  7. <cmdsynopsis>
  8. <command>ajc</command>
  9. <arg><replaceable>Options</replaceable></arg>
  10. <group>
  11. <arg><replaceable>file...</replaceable></arg>
  12. <arg>@<replaceable>file...</replaceable></arg>
  13. <arg>-argfile <replaceable>file...</replaceable></arg>
  14. </group>
  15. </cmdsynopsis>
  16. </refsynopsisdiv>
  17. <refsect1 id="ajc" xreflabel="ajc">
  18. <title>Description</title>
  19. <para>The <command>ajc</command> command compiles and weaves AspectJ and
  20. Java source and .class files, producing .class files compliant with any
  21. Java VM (1.1 or later). It combines compilation and bytecode weaving
  22. and supports incremental builds; you can also weave bytecode
  23. at run-time using <xref linkend="ltw"/>.
  24. </para>
  25. <para> The arguments after the options specify the source file(s) to compile.
  26. To specify source classes, use <parameter>-inpath</parameter> (below).
  27. Files may be listed directly on the command line or in a file.
  28. The <parameter>-argfile <replaceable>file</replaceable></parameter>
  29. and <parameter>@<replaceable>file</replaceable></parameter> forms
  30. are equivalent, and are interpreted as meaning all the arguments
  31. listed in the specified file.
  32. </para>
  33. <para>
  34. <command>Note:</command>
  35. You must explicitly pass <command>ajc</command> all necessary sources.
  36. Be sure to include the source not only for the
  37. aspects or pointcuts but also for any affected types.
  38. Specifying all sources is necessary because, unlike javac, ajc does not
  39. search the sourcepath for classes.
  40. (For a discussion of what affected types might be required,
  41. see <ulink url="../progguide/implementation.html">The AspectJ
  42. Programming Guide, Implementation Appendix</ulink>.)
  43. </para>
  44. <para>
  45. To specify sources, you can list source files as arguments or use the
  46. options <parameter>-sourceroots</parameter> or <parameter>-inpath</parameter>.
  47. If there are multiple sources for any type, the result is undefined
  48. since ajc has no way to determine which source is correct. (This
  49. happens most often when users include the destination directory
  50. on the inpath and rebuild.)
  51. </para>
  52. <refsect2 id="ajc_options" xreflabel="ajc_options">
  53. <title>Options</title>
  54. <variablelist>
  55. <varlistentry>
  56. <term>-injars <replaceable>JarList</replaceable></term>
  57. <listitem><para>
  58. deprecated: since 1.2, use -inpath, which also takes
  59. directories.
  60. </para></listitem>
  61. </varlistentry>
  62. <varlistentry>
  63. <term>-inpath <replaceable>Path</replaceable></term>
  64. <listitem><para>
  65. Accept as source bytecode any .class files in the
  66. .jar files or directories on Path.
  67. The output will include these
  68. classes, possibly as woven with any applicable aspects.
  69. Path is a single argument containing
  70. a list of paths to zip files or directories,
  71. delimited by the platform-specific path delimiter.
  72. </para></listitem>
  73. </varlistentry>
  74. <varlistentry>
  75. <term>-aspectpath <replaceable>Path</replaceable></term>
  76. <listitem><para>
  77. Weave binary aspects from jar files and directories on path into all sources.
  78. The aspects should have been output by the same version
  79. of the compiler.
  80. When running the output classes, the run classpath should contain
  81. all aspectpath entries.
  82. Path, like classpath, is a single argument containing
  83. a list of paths to jar files, delimited by the platform-
  84. specific classpath delimiter.
  85. </para></listitem>
  86. </varlistentry>
  87. <varlistentry>
  88. <term>-argfile <replaceable>File</replaceable></term>
  89. <listitem><para>
  90. The file contains a line-delimited list of arguments.
  91. Each line in the file should contain one option, filename, or
  92. argument string (e.g., a classpath or inpath).
  93. Arguments read from the file are inserted into the argument list
  94. for the command. Relative paths in the file are calculated from
  95. the directory containing the file (not the current working directory).
  96. Comments, as in Java, start with <literal>//</literal> and
  97. extend to the end of the line. Options specified in argument
  98. files may override rather than extending existing option values,
  99. so avoid specifying options like <replaceable>-classpath</replaceable>
  100. in argument files unlike the argument file is the only build
  101. specification. The form <replaceable>@file</replaceable> is the same
  102. as specifying <replaceable>-argfile file</replaceable>.
  103. </para></listitem>
  104. </varlistentry>
  105. <varlistentry>
  106. <term>-outjar <replaceable>output.jar</replaceable></term>
  107. <listitem><para>Put output classes in zip file output.jar.
  108. </para></listitem>
  109. </varlistentry>
  110. <varlistentry>
  111. <term>-outxml</term>
  112. <listitem><para>Generate aop.xml file for load-time weaving with default name.
  113. </para></listitem>
  114. </varlistentry>
  115. <varlistentry>
  116. <term>-outxmlfile <replaceable>custom/aop.xml</replaceable></term>
  117. <listitem><para>Generate aop.xml file for load-time weaving with custom name.
  118. </para></listitem>
  119. </varlistentry>
  120. <varlistentry>
  121. <term>-incremental</term>
  122. <listitem><para>Run the compiler continuously.
  123. After the initial compilation, the compiler will
  124. wait to recompile until it reads a newline from the standard
  125. input, and will quit when it reads a 'q'.
  126. It will only recompile necessary components, so a recompile
  127. should be much faster than doing a second compile.
  128. This requires -sourceroots.
  129. </para></listitem>
  130. </varlistentry>
  131. <varlistentry>
  132. <term>-sourceroots <replaceable>DirPaths</replaceable></term>
  133. <listitem><para>Find and build all .java or .aj source files under
  134. any directory listed in DirPaths.
  135. DirPaths, like classpath, is a single argument containing
  136. a list of paths to directories, delimited by the platform-
  137. specific classpath delimiter.
  138. Required by -incremental.
  139. </para></listitem>
  140. </varlistentry>
  141. <varlistentry>
  142. <term>-crossrefs</term>
  143. <listitem><para>
  144. Generate a build .ajsym file into the output directory. Used for
  145. viewing crosscutting references by tools like the AspectJ
  146. Browser.
  147. </para></listitem>
  148. </varlistentry>
  149. <varlistentry>
  150. <term>-emacssym</term>
  151. <listitem><para>
  152. Generate .ajesym symbol files for emacs support (deprecated).
  153. </para></listitem>
  154. </varlistentry>
  155. <varlistentry>
  156. <term>-Xlint</term>
  157. <listitem><para>Same as -Xlint:warning (enabled by default)
  158. </para></listitem>
  159. </varlistentry>
  160. <varlistentry>
  161. <term>-Xlint:{level}</term>
  162. <listitem><para>Set default level for messages about potential
  163. programming mistakes in crosscutting code.
  164. {level} may be ignore, warning, or error.
  165. This overrides entries in
  166. org/aspectj/weaver/XlintDefault.properties
  167. from aspectjtools.jar, but does not override levels set
  168. using the -Xlintfile option.
  169. </para></listitem>
  170. </varlistentry>
  171. <varlistentry>
  172. <term>-Xlintfile <replaceable>PropertyFile</replaceable></term>
  173. <listitem><para>Specify properties file to set levels for
  174. specific crosscutting messages.
  175. PropertyFile is a path to a Java .properties file that
  176. takes the same property names and values as
  177. org/aspectj/weaver/XlintDefault.properties
  178. from aspectjtools.jar, which it also overrides.
  179. </para></listitem>
  180. </varlistentry>
  181. <varlistentry>
  182. <term>-help</term>
  183. <listitem><para>
  184. Emit information on compiler options and usage
  185. </para></listitem>
  186. </varlistentry>
  187. <varlistentry>
  188. <term>-version</term>
  189. <listitem><para>
  190. Emit the version of the AspectJ compiler
  191. </para></listitem>
  192. </varlistentry>
  193. <varlistentry>
  194. <term>-classpath <replaceable>Path</replaceable></term>
  195. <listitem><para>
  196. Specify where to find user class files.
  197. Path is a single argument containing
  198. a list of paths to zip files or directories,
  199. delimited by the platform-specific path delimiter.
  200. </para></listitem>
  201. </varlistentry>
  202. <varlistentry>
  203. <term>-bootclasspath <replaceable>Path</replaceable></term>
  204. <listitem><para>
  205. Override location of VM's bootclasspath
  206. for purposes of evaluating types when compiling.
  207. Path is a single argument containing
  208. a list of paths to zip files or directories,
  209. delimited by the platform-specific path delimiter.
  210. </para></listitem>
  211. </varlistentry>
  212. <varlistentry>
  213. <term>-extdirs <replaceable>Path</replaceable></term>
  214. <listitem><para>
  215. Override location of VM's extension directories
  216. for purposes of evaluating types when compiling.
  217. Path is a single argument containing
  218. a list of paths to directories,
  219. delimited by the platform-specific path delimiter.
  220. </para></listitem>
  221. </varlistentry>
  222. <varlistentry>
  223. <term>-d <replaceable>Directory</replaceable></term>
  224. <listitem><para>
  225. Specify where to place generated .class files.
  226. If not specified, <replaceable>Directory</replaceable>
  227. defaults to the current working dir.
  228. </para></listitem>
  229. </varlistentry>
  230. <varlistentry>
  231. <term>-target <replaceable>[1.1 to 1.5]</replaceable></term>
  232. <listitem><para>Specify classfile target setting (1.1 to 1.5, default is 1.2)
  233. </para></listitem>
  234. </varlistentry>
  235. <varlistentry>
  236. <term>-1.3</term>
  237. <listitem><para>Set compliance level to 1.3
  238. This implies -source 1.3 and -target 1.1.
  239. </para></listitem>
  240. </varlistentry>
  241. <varlistentry>
  242. <term>-1.4</term>
  243. <listitem><para>Set compliance level to 1.4 (default)
  244. This implies -source 1.4 and -target 1.2.
  245. </para></listitem>
  246. </varlistentry>
  247. <varlistentry>
  248. <term>-1.5</term>
  249. <listitem><para>Set compliance level to 1.5.
  250. This implies -source 1.5 and -target 1.5.
  251. </para></listitem>
  252. </varlistentry>
  253. <varlistentry>
  254. <term>-source <replaceable>[1.3|1.4|1.5]</replaceable></term>
  255. <listitem><para>Toggle assertions (1.3, 1.4, or 1.5 - default is 1.4).
  256. When using -source 1.3, an assert() statement valid under
  257. Java 1.4 will result in a compiler error.
  258. When using -source 1.4,
  259. treat <literal>assert</literal> as a keyword and
  260. implement assertions according to the 1.4 language spec.
  261. When using -source 1.5,
  262. Java 5 language features are permitted.
  263. </para></listitem>
  264. </varlistentry>
  265. <varlistentry>
  266. <term>-nowarn</term>
  267. <listitem><para>Emit no warnings (equivalent to '-warn:none')
  268. This does not suppress messages
  269. generated by <literal>declare warning</literal> or
  270. <literal>Xlint</literal>.
  271. </para></listitem>
  272. </varlistentry>
  273. <varlistentry>
  274. <term>-warn: <replaceable>items</replaceable></term>
  275. <listitem><para>Emit warnings for any instances of
  276. the comma-delimited list of questionable code
  277. (eg '-warn:unusedLocals,deprecation'):
  278. <programlisting><!-- unable to embed itemizedlist? -->
  279. constructorName method with constructor name
  280. packageDefaultMethod attempt to override package-default method
  281. deprecation usage of deprecated type or member
  282. maskedCatchBlocks hidden catch block
  283. unusedLocals local variable never read
  284. unusedArguments method argument never read
  285. unusedImports import statement not used by code in file
  286. none suppress all compiler warnings
  287. </programlisting>
  288. <literal>-warn:none</literal> does not suppress messages
  289. generated by <literal>declare warning</literal> or
  290. <literal>Xlint</literal>.
  291. </para></listitem>
  292. </varlistentry>
  293. <varlistentry>
  294. <term>-deprecation</term>
  295. <listitem><para>Same as -warn:deprecation
  296. </para></listitem>
  297. </varlistentry>
  298. <varlistentry>
  299. <term>-noImportError</term>
  300. <listitem><para>Emit no errors for unresolved imports
  301. </para></listitem>
  302. </varlistentry>
  303. <varlistentry>
  304. <term>-proceedOnError</term>
  305. <listitem><para>Keep compiling after error,
  306. dumping class files with problem methods
  307. </para></listitem>
  308. </varlistentry>
  309. <varlistentry>
  310. <term>-g<replaceable>:[lines,vars,source]</replaceable></term>
  311. <listitem>
  312. <para>debug attributes level, that may take three forms:
  313. <programlisting>
  314. -g all debug info ('-g:lines,vars,source')
  315. -g:none no debug info
  316. -g:{items} debug info for any/all of [lines, vars, source], e.g.,
  317. -g:lines,source
  318. </programlisting>
  319. </para></listitem>
  320. </varlistentry>
  321. <varlistentry>
  322. <term>-preserveAllLocals</term>
  323. <listitem><para>Preserve all local variables during code generation
  324. (to facilitate debugging).
  325. </para></listitem>
  326. </varlistentry>
  327. <varlistentry>
  328. <term>-referenceInfo</term>
  329. <listitem><para>Compute reference information.
  330. </para></listitem>
  331. </varlistentry>
  332. <varlistentry>
  333. <term>-encoding <replaceable>format</replaceable></term>
  334. <listitem><para>Specify default source encoding format.
  335. Specify custom encoding on a per file basis by suffixing
  336. each input source file/folder name with '[encoding]'.
  337. </para></listitem>
  338. </varlistentry>
  339. <varlistentry>
  340. <term>-verbose</term>
  341. <listitem><para>Emit messages about accessed/processed compilation units
  342. </para></listitem>
  343. </varlistentry>
  344. <varlistentry>
  345. <term>-showWeaveInfo</term>
  346. <listitem><para>Emit messages about weaving
  347. </para></listitem>
  348. </varlistentry>
  349. <varlistentry>
  350. <term>-log <replaceable>file</replaceable></term>
  351. <listitem><para>Specify a log file for compiler messages.
  352. </para></listitem>
  353. </varlistentry>
  354. <varlistentry>
  355. <term>-progress</term>
  356. <listitem><para>Show progress (requires -log mode).
  357. </para></listitem>
  358. </varlistentry>
  359. <varlistentry>
  360. <term>-time</term>
  361. <listitem><para>Display speed information.
  362. </para></listitem>
  363. </varlistentry>
  364. <varlistentry>
  365. <term>-noExit</term>
  366. <listitem><para>Do not call System.exit(n) at end of compilation
  367. (n=0 if no error)
  368. </para></listitem>
  369. </varlistentry>
  370. <varlistentry>
  371. <term>-repeat <replaceable>N</replaceable></term>
  372. <listitem><para>Repeat compilation process N times
  373. (typically to do performance analysis).
  374. </para></listitem>
  375. </varlistentry>
  376. <varlistentry>
  377. <term>-XterminateAfterCompilation</term>
  378. <listitem><para>Causes compiler to terminate before weaving
  379. </para></listitem>
  380. </varlistentry>
  381. <varlistentry>
  382. <term>-XaddSerialVersionUID</term>
  383. <listitem><para>Causes the compiler to calculate and add
  384. the SerialVersionUID field to any type implementing
  385. Serializable that is affected by an aspect. The field
  386. is calculated based on the class before weaving has
  387. taken place.
  388. </para></listitem>
  389. </varlistentry>
  390. <varlistentry>
  391. <term>-Xreweavable[:compress]</term>
  392. <listitem><para>(Experimental - deprecated as now default)
  393. Runs weaver in reweavable mode which causes
  394. it to create woven classes that can be rewoven, subject to the restriction that
  395. on attempting a reweave all the types that advised the woven type must be accessible.
  396. </para></listitem>
  397. </varlistentry>
  398. <varlistentry>
  399. <term>-XnoInline</term>
  400. <listitem><para>(Experimental) do not inline around advice
  401. </para></listitem>
  402. </varlistentry>
  403. <varlistentry>
  404. <term>-XincrementalFile <replaceable>file</replaceable></term>
  405. <listitem><para>(Experimental) This works like incremental mode,
  406. but using a file rather than standard input to control the compiler.
  407. It will recompile each time file is changed and
  408. and halt when file is deleted.
  409. </para></listitem>
  410. </varlistentry>
  411. <varlistentry>
  412. <term>-XserializableAspects</term>
  413. <listitem><para>
  414. (Experimental) Normally it is an error to declare
  415. aspects Serializable. This option removes that restriction.
  416. </para></listitem>
  417. </varlistentry>
  418. <varlistentry>
  419. <term>-XnotReweavable</term>
  420. <listitem><para>
  421. (Experimental) Create class files that can't be subsequently rewoven by AspectJ.
  422. </para></listitem>
  423. </varlistentry>
  424. <!-- not documenting this feature yet -->
  425. <!--
  426. <varlistentry>
  427. <term>-XhasMember</term>
  428. <listitem><para>
  429. (Experimental) Allow hasmethod() and hasfield type patterns in declare parents and declare @type.
  430. </para></listitem>
  431. </varlistentry>
  432. -->
  433. <varlistentry>
  434. <term>-Xajruntimelevel:1.2, ajruntimelevel:1.5</term>
  435. <listitem><para>
  436. (Experimental) Allows code to be generated that targets a 1.2 or a 1.5 level AspectJ runtime (default 1.5)
  437. </para></listitem>
  438. </varlistentry>
  439. </variablelist>
  440. </refsect2>
  441. <refsect2>
  442. <title>File names</title>
  443. <para>ajc accepts source files with either the <filename>.java</filename>
  444. extension or the <filename>.aj</filename> extension. We normally use
  445. <filename>.java</filename> for all of our files in an AspectJ system -- files
  446. that contain aspects as well as files that contain classes. However, if
  447. you have a need to mechanically distinguish files that use AspectJ's
  448. additional functionality from those that are pure Java we recommend using
  449. the <filename>.aj</filename> extension for those files.</para>
  450. <para>We'd like to discourage other means of mechanical distinction such as
  451. naming conventions or sub-packages in favor of the <filename>.aj</filename>
  452. extension.</para>
  453. <itemizedlist>
  454. <listitem><para>Filename conventions are hard to enforce and lead to awkward names
  455. for your aspects. Instead of <filename>TracingAspect.java</filename> we
  456. recommend using <filename>Tracing.aj</filename> (or just
  457. <filename>Tracing.java</filename>) instead.</para></listitem>
  458. <listitem><para>Sub-packages move aspects out of their natural place in a system
  459. and can create an artificial need for privileged aspects. Instead of
  460. adding a sub-package like <filename>aspects</filename> we recommend using the
  461. <filename>.aj</filename> extension and including these files in your existing
  462. packages instead.</para></listitem>
  463. </itemizedlist>
  464. </refsect2>
  465. <refsect2>
  466. <title>Compatibility</title>
  467. <para>
  468. AspectJ is a compatible extension to the Java programming language. The
  469. AspectJ compiler adheres to the <ulink
  470. url="http://java.sun.com/docs/books/jls/index.html"> <citetitle
  471. pubwork="book">The Java Language Specification, Second
  472. Edition</citetitle></ulink> and to the <ulink
  473. url="http://java.sun.com/docs/books/vmspec/index.html"><citetitle
  474. pubwork="book">The Java Virtual Machine Specification, Second
  475. Edition</citetitle></ulink> and runs on any Java 2 compatible
  476. platform. The code it generates runs on any Java 1.1 or later
  477. compatible platform.
  478. For more information on compatibility with
  479. Java and with previous releases of AspectJ,
  480. see
  481. <xref linkend="versionCompatibility"/>.
  482. </para>
  483. </refsect2>
  484. <refsect2>
  485. <title>Examples</title>
  486. <example id="simpleexample">
  487. <title>A simple example</title>
  488. <para>Compile two files:</para>
  489. <programlisting>
  490. ajc HelloWorld.java Trace.java
  491. </programlisting>
  492. </example>
  493. <example id="exampleusingargfile">
  494. <title>An example using -argfile/@</title>
  495. <para>
  496. To avoid specifying file names on the command line,
  497. list source files in a line-delimited text argfile.
  498. Source file paths may be absolute or relative to the argfile,
  499. and may include other argfiles by @-reference.
  500. The following file <literal>sources.lst</literal>
  501. contains absolute and relative files and @-references:
  502. </para>
  503. <programlisting>
  504. Gui.java
  505. /home/user/src/Library.java
  506. data/Repository.java
  507. data/Access.java
  508. @../../common/common.lst
  509. @/home/user/src/lib.lst
  510. view/body/ArrayView.java</programlisting>
  511. <para>Compile the files using either the -argfile or @ form:</para>
  512. <programlisting>
  513. ajc -argfile sources.lst
  514. ajc @sources.lst</programlisting>
  515. <para>
  516. Argfiles are also supported by jikes and javac, so you
  517. can use the files in hybrid builds. However, the support varies:
  518. </para>
  519. <itemizedlist>
  520. <listitem><para>Only ajc accepts command-line options</para></listitem>
  521. <listitem><para>Jikes and Javac do not accept internal @argfile references.
  522. </para></listitem>
  523. <listitem><para>Jikes and Javac only accept the @file form on the command line.</para></listitem>
  524. </itemizedlist>
  525. </example>
  526. <example id="examplebytecode">
  527. <title>An example using -inpath and -aspectpath</title>
  528. <para>Bytecode weaving using -inpath:
  529. AspectJ 1.2 supports weaving .class files in input zip/jar files
  530. and directories.
  531. Using input jars is like compiling the corresponding
  532. source files, and all binaries are emitted to output. Although
  533. Java-compliant compilers may differ in their output, ajc should
  534. take as input any class files produced by javac, jikes, eclipse,
  535. and, of course, ajc. Aspects included in -inpath will be woven into
  536. like other .class files, and they will affect other types as usual.
  537. </para>
  538. <para>Aspect libraries using -aspectpath:
  539. AspectJ 1.1 supports weaving from read-only libraries containing
  540. aspects. Like input jars, they affect all input; unlike input
  541. jars, they themselves are not affected or emitted as output.
  542. Sources compiled with aspect libraries must be run with the same
  543. aspect libraries on their classpath.
  544. </para>
  545. <para>The following example builds the tracing example in a
  546. command-line environment; it creates a read-only aspect library,
  547. compiles some classes for use as input bytecode, and
  548. compiles the classes and other sources with the aspect library.
  549. </para>
  550. <para>The tracing example is in the AspectJ distribution
  551. ({aspectj}/doc/examples/tracing). This uses the following files:
  552. </para>
  553. <para><programlisting>
  554. aspectj1.1/
  555. bin/
  556. ajc
  557. lib/
  558. aspectjrt.jar
  559. examples/
  560. tracing/
  561. Circle.java
  562. ExampleMain.java
  563. lib/
  564. AbstractTrace.java
  565. TraceMyClasses.java
  566. notrace.lst
  567. Square.java
  568. tracelib.lst
  569. tracev3.lst
  570. TwoDShape.java
  571. version3/
  572. Trace.java
  573. TraceMyClasses.java
  574. </programlisting></para>
  575. <para>Below, the path separator is taken as ";", but file separators
  576. are "/". All commands are on one line. Adjust paths and
  577. commands to your environment as needed.
  578. </para><para>Setup the path, classpath, and current directory:</para>
  579. <programlisting>
  580. cd examples
  581. export ajrt=../lib/aspectjrt.jar
  582. export CLASSPATH="$ajrt"
  583. export PATH="../bin:$PATH"
  584. </programlisting>
  585. <para>Build a read-only tracing library:</para>
  586. <programlisting>
  587. ajc -argfile tracing/tracelib.lst -outjar tracelib.jar
  588. </programlisting>
  589. <para>Build the application with tracing in one step:</para>
  590. <programlisting>
  591. ajc -aspectpath tracelib.jar -argfile tracing/notrace.lst -outjar tracedapp.jar
  592. </programlisting>
  593. <para>Run the application with tracing:</para>
  594. <programlisting>
  595. java -classpath "$ajrt;tracedapp.jar;tracelib.jar" tracing.ExampleMain
  596. </programlisting>
  597. <para>Build the application with tracing from binaries in two steps:</para>
  598. <itemizedlist><listitem><para>
  599. (a) Build the application classes (using javac for demonstration's sake):</para>
  600. <programlisting>
  601. mkdir classes
  602. javac -d classes tracing/*.java
  603. jar cfM app.jar -C classes .
  604. </programlisting>
  605. </listitem>
  606. <listitem><para>
  607. (b) Build the application with tracing:</para>
  608. <programlisting>
  609. ajc -inpath app.jar -aspectpath tracelib.jar -outjar tracedapp.jar
  610. </programlisting>
  611. </listitem></itemizedlist>
  612. <para>Run the application with tracing (same as above):</para>
  613. <programlisting>
  614. java -classpath "$ajrt;tracedapp.jar;tracelib.jar" tracing.ExampleMain
  615. </programlisting>
  616. <para>Run the application without tracing:</para>
  617. <programlisting>
  618. java -classpath "app.jar" tracing.ExampleMain
  619. </programlisting>
  620. </example>
  621. </refsect2>
  622. <refsect2>
  623. <title>The AspectJ compiler API</title>
  624. <para>The AspectJ compiler is implemented completely in Java and can be
  625. called as a Java class. The only interface that should be considered
  626. public are the public methods in <literal>org.aspectj.tools.ajc.Main</literal>.
  627. E.g., <literal>main(String[] args)</literal> takes the
  628. the standard <command>ajc</command> command line arguments.
  629. This means that an alternative way to run the
  630. compiler is </para>
  631. <cmdsynopsis>
  632. <command><literal>java org.aspectj.tools.ajc.Main</literal></command>
  633. <arg><replaceable>option...</replaceable></arg>
  634. <arg><replaceable>file...</replaceable></arg>
  635. </cmdsynopsis>
  636. <para>To access compiler messages programmatically, use the methods
  637. <literal>setHolder(IMessageHolder holder)</literal> and/or
  638. <literal>run(String[] args, IMessageHolder holder)</literal>.
  639. <literal>ajc</literal> reports each message to the holder
  640. using <literal>IMessageHolder.handleMessage(..)</literal>.
  641. If you just want to collect the messages, use
  642. <literal>MessageHandler</literal> as your
  643. <literal>IMessageHolder</literal>.
  644. For example, compile and run the following with
  645. <literal>aspectjtools.jar</literal> on the classpath:
  646. </para>
  647. <programlisting>
  648. import org.aspectj.bridge.*;
  649. import org.aspectj.tools.ajc.Main;
  650. import java.util.Arrays;
  651. public class WrapAjc {
  652. public static void main(String[] args) {
  653. Main compiler = new Main();
  654. MessageHandler m = new MessageHandler();
  655. compiler.run(args, m);
  656. IMessage[] ms = m.getMessages(null, true);
  657. System.out.println("messages: " + Arrays.asList(ms));
  658. }
  659. }
  660. </programlisting>
  661. </refsect2>
  662. <refsect2>
  663. <title>Stack Traces and the SourceFile attribute</title>
  664. <para>Unlike traditional java compilers, the AspectJ compiler may in
  665. certain cases generate classfiles from multiple source files.
  666. Unfortunately, the original Java class file format does not support
  667. multiple
  668. SourceFile attributes. In order to make sure all source file
  669. information is available, the AspectJ compiler may in some cases
  670. encode multiple filenames in the SourceFile attribute.
  671. When the Java VM generates stack traces, it uses this attribute
  672. to specify the source file.
  673. </para>
  674. <para>(The AspectJ 1.0 compiler also supports the .class file extensions of JSR-45.
  675. These permit compliant debuggers (such as jdb in Java 1.4.1) to identify
  676. the right file and line even given many source files for a single class.
  677. JSR-45 support is planned for ajc in AspectJ 1.1, but is not in the initial
  678. release. To get fully debuggable .class files, use the -XnoInline option.)
  679. </para>
  680. <para>Probably the only time you may see this format is when you view
  681. stack traces, where you may encounter traces of the format
  682. </para>
  683. <programlisting>
  684. java.lang.NullPointerException
  685. at Main.new$constructor_call37(Main.java;SynchAspect.java[1k]:1030)
  686. </programlisting>
  687. <para>where instead of the usual
  688. </para>
  689. <programlisting>
  690. File:LineNumber
  691. </programlisting>
  692. <para>format, you see
  693. </para>
  694. <programlisting>
  695. File0;File1[Number1];File2[Number2] ... :LineNumber
  696. </programlisting>
  697. <para>In this case, LineNumber is the usual offset in lines plus the
  698. "start line" of the actual source file. That means you use LineNumber
  699. both to identify the source file and to find the line at issue.
  700. The number in [brackets] after each file tells you the
  701. virtual "start line" for that file (the first file has a start of 0).
  702. </para>
  703. <para> In our example from the null pointer exception trace,
  704. the virtual start line is 1030. Since the file SynchAspect.java
  705. "starts" at line 1000 [1k], the LineNumber points to line 30 of
  706. SynchAspect.java.
  707. </para>
  708. <para> So, when faced with such stack traces, the way to find the actual
  709. source location is to look through the list of "start line" numbers to
  710. find the one just under the shown line number. That is the file where
  711. the source location can actually be found. Then, subtract that "start
  712. line" from the shown line number to find the actual line number within
  713. that file.
  714. </para>
  715. <para>In a class file that comes from only a single source file, the AspectJ
  716. compiler generates SourceFile attributes consistent with
  717. traditional Java compilers.
  718. </para>
  719. </refsect2>
  720. </refsect1>
  721. </refentry>