@634

AspectJ

Advertisement

AspectJとは

AspectJとは、Javaでアスペクト指向を実現するための言語である。

アスペクトの指定は、Javaプログラムとは別の場所で行うため、既存のJavaオブジェクトをそのまま利用することができる。
よって、新たなアスペクトの追加や定義済みアスペクトの削除などを容易に行うことが出来る。

入手

Officialサイトはhttp://eclipse.org/aspectj/
EclipseでAspectJを利用するためのAJDTプラグインはhttp://www.eclipse.org/ajdt/で入手可能。

簡単なサンプル

POJO
public class Users {
    public static void main(String[] args){
        new Users().printNames();
    }

    public void printNames(){
        System.out.println("my name is taro");
    }
}

aspect
import java.util.Date;

aspect NowDate {
    pointcut user(): call(void sample.Users.printNames());

    before(): user() {
        System.out.println(new Date());
    }
    after(): user() {
        System.out.println(new Date());
    }
}

結果
Mon Feb 21 0:00:00 JST 2005
my name is taro
Mon Feb 21 0:00:00 JST 2005

Aspectの構成

Pointcut

Joinpointの設定位置を定義する。

文法
pointcut [Joinpoint名]([引数リスト]): [Joinpoint];

Joinpoint

プログラム上の任意の位置をポイントとして定義する。
指定意味
callメソッドの呼び出し
executionメソッドの実行
getフィールドの読み出し
setフィールドへ書き込み
initializationインスタンス生成
handler例外ハンドラの実行
args引数
など。

Advise

どのような処理を行うか記述する
指定意味
before()前処理
after()後処理
before() returning正常復帰時 前処理
after() returning正常復帰時 後処理
before() throwing例外発生時 前処理
after() throwing例外発生時 後処理
declare warningコンパイル時に警告
declare errorコンパイル時に例外

Aspect

Advice,Joinpoint,Pointcutの関連付けを行うための単位。

import java.util.Date;

aspect NowDate {
    pointcut user(): call(void sample.Users.printNames());

    before(): user() {
        System.out.println(new Date());
    }
    after(): user() {
        System.out.println(new Date());
    }
}

動作確認サンプル

Test.java
public class Test {
    public static void main(String[] args) throws Exception{
        new Test().testMethod("test");
    }

    public void testMethod(String str) throws Exception{
        System.out.println(str);
        throw new Exception("exception");
    }
}

Test.aj
aspect Test {
    pointcut test():call(void logic.Test.testMethod(String));

    before(String str) : test() && args(str){
        System.out.println("before()");
        System.out.println("\tstr:" + str);
        System.out.println("\tthis.class:" + this.getClass());
    }

    after() : test(){
        System.out.println("after()");
    }

    before() returning : test(){
        System.out.println("before returning()");
    }

    after() returning : test(){
        System.out.println("after returning()");
    }

    before() throwing : test(){
        System.out.println("before() throwing");
    }

    after() throwing : test(){
        System.out.println("after() throwing");
    }
}

結果
before()
    str:TEST
    this.class:class Test
TEST
after()
before() throwing
after() throwing
java.lang.Exception: exception
    at Test.testMethod(Test.java:12)
    at Test.main(Test.java:5)
Exception in thread "main"

メモ

  • AspectJは独特な言語仕様になっているため、言語構造を覚える必要がある。
  • とっつきやすさとしてはSpringなどのAOPのほうが上(Javaで記述できるから。)

Advertisement

ショートカット

634
634ブログ
このカテゴリのトップページに戻る
Incubator(Pukiwiki)
634ラボ
   UIコレクションギャラリー
   ZO-3ジェネレーター

サイト検索


Y!ログール