AspectJ Inner-Class Join points

家住魔仙堡 提交于 2019-12-21 17:40:33

问题


I wonder is there a way to reach the code using aspect in "//do something" part?

Thanks in advance.

Turan.

public class Test {
    private class InnerTest {
        public InnerTest() {
            JButton j = new JButton("button");
            j.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    //do something  
                }
            });
        }
    }
}

回答1:


You can use the within or withincode pointcuts to match the containing class, and the cflow pointcut to match the execution of the addActionListener() method, then combine that with an execute pointcut to match the body of the actionPerformed() method.

For example this pointcut will match execution of the actionPerformed method only within the inner class InnerTest of the class Test (assuming the package is test) and only within the flow of execution of the addActionListener method:

pointcut innerTest(): within(test.Test.InnerTest) && 
    cflow(execution(public void javax.swing.JButton.addActionListener(java.awt.event.ActionListener))) && 
    execution(void actionPerformed(ActionEvent));

If you are only interested in matching calls to actionPerformed() within the inner class you can omit the cflow clause.

It's worth noting that if all you are interested in is matching the execution of any actionPerformed() method, this would suffice:

pointcut innerTest(): 
    execution(void java.awt.event.ActionListener+.actionPerformed(ActionEvent));


来源:https://stackoverflow.com/questions/1366360/aspectj-inner-class-join-points

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!