AspectJ内部类连接点 [英] AspectJ Inner-Class Join points

查看:105
本文介绍了AspectJ内部类连接点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道有没有办法使用"//做某事"部分中的方面来访问代码?

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

谢谢.

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

推荐答案

您可以使用 within withincode 切入点来匹配包含的类和 cflow 切入点以匹配addActionListener()方法的执行,然后将其与 execute 切入点结合以匹配actionPerformed()方法的主体.

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.

例如,此切入点将仅在Test类的内部类InnerTest(假定包为test)中匹配actionPerformed方法的执行,并且仅在addActionListener的执行流程中匹配方法:

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

如果仅对匹配内部类中对actionPerformed()的调用感兴趣,则可以省略cflow子句.

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

值得注意的是,如果您感兴趣的是与任何actionPerformed()方法的执行匹配,就足够了:

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

这篇关于AspectJ内部类连接点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆