收集了几个常用的方面:
NoCallsIntoTestFromOutside 禁止从产品包到测试包的引用
public abstract aspect NoCallsIntoTestFromOutside {
//引用带有test的包的所有可能操作
pointcut referToTestPackage():
call(* *..test..*.*(..)) || call(*..test..*.new(..))
|| get(* *..test..*.*) || set(* *..test..*.*)
|| get(*..test..* *) || set(*..test..* *)
|| (staticinitialization(!*..test..*)
&& staticinitialization(*..test..*+))
|| call(* *(*..test..*, ..))
|| call(* *(*, *..test..*, ..))
|| call(* *(*, *, *..test..*))
|| call(* *(.., *..test..*))
|| execution(* *(*..test..*, ..))
|| execution(* *(*, *..test..*, ..))
|| execution(* *(*, *, *..test..*, ..))
|| execution(* *(.., *..test..*))
;
// getcode scary-tests END
abstract protected pointcut withinTypes();
pointcut fromWithinNonTest(): within(!*..test..*);
declare error: withinTypes() && referToTestPackage()
&& fromWithinNonTest(): "production code referring to test code";
}
UtilityClassEnforced 禁止构造实用工具类
public aspect UtilityClassEnforced {
declare error :
//定义非private 的构造器
execution(!private (@UtilityClass *).new(..))
//调用new 初始化方法
|| call((@UtilityClass *).new(..))
//执行工具类的任何方法
|| execution((@UtilityClass *) *(..))
: "no instances of utility classes";
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface UtilityClass {
}
NoSystemOut 禁止使用 System.err 或 System.out
public abstract aspect NoSystemOut {
abstract protected pointcut withinTypes;
//读取System.out对象
declare error: withinTypes() && get(PrintStream System.out) : "no System.out";
//读取System.err对象
declare error: withinTypes() && get(PrintStream System.err) : "no System.err";
//读取FileDescriptor.err
declare error: withinTypes() && get(PrintStream FileDescriptor.err) : "no FileDescriptor.err";
//读取FileDescriptor.out
declare error: withinTypes() && get(PrintStream FileDescriptor.out) : "no FileDescriptor.out";
//执行Throwable.printStackTrace()方法
declare error: withinTypes() && call(void Throwable.printStackTrace()) : "no System.err";
}
GetterSetter 禁止在除初始化或 getter 方法以外进行字段读取操作,禁止在初始化或 setter 方法以外进行字段写操作
public abstract aspect GetterSetter {
abstract protected pointcut withinTypes();
//所有的sets 非public fields
pointcut sets() : set(!public * *);
//所有的gets 非public fields
pointcut gets() : get(!public * *);
declare error : withinTypes() && gets() &&
//在get*方法或者构造器以外地方使用
!(withincode(!void get*()) || withincode(new(..)))
: "non-public field-get outside constructor or getter method";
declare error : withinTypes() && sets() &&
!(withincode(void set*(*)) || withincode(new(..)))
: "non-public field-set outside constructor or setter method";
}
NoNullParameters 在公共方法传递空参数时抛出异常
public abstract aspect NoNullParameters {
abstract protected pointcut withinTypes();
pointcut publicParameters() :
//执行public 构造器或者public 方法
(execution(public new(*,..)) || execution(public * *(*,..)))
//除了执行非Object的参数,如原类型,int,long
&& !(execution(* *(!Object+)) || execution(*.new(!Object+)));
//如果参数为null,则抛出例外
before() : withinTypes() && publicParameters() {
Object[] args = thisJoinPoint.getArgs();
for (int i = 0; i < args.length; i++) {
if (null == args[i]) {
JoinPoint.StaticPart sp = thisJoinPointStaticPart;
String name = AJReflect.getParameterName(sp, i);
throw new IllegalArgumentException("null " + name);
}
}
}
}
分享到:
相关推荐
主要对Spring AOP的相关概念和简单的静态代理、动态代理以及常见的几种AOP配置方式做总结学习。主要包括:1. AOP的常见概念 2. 静态代理 3. jdk动态代理 4. Aspectj and Aspectjweaver 5. **aop-config** 6. CGLIB ...
精通AspectJ(面向方面的java编程指南)一书的示例代码。
Spring 使用AspectJ 实现 AOP(基于xml文件、基于注解)
spring通过aspectj来实现事务控制
Spring 使用AspectJ 实现 AOP之前置通知小例子,实际跑过,验证可信。
AspectJ 是一种面向 Aspect 编程语言,用于在 Java 语言中实现面向方面编程。AspectJ 可以帮助开发者更好地理解和优化 Android 应用程序的性能和行为。在 Android Studio 中使用 AspectJ 可以实现多种功能,例如分析...
许多开发人员都对aop感兴趣 —— 特别是对aspectj感兴趣,这个java编程语言的开源扩展明确支持aop方法。然而,尽管aspectj纳入在eclipse这个日益流行的java开源ide之内,但是找到一种实用、非理论的方式来学习这种...
面向方面编程和AspectJ(二)面向方面编程和AspectJ(二)面向方面编程和AspectJ(二)
学习aspectJ,帮助学习面向方面的知识。
1: 分析解释静态代理框架 AspectJ演变过程 2:增加AspectJ案例 3:分析AspectJ字节码
NULL 博文链接:https://samter.iteye.com/blog/410618
aspectj-1.7.0.jar aspectj的包
Spring spectJ AOP 前置通知 后置通知 返回通知 异常通知 环绕通知
aspectj
Aspectj驱动Aspectj驱动Aspectj驱动Aspectj驱动Aspectj驱动Aspectj驱动Aspectj驱动Aspectj驱动Aspectj驱动Aspectj驱动
aspectj的有关知识点
AOP的实现有三种,IOC,JavaSisst,AspectJ。AspectJ的Demo最少,这次成功调试出一个
Spring框架(6)AspectJ实现AOP共7页.pdf.zip
Aspectj开发指南 博文链接:https://g007n.iteye.com/blog/249602
spring默认使用的就是AspectJ来实现的动态代理。使用spring的aop的时候,需要引入aspectj.jar