google:[ aspect oriented programming filetype:pdf ]
citeseer.nj.nec.com/cs:[ AOP ]
其中
http://www.aosd.net/archive/index.php
有近两年AOSD会议在ACM的论文集
最近我主要在想两个Aspect,一个就是缓存,还有一个是异常
.NET有个AspectC++ Add-In,上面缓存的例子很不错
以下是code:
aspect
include "ResultCache.h"
aspect Cache { advice call("% square(...)") : around () { if (!cache.find(*(unsigned*)tjp->arg(0), tjp->result())) { tjp->proceed(); cache.insert(*(unsigned*)tjp->arg(0), *tjp->result()); } } };
|
test code
include <stdio.h> #if defined(i386) include <unistd.h> endif
unsigned square(unsigned s) { #if defined(i386) sleep(1); #else volatile int j=0; for (unsigned i=1; i<10000000; i++) j++; endif return s*s; }
int main() { for (unsigned i=0; i<2; i++) { for (unsigned s=0; s<10; s++) { printf("square(%d, %d)\n", s, square(s)); } } getchar(); }
|
缓存和求平方两个关注点分离了。
异常似乎比较麻烦, 主要是出现exception后forward到何地需要解决,比如通常的SQLException...
有一篇paper:‘a study on exception detection and handling using aspect-oriented programming’值得一看。
先给一个很土的例子(别骂)
package net.tongji.hzt;
aspect ExceptionHandle {
pointcut all(): execution(* *(..));
before() throwing(Exception e) : all() { System.out.println("[" + e + "]@" + thisJoinPoint.getSignature()); System.exit(1); } }
|
上面的~aspect~表示, 所有类的所有方法中, 只要出现异常,
就记录异常出现的位置, 并终止程序的执行。以下是一个将要被上面的~aspect~注射的代码:
package net.tongji.hzt;
public class Main {
public static void main(String[] args) { System.out.println("before call foo"); foo(); System.out.println("done with call to foo"); }
static void foo() {; String nullstring = null; if (nullstring.equals("nullstring")) { System.out.println("if::true"); } } }
|
快考试了,没时间想这些问题了,下学起来了可能会实践一下 ;)