一.捕獲何時(shí)連接點(diǎn)上的運(yùn)行時(shí)條件評(píng)估為true
使用if(Expression)語(yǔ)句來(lái)評(píng)估包含要比較的運(yùn)行時(shí)變量的布爾表達(dá)式。if(Expression)語(yǔ)句的語(yǔ)法如下:
pointcut <pointcut name>(<any values to be picked up>) : if(<Boolean expression>);
if(Expression)語(yǔ)句有兩個(gè)關(guān)鍵特征:
1.if(Expression)切入點(diǎn)評(píng)估在運(yùn)行時(shí)提供的變量,得到關(guān)于連接點(diǎn)是否應(yīng)該觸發(fā)相應(yīng)通知的true或false結(jié)果。
2.Expression可以由多個(gè)邏輯元素組成,包括展示的連接點(diǎn)環(huán)境,靜態(tài)變以及其他切入點(diǎn)聲明。
package com.aspectj;


public aspect IfRecipe
{
// Define some variables for comparison
private static final long realisticSalary=300001;


/** *//**
* Specifies calling advice if this is referencing an object of class MyClass
* and the object has a realistic salary:
*/
pointcut ifJoinPointThishasRealisticSalaryPointcut():if(
(thisJoinPoint.getThis() instanceof MyClass)
&& ((MyClass) thisJoinPoint.getThis()).getSalary() < realisticSalary
&& ((MyClass) thisJoinPoint.getThis()).getSalary() > 0)
&& !withincode(* MyClass.get*())
&& !execution(* MyClass.get*());
//Advice declaration
//This advice will be executed before the pointcut that picks it

after() : ifJoinPointThishasRealisticSalaryPointcut() && !within(IfRecipe+)
{
System.out.println("---------- Aspect Advice Logic ----------");
System.out.println("In the advice picked by ifJoinPointThishasRealisticSalaryPointcut()");
System.out.println("Join Point Kind" + thisJoinPoint.getKind());
System.out.println("Executing object: " + thisJoinPoint.getThis());
System.out.println("MyClass instance: " + ((MyClass) thisJoinPoint.getThis()).getName() + ":" + ((MyClass) thisJoinPoint.getThis()).getSalary());
System.out.println("Signature: " + thisJoinPoint.getStaticPart().getSignature());
System.out.println("Source Line: " + thisJoinPoint.getStaticPart().getSourceLocation());
System.out.println("-----------------------------------------");
}
}

if(Expression)語(yǔ)句允許定義是否應(yīng)該把一份通知應(yīng)用于特定連接點(diǎn)的條件邏輯。這個(gè)條件邏輯在運(yùn)行時(shí)執(zhí)行,并且在到達(dá)連接點(diǎn)那一刻必須工作在有效的類型上。
例子中ifJoinPointThisHasRealisticSalaryPointcut()切入點(diǎn)中包含的條件邏輯指定在發(fā)生以下事件時(shí),應(yīng)該觸發(fā)相應(yīng)的after通知:
1.執(zhí)行對(duì)象是MyClass類型
2.對(duì)象的salary屬性小于realisticSalary常量
3.對(duì)象的salary屬性小于0
4.當(dāng)前連接點(diǎn)不在getSalary()方法內(nèi)。通過(guò)使用通配符,連接點(diǎn)絕對(duì)不能出現(xiàn)在開始到達(dá)的MyClass類的任何方法中。
可以使用AND(&&)運(yùn)算符來(lái)邏輯地結(jié)合每個(gè)條件。前3個(gè)條件可以相當(dāng)容易地理解成切入點(diǎn)布爾邏輯的一部分,但是最后一個(gè)條件更有趣一點(diǎn)。必須包含!withincode(* MyClass.get*())條件,以防止從通知內(nèi)調(diào)用getSalary(),這反過(guò)來(lái)又會(huì)觸發(fā)對(duì)通知的遞歸調(diào)用,并導(dǎo)致一個(gè)死循環(huán)。
二.使用邏輯AND(&&)結(jié)合切入點(diǎn)
使用&&運(yùn)算符。&& operator運(yùn)算符的語(yǔ)法如下:
pointcut <pointcut name>(<any values to be picked up>) : <pointcut declaation> && <pointcut declaration>
package com.aspectj;


public aspect LogicalAndRecipe
{

/** *//**
* Specifies calling advice whenever a method
* matching the following rules gets called:
*
* Class Name: MyClass
* Method Name:Any Method
* Method Return Type:Any Return Type
* Method Parameters:Any Parameters
*/
pointcut callAnyMethodOnMyClass():call(* MyClass.* (..));

/** *//**
* Specifies calling advice whenever a method
* matching the following rules gets called:
*
* Class Name: MyClass
* Method Name:bar
* Method Return Type:void
* Method Parameters:None
*/
pointcut callBarPointcut() : call(void MyClass.bar());

/** *//**
* Specifies calling advice whenever a join points is
* encountered that would be picked by both pointcuts
* specified:
*
* Pointcut Name: callAyMethodOnMyClass
* Pointcut Name:callBarPointcut
* Method Return Type:void
* Method Parameters:None
*/
pointcut callIntersectionAnyAndBar():callAnyMethodOnMyClass() && callBarPointcut();
//Advice declaration

before():callAnyMethodOnMyClass()&&!within(LogicalAndRecipe+)
{
System.out.println("---------- Aspect Advice Logic ----------");
System.out.println("In the advice picked by callAnyMethodOnMyClass()");
System.out.println("Signature: " + thisJoinPoint.getSignature());
System.out.println("Source Line: " + thisJoinPoint.getSourceLocation());
System.out.println("-----------------------------------------");
}
//Advice declaration

before():callBarPointcut()&&!within(LogicalAndRecipe+)
{
System.out.println("---------- Aspect Advice Logic ----------");
System.out.println("In the advice picked by callBarPointcut()");
System.out.println("Signature: " + thisJoinPoint.getSignature());
System.out.println("Source Line: " + thisJoinPoint.getSourceLocation());
System.out.println("-----------------------------------------");
}
//Advice declaration

before():callIntersectionAnyAndBar()&&!within(LogicalAndRecipe+)
{
System.out.println("---------- Aspect Advice Logic ----------");
System.out.println("In the advice picked by callIntersectionAnyAndBar()");
System.out.println("Signature: " + thisJoinPoint.getSignature());
System.out.println("Source Line: " + thisJoinPoint.getSourceLocation());
System.out.println("-----------------------------------------");
}
}

在AspectJ環(huán)境中,當(dāng)用&&運(yùn)算符吧兩個(gè)或多個(gè)簡(jiǎn)單的切入點(diǎn)結(jié)合進(jìn)一步復(fù)合切入點(diǎn)時(shí),被兩個(gè)獨(dú)立的切入點(diǎn)選擇的連接點(diǎn)將觸發(fā)復(fù)合切入點(diǎn)的關(guān)聯(lián)通知。更簡(jiǎn)單地說(shuō),如果任一個(gè)簡(jiǎn)單切入點(diǎn)選擇了任何一個(gè)連接點(diǎn),那么整個(gè)復(fù)合切入點(diǎn)就不會(huì)選擇這個(gè)連接點(diǎn)。
使用&&運(yùn)算符結(jié)合切入點(diǎn)的次序還會(huì)影響復(fù)合切入點(diǎn)的解釋方式。&&運(yùn)算符的運(yùn)行時(shí)分析是從左到右執(zhí)行的。這意味著在檢查候選連接點(diǎn)時(shí),指示它不包含連接點(diǎn)的第一個(gè)切入點(diǎn)就是停止比較的地方。這對(duì)于Java中的&&運(yùn)算符是成立的,并且當(dāng)復(fù)合中的比較之一必須通過(guò)前一個(gè)條件加以保護(hù)時(shí)特別有用。
三. 使用邏輯OR(||)結(jié)合切入點(diǎn)
使用||運(yùn)算符。|| operator運(yùn)算符的語(yǔ)法如下:
pointcut <pointcut name>(<any values to be picked up>) : <pointcut declaation> || <pointcut declaration>
package com.aspectj;


public aspect LogicalOrRecipe
{

/** *//**
* Specifies calling advice whenever a method
* matching the following rules gets called:
*
* Class Name: MyClass
* Method Name:foo
* Method Return Type:void
* Method Parameters:int and a String
*/
pointcut callFooIntStringPointcut():call(void MyClass.foo(int,String));

/** *//**
* Specifies calling advice whenever a method
* matching the following rules gets called:
*
* Class Name: MyClass
* Method Name:bar
* Method Return Type:void
* Method Parameters:None
*/
pointcut callBarPointcut() : call(void MyClass.bar());

/** *//**
* Specifies calling advice whenever a join points is
* encountered that would be picked by both pointcuts
* specified:
*
* Pointcut Name: callFooIntStringPointcut
* Pointcut Name:callBarPointcut
* Method Return Type:void
* Method Parameters:None
*/
pointcut callIntersectionFooOrBar():callFooIntStringPointcut() || callBarPointcut();
//Advice declaration

before():callFooIntStringPointcut()&&!within(LogicalOrRecipe+)
{
System.out.println("---------- Aspect Advice Logic ----------");
System.out.println("In the advice picked by callFooIntStringPointcut()");
System.out.println("Signature: " + thisJoinPoint.getSignature());
System.out.println("Source Line: " + thisJoinPoint.getSourceLocation());
System.out.println("-----------------------------------------");
}
//Advice declaration

before():callBarPointcut()&&!within(LogicalOrRecipe+)
{
System.out.println("---------- Aspect Advice Logic ----------");
System.out.println("In the advice picked by callBarPointcut()");
System.out.println("Signature: " + thisJoinPoint.getSignature());
System.out.println("Source Line: " + thisJoinPoint.getSourceLocation());
System.out.println("-----------------------------------------");
}
//Advice declaration

before():callIntersectionFooOrBar()&&!within(LogicalOrRecipe+)
{
System.out.println("---------- Aspect Advice Logic ----------");
System.out.println("In the advice picked by callIntersectionFooOrBar()");
System.out.println("Signature: " + thisJoinPoint.getSignature());
System.out.println("Source Line: " + thisJoinPoint.getSourceLocation());
System.out.println("-----------------------------------------");
}
}


在AspectJ語(yǔ)言中,如果連接點(diǎn)在使用邏輯OR結(jié)合進(jìn)入復(fù)合切入點(diǎn)的任何成分切入點(diǎn)上啟動(dòng)通知,連接點(diǎn)就會(huì)觸發(fā)那個(gè)通知。
AspectJ中的||運(yùn)算符還顯示了短路行為,這個(gè)&&運(yùn)算符相似且相反。
四.捕獲所有未通過(guò)切入點(diǎn)聲明指定的連接點(diǎn)
使用一元!運(yùn)算符,指定忽略那些通常由特定切入點(diǎn)捕獲的連接點(diǎn)。!運(yùn)算符的語(yǔ)法如下:
pointcut <pointcut name>(<any values to be picked up>): !<pointcut declaation>
package com.aspectj;


public aspect LogicalNotRecipe
{

/** *//**
* Specifies calling advice whenever a method
* matching NOT match the following rules:
*
* Class Name: MyClass
* Method Name:foo
* Method Return Type:void
* Method Parameters:int and a String
*/
pointcut notCallPointcutFooIntString() : !call(void MyClass.foo(int,String));
//Advice declaration

before():notCallPointcutFooIntString()&&!within(LogicalNotRecipe+)
{
System.out.println("---------- Aspect Advice Logic ----------");
System.out.println("In the advice picked by callFooIntStringPointcut()");
System.out.println("Signature: " + thisJoinPoint.getSignature());
System.out.println("Source Line: " + thisJoinPoint.getSourceLocation());
System.out.println("-----------------------------------------");
}
}

五.聲明匿名切入點(diǎn)
匿名切入點(diǎn)是切入點(diǎn)聲明的構(gòu)件。在所有基于切入點(diǎn)的章節(jié)中都會(huì)用它,但是我們?cè)谶@里更細(xì)致地關(guān)注匿名切入點(diǎn)。
package com.aspectj;


public aspect AnonymousPointcutRecipe
{

/** *//**
* A pointcut declaration that is built up from one
* anonymous pointcut:
*
* Anonymous Pointcuts:call(void MyClass.foo(int,String))
*/
pointcut singleAnonymousPointcut() : call(void MyClass.foo(int,String));

/** *//**
* A pointcut declaration that is built up from two
* anonymous pointcut:
*
* Anonymous Pointcuts:call(void MyClass.foo(int,String))
* call(void MyClass.foo(int,String))
* !within(AnonymousPointcutRecipe+)
*/
pointcut multipleAnonymousPointcut() : (
call(void MyClass.bar())
|| call(void MyClass.foo(int,String))
&& !within(AnonymousPointcutRecipe+));

/** *//**
* A pointcut declaration attached to the advice it will invoke,
* built up from anonymous pointcut:
*
* Anonymous Pointcuts:within(LogicalOrRecipe+)
*/
//Advice declaration

before():singleAnonymousPointcut()&&!within(LogicalNotRecipe+)
{
System.out.println("---------- Aspect Advice Logic ----------");
System.out.println("In the advice picked by singleanonymousPointcut and !within(AnonymousPointcutRecipe+)");
System.out.println("Signature: " + thisJoinPoint.getSignature());
System.out.println("Source Line: " + thisJoinPoint.getSourceLocation());
System.out.println("-----------------------------------------");
}

/** *//**
* A pointcut declaration attached to the advice it will invoke,
* built up from anonymous pointcut:
*
* Anonymous Pointcuts:None
*/
//Advice declaration

before():multipleAnonymousPointcut()
{
System.out.println("---------- Aspect Advice Logic ----------");
System.out.println("In the advice picked by multipleAnonymousPointcut()");
System.out.println("Signature: " + thisJoinPoint.getSignature());
System.out.println("Source Line: " + thisJoinPoint.getSourceLocation());
System.out.println("-----------------------------------------");
}
}

匿名切入點(diǎn)用于聲明切入點(diǎn),而不是檢查特定的切入點(diǎn)類型。可以在命名切入點(diǎn)聲明內(nèi)匿名地聲明切入點(diǎn),或者通過(guò)把切入點(diǎn)直接附加到它們將調(diào)用的通知上來(lái)進(jìn)行聲明。
六.重用切入點(diǎn)
聲明一個(gè)切入點(diǎn),可以在需要重用它的地方通過(guò)名稱來(lái)引用它。
package com.aspectj;


public aspect PointcutReuseRecipe
{

/** *//**
* A pointcut declaration that is to be used and reused:
* Anonymous Pointcuts: call(void MyClass.foo(int,String))
*/
pointcut foundationNamedPointcut():call(void MyClass.foo(int,String));

/** *//**
* A pointcut declaration that is built up from two
* pointcuts:
*
* Anonymous Pointcuts:!within(AnonymousPointcutRecipe+)
* Named Pointcuts:foundationNamedPointcut()
*/
pointcut reuseNamedPointcut():foundationNamedPointcut()&&!within(PointcutReuseRecipe+);

/** *//**
* A pointcut declaration attached to the advice it will invoke,
* built up from simple named and anonymous pointcuts:
* Anonymous Pointcut:!within(LogicOrRecipe+)
* Named Pointcuts:foundationNamedPointcut();
*/

before():foundationNamedPointcut()&&!within(PointcutReuseRecipe+)
{
System.out.println("---------- Aspect Advice Logic ----------");
System.out.println("In the advice picked by foundationNamedPointcut and !within(PointcutReuseRecipe+)");
System.out.println("Signature: " + thisJoinPoint.getSignature());
System.out.println("Source Line: " + thisJoinPoint.getSourceLocation());
System.out.println("-----------------------------------------");
}

/** *//**
* A pointcut declaration attached to the advice it will invoke,
* built up from complex pointcut built reusing other pointcut
* declarations:
*
* Named Pointcuts:reuseNamedPointcut();
*/

before():reuseNamedPointcut()
{
System.out.println("---------- Aspect Advice Logic ----------");
System.out.println("In the advice picked by reuseNamedPointcut()");
System.out.println("Signature: " + thisJoinPoint.getSignature());
System.out.println("Source Line: " + thisJoinPoint.getSourceLocation());
System.out.println("-----------------------------------------");
}
}
