By Russell Miles
Price: $44.95 USD
£31.95 GBP
Cover | Table of Contents | Colophon
pragma instructions, and even debuggers all
contain some of the behavior that underlies the aspect-oriented
approach. But the significant difference is in the philosophy behind
the approach and how that philosophy drives the technology and tools.
Aspect orientation is not about any one of these technologies on its
own, though it is a new and more modular implementation of the
advantages that these technologies have brought to their own domains
in the past.pragma instructions, and even debuggers all
contain some of the behavior that underlies the aspect-oriented
approach. But the significant difference is in the philosophy behind
the approach and how that philosophy drives the technology and tools.
Aspect orientation is not about any one of these technologies on its
own, though it is a new and more modular implementation of the
advantages that these technologies have brought to their own domains
in the past.http://www.parc.xerox.com/research/csl/projects/aspectj/default.html
http://www.eclipse.org/aspectj
http://www.eclipse.org/ajdt
http://sourceforge.net/projects/aspectj4jbuildr/
http://sourceforge.net/projects/aspectj4netbean/
ajc,
that interacts with the existing Java compiler,
javac. Working within the constraints of the Java
compilation process ensures that your aspect-oriented programs
produce standard Java classes that can be run by any Java Virtual
Machine (JVM). ajc simply automates the mapping of
your aspects onto your Java classes.ajc tool is used for
compile-time
weaving of aspects either from the command line or within an IDE or
other build tool, but there is also an alternative weaving method
supported in AspectJ,
load-time
weaving. As its title suggests, load-time weaving supports the
weaving of aspects into your application at the time when the Java
class loader is loading the application into the JVM. This is a
fairly new feature of AspectJ and is briefly described in this
chapter with the caveat that the current methods by which load-time
weaving is achieved using AspectJ may change in the future as this
facility matures.ajc
command-line compiler then it's time to move on to
installing and using some of the capabilities available to the
AspectJ developer within the Eclipse development environment. These
include how to set up an Eclipse project with an AspectJ Nature using
the new project creation wizards and using Eclipse to vary the
aspects that are actually built into a single project using AspectJ
build configuration files.ajc,
that interacts with the existing Java compiler,
javac. Working within the constraints of the Java
compilation process ensures that your aspect-oriented programs
produce standard Java classes that can be run by any Java Virtual
Machine (JVM). ajc simply automates the mapping of
your aspects onto your Java classes.ajc tool is used for
compile-time
weaving of aspects either from the command line or within an IDE or
other build tool, but there is also an alternative weaving method
supported in AspectJ,
load-time
weaving. As its title suggests, load-time weaving supports the
weaving of aspects into your application at the time when the Java
class loader is loading the application into the JVM. This is a
fairly new feature of AspectJ and is briefly described in this
chapter with the caveat that the current methods by which load-time
weaving is achieved using AspectJ may change in the future as this
facility matures.ajc
command-line compiler then it's time to move on to
installing and using some of the capabilities available to the
AspectJ developer within the Eclipse development environment. These
include how to set up an Eclipse project with an AspectJ Nature using
the new project creation wizards and using Eclipse to vary the
aspects that are actually built into a single project using AspectJ
build configuration files.http://www.aspectj.org.> ajc
AspectJ Compiler
Usage: <options> <source file | @argfile>..
AspectJ-specific options:
... Listed compiler options
1 fail|abort
1
fail
|
abort
message here; if you get the output shown above, then the AspectJ
tools have been successfully installed and located and are available
for use.http://www.aspectj.org and following the
links to Downloads.package com.oreilly.aspectjcookbook;
public class MyClass
{
public void foo(int number, String name)
{
System.out.println("Inside foo (int, String)");
}
public static void main(String[] args)
{
// Create an instance of MyClass
MyClass myObject = new MyClass( );
// Make the call to foo
myObject.foo(1, "Russ Miles");
}
}
void
foo(int, String) method in the MyClass
class.package com.oreilly.aspectjcookbook;
public aspect HelloWorld
{
pointcut callPointcut( ) :
call(void com.oreilly.aspectjcookbook.MyClass.foo(int, String));
before( ) : callPointcut( )
{
System.out.println(
"Hello World");
System.out.println(
"In the advice attached to the call pointcut");
}
}
ajc command to compile this simple application and
produce the byte code .class files for both the
aspect and the class:> ajc -classpath %MY_CLASSPATH% -d %MY_DESTINATION_DIRECTORY% com/oreilly/ aspectjcookbook/MyClass.java com/oreilly/aspectjcookbook/HelloWorld.java
warning couldn't find aspectjrt.jar on classpath, checked: error can't find type org.aspectj.lang.JoinPoint 1 error, 1 warning
// File files.lst com/oreilly/aspectjcookbook/MyClass.java com/oreilly/aspectjcookbook/MyAspect.java com/oreilly/aspectjcookbook/AnotherClass.java com/oreilly/aspectjcookbook/AnotherAspect.java
ajc
compiler to apply the aspects to the classes:> ajc -argfile files.lst -classpath %MY_CLASSPATH% -d %MY_DESTINATION_DIRECTORY%
ajc tool completes the
compilation of aspects and classes is largely transparent to the
developer and can be treated as a black box. You
shouldn't really worry about the interim steps that
may be taking place inside the AspectJ compiler, short of a desire to
get into development work on ajc itself.ajc compiler does not
search the source or class path for files to compile; it must be told
which files are to be involved in the compilation. This means that
all of your source that is to be compiled with
aspects must be fed directly to the
ajc compiler.
There are three ways to supply the files
to be compiled to the ajc compiler (two of which
are semantically equivalent):-argfile option-inpath command-line option when running
the ajc command.ajc command weaves aspects into Java byte code
which can reside in .class files, within a Java
.jar library file or a mixture of the two. The
following instructions show you how to take the code from
Recipe 2.2 and
package the MyClass class in a
.jar file before weaving the
HelloWorld aspect into the library:MyClass class using the traditional
javac command:> javac -classpath %MY_CLASSPATH% -d %MY_DESTINATION_DIRECTORY% com/oreilly/ aspectjcookbook/MyClass.java
> jar -cvf MyApp.jar com/oreilly/aspectjcookbook/MyClass.class
ajc command, specifying the new
MyApp.jar on the command line using the
-inpath option:> ajc -classpath %MY_CLASSPATH% -d %MY_DESTINATION_DIRECTORY% -inpath MyApp.jar
com/oreilly/aspectjcookbook/HelloWorld.java
inpath option forces the
ajc compiler to extract the Java byte code from
the supplied .jar files into the destination
directory as specified by the -d option. The
ajc compiler then includes that extracted byte
code in the aspect weaving process.ajc
then you will have successfully woven the classes contained within
the MyApp.jar file with the
HelloWorld aspect. Because the
ajc command extracts the classes from the
.jar files supplied to the
-inpath option, they are no longer needed to run
the application. However, you can optionally re-package your new
application in a HelloWorld
aspect to the MyClass class at load time:MyClass class using the traditional
javac command:> javac -classpath %MY_CLASSPATH% -d %MY_DESTINATION_DIRECTORY% com/oreilly/ aspectjcookbook/MyClass.java
javac command, you are completely
avoiding weaving any aspects into your application at compile time.
If you have some aspects that you want to include at compile time,
you can use the ajc command and list the aspects
to be included at that point in the AspectJ build configuration file,
not specifying any aspects you intend to only weave at load time.MyClass class
has been compiled without any aspects by running the application
using the java command:> java com.oreilly.aspectjcookbook.MyClass Inside foo (int, String)
HelloWorld aspect into an aspect
library .jar file using the
ajc command:> ajc -outjar helloworldlibrary.jar -d %MY_DESTINATION_DIRECTORY% com/oreilly/ aspectjcookbook/HelloWorld.java
HelloWorld aspect:warning no match for this type name: com$oreilly$aspectjcookbook$MyClass [Xlint:
invalidAbsoluteTypeName]
call(void com.oreilly.aspectjcookbook.MyClass.foo(int, String));
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
javadoc format documentation
for your AspectJ application.ajdoc tool from the command line or from
within Eclipse.javadoc tool for
generating comprehensive documentation for their Java applications
since Java 1.1. The javadoc tool was never
designed to handle the new language constructs that AspectJ
introduces and is not able to generate documentation for an AspectJ
application. To meet this problem, the developers of AspectJ created
ajdoc, a tool that extends
javadoc so that it can correctly and usefully
document the aspect-oriented structures in your application.ajdoc tool can be accessed from the command line
by typing the following command:> ajdoc -sourcepath <The root location of you code> -d <The directory in which you want your documentation to be placed> <The file locations of each of the classes aspects and classes to include in the generated documentation>
ajdoc tool provides the
-argfile parameter so that you can provide an
explicit AspectJ build configuration .lst file,
as mentioned in Recipe 2.3. A
.lst file contains the build configuration for a
set of classes and aspects in your application for which
documentation should be produced.ajdoc means that you can now document your
pointcuts, advice, and aspects as shown in Example 2-5.package com.oreilly.aspectjcookbook;
/**
* A simple aspect to weave in a HelloWorld message when foo(int,name)
* is called on objects of the MyClass class.
* @author russellmiles
* @version 1.0
*
*/
public aspect HelloWorld
{
/**
* Selects join points on calls to the MyClass.foo(int,String) method.
*/
pointcut callPointCut( ) :
call(void com.oreilly.aspectjcookbook.MyClass.foo(int, String));
/**
* Simple before advice that prints HelloWorld to the standard output.
*/
before( ) : callPointCut( )
{
System.out.println(
"Hello World");
System.out.println(
"In the advice attached to the call point cut");
}
}
MyAspect before( ) advice In the advice attached to the call point cut Inside foo (int, String)
MyAspect aspect in the project
from the build and therefore change the behavior of the application
as shown by the amended output on the Console.<?xml version="1.0" encoding="UTF-8"?>
<project basedir="." default="compile" name="test">
<property name="src" value="src"/>
<property name="build" value="build"/>
<taskdef resource="org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.
properties">
<classpath>
<pathelement location="%ASPECTJ_INSTALLATION%/lib/
aspectjtools.jar"/>
</classpath>
</taskdef>
<target name="compile">
<mkdir dir="${build}"/>
<iajc destdir="${build}" sourceroots="${src}">
<classpath>
<pathelement location="%ASPECTJ_INSTALLATION%/
lib/aspectjrt.jar"/>
</classpath>
</iajc>
</target>
</project>
iajc task that in turn relies upon the
aspectjrt.jar to executehttp://jakarta.apache.org/ant/manual/index.html.
ajc tool with the destination flag,
-d, set to the classes
directory:> ajc -classpath %MY_CLASSPATH% -d %PROJECT_ROOT_DIRECTORY%/deployment/classes com/oreilly/aspectjcookbook/MyClass.java com/oreilly/aspectjcookbook/HelloWorld. java
public
static
void
main(String[]) method
entry point for running Java applications.jar tool from the command line:jar -xvf aspectjrt.jar
ajc command or inside an Eclipse AspectJ project.
Under the webapps directory inside Apache
Tomcat, set up a new web application directory and WEB-INF
subdirectory. Make the appropriate amendments to the
server.xml file in the Tomcat configuration to
enable your web application.package com.oreilly.aspectjcookbook;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class AOHelloWorldServlet extends HttpServlet
{
public void doGet (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
ServletOutputStream out = response.getOutputStream( );
out.println("<h1>Hello World from an aspect-oriented Servlet!</h1>");
}
public String getServletInfo( )
{
return "Create a page that says <i>Hello World</i> and send it back";
}
}ajc command or inside
an Eclipse AspectJ project.ajc command-line tool.<html> <body bgcolor="white"> <h1> Request Information </h1> <font size="4"> JSP Request Method: <%= request.getMethod( ) %> <br> Request URI: <%= request.getRequestURI( ) %> <br> Request Protocol: <%= request.getProtocol( ) %> <br> Servlet path: <%= request.getServletPath( ) %> <br> </font> </body> </html>
ajc command line tool or in AspectJ
project in Eclipse. Copy your application's
.class files to the
%AXIS_INSTALLATION_IN_TOMCAT%/WEB-INF/classes
directory and the aspectjrt.jar to
%AXIS_INSTALLATION_IN_TOMCAT%/WEB-INF/lib.org.apache.axis.client.AdminClient command-line
tool to register your new web service with Apache Axis.package com.oreilly.aspectjcookbook;
public class MyWebService
{
public String echo(String message)
{
return message;
}
}
package com.oreilly.aspectjcookbook;
public aspect AddMessageHeaderAspect
{
public pointcut captureEcho(String message) :
execution(public void MyWebService.echo(String)) &&
args(message);
Object around(String message) : captureEcho(message)
{
return "Your original message was: " + message;
}
}before( ) form of
advice is used for most of these pointcut-based recipes that make up
the next eight chapters to avoid confusing things by using different
types of advice. Where it's unavoidable, other forms
of advice may have to be used, so it might be helpful to refer to
Chapter 9 to understand the implications that
the different forms of advice bring to the solutions provided.http://www.eclipse.org/aspectj
and then by clicking on Documentation
→ standard pointcut idioms.
These reusable pointcut definitions provide some great tools with
which to construct your own pointcut logic.call(Signature)
pointcut. The syntax of the call(Signature)
pointcut is:pointcut <pointcut name>(<any values to be picked up>) : call(<optional modifier> <return type> <class>.<method>(<paramater types>));
Signature parameter is important as it is used to
separate out the different components.