说明
本节结合《aspectj cookbook》第4章进行学习,利用官网以及demo学习
连接点有多种,列举如下
http://www.eclipse.org/aspectj/doc/released/progguide/quick.html#quick-pointcuts
这里以方法为切点
捕获方法的调用
形式:call(MethodPattern)
定义:When a method is called, not including super calls of non-static methods.
作用:在方法调用时触发,环境是调用类
MethodPattern定义见后面备注,示例如下
demo
MyClass.java
package aspectj;
public class MyClass {
public void foo() {
System.out.println("foo");
}
public static void main(String[] args) {
MyClass myClass = new MyClass();
myClass.foo();
}
}
HelloWorld.aj
package aspectj;
public aspect HelloWorld {
pointcut callPointcut():call(void aspectj.MyClass.foo());
before(): callPointcut()
{
// System.out.println("Entering: " + thisJoinPoint.getSourceLocation());
System.out.println("hello");
}
}
结果
hello
foo
说明:call调用依赖于方法目标的动静态类型,这里不展开
捕获方法调用上传递的参数值
结合args完成绑定
形式:args(Type or Id, ...)
定义: Picks out each join point where the arguments are instances of the appropriate type (or type of the identifier if using that form). A null argument is matched iff the static type of the argument (declared parameter type or field type) is the same as, or a subtype of, the specified args type.
作用: 从join point获取参数,提供给pointcut
demo
package aspectj;
public aspect HelloWorld {
//pointcut定义参数类型以及名字,args定义参数名,和pointcut参数名匹配
pointcut callPointcut(int x):call(void aspectj.MyClass.foo(int)) && args(x);
//advice需要定义参数类型以及参数名,和后面的pointcut的参数名匹配
before(int y): callPointcut(y)
{
System.out.println("hello " + y);
}
}
package aspectj;
public class MyClass {
public void foo(int x) {
System.out.println("foo " + x);
}
public static void main(String[] args) {
MyClass myClass = new MyClass();
myClass.foo(3);
}
}
结果
hello 3
foo 3
捕获方法调用的目标
也就是说捕获是哪个对象(实例,instance)调用了作为join point的方法
结合tartge完成绑定
形式:target(Type or Id, ...)
定义: Picks out each join point where the target object (the object on which a call or field operation is applied to) is an instance of Type, or of the type of the identifier Id (which must be bound in the enclosing advice or pointcut definition). Will not match any calls, gets, or sets of static members.
作用: 从join point获取调用的目标对象,提供给pointcut进行绑定
demo
package aspectj;
public class MyClass {
public void foo(int x) {
System.out.println("foo " + x);
}
public static void main(String[] args) {
MyClass myClass = new MyClass();
System.out.println(myClass);
myClass.foo(3);
}
}
package aspectj;
public aspect HelloWorld {
//pointcut定义参数类型以及名字,args定义参数名,和pointcut参数名匹配
pointcut callPointcut(int x, MyClass y):call(void aspectj.MyClass.foo(int)) && args(x) && target(y);
//advice需要定义参数类型以及参数名,和后面的pointcut的参数名匹配
before(int x, MyClass y): callPointcut(x,y)
{
System.out.println("hello " + x + " " + y);
}
}
结果
aspectj.MyClass@85ede7b
hello 3 aspectj.MyClass@85ede7b
foo 3
执行一个方法时捕获它
形式 execution(MethodPattern)
定义: Picks out each method execution join point whose signature matches MethodPattern.
作用:在方法执行时触发
备注:触发的环境是在目标类方法中(call的环境是调用类)
和call的区别就是,call是调用,是在方法外面(调用前后),execution是执行,在方法里面(执行前后)
demo
package aspectj;
public aspect HelloWorld {
//pointcut定义参数类型以及名字,args定义参数名,和pointcut参数名匹配
pointcut callPointcut(int x, MyClass y):call(void aspectj.MyClass.foo(int)) && args(x) && target(y);
//advice需要定义参数类型以及参数名,和后面的pointcut的参数名匹配
before(int x, MyClass y): callPointcut(x,y)
{
System.out.println("before call ");
System.out.println(thisJoinPoint.getSourceLocation());
}
pointcut exePointcut(int x, MyClass y):execution(void aspectj.MyClass.foo(int)) && args(x) && target(y);
before(int x, MyClass y): exePointcut(x,y)
{
System.out.println("before execution ");
System.out.println(thisJoinPoint.getSourceLocation());
}
}
输出
aspectj.MyClass@723279cf
before call
MyClass.java:10
before execution
MyClass.java:4
foo 3
在执行方法时捕获this引用的值
形式:this(Type or Id)
定义:Picks out each join point where the currently executing object (the object bound to this) is an instance of Type, or of the type of the identifier Id (which must be bound in the enclosing advice or pointcut definition). Will not match any join points from static contexts.
作用:执行期间(仅限execution),获取this引用指向的对象
demo
package aspectj;
public aspect HelloWorld {
pointcut exePointcut(int x, MyClass y):execution(void aspectj.MyClass.foo(int)) && args(x) && this(y);
before(int x, MyClass y): exePointcut(x,y)
{
System.out.println("before execution " + y);
System.out.println(thisJoinPoint.getSourceLocation());
}
}
输出
aspectj.MyClass@723279cf
before execution aspectj.MyClass@723279cf
MyClass.java:4
foo 3
备注
Pattern形式的定义
https://eclipse.org/aspectj/doc/released/progguide/semantics-pointcuts.html
MethodPattern =
[ModifiersPattern] TypePattern
[TypePattern . ] IdPattern (TypePattern | ".." , ... )
[ throws ThrowsPattern ]
ConstructorPattern =
[ModifiersPattern ]
[TypePattern . ] new (TypePattern | ".." , ...)
[ throws ThrowsPattern ]
FieldPattern =
[ModifiersPattern] TypePattern [TypePattern . ] IdPattern
ThrowsPattern =
[ ! ] TypePattern , ...
TypePattern =
IdPattern [ + ] [ [] ... ]
| ! TypePattern
| TypePattern && TypePattern
| TypePattern || TypePattern
| ( TypePattern )
IdPattern =
Sequence of characters, possibly with special * and .. wildcards
ModifiersPattern =
[ ! ] JavaModifier ...
不同signature需要的参数
https://eclipse.org/aspectj/doc/released/adk15notebook/join-point-signatures.html
环境说明
https://eclipse.org/aspectj/doc/released/progguide/semantics-joinPoints.html
问题
this和target环境的区分的意义
refer
语法说明 https://eclipse.org/aspectj/doc/released/progguide/semantics.html
《aspectj cookbook》