overloaded-advices example step by step. This example comes bundled with JBoss AOP release, so all you need to do is to download the 2.0.0.GA release and start following the next steps.overloaded-advices tutorial example using Eclipse (the URL location is jboss-aop-2.0.0.GA/docs/aspect-framework/examples/overloaded-advices) and solve classpath issues by adding the JBoss AOP jars to the classpath (just include the jars of the jboss-aop-2.0.0.GA/lib directory).Driver class, line 29, where there is a call to POJO constructor, the first joinpoint of the example that will be intercepted: 
Driver class, setting the AOP arguments as has been showed in the first part of this tutorial.F5 to enter the constructor execution joinpoint. This is what you are going to see:
POJO class: POJO.POJO_new_$aop(), followed by the wrapper that is our focus of interest, the one belonging to the Advisor class: POJO$POJOAdvisor.POJOAdvisor_new_$aop().ConstructorInfo method that returns some a sort of concurrency lock. So, press F7 to return to the Advisor wrapper method. Once you do this, you should see the stack trace below:
Advisor wrapper method, you should press F5. This is what we get once we press F5:
F7, return to the wrapper method, and press F5 on this wrapper until you enter a new block of code. Continue this process until you have finally entered the Joinpoint class.


ConstructorInfo.hasAdvices()) will appear twice in a row.F7, Eclipse finally enters the first advice execution:
F6 until we return to the Joinpoint class:
JoinPoint class. Press F5 until you find the next advice. Before getting there, you are going to run into another couple of “noise” elements:
F7. Right below it in the stack, there are three calls to constructors (<init> methods). Again, we have mentioned previously that calls to constructors should be skipped with F7. Skipping them and then pressing F5 will take you to the next advice:
F6. There is one line, however, that you must treat differently and press F5: line 39 of JoinPointAspect, containing the statement return invocation.invokeNext();POJO constructor execution, you must enter the invocation.invokeNext() call. After pressing F5 in this line, you will see the following stack:
F6 to return to the previous item in the stack:
Joinpoint class. Now, you should press F5 again following the tips given in step 5. Doing so will take us to the last advice:
F6 to return to the Joinpoint class:
F5 now does not take us to the next advice,as all advice have already run. It takes us back to the advisor wrapper method.
F7 twice to exit both wrappers and go back to the driver class. Now you can continue debugging. Try to enter the next joinpoints of the overloaded-advices example to see the other advice running! Remember that you must press F5 on field read and field write joinpoints, in order to enter their wrapper method code.overloaded-advices example, you are ready to debug any other JBoss AOP application you like. Try doing so using a different JBoss AOP tutorial, or just try debugging your own JBoss AOP application.Aophelper is a small swing application that provide all the dependencies needed to run JBoss AOP and has features to add options to JBoss AOP. Aophelper has two modes; compile mode and run mode (use the File menu to change between the modes). They are nearly identical (visually), but the do different things. Here is a view of the compile mode:

- a larger picture can be found here.
The compile mode will try to aop compile classes specified by a path. If additional jars are needed, they can be added to the classpath. Then finally we need to specify a jboss aop xml file. After this is we can press compile and all the classfiles that where specified by the working directory option will be aop compiled.The run mode will try to run either a aop precompiled program, or it will run the program with loadtime weaving (aop is weaved into the class when the program starts, no changes are done to the .class files). Here is an example of the run view:

- a larger picture can be found here.
The only difference between run and compile mode is the input field were you add the main execution class and the checkoption to choose if you want to enable runtime weaving. Aophelper can also save your settings so its very easy to get back on track with an application you've used before.
Even though Aophelper support most of the basic aop functionality, Aophelper is currently in a beta stage where much will change and additional features will be added. Some points we have on the todo list:
Aophandler is bundled in the JBoss AOP distro. Go into the aophelper directory and run it by executing either aophelper.sh on linux/mac/etc or aophelper.bat on windows.
You can always use the latest version from svn. Download JBoss AOP and run: mvn install -Prun-aophelper inside the aophelper directory and you can check out the latest changes.
F5/F6/F7 until you enter your advice or joinpoint code.Advisor -> JoinPoint -> advice + joinpointJoinPoint class. And the JoinPoint class is the one that is actually going to invoke your advice and your joinpoint, the points that you want to be able of debugging.Advisor is responsible for managing interception of your classes. There is an Advisor per intercepted class, which manages all the advice stacks related to that specific class. Similarly, there is an Advisor per instance and this advisor is going to appear at your execution stack only when there is specific configuration per instance, which is rare. From version 2.0 on, the default weaving mode is the generated-advisor mode. From this point on, we will assume you are using this mode. In this mode, advisors are also responsible for generating code that will invoke your advice and joinpoint, and for invoking this code as well.
It is very easy to identify an advisor on the debugging stack trace. The advisor of class POJO is called POJOAdvisor, and is an internal class of POJO. In this case, you are going to see POJO$POJOAdvisor as the name of the advisor class in the stack trace.
Wrapper Methods
Every time a joinpoint is weaved by JBoss AOP, this joinpoint execution is replaced by a call to a wrapper method. These methods know what to do to trigger execution of your advice and the intercepted joinpoint.
The signature of a wrapper methods varies according to the type of joinpoint you are intercepting. For example, the wrapper of a method called public void run(Withdrawal) would have the signature:
public void run_N_8003352271541955702(Withdrawal)
Once you start debugging, you
will get used to the signatures of wrapper methods and will start recognizing them right away. For now, there is a simple rule you can use to identify those. They are part of generated advisors and are the first extra item that will appear on the stack trace when you are debugging an intercepted joinpoint.
That said, if the method run of the previous example belongs to a class named Withdrawal, you are going to see something like this:

Note that for some types of joinpoints, such as field read and constructor execution, you are going to see an extra wrapper method at the stack. This extra wrapper has the same signature as the wrapper belonging to the Advisor class, but it will instead belong to the class that contains the joinpoint being execute
d:
In this picture, we see that there are two wrappers: intField_w_$aop(Object, int), belonging to the POJO class, and POJO$POJOAdvisor$intField_w_$aop(Object, int), belonging to to the POJOAdvisor class.
Notice that those method names do not indicate a exotic taste, as they are made this way so we can avoid crashing with any name of other classes/fields/methods you may have included in your application. Plus, those method names make it easier to identify code generated inside of JBoss AOP.
JoinPoint classes
A JoinPoint class is generated for each joinpoint of your system. Their names always start with "Joinpoint", followed by something that identifies the joinpoint they represent, like the example below:
JoinPoint_run_N_80033522715419555702_2
JoinPoint is your main class of interest here. Despite the fact that you ca not see its code or the lines being executed when debugging, this class is responsible for calling your aspects and invoking the joinpoint they represent as well. So, if you have advice A and B intercepting execution of, say, void myMethod(), it is the JoinPoint class that will invoke A, B and myMethod as well.
The entry point for the JoinPoint class is the invokeJoinPoint method:

When debugging this method, and the inner methods it calls, you want to use F5 so you can enter your advice and joinpoint codes in order to debug them.
The elements presented in the previous section are not the only ones you are going to face when debugging your weaved joinpoint. Other calls will appear in the stack. While the elements you are going to see are important for the inner working of JBoss AOP applications, they are not your focus of interest when debugging.
So, the following elements of the stack trace are not going to take you to the execution of advice or the weaved joinpoint, and that is why we say they are “noise over the line”:
JoinPoint class will enter an info class to query that type of information. The main info classes are: FieldInfo, MethodInfo, ConstructorInfo, MethodByMethodInfo, ConByMethodInfo, ConByConInfo, and MethodByConInfo.


JoinPoint class we mentioned in a previous section is generated at the first time a specific joinpoint is reached:
All the points contained in the previous list should be skipped. So, whenever you see those, just press F7 to return to one of the points mentioned in the “Which classes call advice and trigger intercepted joinpoints?” section.
Once you are more familiar with the elements presented in the previous sections, you are
ready to start.
Before continuing, lets recall what are the keys you can use during Eclipse debugging and what they represent:
F5: Step into
F6: Step over
F7: Step return
These are the steps you must follow when a weaved joinpoint is reached:
The joinpoint is reached. The first thing you will notice is that, despite you can see the joinpoint code in your application, Eclipse will flag that it cannot find the line correspondent to the joinpoint. This is expected and indicates that your joinpoint has been weaved. Press F5 to enter the wrapper method.
Now you can see the wrapper method of the Advisor class in the execution stack. This is the method that is going to invoke the JoinPoint class, so press F5 again.
This is the tricky part. Besides invoking the JoinPoint class, the wrapper method performs a couple of other actions: enabling concurrent lock and invoking code to generate the joinpoint class are a few of those. Those actions are what we called “noise over the line”. Whenever you enter a block that is not part of the JoinPoint class, press F7 to return to the wrapper method.
When you finally get into the JoinPoint class, press F5 constantly, until you enter your advice.
If , before getting to your advice or joinpoint, you run into constructors ( elements in the stack), info methods, or any call to a JDK class, you should press F7 to return to the Joinpoint class;
When you enter an advice, Eclipse will start recognizing the line numbers again and you will be able of debugging your advice just as any normal method of your application.
The same holds for your joinpoint. If your joinpoint is a method or constructor, you are going to be able of entering those and debugging them normally.
Once all advice and the joinpoint have been executed, the JoinPoint class is done with its work and execution will return to the wrapper method. When this happens, press F7 until you return to your normal application code.
TIP: When debugging around advice, remember of pressing F5 on the call to Invocation.invokeNext(). Or you won't be able of seeing the execution of other around advice, nor the execution of the joinpoint being intercepted.
In the next part of our tutorial, we are going to illustrate the steps above, walking you through a debugging process over Eclipse. See you there!


javaagent argument and define the system property “jboss.aop.path” as in the example:
-javaagent:/home/fla/Development/workspace/jboss-aop/aop/output/lib/jboss-aop-jdk50.jar
-Djboss.aop.path=/home/fla/Development/workspace/MyApp/src/resources/jboss-aop.xml

AdviceFactory
.org.jboss.aop.advice
package:AspectFactory
: responsible for creating an instance of an aspect. While you can provide your own implementation of this interface, the most commonly used implementation is the GenericAspectFactory
, that creates aspect instances given the class of the aspect to be created.AspectDefinition
: this is how aspect factories are managed internally. Whenever you declare an aspect, JBoss AOP creates an AspectDefinition
. This definition contains the aspect factory, the aspect scope, and the aspect name.InterceptorFactory
: responsible for creating instances of interceptors. To represent a simple interceptor, the GenericInterceptorFactory
is used; the AdviceFactory
represents advices.// aspect class
public class MyAspect
{
public void helloWorld()
{
System.out.println(“Hello world!”);
}
public Object helloWorld2(Invocation invocation) throws Throwable
{
System.out.println(“Hello world!”);
return invocation.invokeNext();
}
}
-----
public static void main(String [] args)
{
AspectFactory aspectFactory = new GenericAspectFactory(MyAspect.class, null);
AspectDefinition aspectDefinition = new AspectDefinition(“myAspect”, Scope.PER_INSTANCE, aspectFactory);
AdviceFactory adviceFactory = new AdviceFactory(aspectDefinition, “helloWorld”, AdviceType.BEFORE);
...
}
MyAspect
class, with two advices: helloWorld
and helloWorld2
. In the second part, we represent the aspect MyAspect
and the advice helloWorld
using the classes we have just learned. First, an aspect factory is created. The constructor of GenericAspectFactory
must receive the class of the aspect, and an xml element. This second parameter allows to pass extra configuration to the aspect class. Since we are not loading anything from an xml file, the second parameter must be null
.// declare the helloWorld2 advice as being of the default advice type, which is around
new AdviceFactory(aspectDefinition, “helloWorld2”);
org.jboss.aop.advice.AdviceBinding
. This class contains a pointcut expression and a series of interceptor factories. Optionally, it can also contain a cflow expression.AdviceBinding(String pointcutExpression, String cflow)
null
. While there is a second constructor provided by AdviceBinding
. It is for internal use only, so always use the one above.addInterceptor(Class clazz)
addInterceptorFactory(InterceptorFactory interceptorFactory)
AdviceFactory
instances (as we have seen above, this class is an implementation of InterceptorFactory
, and creates interceptor adapters to call your advice).org.jboss.aop.AspectManager
class. This class is our facade and provides all sort of AOP functionalities. Among them, you can find the dynamic AOP methods:addAspectDefinition(AspectDefinition definition)
: deploys an aspectaddAdviceBinding(AdviceBinding binding)
: deploys a bindingremoveAdviceBinding(String bindingName)
: removes a bindingremoveAdviceBindings(Collection bindings)
: removes a collection of bindings.org.jboss.aop.Advised
interface. Through this interface, you can access the class Advisor
, a sort of AspectManager with scope limited to that single class, and the InstanceAdvisor
, with an even more specific scope, a single instance.Advised
interface:Advisor _getAdvisor()
InstanceAdvisor _getInstanceAdvisor()
insertInterceptor(Interceptor)
appendInterceptor(Interceptor)
Adds the interceptor to the end of the instance's chainsremoveInterceptor(String)
Removes an interceptor that has been inserted or appended. The parameter specifies the name of the interceptor to be removed.
AdviceBinding binding = new AdviceBinding(“execution(public !static void Pojo->*(int, long))”, null);
binding.addInterceptor(MyInterceptor.class);
AspectManager.getInstance().addAdviceBinding(binding);
<aop>
<binding pointcut="execution(public !static void Pojo->*(int, long))">
<interceptor class="MyInterceptor.class/"/>
</binding>
</aop>
Now, an example with advices: // declares the aspect class
AspectFactory aspectFactory = new GenericAspectFactory(MyAspect.class, null);
AspectDefinition aspectDefinition = new AspectDefinition(“myAspect”, Scope.PER_INSTANCE, aspectFactory);
AspectManager manager = AspectManager.instance();
// deploys the aspect
manager.addAspectDefinition(aspectDefinition);
// creates the advice factory
AdviceFactory adviceFactory = new AdviceFactory(aspectDefinition, “helloWorld”, AdviceType.BEFORE);
// creates the binding
AdviceBinding binding = new AdviceBinding(“execution(public !static void Pojo->*(int, long))”, null);
binding.addInterceptorFactory(adviceFactory);
// adds the binding
manager.addAdviceBinding(binding);
AspectManager.addAdviceBinding
method. It is the API counterpart of the xml below:<aop>
<aspect name="”myAspect”" class="”MyAspect”/">
<binding pointcut="”execution(public !static void Pojo->*(int, long))”>
<before aspect="”myAspect”" name="”helloWorld”/">
</binding>
</aop>
removeAdviceBinding
method:AdviceBinding.addInterceptor
and AdviceBinding.addInterceptorFactory
methods can be called only before you deploy your binding. Doing so after that will cause inconsistencies.Pojo
: // create Pojo
Pojo pojo = new Pojo();
// retrieve its instance advisor
InstanceAdvisor instanceAdvisor = ((Advised) pojo)._getInstanceAdvisor();
// append an interceptor to the chains of this instance only
instanceAdvisor.append(new MyInterceptor());
// executing someMethod will trigger MyInterceptor
pojo.someMethod();
Pojo
, this instance will not be intercepted by MyInterceptor, as the interceptor has been added to the InstanceAdvisor of pojo: Pojo otherPojo = new Pojo();
pojo.someMethod(); // will not be intercepted by MyInterceptor
<aop>
<prepare expr="all(Pojo)"/>
</aop>
@Aspect(scope = Scope.PER_VM)
public class MyAspect
{
@Prepare ("all(Pojo)")
public static Pointcut preparePOJO;
@PointcutDef("execution(Pojo->new())")
public static Pointcut aPointcut;
}
@Prepare
annotation, and of a pointcut expression, with the @PointcutDef
expression. Except for the annotation used, their syntax is the same.@Prepare
can also be used to annotate plain-old Java classes:@Prepare
public class Pojo
{
...
}
.aop
suffix. By deploying such an unit at runtime, the aspects contained in it will be automatically deployed to all prepared joinpoints of the previously deployed applications. If you are running the server with load time weaving, all the applications deployed after this moment will also be instrumented by JBoss AOP to insert the aspects contained in the deployment unit, according to the bindings and pointcuts it contains..aop
file is removed from the deploy
dir, the bindings it contains will be automatically removed from the running applications..aop
units, you can use other deployment units and xml files to hot deploy aspects to the server. In this case, the aspect classes are bundled into a non-aop deployment unit (such as a .jar
or an .ear
) and deployed to the server. The binding declarations are deployed to the server separately in an xml file whose name must end with -aop.xml
. The effect of this two-part deployment is the same as you get by deploying an .aop
file, but with one advantage. This type of deployment allows you to redeploy only the *-aop.xml
file, without causing the aspect classes to be reloaded. The simple removal of your *-aop.xml
file will cause the bindings to be undeployed. And the addition of a new *-aop.xml
file will cause the bindings declared in it to be deployed to the server..aop
files.-aop.xml
, containing the declarations of bindings and pointcut expressions.injboss
example, available as part of the JBoss AOP documentation. It shows the different ways of packaging aspectized applications to be run in JBoss AS. Try running those examples and playing with dynamic AOP to see it in action! (For example, run $ ant deploy-basic-lt-war
and try removing the jboss-aop.xml
file from your server deploy
dir in order to have the aspects undeployed)