2012-05-04 25 views
1

我有一個方法,接受一個字符串作爲輸入,我想有一個方面攔截方法的執行,並在一定的條件下,修改字符串,並讓連接點接收新的輸入,所以它不知道它已經被改變了。變換方法輸入與方面

這是可能的,還是因安全問題而無法完成?

回答

2

使用AspectJ,你可以這樣聲明around建議:

public aspect TransformationAspect { 
    pointcut transform(String s) : call(* yourMethodName(..)) && args(s); 

    Object around(String s): transform(s) { 
    // ... Transform String here 
    return proceed(s); 
    } 
} 

在你的代碼已經在編譯時或在使用AspectJ Java代理運行時要intrumented。

+0

就像一個魅力,謝謝! –

0

這應該是可能的,但取決於您使用的AOP技術。

使用EJB 3.x或CDI它可能是這樣的pseudcode:

Object intercept(InvocationContext context) { 

    Object[] params = context .getParameters(); 

    //modify the string and update the array 
    //the value of i depends on the method signature (the parameter position) 
    //and getting it is left for you as an excersise 
    params[i] = modify((String)params[i]); 

    context.setParameters(params); 
    return context.proceed(); 
}