ajc
compiler for the AspectJ language
ajc
Options
file...
@file...
-argfile file...
Description
The command ajc compiles AspectJ and Java
language source files into class files. Options and files may be
specified directly on the command line, or indirectly by naming a
file which contains them.
The arguments after the options specify the file(s) to compile.
Files may be listed directly on the command line, or listed in a file.
The @file and
-argfile 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 of the source files necessary
for the compilation. When you are 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.
(If you wish 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.
Options
-verbose
Output messages about what ajc is doing
-version
Print the version of ajc
-nocomments
Don't generate any comments into the woven code.
Only relevant with -preprocess mode.
-emacssym
Generate symbols used by AJDE for Emacs
-usejavac
Use javac to generate .class files
-preprocess
Don't try to generate any .class files.
Generate regular Java code into workingdir
-workingdir Directory
Specify where to place intermediate .java files
Directory defaults to ./ajworkingdir.
Only relevant with -usejavac or -preprocess modes.
-O
Optimize; may hinder debugging or enlarge class files
-d Directory
Specify where to place generated .class files
Directory defaults to the current working dir
-classpath Path
Specify where to find user class files
-bootclasspath Path
Override location of bootstrap class files
-extdirs Path
Override location of installed extensions
-argfile File
the file is a line-delimited list of arguments
these arguments are inserted into the argument list
-encoding Encoding
Specify character encoding used by source files
-source 1.4
Specify support for assertions according to the 1.4 Java language.
This will treat assert as a keyword and will
implement assertions according to the 1.4 language spec.
-lenient
Be extra-lenient in interpreting the java specification
This allows some statements that some compilers consider errors.
-strict
Be extra-strict in interpreting the java specification
This signals errors for some statements that many compilers don't
catch, and generates code strictly according to the Java Language
Specification, even though such code may not run on 1.2 VMs.
File names
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.
Filename conventions are hard to enforce and lead to awkward names
for your aspects. Instead of TracingAspect.java we
recommend using Tracing.aj (or just
Tracing.java) instead.
Sub-packages move aspects out of their natural place in a system
and can create an artificial need for privileged aspects. Instead of
adding a sub-package like aspects we recommend using the
.aj extension and including these files in your existing
packages instead.
Compatibility
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.
Examples
A simple example
Compile two files:
ajc HelloWorld.java Trace.java
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.java
Compile the files using either the -argfile or @ form:
ajc -argfile sources.lst
ajc @sources.lst
Argfiles are also supported by jikes, javac, and ajdoc, so you
can use the files in hybrid builds. However, the support varies:
Only ajc accepts command-line options
Jikes and Javac do not accept internal @argfile references.
Jikes and Javac only accept the @file form on the command line.
The AspectJ compiler API
The AspectJ compiler is implemented completely in Java and can be
called as a Java class. The only interface that should be considered
public is the method org.aspectj.tools.ajc.Main.main(String[]
args)
where args are the standard ajc
command line arguments. This means that an alternative way to run the
compiler is
java org.aspectj.tools.ajc.Main
option...
file...
To run in -usejavac mode,
you must include in your classpath the
tools.jar from your Java 2 developer's kit.
Stack Traces and the SourceFile attribute
Unlike traditional java compilers, the AspectJ compiler may in
certain cases generate classfiles from multiple source files.
Unfortunately, the Java class file format does not support multiple
SourceFile attributes. So, 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.
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.
Of course, AspectJ tools will do this decoding for you, and in a
class file that comes from only a single source file, the AspectJ
compiler generates SourceFile attributes consistent with
traditional Java compilers.