<$BlogRSDUrl$>

Monday, December 29, 2003

OK, saya buh je RnD yg saya dah buat tentang AspectJ. Pandai2 la baca, faham dan tafsir sendiri :)

download aspectj, dan unzip katakan di C.

env.bat:
set ASPECT_HOME=C:\aspectj1.1
set JAVA_HOME=C:\j2sdk1.4.2_03
set PATH=%PATH%;%JAVA_HOME%\bin;%ASPECT_HOME%\bin
set CLASSPATH=%CLASSPATH%;%ASPECT_HOME%\lib\aspectjrt.jar

file utama / mula2: Test.java
public class Test
{
public static void say(String msg)
{
System.out.println(msg);
}

public static void sayToPerson(String name, String msg)
{
System.out.println(name + ", " + msg);
}

public static void main(String[] args)
{
Test.say("To be or not to be.");
System.out.println();
Test.sayToPerson("ahmad", "To be or not to be the 2nd.");
}
}

compile pakai aspectj compiler:
ajc Test.java

outputnya apabila run java Test:
To be or not to be.

ahmad, To be or not to be the 2nd.



file aspect pertama: TestAspect.java
public aspect TestAspect
{
pointcut callSayMessage() : call(public static void Test.say*(..));

before() : callSayMessage()
{
sayPekaba();
}

after() : callSayMessage()
{
sayBabai();
}

public void sayPekaba()
{
System.out.println("Pekaba!");
}

public void sayBabai()
{
System.out.println("Babai!");
}
}

compile:
ajc *.java

outputnya:
Pekaba!
To be or not to be.
Babai!

Pekaba!
ahmad, To be or not to be the 2nd.
Babai!



file aspect kpd aspect:
public aspect TestTestAspect
{
pointcut callMulaJumpa() : call(public void TestAspect.sayPekaba());
pointcut callBerpisah() : call(public void TestAspect.sayBabai());

before() : callMulaJumpa()
{
sayAkum();
}

after() : callBerpisah()
{
sayAkum();
}

public void sayAkum()
{
System.out.println("Akum!");
}

}

compile dan run, outputnya:
Akum!
Pekaba!
To be or not to be.
Babai!
Akum!

Akum!
Pekaba!
ahmad, To be or not to be the 2nd.
Babai!
Akum!



amacam, senang je kan, kan :)

Comments: Post a Comment

This page is powered by Blogger. Isn't yours?