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

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