为什么80%的码农都做不了架构师?>>>   

Annotation Patterns

For any kind of annotated element (type, method, constructor, package, etc.), an annotation pattern can be used to match against the set of annotations on the annotated element.An annotation pattern element has one of two basic forms:

  • @<qualified-name>, for example, @Foo, or @org.xyz.Foo.
  • @(<type-pattern>), for example, @(org.xyz..*), or @(Foo || Boo)

These simple elements may be negated using !, and combined by simple concatentation. The pattern @Foo @Boo matches an annotated element that has both an annotation of type Foo and an annotation of type Boo.

Some examples of annotation patterns follow:

@Immutable

Matches any annotated element which has an annotation of type Immutable.

!@Persistent

Matches any annotated element which does not have an annotation of type Persistent.

@Foo @Goo

Matches any annotated element which has both an annotation of type Foo and an annotation of type Goo.

@(Foo || Goo)

Matches any annotated element which has either an annotation of a type matching the type pattern (Foo || Goo). In other words, an annotated element with either an annotation of type Foo or an annotation of type Goo (or both). (The parenthesis are required in this example).

@(org.xyz..*)

Matches any annotated element which has either an annotation of a type matching the type pattern (org.xyz..*). In other words, an annotated element with an annotation that is declared in the org.xyz package or a sub-package. (The parenthesis are required in this example).

Type Patterns

AspectJ 1.5 extends type patterns to allow an optional AnnotationPattern prefix.

TypePattern := SimpleTypePattern |'!' TypePattern |'(' AnnotationPattern? TypePattern ')'TypePattern '&&' TypePattern |TypePattern '||' TypePattern SimpleTypePattern := DottedNamePattern '+'? '[]'*DottedNamePattern := FullyQualifiedName RestOfNamePattern? |'*' NotStarNamePattern?RestOfNamePattern := '..' DottedNamePattern |'*' NotStarNamePattern?NotStarNamePattern := FullyQualifiedName RestOfNamePattern? |'..' DottedNamePattern               FullyQualifiedName := JavaIdentifierCharacter+ ('.' JavaIdentifierCharacter+)*

Note that in most cases when annotations are used as part of a type pattern, the parenthesis are required (as in (@Foo Hello+)). In some cases (such as a type pattern used within a within or handler pointcut expression), the parenthesis are optional:

OptionalParensTypePattern := AnnotationPattern? TypePattern

The following examples illustrate the use of annotations in type patterns:

(@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.

Signature Patterns

Field Patterns

A FieldPattern can optionally specify an annotation-matching pattern as the first element:

FieldPattern := AnnotationPattern? FieldModifiersPattern? TypePattern (TypePattern DotOrDotDot)? SimpleNamePatternFieldModifiersPattern := '!'? FieldModifier FieldModifiersPattern*FieldModifier := 'public' | 'private' | 'protected' | 'static' | 'transient' | 'final' DotOrDotDot := '.' | '..'                                  SimpleNamePattern := JavaIdentifierChar+ ('*' SimpleNamePattern)?

If present, the AnnotationPattern restricts matches to fields with annotations that match the pattern. For example:

@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 prefix org.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 prefix org.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 and Constructor Patterns

A MethodPattern can optionally specify an annotation-matching pattern as the first element.

MethodPattern := AnnotationPattern? MethodModifiersPattern? TypePattern (TypePattern DotOrDotDot)? SimpleNamePattern '(' FormalsPattern ')'ThrowsPattern?MethodModifiersPattern := '!'? MethodModifier MethodModifiersPattern*MethodModifier := 'public' | 'private' | 'protected' | 'static' | 'synchronized' | 'final' FormalsPattern := '..' (',' FormalsPatternAfterDotDot)* |OptionalParensTypePattern (',' FormalsPattern)* |TypePattern '...'FormalsPatternAfterDotDot := OptionalParensTypePattern (',' FormalsPatternAfterDotDot)* |TypePattern '...'ThrowsPattern := 'throws' TypePatternListTypePatternList := TypePattern (',' TypePattern)*

A ConstructorPattern has the form

ConstructorPattern := AnnotationPattern? ConstructorModifiersPattern?  (TypePattern DotOrDotDot)? 'new' '(' FormalsPattern ')'ThrowsPattern?ConstructorModifiersPattern := '!'? ConstructorModifier ConstructorModifiersPattern*ConstructorModifier := 'public' | 'private' | 'protected'

The optional AnnotationPattern at the beginning of a method or constructor pattern restricts matches to methods/constructors with annotations that match the pattern. For example:

@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.

Example Pointcuts

within(@Secure *)

Matches any join point where the code executing is declared in a type with an @Secure annotation. The format of the within pointcut designator in AspectJ 5 is 'within' '(' OptionalParensTypePattern ')'.

staticinitialization(@Persistent *)

Matches the staticinitialization join point of any type with the @Persistent annotation. The format of the staticinitialization pointcut designator in AspectJ 5 is 'staticinitialization' '(' OptionalParensTypePattern ')'.

call(@Oneway * *(..))

Matches a call to a method with a @Oneway annotation.

execution(public (@Immutable *) org.xyz..*.*(..))

The execution of any public method in a package with prefix org.xyz, where the method returns an immutable result.

set(@Cachable * *)

Matches the set of any cachable field.

handler(!@Catastrophic *)

Matches the handler join point for the handling of any exception that is not Catastrophic. The format of the handler pointcut designator in AspectJ 5 is 'handler' '(' OptionalParensTypePattern ')'.

Runtime type matching and context exposure

AspectJ 5 supports a set of "@" pointcut designators which can be used both to match based on the presence of an annotation at runtime, and to expose the annotation value as context in a pointcut or advice definition. These designators are @args, @this, @target, @within, @withincode, and @annotation

It is a compilation error to attempt to match on an annotation type that does not have runtime retention using @this, @target or @args. It is a compilation error to attempt to use any of these designators to expose an annotation value that does not have runtime retention.

The this(), target(), and args() pointcut designators allow matching based on the runtime type of an object, as opposed to the statically declared type. In AspectJ 5, these designators are supplemented with three new designators : @this() (read, "this annotation"), @target(), and @args().

Like their counterparts, these pointcut designators can be used both for join point matching, and to expose context. The format of these new designators is:

AtThis := '@this' '(' AnnotationOrIdentifer ')'AtTarget := '@target' '(' AnnotationOrIdentifier ')'AnnotationOrIdentifier := FullyQualifiedName | IdentifierAtArgs := '@args' '(' AnnotationsOrIdentifiersPattern ')'AnnotationsOrIdentifiersPattern :='..' (',' AnnotationsOrIdentifiersPatternAfterDotDot)? |AnnotationOrIdentifier (',' AnnotationsOrIdentifiersPattern)* |'*' (',' AnnotationsOrIdentifiersPattern)*AnnotationsOrIdentifiersPatternAfterDotDot := AnnotationOrIdentifier (',' AnnotationsOrIdentifiersPatternAfterDotDot)* |'*' (',' AnnotationsOrIdentifiersPatternAfterDotDot)*

The forms of @this() and @target() that take a single annotation name are analogous to their counterparts that take a single type name. They match at join points where the object bound to this (or target, respectively) has an annotation of the specified type. For example:

@this(Foo)

Matches any join point where the object currently bound to 'this' has an annotation of type Foo.

call(* *(..)) && @target(Classified)

Matches a call to any object where the target of the call has a @Classified annotation.

Annotations can be exposed as context in the body of advice by using the forms of @this(), @target() and @args() that use bound variables in the place of annotation names. For example:

pointcut callToClassifiedObject(Classified classificationInfo) :call(* *(..)) && @target(classificationInfo);pointcut txRequiredMethod(Tx transactionAnnotation) :execution(* *(..)) && @this(transactionAnnotation) && if(transactionAnnotation.policy() == TxPolicy.REQUIRED);

The @args pointcut designator behaves as its args counterpart, matching join points based on number and position of arguments, and supporting the * wildcard and at most one .. wildcard. An annotation at a given position in an @args expression indicates that the runtime type of the argument in that position at a join point must have an annotation of the indicated type. For example:

/*** 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);

In addition to accessing annotation information at runtime through context binding, access to AnnotatedElement information is also available reflectively with the body of advice through the thisJoinPoint,thisJoinPointStaticPart, and thisEnclosingJoinPointStaticPart variables. To access annotations on the arguments, or object bound to this or target at a join point you can use the following code fragments:

Annotation[] thisAnnotations = thisJoinPoint.getThis().getClass().getAnnotations();Annotation[] targetAnnotations = thisJoinPoint.getTarget().getClass().getAnnotations();Annotation[] firstParamAnnotations = thisJoinPoint.getArgs()[0].getClass().getAnnotations();

The @within and @withincode pointcut designators match any join point where the executing code is defined within a type (@within), or a method/constructor (@withincode) that has an annotation of the specified type. The form of these designators is:

AtWithin := '@within' '(' AnnotationOrIdentifier ')'AtWithinCode := '@withincode' '(' AnnotationOrIdentifier ')'

Some examples of using these designators follow:

@within(Foo)

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

pointcut insideCriticalMethod(Critical c) : @withincode(c);

Matches any join point where the executing code is defined in a method or constructor which has an annotation of type @Critical, and exposes the value of the annotation in the parameter c.

The @annotation pointcut designator matches any join point where the subject of the join point has an annotation of the given type. Like the other @pcds, it can also be used for context exposure.

AtAnnotation := '@annotation' '(' AnnotationOrIdentifier ')'

The subject of a join point is defined in the table in chapter one of this guide.

Access to annotation information on members at a matched join point is also available through the getSignature method of the JoinPoint and JoinPoint.StaticPart interfaces. The Signature interfaces are extended with additional operations that provide access to the java.lang.reflect Method, Field and Constructor objects on which annnotations can be queried. The following fragment illustrates an example use of this interface to access annotation information.

Signature sig = thisJoinPointStaticPart.getSignature();AnnotatedElement declaringTypeAnnotationInfo = sig.getDeclaringType();if (sig instanceof MethodSignature) {// this must be a call or execution join pointMethod method = ((MethodSignature)sig).getMethod();}

Note again that it would be nicer to add the method getAnnotationInfo directly to MemberSignature, but this would once more couple the runtime library to Java 5.

The @this,@target and @args pointcut designators can only be used to match against annotations that have runtime retention. The @within, @withincode and @annotation pointcut designators can only be used to match against annotations that have at least class-file retention, and if used in the binding form the annotation must have runtime retention.

Package and Parameter Annotations

Matching on package and parameter annotations is not supported in AspectJ 1.5.0. Support for this capability may be considered in a future release.

Annotation Inheritance and pointcut matching

According to the Java 5 specification, non-type annotations are not inherited, and annotations on types are only inherited if they have the @Inherited meta-annotation. Given the following program:

class C1 {@SomeAnnotationpublic void aMethod() {...}}class C2 extends C1 {public void aMethod() {...}}class Main {public static void main(String[] args) {C1 c1 = new C1();C2 c2 = new C2();c1.aMethod();c2.aMethod();}}aspect X {pointcut annotatedC2MethodCall() : call(@SomeAnnotation * C2.aMethod());pointcut annotatedMethodCall() :call(@SomeAnnotation * aMethod());}

The pointcut annotatedC2MethodCall will not match anything since the definition of aMethod in C2 does not have the annotation.

The pointcut annotatedMethodCall matches c1.aMethod() but not c2.aMethod(). The call to c2.aMethod is not matched because join point matching for modifiers (the visibility modifiers, annotations, and throws clause) is based on the subject of the join point (the method actually being called).

Matching based on annotation values

The if pointcut designator can be used to write pointcuts that match based on the values annotation members. For example:

pointcut txRequiredMethod(Tx transactionAnnotation) :execution(* *(..)) && @this(transactionAnnotation) && if(transactionAnnotation.policy() == TxPolicy.REQUIRED);

转载于:https://my.oschina.net/yqz/blog/1604510

AspectJ Join Point Matching based on Annotations相关推荐

  1. VideoMatch: Matching based Video Object Segmentation

    Abstract: 1.视频目标分割是一个很重要的挑战,对于各种各样的视频分析任务.最近视频目标分割任务基于深度网络取得了state-of-the-art的结果. 2.由于作为预测任务的表述,这些方法 ...

  2. Optic Disc Detection using Template Matching based on Color Plane Histograms

    Optic Disc Detection using Template Matching based on Color Plane Histograms 论文翻译:基于彩色平面直方图的模板匹配的视盘检 ...

  3. Hierarchical line matching based on Line–Junction–Line structure

    一.简介 一般的线匹配的方法分为两种:基于个体的和基于小组的.简单来说就是一次匹配一个还是一次匹配一组. 对于基于个体的匹配方法,主要是光度信息,这种方法主要都是基于匹配的线段之间都会存在一定的重叠部 ...

  4. Spring AOP使用教程

    AOP 简介 AOP 思想是Spring的核心设计思想之一,通过基于切面的编程设计理念可以将业务逻辑与系统逻辑有效的分隔开来.使得系统的架构更加清晰,模块之间的界限也变的更加明确. AOP 全称为 A ...

  5. 框架:AspectJ

    AOP虽然是方法论,但就好像OOP中的Java一样,一些先行者也开发了一套语言来支持AOP.目前用得比较火的就是AspectJ了,它是一种几乎和Java完全一样的语言,而且完全兼容Java(Aspec ...

  6. 5、HIVE DML操作、load数据、update、Delete、Merge、where语句、基于分区的查询、HAVING子句、LIMIT子句、Group By语法、Hive 的Join操作等

    目录: 4.2.1 Load文件数据到表中 4.2.2查询的数据插入到表中 4.2.3将Hive查询的结果存到本地Linux的文件系统目录中 4.2.4通过SQL语句的方式插入数据 4.2.5 UPD ...

  7. 图匹配(Graph Matching)入门学习笔记——以《Factorized Graph Matching》为例(一)

    这篇文章本身是图匹配经典论文<Factorized Graph Matching>的阅读笔记,后来发现该文介绍并串联了许多图匹配相关的知识,甚至可以看作一个小小的综述性文章,因此就作为图匹 ...

  8. spring aop的@target与@within的真正区别到底是什么?

    文档里面是这么说的: @target: Limits matching to join points (the execution of methods when using Spring AOP) ...

  9. spring Transaction Management --官方

    原文链接:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/transaction.html 12.  ...

最新文章

  1. hive数据仓库摘录和总结
  2. html loading原理,加载HTML-Loading HTML
  3. 为不同的屏幕尺寸提供不同的图片(为那些没有必要下载全尺寸大图的设备节省带宽)...
  4. mysql什么是长事务
  5. Java List添加元素
  6. 图片异步加载框架 Android-Universal-Image-Loader
  7. promise的链式调用
  8. delphi相关文件扩展名
  9. java安全管理器视频_安全管理器 (Security Manager)
  10. vue项目接入高拍仪
  11. 动态效果html wow,bootstrap 动态效果WOW animate插件
  12. 小林相册批量下载器--相册图片万能批量下载器
  13. 太阳电池板特性实验_太阳电池伏安特性的测量
  14. RS485_Modbus通讯笔记
  15. mysql优化之query_cache_type的DEMAND参数介绍
  16. Percona Server 安装
  17. 微积分入门书籍(二)
  18. 搭建 rasa 框架中遇到的 domain.yml 无效问题
  19. 前端每日实战:97# 视频演示如何用纯 CSS 创作一组昂首阔步的圆点
  20. 使用软碟通Ultraiso制作PE/系统盘的U盘系统--两个分区,一个启动/系统盘分区,一个任意格式分区

热门文章

  1. Mybatis的ResultMap的使用
  2. hadoop下的Kmeans算法实现
  3. 那些计算机界的伟大女性
  4. 数字图像处理:第八章 形态学运算
  5. 编程之美-从无头单链表中删除节点方法整理
  6. 【题解】P4124 [CQOI2016]手机号码
  7. 对称加密实现重要日志上报Openresty接口服务
  8. nginx 如何处理请求系列1-Nginx安装
  9. (转载)Android进阶2之Activity之间数据交流(onActivityResult的用法)
  10. 用户注册,用邮箱来验证用户是否存在