简体   繁体   中英

AspectJ pointcut for constructor calls to different classes - identify type of object created

I am using AspectJ and I have defined a pointcut to capture the execution of the constructor methods for a couple of classes as follows:

pointcut newobject(): execution(class1.new(..) || class2.new(..) || class3.new(..));

then I have

after()returning():newobject(){ 

and here I would like to do different things depending on the object just being created being a class1, class2, or class3 if there a way I can refer to the type of the object at this point without having to split the pointcut?

Your pointcut syntax is wrong. You cannot chain multiple method or constructor patterns within one execution pointcut. Instead, you need to chain multiple execution pointcuts or, if possible, use jokers like MyClass* in order to catch multiple ones. A little example:

pointcut newobject(Object createdObject) :
    (execution(class1.new(..)) || execution(class2.new(..)) || execution(class3.new(..)))
        && this(createdObject);

after(Object createdObject) : newobject(createdObject) {
    System.out.println(thisJoinPoint);
    System.out.println(createdObject);
    System.out.println(createdObject.getClass());
}

After binding this to a variable name, you can do anything you like with it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM