annotation-style in AspectJ
AspectJでは、pointcutやadviceを記述する際にJavaを拡張した記法を用いますが、AspectJ5からはAnnotationを用いてpointcutを定義し、advice側でもAnnotationを用いてpointcutを指定することができます。具体的には、以下のようなコードになります。
@Aspect public abstract class ModelMetadataAspect { // Annotationでpointcutを定義する @Pointcut("call(net.wrap_trap.bitro.model.Model+.new())") public void metadataCollectPointcut(){}; // adviceではAnnotationでpointcut名を指定する @After("metadataCollectPointcut()") public void collectMetadata(ProceedingJoinPoint joinPoint){ ApplicationContainer container = ApplicationContainer.getContainer(); MetadataCollector collector = (MetadataCollector)container.getComponent(MetadataCollector.class, Scope.APPLICATION); collector.collectMetadata(joinPoint); } }
Annotationをpointcutとするようなアスペクトを、上記のような記法(annotation-style)で定義して動かしてきたのですが、一部のpointcut(またはそれを対象とするadvice)はうまく動かないことがあるようです。今回は上記コードのnew(コンストラクタに関するpointcut)に対するadviceが起動しません(AJDT2.0.1)。AJDTを使うと定義したpointcutにmatchする処理が1つもない場合はwarningが出るのですが、それも出ておらず、起動もしないという困った状況になりました。
通常のアスペクトの記法で書き直してみたところ、期待通りの挙動になりました。
public aspect ModelMetadataCollectAspect { pointcut modelMetadataCollectPointcut() : call(net.wrap_trap.bitro.model.Model+.new()); after() : modelMetadataCollectPointcut(){ ApplicationContainer container = ApplicationContainer.getContainer(); MetadataCollector collector = (MetadataCollector)container.getComponent(MetadataCollector.class, Scope.APPLICATION); collector.collectMetadata((ProceedingJoinPoint)thisJoinPoint); } }
そのうちAJDTのバージョンを上げて再度トライしてみます。