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

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