views:

215

answers:

1

Hi everyone,

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  A: 

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));
Rich Seller

related questions