自己写的第一段AOP代码。
2005-04-13 22:36
357 查看
代码1:使用安全的rand()替代原来相对薄弱的rand()。
Rand.java
/*
* 项目名称 AspectJShow
* 包 名 aop.demo1
* 文 件 名 Rand.java
* 创建日期 2005-4-7
*/
package aop.demo1;
/**
* 类名 Rand
*
* 提供非安全的rand()方法。
*
* @author DigitalSonic
*/
public class Rand {
public static void main(String[] args) {
rand();
}
/**
* 非安全的rand()方法。
*
* @return -1
*/
public static int rand() {
System.out.println("The undesirable rand() method is called.");
return -1;
}
}
Secure_Rand.aj
/*
* 项目名称 AspectJShow
* 包 名 aop.demo1
* 文 件 名 Secure_Rand.aj
* 创建日期 2005-4-7
*/
package aop.demo1;
/**
* 方面名 Secure_Rand
*
* 提供安全的rand()方法。
*
* @author DigitalSonic
*/
public aspect Secure_Rand {
pointcut Rand() : call(public static int rand()); //切入点
int around() : Rand() {
System.out.println("Security rand() method is called.");
return 1;
}
}
代码2:检查malloc()返回是否正常,这里我新建一个String[]代替C中的malloc()。
Malloc.java
/*
* 项目名称 AspectJShow
* 包 名 aop.demo2
* 文 件 名 Malloc.java
* 创建日期 2005-4-7
*/
package aop.demo2;
/**
* 类名 Malloc
*
* 提供非安全的malloc()方法。
*
* @author DigitalSonic
*/
public class Malloc {
public static void main(String[] args) {
malloc(10);
}
/**
* 非安全的malloc()方法。
*
* @param Size 数组的大小
* @return String[] 生成的数组
*/
public static String[] malloc(int Size) {
System.out.println("Unsafe malloc(" + Size + ") method is called.");
return new String[Size];
}
}
Malloc_Check.aj
/*
* 项目名称 AspectJShow
* 包 名 aop.demo2
* 文 件 名 Malloc_Check.aj
* 创建日期 2005-4-7
*/
package aop.demo2;
/**
* 方面名 Malloc_Check
*
* 提供malloc_check()方法。
*
* @author DigitalSonic
*/
public aspect Malloc_Check {
pointcut Malloc() : call(public static String[] malloc(int));
after() returning(String[] RetrunValue) : Malloc() {
if (!(RetrunValue instanceof String[])) {
System.out.println("malloc() failed. Exiting...");
System.exit(-1);
} else
System.out.println("String[" + RetrunValue.length + "] is constructed.");
}
}
代码3:综合上述两端代码,加上一个Debug,在init_msg()前后各输出一段文字。
ComprehensiveDemo.java
/*
* 项目名称 AspectJShow
* 包 名 aop.demo3
* 文 件 名 ComprehensiveDemo.java
* 创建日期 2005-4-7
*/
package aop.demo3;
import aop.demo1.*;
import aop.demo2.*;
/**
* 类名 ComprehensiveDemo
*
* 综合范例
*
* @author DigitalSonic
*/
public class ComprehensiveDemo {
public static void main(String[] args) {
String[] Msg;
int i;
Msg = init_msg(4);
i = Rand.rand() % 4;
System.out.println(Msg);
}
/**
* 生成一个字符串数组。
*
* @param Size 数组大小
* @return String[] 字符串数组
*/
public static String[] init_msg(int Size) {
System.out.println("Message Number " + Size);
return Malloc.malloc(Size);
}
}
[i]Debug.java
/*
* 项目名称 AspectJShow
* 包 名 aop.demo3
* 文 件 名 Debug.aj
* 创建日期 2005-4-7
*/
package aop.demo3;
/**
* 方面名 Debug
*
* @author DigitalSonic
*/
public aspect Debug {
pointcut init_msg() : call(public static String[] init_msg(int));
before() : init_msg() {
System.out.println("Entering init_msg.");
}
after() : init_msg() {
System.out.println("Leaving init_msg.");
}
}
环境:Eclipse 3.01+AJDT 1.2.1+JDK1.5
Rand.java
/*
* 项目名称 AspectJShow
* 包 名 aop.demo1
* 文 件 名 Rand.java
* 创建日期 2005-4-7
*/
package aop.demo1;
/**
* 类名 Rand
*
* 提供非安全的rand()方法。
*
* @author DigitalSonic
*/
public class Rand {
public static void main(String[] args) {
rand();
}
/**
* 非安全的rand()方法。
*
* @return -1
*/
public static int rand() {
System.out.println("The undesirable rand() method is called.");
return -1;
}
}
Secure_Rand.aj
/*
* 项目名称 AspectJShow
* 包 名 aop.demo1
* 文 件 名 Secure_Rand.aj
* 创建日期 2005-4-7
*/
package aop.demo1;
/**
* 方面名 Secure_Rand
*
* 提供安全的rand()方法。
*
* @author DigitalSonic
*/
public aspect Secure_Rand {
pointcut Rand() : call(public static int rand()); //切入点
int around() : Rand() {
System.out.println("Security rand() method is called.");
return 1;
}
}
代码2:检查malloc()返回是否正常,这里我新建一个String[]代替C中的malloc()。
Malloc.java
/*
* 项目名称 AspectJShow
* 包 名 aop.demo2
* 文 件 名 Malloc.java
* 创建日期 2005-4-7
*/
package aop.demo2;
/**
* 类名 Malloc
*
* 提供非安全的malloc()方法。
*
* @author DigitalSonic
*/
public class Malloc {
public static void main(String[] args) {
malloc(10);
}
/**
* 非安全的malloc()方法。
*
* @param Size 数组的大小
* @return String[] 生成的数组
*/
public static String[] malloc(int Size) {
System.out.println("Unsafe malloc(" + Size + ") method is called.");
return new String[Size];
}
}
Malloc_Check.aj
/*
* 项目名称 AspectJShow
* 包 名 aop.demo2
* 文 件 名 Malloc_Check.aj
* 创建日期 2005-4-7
*/
package aop.demo2;
/**
* 方面名 Malloc_Check
*
* 提供malloc_check()方法。
*
* @author DigitalSonic
*/
public aspect Malloc_Check {
pointcut Malloc() : call(public static String[] malloc(int));
after() returning(String[] RetrunValue) : Malloc() {
if (!(RetrunValue instanceof String[])) {
System.out.println("malloc() failed. Exiting...");
System.exit(-1);
} else
System.out.println("String[" + RetrunValue.length + "] is constructed.");
}
}
代码3:综合上述两端代码,加上一个Debug,在init_msg()前后各输出一段文字。
ComprehensiveDemo.java
/*
* 项目名称 AspectJShow
* 包 名 aop.demo3
* 文 件 名 ComprehensiveDemo.java
* 创建日期 2005-4-7
*/
package aop.demo3;
import aop.demo1.*;
import aop.demo2.*;
/**
* 类名 ComprehensiveDemo
*
* 综合范例
*
* @author DigitalSonic
*/
public class ComprehensiveDemo {
public static void main(String[] args) {
String[] Msg;
int i;
Msg = init_msg(4);
i = Rand.rand() % 4;
System.out.println(Msg);
}
/**
* 生成一个字符串数组。
*
* @param Size 数组大小
* @return String[] 字符串数组
*/
public static String[] init_msg(int Size) {
System.out.println("Message Number " + Size);
return Malloc.malloc(Size);
}
}
[i]Debug.java
/*
* 项目名称 AspectJShow
* 包 名 aop.demo3
* 文 件 名 Debug.aj
* 创建日期 2005-4-7
*/
package aop.demo3;
/**
* 方面名 Debug
*
* @author DigitalSonic
*/
public aspect Debug {
pointcut init_msg() : call(public static String[] init_msg(int));
before() : init_msg() {
System.out.println("Entering init_msg.");
}
after() : init_msg() {
System.out.println("Leaving init_msg.");
}
}
环境:Eclipse 3.01+AJDT 1.2.1+JDK1.5
相关文章推荐
- Git/Github--push自己的第一段代码
- 自己写的第一段感觉有点小麻烦的代码!但全程自己写hhhh开心
- 自己写的第一段可运行多段数据的Python代码。。
- 在baidu上搜到自己以前翻译的文章:C++代码检查方法
- VC6.0中自己定义注释代码的快捷键
- 编程之美 - 1.3 翻烙饼 书中代码可运行版 ,看懂了没自己写
- 今天写了这样一段代码, 恶心到自己了!
- asp.net 的一个新bug. 当你的程序出问题时, 不仅仅要检查自己的代码和逻辑, 还要检查微软的代码, 累吗?
- Siebel开发编程——记录写第一段代码
- HDU Today(自己的代码不知道哪里错了一直没A抄袭大神的)
- 一段垃圾代码,自己写的(发送图书馆新书通告)
- MVC2.0中,不用前端代码去获取跳页文本的值,在跳转到action。用MVC自己的方法怎么做?
- Spark运行自己的代码
- 自己写的.Net(C#)代码自动生成器
- 浅谈 strlen() strcpy() strcat() 自己实现代码
- Mac在github中管理自己的代码(入门篇)
- 自己完成的一个电子表代码。
- AOP:通过面向方面编程提高代码的封装和复用性
- 只用200行Go代码写一个自己的区块链!