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

前端 未结 2 1586
梦谈多话
梦谈多话 2021-02-05 06:03

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 * c         


        
2条回答
  •  隐瞒了意图╮
    2021-02-05 06:12

    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();
    

提交回复
热议问题