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

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