Code 1: Use secure RAND () to replace the original relatively weak RAND (). Rand.java /*
* Project name aspectjshow
* Package name aop.demo1
* Document name Rand.java
* Create a date 2005-4-7
* /
Package aop.demo1;
/ **
* Class name RAND
*
* Provide a non-secure RAND () method.
*
* @Author Digitalsonic
* /
Public class rand {
Public static void main (String [] args) {
Rand ();
}
/ **
* Non-secure RAND () method.
*
* @Return -1
* /
Public static int rand () {
System.out.println ("The undesirable rand () Method is Called.");
Return -1;
}
}
Secure_Rand.aj / *
* Project name aspectjshow
* Package name aop.demo1
* Document name secure_rand.aj
* Create a date 2005-4-7
* /
Package aop.demo1;
/ **
* Name Secure_Rand
*
* Provide a safe RAND () method.
*
* @Author Digitalsonic
* /
Public aspect secure_rand {
Pointcut Rand (): Call (public static int rand ()); // Implement point
Int arround (): rand () {
System.out.println ("Security Rand () Method Is Called.");
Return 1;
}
}
Code 2: Check if malloc () returns to normal, here I create a new String [] instead of malloc (). Malloc.java/*
* Project name aspectjshow
* Package name aop.demo2
* Document name Malloc.java
* Create a date 2005-4-7
* /
Package aop.demo2;
/ **
* Class name Malloc
*
* Provide a non-secure malloc () method.
*
* @Author Digitalsonic
* /
Public class malloc {
Public static void main (String [] args) {
Malloc (10);
}
/ **
* Non-secure malloc () method.
*
* @Param size array size
* @Return String [] Generated array
* /
Public static string [] malloc (int size) {
System.out.println ("Unsafe Malloc (" Size ") Method Is Called.");
Return New String [size];
}
}
Malloc_check.aj / *
* Project name aspectjshow
* Package name aop.demo2
* Document name malloc_check.aj
* Create a date 2005-4-7
* /
Package aop.demo2;
/ **
* Name Malloc_Check
*
* Provide a malloc_check () method.
*
* @Author Digitalsonic
* /
Public aspect malloc_check {
Pointcut malloc (): Call (public static string [] malloc (int)); after () Returning (String [] retrunvalue): malloc () {
IF (! (! (! (rValue instanceof string [])) {
System.out.println ("Malloc () Failed. EXITING ...");
System.exit (-1);
Else
System.out.println ("String [" RetrunValue.Length "] is constructed.");
}
}
Code 3: Integrated the above-mentioned two end code, plus a DEBUG, and outputs a piece of text before and after init_msg (). CompRehensiveDemo.java/*
* Project name aspectjshow
* Package name aop.demo3
* Document name CompRensiveDemo.java
* Create a date 2005-4-7
* /
Package aop.deemo3;
Import aop.demo1. *;
Import aop.demo2. *;
/ **
* Class name CompRensiveDemo
*
* Comprehensive example
*
* @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 [i]);
}
/ **
* Generate a string array.
*
* @Param size array size
* @Return String [] string array
* /
Public static string [] init_msg (int size) {
System.out.Println ("Message Number" size);
Return malloc.malloc (size);
}
}
Debug.java /*
* Project name aspectjshow
* Package name aop.demo3
* Document name Debug.aj
* Create a date 2005-4-7
* /
Package aop.deemo3;
/ **
* Name 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.");
}
}
Environment: Eclipse 3.01 AJDT 1.2.1 JDK1.5