The ajc command compiles AspectJ and Java language files, weaving aspects as necessary to produce .class files compliant with any Java VM (1.1 or later). To do bytecode weaving, it also accepts input classes or aspects in binary form.
The arguments after the options specify the source file(s) to compile. (Specify source classes using the -injars or -inpath options, below.) Files may be listed directly on the command line, or listed in a file. The -argfile file and @file forms are equivalent, and are interpreted as meaning all the files listed in the specified file. Each line in these files should contain one option or filename. Comments, as in Java, start with // and extend to the end of the line.
NB: You must explicitly pass ajc all sources necessary for the compilation. When compiling source files containing aspects or pointcuts, be sure to include the source files for any types affected by the aspects or picked out by the pointcuts. (To exclude types from the scope affected by the aspect, change the corresponding pointcut or declaration.) This is necessary because, unlike javac, ajc does not search the sourcepath for classes. You may use the -sourceroots option to specify as source all the .aj and .java files in a set of directory trees.
deprecated: since 1.2, use -inpath, which also takes directories.
Accept as source bytecode any .class files in the .jar files or directories on Path. The output will include these classes, possibly as woven with any applicable aspects. Path is a single argument containing a list of paths to zip files or directories, delimited by the platform-specific path delimiter.
Weave binary aspects from JarList zip files into all sources. The aspects should have been output by the same version of the compiler. To run the output classes requires putting all the aspectpath entries on the run classpath. JarList, like classpath, is a single argument containing a list of paths to jar files, delimited by the platform- specific classpath delimiter.
The file is a line-delimited list of arguments. These arguments are inserted into the argument list.
Run the compiler continuously. After the initial compilation, the compiler will wait to recompile until it reads a newline from the standard input, and will quit when it reads a 'q'. It will only recompile necessary components, so a recompile should be much faster than doing a second compile. This requires -sourceroots.
Find and build all .java or .aj source files under any directory listed in DirPaths. DirPaths, like classpath, is a single argument containing a list of paths to directories, delimited by the platform- specific classpath delimiter. Required by -incremental.
Set default level for messages about potential programming mistakes in crosscutting code. {level} may be ignore, warning, or error. This overrides entries in org/aspectj/weaver/XlintDefault.properties from aspectjtools.jar, but does not override levels set using the -Xlintfile option.
Specify properties file to set levels for specific crosscutting messages. PropertyFile is a path to a Java .properties file that takes the same property names and values as org/aspectj/weaver/XlintDefault.properties from aspectjtools.jar, which it also overrides.
Specify where to find user class files. Path is a single argument containing a list of paths to zip files or directories, delimited by the platform-specific path delimiter.
Override location of VM's bootclasspath for purposes of evaluating types when compiling. Path is a single argument containing a list of paths to zip files or directories, delimited by the platform-specific path delimiter.
Override location of VM's extension directories for purposes of evaluating types when compiling. Path is a single argument containing a list of paths to directories, delimited by the platform-specific path delimiter.
Specify where to place generated .class files. If not specified, Directory defaults to the current working dir.
Specify classfile target setting (1.1 to 1.4, default is 1.2)
Toggle assertions (1.3 or 1.4, default is 1.3). When using -source 1.3, an assert() statement valid under Java 1.4 will result in a compiler error. When using -source 1.4, treat assert as a keyword and implement assertions according to the 1.4 language spec.
Emit no warnings (equivalent to '-warn:none') This does not suppress messages generated by declare warning or Xlint.
Emit warnings for any instances of the comma-delimited list of questionable code (eg '-warn:unusedLocals,deprecation'):
constructorName method with constructor name packageDefaultMethod attempt to override package-default method deprecation usage of deprecated type or member maskedCatchBlocks hidden catch block unusedLocals local variable never read unusedArguments method argument never read unusedImports import statement not used by code in file none suppress all compiler warnings-warn:none does not suppress messages generated by declare warning or Xlint.
Keep compiling after error, dumping class files with problem methods
debug attributes level, that may take three forms:
-g all debug info ('-g:lines,vars,source') -g:none no debug info -g:{items} debug info for any/all of [lines, vars, source], e.g., -g:lines,source
Preserve all local variables during code generation (to facilitate debugging).
Specify default source encoding format. Specify custom encoding on a per file basis by suffixing each input source file/folder name with '[encoding]'.
Do not call System.exit(n) at end of compilation (n=0 if no error)
Repeat compilation process N times (typically to do performance analysis).
(Experimental) produce unwoven class files for input using -injars.
(Experimental) runs weaver in reweavable mode which causes it to create woven classes that can be rewoven, subject to the restriction that on attempting a reweave all the types that advised the woven type must be accessible.
(Experimental) This works like incremental mode, but using a file rather than standard input to control the compiler. It will recompile each time file is changed and and halt when file is deleted.
(Experimental) Normally it is an error to declare aspects Serializable. This option removes that restriction.
ajc accepts source files with either the .java extension or the .aj extension. We normally use .java for all of our files in an AspectJ system -- files that contain aspects as well as files that contain classes. However, if you have a need to mechanically distinguish files that use AspectJ's additional functionality from those that are pure Java we recommend using the .aj extension for those files.
We'd like to discourage other means of mechanical distinction such as naming conventions or sub-packages in favor of the .aj extension.
AspectJ is a compatible extension to the Java programming language. The AspectJ compiler adheres to the The Java Language Specfication, Second Edition and to the The Java Virtual Machine Specification, Second Edition and runs on any Java 2 compatible platform. The code it generates runs on any Java 1.1 or later compatible platform.
Example 2. An example using -argfile/@
To avoid specifying file names on the command line, list source files in a line-delimited text argfile. Source file paths may be absolute or relative to the argfile, and may include other argfiles by @-reference. The following file sources.lst contains absolute and relative files and @-references:
Gui.java /home/user/src/Library.java data/Repository.java data/Access.java @../../common/common.lst @/home/user/src/lib.lst view/body/ArrayView.javaCompile the files using either the -argfile or @ form:
ajc -argfile sources.lst ajc @sources.lst
Argfiles are also supported by jikes and javac, so you can use the files in hybrid builds. However, the support varies:
Example 3. An example using -injars and -aspectpath
Bytecode weaving using -injars: AspectJ 1.1 supports weaving from input zip or jar files containing class files. Using input jars is like compiling the corresponding source files, and all binaries are emitted to output. Although Java-compliant compilers may differ in their output, ajc should take as input any class files produced by javac, jikes, eclipse, and, of course, ajc. Aspects included in -injars will be woven into like other .class files, but they will specify any crosscutting code (i.e., they will not be woven into other types). To use aspects in their binary form to specify crosscutting, see -aspectpath below.
Aspect libraries using -aspectpath: AspectJ 1.1 supports weaving from read-only libraries containing aspects. Like input jars, they affect all input; unlike input jars, they themselves are not affected or emitted as output. Sources compiled with aspect libraries must be run with the same aspect libraries on their classpath.
The following example builds the tracing example in a command-line environment; it creates a read-only aspect library, compiles some classes for use as input bytecode, and compiles the classes and other sources with the aspect library.
The tracing example is in the AspectJ distribution ({aspectj}/doc/examples/tracing). This uses the following files:
aspectj1.1/ bin/ ajc lib/ aspectjrt.jar examples/ tracing/ Circle.java ExampleMain.java lib/ AbstractTrace.java TraceMyClasses.java notrace.lst Square.java tracelib.lst tracev3.lst TwoDShape.java version3/ Trace.java TraceMyClasses.java
Below, the path separator is taken as ";", but file separators are "/". All commands are on one line. Adjust paths and commands to your environment as needed.
Setup the path, classpath, and current directory:
cd examples export ajrt=../lib/aspectjrt.jar export CLASSPATH="$ajrt" export PATH="../bin:$PATH"
Build a read-only tracing library:
ajc -argfile tracing/tracelib.lst -outjar tracelib.jar
Build the application with tracing in one step:
ajc -aspectpath tracelib.jar -argfile tracing/notrace.lst -outjar tracedapp.jar
Run the application with tracing:
java -classpath "$ajrt;tracedapp.jar;tracelib.jar" tracing.ExampleMain
Build the application with tracing from binaries in two steps:
(a) Build the application classes (using javac for demonstration's sake):
mkdir classes javac -d classes tracing/*.java jar cfM app.jar -C classes .
(b) Build the application with tracing:
ajc -injars app.jar -aspectpath tracelib.jar -outjar tracedapp.jar
Run the application with tracing (same as above):
java -classpath "$ajrt;tracedapp.jar;tracelib.jar" tracing.ExampleMain
Run the application without tracing:
java -classpath "app.jar" tracing.ExampleMain
The AspectJ compiler is implemented completely in Java and can be called as a Java class. The only interface that should be considered public are the public methods in org.aspectj.tools.ajc.Main. E.g., main(String[] args) takes the the standard ajc command line arguments. This means that an alternative way to run the compiler is
To access compiler messages programmatically, use the methods setHolder(IMessageHolder holder) and/or run(String[] args, IMessageHolder holder).
Unlike traditional java compilers, the AspectJ compiler may in certain cases generate classfiles from multiple source files. Unfortunately, the original Java class file format does not support multiple SourceFile attributes. In order to make sure all source file information is available, the AspectJ compiler may in some cases encode multiple filenames in the SourceFile attribute. When the Java VM generates stack traces, it uses this attribute to specify the source file.
(The AspectJ 1.0 compiler also supports the .class file extensions of JSR-45. These permit compliant debuggers (such as jdb in Java 1.4.1) to identify the right file and line even given many source files for a single class. JSR-45 support is planned for ajc in AspectJ 1.1, but is not in the initial release. To get fully debuggable .class files, use the -XnoInline option.)
Probably the only time you may see this format is when you view stack traces, where you may encounter traces of the format
java.lang.NullPointerException at Main.new$constructor_call37(Main.java;SynchAspect.java[1k]:1030)
where instead of the usual
File:LineNumber
format, you see
File0;File1[Number1];File2[Number2] ... :LineNumber
In this case, LineNumber is the usual offset in lines plus the "start line" of the actual source file. That means you use LineNumber both to identify the source file and to find the line at issue. The number in [brackets] after each file tells you the virtual "start line" for that file (the first file has a start of 0).
In our example from the null pointer exception trace, the virtual start line is 1030. Since the file SynchAspect.java "starts" at line 1000 [1k], the LineNumber points to line 30 of SynchAspect.java.
So, when faced with such stack traces, the way to find the actual source location is to look through the list of "start line" numbers to find the one just under the shown line number. That is the file where the source location can actually be found. Then, subtract that "start line" from the shown line number to find the actual line number within that file.
In a class file that comes from only a single source file, the AspectJ compiler generates SourceFile attributes consistent with traditional Java compilers.