aspectj 11 捕获基于对象类型的连接点

aspectj支持对连接点类型的判断,包括this(当前对象),可选目标引用target(被调用对象),以及指定参数类型

捕获何时this引用是一个特定的类型

即连接点处的this引用是给定的类型

this(Type or Id)
Picks out each join point where the currently executing object (the object bound to this) is an instance of Type, or of the type of the identifier Id (which must be bound in the enclosing advice or pointcut definition). Will not match any join points from static contexts.

后面例子针对this(Type)不包括this(Id)

this捕获当前对象是给定类型

demo在下面

捕获何时连接点的目标对象是特定的类型

target(Type or Id)
Picks out each join point where the target object (the object on which a call or field operation is applied to) is an instance of Type, or of the type of the identifier Id (which must be bound in the enclosing advice or pointcut definition). Will not match any calls, gets, or sets of static members.

this 和 target 区别(个人理解)
this是连接点处的当前对象(即调用的对象)
target是连接点处的目标对象(即被调用的对象)
比如连接点是A.func()中,里面调用B.func();
此时this就是A当前对象a,
target就是B当前对象b.

target捕获目标对象时特定类型

上面this和target的demo

package aspectj;

public class MyClass  {
    public void doSth(){
    }

    public static void main(String[] args) {
        MyClass myClass = new MyClass();
        myClass.doSth();
    }
}

package aspectj;

public class Main {
    public static void main(String[] args) {
        MyClass myClass = new MyClass();
        myClass.doSth();
    }
}

package aspectj;

public aspect HelloWorld {

    pointcut thisPoint(MyClass myClass): this(myClass);

    before(MyClass myClass) : thisPoint(myClass) {
        System.out.println(thisJoinPoint.getSignature());
        System.out.println(thisJoinPoint.getSourceLocation());
        System.out.println("thisPoint " + thisJoinPoint.getKind());
        System.out.println();
    }

    pointcut targetPoint(MyClass myClass): target(myClass);

    before(MyClass myClass) : targetPoint(myClass) {
        System.out.println(thisJoinPoint.getSignature());
        System.out.println(thisJoinPoint.getSourceLocation());
        System.out.println("targetPoint " + thisJoinPoint.getKind());
        System.out.println();
    }
}

输出

aspectj.MyClass()
MyClass.java:3
thisPoint initialization

aspectj.MyClass()
MyClass.java:3
targetPoint initialization

aspectj.MyClass()
MyClass.java:3
thisPoint constructor-execution

aspectj.MyClass()
MyClass.java:3
targetPoint constructor-execution

void aspectj.MyClass.doSth()
Main.java:6
targetPoint method-call

void aspectj.MyClass.doSth()
MyClass.java:4
thisPoint method-execution

void aspectj.MyClass.doSth()
MyClass.java:4
targetPoint method-execution

还是比较好理解的

捕获何时连接点的参数是某个数字,类型和次序

args(Type or Id, ...)
Picks out each join point where the arguments are instances of the appropriate type (or type of the identifier if using that form). A null argument is matched iff the static type of the argument (declared parameter type or field type) is the same as, or a subtype of, the specified args type.

args捕获参数是特定类型

demo

package aspectj;

public class Main {
    public static void main(String[] args) {
        MyClass myClass = new MyClass();
        test(myClass);
    }

    public static void test(MyClass myClass) {
        myClass.doSth();
    }
}

package aspectj;

public class MyClass  {
    public void doSth(){
    }
}

package aspectj;

public aspect HelloWorld {

    pointcut argPoint(MyClass myClass): args(myClass);

    before(MyClass myClass) : argPoint(myClass) && !within(HelloWorld){
        System.out.println(thisJoinPoint.getSignature());
        System.out.println(thisJoinPoint.getSourceLocation());
        System.out.println("argPoint " + thisJoinPoint.getKind());
        System.out.println();
    }
}

输出

void aspectj.Main.test(MyClass)
Main.java:6
argPoint method-call

void aspectj.Main.test(MyClass)
Main.java:9
argPoint method-execution

思考

区分this和target区别
参数为基本类型在前面讲过如args(int)还能获取传int参数值

refer

https://eclipse.org/aspectj/doc/released/progguide/semantics-pointcuts.html
《aspectj cookbook》

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的阅读 13,568评论 5 6
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,280评论 19 139
  • 说明 本节结合《aspectj cookbook》第4章进行学习,利用官网以及demo学习 连接点有多种,列举如下...
    赤子心_d709阅读 676评论 0 0
  • 目录链接 接连几天,夕雾的心不知道跑哪去了,她一直被那个和向远有关的梦扰的心思不定,她想见见他。 眼看夕雾已经辞职...
    紫蔷薇花阅读 372评论 4 6
  • 上午练大字,横写不好 晚上练横,看了一些网络视频,看评论写法上有争议,不敢学了。 极简中国书法史,看完了隶书,虽是...
    mw568阅读 388评论 0 0