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.adoc 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. [[ajc]]
  2. = `ajc`, the AspectJ compiler/weaver
  3. == Name
  4. `ajc` - compiler and bytecode weaver for the AspectJ and Java languages
  5. == Synopsis
  6. [subs=+quotes]
  7. ajc [_option_...] [_file_... | @_file_... | -argfile _file_...]
  8. == Description
  9. The `ajc` command compiles and weaves AspectJ and Java source and .class
  10. files, producing .class files compliant with any Java VM (1.1 or later).
  11. It combines compilation and bytecode weaving and supports incremental
  12. builds; you can also weave bytecode at run-time using xref:ltw.adoc#ltw[Load-Time Weaving].
  13. The arguments after the options specify the source file(s) to compile.
  14. To specify source classes, use `-inpath` (below). Files may be listed
  15. directly on the command line or in a file. The `-argfile file` and
  16. `@file` forms are equivalent, and are interpreted as meaning all the
  17. arguments listed in the specified file.
  18. `Note:` You must explicitly pass `ajc` all necessary sources. Be sure to
  19. include the source not only for the aspects or pointcuts but also for
  20. any affected types. Specifying all sources is necessary because, unlike
  21. javac, ajc does not search the sourcepath for classes. (For a discussion
  22. of what affected types might be required, see
  23. xref:../progguide/implementation.adoc[The AspectJ Programming Guide,
  24. Implementation Appendix].)
  25. To specify sources, you can list source files as arguments or use the
  26. options `-sourceroots` or `-inpath`. If there are multiple sources for
  27. any type, the result is undefined since ajc has no way to determine
  28. which source is correct. (This happens most often when users include the
  29. destination directory on the inpath and rebuild.)
  30. [[ajc_options]]
  31. === Options
  32. `-injars <JarList>`::
  33. deprecated: since 1.2, use -inpath, which also takes directories.
  34. `-inpath <Path>`::
  35. Accept as source bytecode any .class files in the .jar files or
  36. directories on Path. The output will include these classes, possibly
  37. as woven with any applicable aspects. Path is a single argument
  38. containing a list of paths to zip files or directories, delimited by
  39. the platform-specific path delimiter.
  40. `-aspectpath <Path>`::
  41. Weave binary aspects from jar files and directories on path into all
  42. sources. The aspects should have been output by the same version of
  43. the compiler. When running the output classes, the run classpath
  44. should contain all aspectpath entries. Path, like classpath, is a
  45. single argument containing a list of paths to jar files, delimited by
  46. the platform- specific classpath delimiter.
  47. `-argfile <File>`::
  48. The file contains a line-delimited list of arguments. Each line in the
  49. file should contain one option, filename, or argument string (e.g., a
  50. classpath or inpath). Arguments read from the file are inserted into
  51. the argument list for the command. Relative paths in the file are
  52. calculated from the directory containing the file (not the current
  53. working directory). Comments, as in Java, start with `//` and extend
  54. to the end of the line. Options specified in argument files may
  55. override rather than extending existing option values, so avoid
  56. specifying options like <-classpath> in argument files unlike the
  57. argument file is the only build specification. The form <@file> is the
  58. same as specifying <-argfile file>.
  59. `-outjar <output.jar>`::
  60. Put output classes in zip file output.jar.
  61. `-outxml`::
  62. Generate aop.xml file for load-time weaving with default name.
  63. `-outxmlfile <custom/aop.xml>`::
  64. Generate aop.xml file for load-time weaving with custom name.
  65. `-incremental`::
  66. Run the compiler continuously. After the initial compilation, the
  67. compiler will wait to recompile until it reads a newline from the
  68. standard input, and will quit when it reads a 'q'. It will only
  69. recompile necessary components, so a recompile should be much faster
  70. than doing a second compile. This requires -sourceroots.
  71. `-sourceroots <DirPaths>`::
  72. Find and build all .java or .aj source files under any directory
  73. listed in DirPaths. DirPaths, like classpath, is a single argument
  74. containing a list of paths to directories, delimited by the platform-
  75. specific classpath delimiter. Required by -incremental.
  76. `-crossrefs`::
  77. Generate a build .ajsym file into the output directory. Used for
  78. viewing crosscutting references by tools like the AspectJ Browser.
  79. `-emacssym`::
  80. Generate .ajesym symbol files for emacs support (deprecated).
  81. `-Xlint`::
  82. Same as -Xlint:warning (enabled by default)
  83. `-Xlint:\{level}`::
  84. Set default level for messages about potential programming mistakes in
  85. crosscutting code. \{level} may be ignore, warning, or error. This
  86. overrides entries in org/aspectj/weaver/XlintDefault.properties from
  87. aspectjtools.jar, but does not override levels set using the
  88. -Xlintfile option.
  89. `-Xlintfile <PropertyFile>`::
  90. Specify properties file to set levels for specific crosscutting
  91. messages. PropertyFile is a path to a Java .properties file that takes
  92. the same property names and values as
  93. org/aspectj/weaver/XlintDefault.properties from aspectjtools.jar,
  94. which it also overrides.
  95. `-help`::
  96. Emit information on compiler options and usage
  97. `-version`::
  98. Emit the version of the AspectJ compiler
  99. `-classpath <Path>`::
  100. Specify where to find user class files. Path is a single argument
  101. containing a list of paths to zip files or directories, delimited by
  102. the platform-specific path delimiter.
  103. `-bootclasspath <Path>`::
  104. Override location of VM's bootclasspath for purposes of evaluating
  105. types when compiling. Path is a single argument containing a list of
  106. paths to zip files or directories, delimited by the platform-specific
  107. path delimiter.
  108. `-extdirs <Path>`::
  109. Override location of VM's extension directories for purposes of
  110. evaluating types when compiling. Path is a single argument containing
  111. a list of paths to directories, delimited by the platform-specific
  112. path delimiter.
  113. `-d <Directory>`::
  114. Specify where to place generated .class files. If not specified,
  115. <Directory> defaults to the current working dir.
  116. `-target <[1.1 to 1.5]>`::
  117. Specify classfile target setting (1.1 to 1.5, default is 1.2)
  118. `-1.3`::
  119. Set compliance level to 1.3 This implies -source 1.3 and -target 1.1.
  120. `-1.4`::
  121. Set compliance level to 1.4 (default) This implies -source 1.4 and
  122. -target 1.2.
  123. `-1.5`::
  124. Set compliance level to 1.5. This implies -source 1.5 and -target 1.5.
  125. `-source <[1.3|1.4|1.5]>`::
  126. Toggle assertions (1.3, 1.4, or 1.5 - default is 1.4). When using
  127. -source 1.3, an assert() statement valid under Java 1.4 will result in
  128. a compiler error. When using -source 1.4, treat `assert` as a keyword
  129. and implement assertions according to the 1.4 language spec. When
  130. using -source 1.5, Java 5 language features are permitted.
  131. `-nowarn`::
  132. Emit no warnings (equivalent to '-warn:none') This does not suppress
  133. messages generated by `declare warning` or `Xlint`.
  134. `-warn: <items>`::
  135. Emit warnings for any instances of the comma-delimited list of
  136. questionable code (eg '-warn:unusedLocals,deprecation'):
  137. +
  138. [source, text]
  139. ....
  140. constructorName method with constructor name
  141. packageDefaultMethod attempt to override package-default method
  142. deprecation usage of deprecated type or member
  143. maskedCatchBlocks hidden catch block
  144. unusedLocals local variable never read
  145. unusedArguments method argument never read
  146. unusedImports import statement not used by code in file
  147. none suppress all compiler warnings
  148. ....
  149. +
  150. `-warn:none` does not suppress messages generated by `declare warning`
  151. or `Xlint`.
  152. `-deprecation`::
  153. Same as -warn:deprecation
  154. `-noImportError`::
  155. Emit no errors for unresolved imports
  156. `-proceedOnError`::
  157. Keep compiling after error, dumping class files with problem methods
  158. `-g<:[lines,vars,source]>`::
  159. debug attributes level, that may take three forms:
  160. +
  161. [source, text]
  162. ....
  163. -g all debug info ('-g:lines,vars,source')
  164. -g:none no debug info
  165. -g:{items} debug info for any/all of [lines, vars, source], e.g.,
  166. -g:lines,source
  167. ....
  168. `-preserveAllLocals`::
  169. Preserve all local variables during code generation (to facilitate
  170. debugging).
  171. `-referenceInfo`::
  172. Compute reference information.
  173. `-encoding <format>`::
  174. Specify default source encoding format. Specify custom encoding on a
  175. per file basis by suffixing each input source file/folder name with
  176. '[encoding]'.
  177. `-verbose`::
  178. Emit messages about accessed/processed compilation units
  179. `-showWeaveInfo`::
  180. Emit messages about weaving
  181. `-log <file>`::
  182. Specify a log file for compiler messages.
  183. `-progress`::
  184. Show progress (requires -log mode).
  185. `-time`::
  186. Display speed information.
  187. `-noExit`::
  188. Do not call System.exit(n) at end of compilation (n=0 if no error)
  189. `-repeat <N>`::
  190. Repeat compilation process N times (typically to do performance
  191. analysis).
  192. `-XterminateAfterCompilation`::
  193. Causes compiler to terminate before weaving
  194. `-XaddSerialVersionUID`::
  195. Causes the compiler to calculate and add the SerialVersionUID field to
  196. any type implementing Serializable that is affected by an aspect. The
  197. field is calculated based on the class before weaving has taken place.
  198. `-Xreweavable[:compress]`::
  199. (Experimental - deprecated as now default) Runs weaver in reweavable
  200. mode which causes it to create woven classes that can be rewoven,
  201. subject to the restriction that on attempting a reweave all the types
  202. that advised the woven type must be accessible.
  203. `-XnoInline`::
  204. (Experimental) do not inline around advice
  205. `-XincrementalFile <file>`::
  206. (Experimental) This works like incremental mode, but using a file
  207. rather than standard input to control the compiler. It will recompile
  208. each time file is changed and and halt when file is deleted.
  209. `-XserializableAspects`::
  210. (Experimental) Normally it is an error to declare aspects
  211. Serializable. This option removes that restriction.
  212. `-XnotReweavable`::
  213. (Experimental) Create class files that can't be subsequently rewoven
  214. by AspectJ.
  215. `-Xajruntimelevel:1.2, ajruntimelevel:1.5`::
  216. (Experimental) Allows code to be generated that targets a 1.2 or a 1.5
  217. level AspectJ runtime (default 1.5)
  218. === File names
  219. ajc accepts source files with either the `.java` extension or the `.aj`
  220. extension. We normally use `.java` for all of our files in an AspectJ
  221. system -- files that contain aspects as well as files that contain
  222. classes. However, if you have a need to mechanically distinguish files
  223. that use AspectJ's additional functionality from those that are pure
  224. Java we recommend using the `.aj` extension for those files.
  225. We'd like to discourage other means of mechanical distinction such as
  226. naming conventions or sub-packages in favor of the `.aj` extension.
  227. * Filename conventions are hard to enforce and lead to awkward names for
  228. your aspects. Instead of `TracingAspect.java` we recommend using
  229. `Tracing.aj` (or just `Tracing.java`) instead.
  230. * Sub-packages move aspects out of their natural place in a system and
  231. can create an artificial need for privileged aspects. Instead of adding
  232. a sub-package like `aspects` we recommend using the `.aj` extension and
  233. including these files in your existing packages instead.
  234. === Compatibility
  235. AspectJ is a compatible extension to the Java programming language. The
  236. AspectJ compiler adheres to the
  237. http://java.sun.com/docs/books/jls/index.html[The Java Language
  238. Specification, Second Edition] and to the
  239. http://java.sun.com/docs/books/vmspec/index.html[The Java Virtual
  240. Machine Specification, Second Edition] and runs on any Java 2 compatible
  241. platform. The code it generates runs on any Java 1.1 or later compatible
  242. platform. For more information on compatibility with Java and with
  243. previous releases of AspectJ, see xref:compatibility.adoc#versionCompatibility[Version Compatibility].
  244. === Examples
  245. Compile two files:
  246. [source, text]
  247. ....
  248. ajc HelloWorld.java Trace.java
  249. ....
  250. To avoid specifying file names on the command line, list source files in
  251. a line-delimited text argfile. Source file paths may be absolute or
  252. relative to the argfile, and may include other argfiles by @-reference.
  253. The following file `sources.lst` contains absolute and relative files
  254. and @-references:
  255. [source, text]
  256. ....
  257. Gui.java
  258. /home/user/src/Library.java
  259. data/Repository.java
  260. data/Access.java
  261. @../../common/common.lst
  262. @/home/user/src/lib.lst
  263. view/body/ArrayView.java
  264. ....
  265. Compile the files using either the -argfile or @ form:
  266. [source, text]
  267. ....
  268. ajc -argfile sources.lst
  269. ajc @sources.lst
  270. ....
  271. Argfiles are also supported by jikes and javac, so you can use the files
  272. in hybrid builds. However, the support varies:
  273. * Only ajc accepts command-line options
  274. * Jikes and Javac do not accept internal @argfile references.
  275. * Jikes and Javac only accept the @file form on the command line.
  276. Bytecode weaving using -inpath: AspectJ 1.2 supports weaving .class
  277. files in input zip/jar files and directories. Using input jars is like
  278. compiling the corresponding source files, and all binaries are emitted
  279. to output. Although Java-compliant compilers may differ in their output,
  280. ajc should take as input any class files produced by javac, jikes,
  281. eclipse, and, of course, ajc. Aspects included in -inpath will be woven
  282. into like other .class files, and they will affect other types as usual.
  283. Aspect libraries using -aspectpath: AspectJ 1.1 supports weaving from
  284. read-only libraries containing aspects. Like input jars, they affect all
  285. input; unlike input jars, they themselves are not affected or emitted as
  286. output. Sources compiled with aspect libraries must be run with the same
  287. aspect libraries on their classpath.
  288. The following example builds the tracing example in a command-line
  289. environment; it creates a read-only aspect library, compiles some
  290. classes for use as input bytecode, and compiles the classes and other
  291. sources with the aspect library.
  292. The tracing example is in the AspectJ distribution
  293. (\{aspectj}/doc/examples/tracing). This uses the following files:
  294. [source, text]
  295. ....
  296. aspectj1.1/
  297. bin/
  298. ajc
  299. lib/
  300. aspectjrt.jar
  301. examples/
  302. tracing/
  303. Circle.java
  304. ExampleMain.java
  305. lib/
  306. AbstractTrace.java
  307. TraceMyClasses.java
  308. notrace.lst
  309. Square.java
  310. tracelib.lst
  311. tracev3.lst
  312. TwoDShape.java
  313. version3/
  314. Trace.java
  315. TraceMyClasses.java
  316. ....
  317. Below, the path separator is taken as ";", but file separators are "/".
  318. All commands are on one line. Adjust paths and commands to your
  319. environment as needed.
  320. Setup the path, classpath, and current directory:
  321. [source, text]
  322. ....
  323. cd examples
  324. export ajrt=../lib/aspectjrt.jar
  325. export CLASSPATH="$ajrt"
  326. export PATH="../bin:$PATH"
  327. ....
  328. Build a read-only tracing library:
  329. [source, text]
  330. ....
  331. ajc -argfile tracing/tracelib.lst -outjar tracelib.jar
  332. ....
  333. Build the application with tracing in one step:
  334. [source, text]
  335. ....
  336. ajc -aspectpath tracelib.jar -argfile tracing/notrace.lst -outjar tracedapp.jar
  337. ....
  338. Run the application with tracing:
  339. [source, text]
  340. ....
  341. java -classpath "$ajrt;tracedapp.jar;tracelib.jar" tracing.ExampleMain
  342. ....
  343. Build the application with tracing from binaries in two steps:
  344. * (a) Build the application classes (using javac for
  345. demonstration's sake):
  346. +
  347. [source, text]
  348. ....
  349. mkdir classes
  350. javac -d classes tracing/*.java
  351. jar cfM app.jar -C classes .
  352. ....
  353. * (b) Build the application with tracing:
  354. +
  355. [source, text]
  356. ....
  357. ajc -inpath app.jar -aspectpath tracelib.jar -outjar tracedapp.jar
  358. ....
  359. Run the application with tracing (same as above):
  360. [source, text]
  361. ....
  362. java -classpath "$ajrt;tracedapp.jar;tracelib.jar" tracing.ExampleMain
  363. ....
  364. Run the application without tracing:
  365. [source, text]
  366. ....
  367. java -classpath "app.jar" tracing.ExampleMain
  368. ....
  369. === The AspectJ compiler API
  370. The AspectJ compiler is implemented completely in Java and can be called
  371. as a Java class. The only interface that should be considered public are
  372. the public methods in `org.aspectj.tools.ajc.Main`. E.g.,
  373. `main(String[] args)` takes the the standard `ajc` command line
  374. arguments. This means that an alternative way to run the compiler is
  375. [subs=+quotes]
  376. java org.aspectj.tools.ajc.Main [_option_...] [_file_...]
  377. To access compiler messages programmatically, use the methods
  378. `setHolder(IMessageHolder holder)` and/or
  379. `run(String[] args, IMessageHolder holder)`. `ajc` reports each message
  380. to the holder using `IMessageHolder.handleMessage(..)`. If you just want
  381. to collect the messages, use `MessageHandler` as your `IMessageHolder`.
  382. For example, compile and run the following with `aspectjtools.jar` on
  383. the classpath:
  384. [source, java]
  385. ....
  386. import org.aspectj.bridge.*;
  387. import org.aspectj.tools.ajc.Main;
  388. import java.util.Arrays;
  389. public class WrapAjc {
  390. public static void main(String[] args) {
  391. Main compiler = new Main();
  392. MessageHandler m = new MessageHandler();
  393. compiler.run(args, m);
  394. IMessage[] ms = m.getMessages(null, true);
  395. System.out.println("messages: " + Arrays.asList(ms));
  396. }
  397. }
  398. ....
  399. === Stack Traces and the SourceFile attribute
  400. Unlike traditional java compilers, the AspectJ compiler may in certain
  401. cases generate classfiles from multiple source files. Unfortunately, the
  402. original Java class file format does not support multiple SourceFile
  403. attributes. In order to make sure all source file information is
  404. available, the AspectJ compiler may in some cases encode multiple
  405. filenames in the SourceFile attribute. When the Java VM generates stack
  406. traces, it uses this attribute to specify the source file.
  407. (The AspectJ 1.0 compiler also supports the .class file extensions of
  408. JSR-45. These permit compliant debuggers (such as jdb in Java 1.4.1) to
  409. identify the right file and line even given many source files for a
  410. single class. JSR-45 support is planned for ajc in AspectJ 1.1, but is
  411. not in the initial release. To get fully debuggable .class files, use
  412. the -XnoInline option.)
  413. Probably the only time you may see this format is when you view stack
  414. traces, where you may encounter traces of the format
  415. [source, text]
  416. ....
  417. java.lang.NullPointerException
  418. at Main.new$constructor_call37(Main.java;SynchAspect.java[1k]:1030)
  419. ....
  420. where instead of the usual
  421. [source, text]
  422. ....
  423. File:LineNumber
  424. ....
  425. format, you see
  426. [source, text]
  427. ....
  428. File0;File1[Number1];File2[Number2] ... :LineNumber
  429. ....
  430. In this case, LineNumber is the usual offset in lines plus the "start
  431. line" of the actual source file. That means you use LineNumber both to
  432. identify the source file and to find the line at issue. The number in
  433. [brackets] after each file tells you the virtual "start line" for that
  434. file (the first file has a start of 0).
  435. In our example from the null pointer exception trace, the virtual start
  436. line is 1030. Since the file SynchAspect.java "starts" at line 1000
  437. [1k], the LineNumber points to line 30 of SynchAspect.java.
  438. So, when faced with such stack traces, the way to find the actual source
  439. location is to look through the list of "start line" numbers to find the
  440. one just under the shown line number. That is the file where the source
  441. location can actually be found. Then, subtract that "start line" from
  442. the shown line number to find the actual line number within that file.
  443. In a class file that comes from only a single source file, the AspectJ
  444. compiler generates SourceFile attributes consistent with traditional
  445. Java compilers.