Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > Java > 几个有用的静态方面,用于审查代码(aspectJ实现)
【标  题】:几个有用的静态方面,用于审查代码(aspectJ实现)
【关键字】:aspectJ
【来  源】:http://www.blogjava.net/zyl/archive/2006/11/16/81587.html

几个有用的静态方面,用于审查代码(aspectJ实现)

Your Ad Here 收集了几个常用的方面:

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);
?? ??? ??? ?}
?? ??? ?}
?? ?}
}
在 ASP.NET 中用匿名委托简单模拟 AOP 做异常和日志处理:【上一篇】
把DWR的配置写到Spring的配置文件里(Spring2 新特性-自定义schema初体验):【下一篇】
【相关文章】
  • AOP 与 AspectJ5
  • AspectJ类名称模式
  • 用AspectJ做的一个回合格斗小游戏
  • AspectJ 学习笔记:Aspect的生命周期
  • AspectJ学习笔记之Pointcut
  • AspectJ学习笔记之Advice
  • 关于AspectJ 中的pointcut 语法
  • AspectJ: 通往AOSD之路的最佳军火
  • 关于AspectJ的断点调试
  • 【随机文章】
  • How do I configure a Virtual IP
  • 条件表达式的语法
  • C#简明教程(四)
  • CSDN第一期总结之四:Stream的问题
  • 如何由两个ASC码(区位码)复原成一个汉字?
  • Tomcat5中文问题解决之道
  • WEB服务的规划==>HTTPD安装/应用模块配置==>升级/维护等过程
  • DWR源码学习函数调用篇
  • 从硬盘iso安装RedHat AS 3
  • C++和C#构造函数的区别
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.