Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

AspectJ in Practice: Reap the Benefits of Modularity : Page 3

AspectJ, a general-purpose, aspect-oriented extension to the Java language, contains expansions and tools that enable Java developers to better manage the problems in large program systems and to reap the benefits of modularity. Get a crash course in this valuable new technology.


advertisement
Install AspectJ
Since seeing a product work is better than just reading about it, download the most recent version of AspectJ. Once it's downloaded, installation is pretty easy (assuming you already have the Java 2 SDK installed). Just execute the following command:

java jar downloaded_filename.jar

This will run an installer, which will help you complete the installation. After that, you need to correctly set the path names mentioned on the last step of installation. You are then ready to work with AspectJ in the "big project" example to follow.



Look at the following code for your big project:

1: // Our Java example TestClass.java 2: public class TestClass { 3: 4: public static void method1() { 5: System.out.println("Hello World!"); 6: } 7: 8: public static void method2() { 9: System.out.println("Really Hello!"); 10: } 11: 12: public static void say(String x) { 13: System.out.println(x); 14: } 15: 16: public static void main(String[] args) { 17: method1(); 18: method2(); 19: say("I can talk!"); 20: } 21: }

This code intentionally is easy, beginner-level Java to make understanding the technology easier. You can create more complex code after you've mastered aspects. Lines 4-6 and 8-10 define the class methods, which perform the only job: printing the well-known "Hello World" prompt. Lines 12-14 contain the other method, which says whatever you "ask" it to say. And last but not least, the main() function of the class calls all the methods.

Compile and run this example:

javac TestClass.java java TestClass

The output is the following:

Hello World! Really Hello! I can talk!

Nothing unexpected, I presume. Now, create the AspectJ code in Listing 1.

Strange code, isn't it? It looks pretty hard at first glance. But don't worry, it's not. Line 2 creates the aspect TestMe. Line 4 defines pointcut (it picks out certain join points in the program flow) for all public methods of class TestClass that are without arguments. Line 5 defines pointcut for all public methods of class TestClass that accept arguments of String type. Lines 7-10 define what will be executed before all public methods of class TestClass that accept one String type argument (defined in second pointcut). Lines 12-15 define what will be done after the previously mentioned methods. Lines 17-19 and 21-23 share the same definitions as the first pointcut methods. Notice that lines 9, 14, 18, and 22 use the following call:

thisJointPointStaticPart.getSignature().getName()

The result is the name of the method before or after which you are functioning. Argument a in lines 7 and 12 holds the value of the argument that passed to the methods as a String type argument.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap