Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AspectJ pointcuts - get a reference to the joinpoint class and name

Tags:

I am using the @AspectJ style for writing aspects, to handle logging in our application. Basically I have a pointcut set up like so:

@Pointcut("call(public * com.example..*(..))") public void logging() {} 

and then a before and after advice like so:

@Before("logging()") public void entering() {...} ... @After("logging()") public void exiting() {...} 

I want to create a log in these methods in the following format:

logger.trace("ENTERING/EXITING [" className + "." + methodName "()]"); 

The problem is I don't know how to get a reference to the class and method names. I have tried:

joinPoint.getThis().getClass() 

but this seems to return the caller's class name.

class A {     public void a() {         B.b();     } }   class B {     public void b() {         ...     } } 

would result in the following log

ENTERING [A.b()] 

can someone give some help on how to get the actual joinpoint class and method name

like image 234
smauel Avatar asked Feb 21 '11 17:02

smauel


People also ask

What is JoinPoint AspectJ?

JoinPoint is an AspectJ interface that provides reflective access to the state available at a given join point, like method parameters, return value, or thrown exception. It also provides all static information about the method itself.

Which type of advice allows to access the return value of a JoinPoint?

After (finally) advice: Advice to be executed regardless of the means by which a join point exits (normal or exceptional return). Around advice: Advice that surrounds a join point such as a method invocation. This is the most powerful kind of advice.

Is a method get executed when a specific JoinPoint with matching pointcut is reached in the application?

Advice: Advices are actions taken for a particular join point. In terms of programming, they are methods that get executed when a certain join point with matching pointcut is reached in the application.

How do you get the class name from ProceedingJoinPoint?

The ProceedingJoinPoint. getSignature() method returns everything you need to get the actual class name, method name, return type and parameters for the joinpoint.


1 Answers

You need to use joinPoint.getTarget().getClass(). Since you are using advising a call join point, the object of your interest is the target of the call.

Please note the API specification states:

Returns the target object. This will always be the same object as that matched by the target pointcut designator. Unless you specifically need this reflective access, you should use the target pointcut designator to get at this object for better static typing and performance.

Returns null when there is no target object.

Blindly using joinPoint.getTarget().getClass() can result in a NullPointerException. Consider using the join point's signature, such as:

final Signature signature = joinPoint.getSignature(); 

Then:

final Class clazz = signature.getDeclaringType(); 

Or if all you need is the class name:

final String clazz = signature.getDeclaringTypeName(); 
like image 113
ramnivas Avatar answered Sep 30 '22 03:09

ramnivas