[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
Re: [aspectj-users] static cross cutting and method call introduction
|
How about the following aspect?
/* privileged */ aspect TestAspect {
pointcut contentHandler( ) :
execution(* org.xml.sax.ContentHandler.*(..));
Object around(ContentHandler ch): contentHandler() && this(ch) {
return proceed(ch.anotherContentHandler);
}
}
You will need to mark the aspect as privileged if
anotherContentHandler is not accessible from the aspect
(and there is no getter method to access that field, either).
-Ramnivas
===
Ramnivas Laddad,
Author, AspectJ in Action
http://www.manning.com/laddad
http://www.amazon.com/exec/obidos/ASIN/1930110936
Check out my aspect-oriented refactoring articles:
http://tinyurl.com/yqm96
http://tinyurl.com/288nn
--- Jakob Praher <jpraher@xxxxxxxx> wrote:
> hi all,
>
> today I looked at the execution( ) pointcuts and was asking myself,
> if
> it would be possible (with aspectj-1.2) to introduce a method
> redirection, without using java reflection.
>
> It would certainly work, since the type information is known at
> compile
> time.
>
> like for instance:
>
> aspect TestAspect {
>
> pointcut contentHandler( ): execution( *
> org.xml.sax.ContentHandler.*(..));
>
>
> around( ): contentHandler() {
>
> // assert : thisJoinPoint.getSignature( ).getKind( ) ==
> Signature.METHOD_EXECUTION;
>
> MethodSignature ms = (MethodSignature) thisJoinPoint(
> ).getSignature(
> );
>
> Field f = thisJoinPoint.getThis( ).getClass( ).getField(
> "anotherContentHandler" );
>
> ContentHandler anotherContentHandler = (ContentHanlder ) f.get(
> thisJoinPoint.getThis( ) );
>
> Method m = anotherContentHandler.getClass( ).getMethod( ms.getName(
> ),
> ms.getParameterTypes( ) );
>
> return m.invoke( anotherContentHandler, thisJoinPoint.getArgs( ) );
>
> } // end arround advice
>
> } // end aspect
>
> Here I have to use java.lang.reflect.Method to get to the target
> Method.
> But since you are doing bytecode manipulation anyway adding an entry
> to
> the constant pool and calling
>
> invokevirtual #newId
>
> would not be that hard, and would certainly be faster than using
> java.lang.reflect.Method.invoke.
>
> I don't know whether thats already possible or why it is not
> possible,
> but perhaps you can give me some info.
>
> And another question: Is it possible to circumvent the static
> redirection which apparently the static crosscutting introduces.
>
> I noticed (through javap) that the body of a static croscutted method
> is
> a static method implemented in the aspects class, and there is only a
> stub implementation on the class which got crosscutted.
>
> I would like to access for instance some fields of the class which
> are
> not public. (Ok putting both in the same package would reduce the
> access
> level to package private).
>
> thanks
> -- Jakob
>
>
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
> http://dev.eclipse.org/mailman/listinfo/aspectj-users
>