if you don’t know what AOP (aspect oriented programming) is, you might want to stop reading at this point, or at least educate yourself.
jboss aop provides developers with two ways of intercepting a pointcut: Interceptors, and Aspects. the key difference is the way they are implemented by the developer.
an Interceptor is nothing more than an interface that must be implemented by the developer. any Interceptor implementing class can be supplied to a pointcut and the invoke method will be called at the time of execution of that pointcut. some of the reasons you might choose an Interceptor implementation:
- it is a guaranteed contract.
- if your interception routines are large in code size, it might be best to seperate them out into seperate classes.
- slightly better performance. jboss aop uses some extra reflection/code generation to wrap aspect execution.
an Aspect, on the other hand, is any random class that has *some* method that takes a variation of Invocation as an argument (just like invoke of the Interceptor interface does). this method is known as “advice” of your aspect. jboss aop needs to know the name of your advice (aka the method name) so it knows what to call at interception time. advice methods have a few key advantage:
- they can use more granular Invocation objects as their arguments (i.e, if you intercept a method you can use MethodInvocation which has more meta-information about the method you are intercepting)
- you are allowed as many advice methods per aspect as you want. this allows for grouping of interception into a single class.. making it an easy one stop.
- you can name your methods whatever you want.
now, here is my proposal for an addition to these 2 methods. we’ll call it “reflected advice”.
a lot of the time when I’m intercepting a method, or field, I need to have the arguments to the method, or a reference to the field so I can work with them in context. currently, this is possible.. but extremely tedious. let’s do an example:
Here’s my POJO I want to intercept:
public class POJO {
public void myMethod (String arg1, int arg2, float arg3)
{
//...
}
private int myField;
}
And, here’s the Aspect:
public class TestAspect {
/** Intercepts myMethod */
public Object myAdvice (MethodInvocation invocation)
throws Throwable
{
Object arguments[] = invocation.getArguments();
String arg1 = (String) arguments[0];
int arg2 = ((Integer) arguments[1]).intValue();
float arg3 = ((Float) arguments[2]).floatValue();
// interception logic...
return invocation.invokeNext();
}
/** Intercepts myField */
public Object myFieldAdvice (FieldInvocation invocation)
throws Throwable
{
Field field = invocation.getField();
Object fieldValue = field.get(invocation.getTargetObject());
int myField = ((Integer)fieldValue).intValue();
// interception logic...
return invocation.invokeNext();
}
}
in my opinion, it is extremely painful, and non elegant to have to pull these arguments from an Object array, or use reflection to get the field’s value. with reflected advice, you would be able to do something akin to:
public class TestAspect {
/** Intercepts myMethod */
public void myAdvice (String arg1, int arg2, float arg3)
{
// interception logic...
}
/** Intercepts myField */
public void myFieldAdvice (int myField)
{
// interception logic...
}
}
of course, this would only be useful if your pointcuts all deal with fields or methods that take the same arguments, or are of the same type. I can imagine this being the case often enough, though.. (intercepting struts actions for example). i’m going to run this idea by bill and try to commit it in the near future.. i think it’s worth exploring. especially since the aspectj language does things very similar to this..






1 Comment Received
Hi,

I found your blog via google by accident and have to admit that youve a really interesting blog
Just saved your feed in my reader, have a nice day
Leave A Reply