0

我试图在下面的示例代码中执行 readObject() 后从建议 readObject() 连接点的 after 建议中获取读取字符串的值。

public class Eg {

    private ObjectOutputStream output;
    private ObjectInputStream input;
    Public String request="";

    public Eg(File source){
            output; // output stream to client
            input= new ObjectInputStream( file ); 
        }

    public void method() {
            try{            
                while(true) {
                    request =(String) input.readObject();
                    //do something with request
                }//while
            }
            catch (IOException ex) 
            {
                ex.printStackTrace();
            } 
    }

}

和我的方面:

public aspect ReadStringGetter {
    public pointcut readOperation() :
      execution(* java.io.ObjectInputStream.readObject());

    after() : readOperation() {
        Eg eg = (Eg) thisJoinPoint.getThis();
        System.out.println(eg.request);
    }
}
4

1 回答 1

0

您可以使用this切入点来获取连接点的上下文(的实例Eg):

class Eg {
    public String request = "";
    public void method() {
        request = "xy";
        System.out.println(request);
    }
}

呼叫者:

Eg c = new Eg();
c.method();

方面:

public aspect MyAspect {

    pointcut EgMethodsExecution(Eg eg) : 
        execution(* Eg.method(..)) && this(eg);

    after(Eg eg) : EgMethodsExecution(eg) {
        System.out.println("~ in aspect ~ " + eg.request);
    }

}

输出:

xy
~ 在方面 ~ xy

于 2013-10-29T22:19:24.650 回答