跟我学aspectj之十 ----- Aspectj5支持Annotaion

谭高峯
2023-12-01


         自从JDK5.0加入了annotation以后,asepctj也提供对annotaion的支持,而且命名也模仿JDK,从1.4的版本改为5.0  也就是Aspectj5,或者称@Aspectj。其中最重要的一项就是pointcut  支持对Annotaion的选取了。


        不管你做没做过设计,你肯定用过自定义的Annotation(如果有不会的同学请Google)。为什么要自定义Annotaion勒?其实就是要给我们的某些类或者方法,加上一个标示,用于与其他的普通类或者方法进行区分,并且通过Annotaion参数值携带一定的附加信息。


        那么@Aspectj 对 Annotation的支持是什么用的勒?  让我们看以下官方给出的Demo。


一、类级别的Annotaion(假定我们定义了一个@Immutable的 类级别Annotation)。

(@Immutable *)

Matches any type with an @Immutable annotation.

(!@Immutable *)

Matches any type which does not have an @Immutable annotation.

(@Immutable (org.xyz.* || org.abc.*))

Matches any type in the org.xyz or org.abc packages with the@Immutable annotation.

((@Immutable Foo+) || Goo)

Matches a type Foo or any of its subtypes, which have the@Immutable annotation, or a type Goo.

((@(Immutable || NonPersistent) org.xyz..*)

Matches any type in a package beginning with the prefix org.xyz, which has either the @Immutable annotation or the @NonPersistent annotation.

(@Immutable @NonPersistent org.xyz..*)

Matches any type in a package beginning with the prefix org.xyz, which has both an @Immutable annotation and an @NonPersistent annotation.

(@(@Inherited *) org.xyz..*)

Matches any type in a package beginning with the prefix org.xyz, which has an inheritable annotation. The annotation pattern @(@Inherited *) matches any annotation of a type matching the type pattern@Inherited *, which in turn matches any type with the @Inherited annotation.


二、作用于Field的Annotaion。

  

@SensitiveData * *

Matches a field of any type and any name, that has an annotation of type@SensitiveData

@SensitiveData List org.xyz..*.*

Matches a member field of a type in a package with prefixorg.xzy, where the field is of type List, and has an annotation of type@SensitiveData

(@SensitiveData *) org.xyz..*.*

Matches a member field of a type in a package with prefixorg.xzy, where the field is of a type which has a @SensitiveData annotation.

@Foo (@Goo *) (@Hoo *).*

Matches a field with an annotation @Foo, of a type with an annotation@Goo, declared in a type with annotation @Hoo.

@Persisted @Classified * *

Matches a field with an annotation @Persisted and an annotation@Classified.



三、作用于Method的Annotaion


 

@Oneway * *(..)

Matches a method with any return type and any name, that has an annotation of type@Oneway.

@Transaction * (@Persistent org.xyz..*).*(..)

Matches a method with the @Transaction annotation, declared in a type with the@Persistent annotation, and in a package beginning with the org.xyz prefix.

* *.*(@Immutable *,..)

Matches any method taking at least one parameter, where the parameter type has an annotation@Immutable.


上面的英问说明很简单,我没有翻译,如果觉得有困难的同学可以用Google翻译以下大致的意思,基本也就明白了。上面的列表基本上列出了所有我们在实际应用中会用到的语法,而且有了前面的基础,这块的Demo,我也就不一一写了,感兴趣的同学可以自己写点Demo感觉一下。



             本章唯一想说的就是3个东西:        

@this(Foo)

匹配绑定了@Foo这个注解的任何对象

@args

     

/**
  	 * matches any join point with at least one argument, and where the
  	 * type of the first argument has the @Classified annotation
  	 */
  	pointcut classifiedArgument() : @args(Classified,..);
  	
  	/**
  	 * matches any join point with three arguments, where the third
  	 * argument has an annotation of type @Untrusted.
  	 */
  	pointcut untrustedData(Untrusted untrustedDataSource) : 
  	    @args(*,*,untrustedDataSource);


@target

     call(* *(..)) && @target(Classified)   匹配所有的方法,但是被匹配的方法所在的Class须有@Classfied注解

@withincode

@within(Foo)

Matches any join point where the executing code is defined within a type which has an annotation of typeFoo.

@annotaion(注解类型)

        匹配任何包含该注解的 Jointpoint

      使用以上语法,都必须要求你的注解的Policy是RUNTIME,否则会编译不通过。





参考资料以及Demo : http://www.eclipse.org/aspectj/doc/released/adk15notebook/annotations-pointcuts-and-advice.html




 类似资料: