2016-06-24 4 views
0

私はParentInterfaceインターフェイスをあるメソッドシグネチャに親クラスがある場合、どのように@AspectJを特定のサブクラスにすることができますか?

public void accept(ParentInterface parent) 

あるメソッドシグネチャを持っていると言います。私は私のpointcutが、TestAクラスだけを対象とし、TestBクラスは対象としません。どちらもParentInterfaceを実装します。

現在、私は次のポイントカットがあります

@Pointcut("call(public void accept(package.ParentInterface))") 

をしかし、それはあまりにもTESTBインスタンスに取って受け入れインスタンスをキャッチします。これを修正する方法はありますか?

答えて

0

インタフェース+実装+ドライバアプリケーション:

package de.scrum_master.aspect; 

import org.aspectj.lang.JoinPoint; 
import org.aspectj.lang.annotation.Aspect; 
import org.aspectj.lang.annotation.Before; 
import org.aspectj.lang.annotation.Pointcut; 

import de.scrum_master.app.TestA; 

@Aspect 
public class MyAspect { 
    @Pointcut("call(public void accept(de.scrum_master.app.ParentInterface)) && args(argument)") 
    static void acceptCalls(TestA argument) {} 

    @Before("acceptCalls(argument)") 
    public void intercept(TestA argument, JoinPoint thisJoinPoint) { 
     System.out.println(thisJoinPoint + " -> " + argument); 
    } 
} 

package de.scrum_master.app; 

public interface ParentInterface {} 
package de.scrum_master.app; 

public class TestA implements ParentInterface {} 
package de.scrum_master.app; 

public class TestB implements ParentInterface {} 
package de.scrum_master.app; 

public class Application { 
    public void accept(ParentInterface parent) {} 

    public static void main(String[] args) { 
     Application application = new Application(); 
     application.accept(new TestA()); 
     application.accept(new TestB()); 
    } 
} 

アスペクトは、args() +ポイントカットメソッドシグネチャを介して引数の型をダウンピニング

コンソールログ:

call(void de.scrum_master.app.Application.accept(ParentInterface)) -> [email protected] 
関連する問題