Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] static cross cutting and method call introduction

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




Back to the top