倭マン's BLOG

主に Java, Groovy, Griffon 関連の基礎的な記事を書いてます。

「アスペクト・ライブラリを作る」への注意

前回までに、アスペクト・ライブラリの作成の仕方を見てきました。 今回は、アスペクト・ライブラリの作成に当たっての注意を1つ。

アスペクト・ライブラリの作成方法を書くに当たって参考にしたドキュメントは、AspectJ のコンパイル&織り込みを行うツール「ajc」のドキュメント()です。 そこでは既存のライブラリのソースコードからアスペクト・ライブラリを作成しています。 しかし、これは結構使い方が制限されてしまう方法です。 アスペクト・ライブラリを作成するために、既存のライブラリのソースコードをダウンロード等しなければいけませんし、そもそもソースコードが入手できるかどうかも分かりません。

一方、前回までに見てきた方法では、必要なのはバイトコードのみです。

で、注意事項はというと、上記2通りの方法で作成したアスペクト・ライブラリが同じではないということです。 それぞれのアスペクト・ライブラリがどのようなバイトコードからなっているのかまでは把握していませんが、バイトコードから作成したアスペクト・ライブラリでは、Generics の型パラメータの情報が失われています*1。 これは、仕様なのか、今後改善予定があるのかは不明。 そもそもバイトコードに Generics の型パラメータの情報が残っているのかも怪しい*2

*1:dom4j に Generics に対応したメソッド(List Node#selectElements(String xpath) のようなもの)を付け加えようとしたけど、全く役に立たず・・・

*2:一応、バイトコード上にそういう型情報が残されているようではありますが

アスペクト・ライブラリを作る 其ノ参 -- アスペクト・ライブラリを使用する

今回は、前回作成したアスペクト・ライブラリを使用してみましょう。

アスペクト・ライブラリを使用するプロジェクトは「greeting-app」とします。

pom.xml


とりあえず、サンプルを。

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.sample</groupId>
  <artifactId>greeting-app</artifactId>
  <packaging>jar</packaging>
  <version>0.1</version>

  <dependencies>
    <dependency>
      <groupId>org.sample</groupId>
      <artifactId>greeting</artifactId>
      <version>0.1</version>
    </dependency>

    <dependency>
      <groupId>org.sample</groupId>
      <artifactId>greeting-lib</artifactId>
      <version>0.1</version>
    </dependency>

    <dependency>
      <groupId>aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>1.5.2a</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
          
        <configuration>
          <weaveDependencies>
            <waveDependency>
              <groupId>org.sample</groupId>
              <artifactId>greeting</artifactId>
            </waveDependency>
          </weaveDependencies>

          <aspectLibraries>
            <aspectLibrary>
              <groupId>org.sample</groupId>
              <artifactId>greeting-lib</artifactId>
            </aspectLibrary>
          </aspectLibraries>
        </configuration>
          
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

幾つか注意を:

  • 「既存のプロジェクト」と「アスペクト・ライブラリのプロジェクト」への依存性を付加する。
  • aspectjrt への依存性を付加する(AspectJ プロジェクトに必須)。
  • aspectj-maven-plugin の <executions> 要素を設定する(AspectJ プロジェクトに必須)。
  • aspectj-maven-plugin の <weaveDependency>, <aspectLibrary> を設定する。

<weaveDependency> 要素には既存のライブラリを、また、<aspectLibrary> 要素には作成したアスペクト・ライブラリのプロジェクトを設定します*1

実行クラス ExampleMain と実行結果


プロジェクトに以下のような実行クラス

package greeting;

public class ExampleMain {
    
    public static void main(String[] args) {
        Person waman = new Person("Waman");
        waman.greet();
    }
}

を作成し、これを実行すると、以下のメッセージが表示されます:

Waman says : Hello, world !

*1:Eclipse 上で開発する場合には同様の設定が必要になります。 この設定はプロジェクトの「Properties」から行います。 詳しくはそのうちに。

Inpath と Aspect Path の追加

今回は、AspectJ を用いた開発で、他のライブラリを使用する際に必要となる、「Inpath」「Aspect Path」の追加方法を見ていきましょう。

Inpath

既存プロジェクトにアスペクトを織り込みたい場合は、既存プロジェクトを「Inpath」に設定します。 Maven2 で「weaveDependency」に設定してあるプロジェクトは、「Inpath」に設定する必要があります。

Aspect Path

既存のアスペクト・ライブラリを使用したい場合は、アスペクト・ライブラリを「Aspect Path」に設定します。 Maven2 で「aspectLibrary」に設定してあるプロジェクトは、「Aspect Path」に設定する必要があります。

設定方法


「Package Explorer」で AspectJ プロジェクトを選択して、ポップアップメニューから

[AspectJ Tools] → [Configure AspectJ Build Path...]

を選択して「Properties ダイアログ」を開き*1、以下のように設定していきます:

f:id:waman:20080228044228p:image

f:id:waman:20080228044226p:image

*1:ポップアップメニューから「Properties」を選択しても「Properties ダイアログ」が開きます。

アスペクト・ライブラリを作る 其ノ弐 -- 「アスペクト・ライブラリ」プロジェクト

今回は、前回のサンプル・ライブラリに対するアスペクト・ライブラリを作成しましょう。 プロジェクト名は「greeting-lib」とします。

pom.xml


とりあえずサンプルを。

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.sample</groupId>
  <artifactId>greeting-lib</artifactId>
  <packaging>jar</packaging>
  <version>0.1</version>

  <dependencies>
    <dependency>
      <groupId>org.sample</groupId>
      <artifactId>greeting</artifactId>
      <version>0.1</version>
    </dependency>

    <dependency>
      <groupId>aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>1.5.2a</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
          
        <configuration>
          <weaveDependencies>
            <waveDependency>
              <groupId>org.sample</groupId>
              <artifactId>greeting</artifactId>
            </waveDependency>
          </weaveDependencies>
        </configuration>
          
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

注意点をいくつか:

  • 拡張機能を付け加える対象となるプロジェクト(今の場合「greeting」プロジェクト)への依存性を付加します。
  • aspectjrt への依存性を付加します(AspectJ プロジェクトには必須)。
  • aspectj-maven-plugin の <executions> 要素を設定します(プロジェクトのビルド時に「織り込み」を行うために必要)。
  • aspectj-maven-plugin の <weaveDependency> 要素(configuation/weaveDependencies/weaveDependency 要素)を設定します。

最後の <weaveDependency> 要素の設定は、外部ライブラリにアスペクトを織り込むのに必要となります*1

AspectJ アスペクト:Narration


Narration アスペクトは Person#greet() メソッドが呼び出されたとき、それを誰が言ったのか付け加えるアスペクトです。 もう少し具体的に言えば、Person#greet() が呼び出されたとき、「Hello, world !」というメッセージの前に「《名前》+ "says : "」という文字列を付け加えます:

package greeting;

public aspect Narration{

    before(Person person):
            execution(* Person.greet()) && this(person){
        System.out.print(person.getName()+" says : ");
    }
}

ローカル・リポジトリへインストール


プロジェクトが完成したら、ローカル・リポジトリへインストールしましょう:

mvn clean install

*1:Eclipse 上で開発する場合には同様の設定が必要になります。 この設定はプロジェクトの「Properties」から行います。 詳しくはそのうちに。