CSC/ECE 517 Fall 2009/wiki29 VB: Difference between revisions
No edit summary |
No edit summary |
||
Line 33: | Line 33: | ||
} | } | ||
} | } | ||
This is the existing Java code called TestClass.java. Now if we want to make use of aspects to make two modifications such as: | |||
1.We would like to print a message before and after any call to the TestClass.sayHello() method. | |||
2.We need to test that the argument of the TestClass.sayAnyThing() method is at least three characters. | |||
Here is the AspectJ implementation | |||
1: public aspect MyAspect { | |||
2: public pointcut sayMethodCall (): call (public void TestClass.say*() ); | |||
3: public pointcut sayMethodCallArg (String str): call | |||
(public void TestClass.sayAnyThing (String)) | |||
&& args(str); | |||
4: before(): sayMethodCall() { | |||
5: System.out.println("\n TestClass." + | |||
thisJoinPointStaticPart.getSignature().getName() + | |||
"start..." ); | |||
6: } | |||
7: after(): sayMethodCall() { | |||
8: System.out.println("\n TestClass." + | |||
thisJoinPointStaticPart.getSignature().getName() + | |||
" end..."); | |||
9: } | |||
10: before(String str): sayMethodCallArg(str) { | |||
11: if (str .length() < 3) { | |||
12: System.out.println ("Error: I can't say words less than 3 | |||
characters"); | |||
13: return; | |||
14: } | |||
15: } | |||
16: } |
Revision as of 15:38, 9 October 2009
When Object-Oriented (OO) programming was entering into the field of software development, it changed the way how software was developed. The software systems begun to be viewed as groups of entities and the interaction between those entities, which allowed them to tackle larger, more complicated systems and develop them in less time than ever before. However, OO programming is static, and a thus a change in requirements could have a brought a major impact on development time lines.
Aspect-Oriented Programming (AOP) complements OO programming by allowing the developer to dynamically modify the static OO model to create a system that can grow to meet new requirements. Just as objects in the real world can change their states during their lifecycles, an application can adopt new characteristics as it develops.
For example, we develop a web application where a servlet accepts the values of an HTML form, binds them to an object, passes them into the application to be processed, and then returns a response to the user. This basic functionality of the servlet may be very simple, and can be fulfilled with minimum amount of code. But, by the time secondary requirements such as exception handling, security, and logging have been implemented , the size of the code increases to about three times of original. Having said that, it is not mandatory for the servlet to know about the logging or security mechanisms being used, and hence can be called as secondary requirements. Its primary function is to just accept input and process it.
Using AOP , we can dynamically modify our static model to include the code required to fulfill the secondary requirements without having to modify the original static model. We can also keep this additional code in a single location rather than having to scatter it across the existing model, as we would have to if we were using OO on its own.
ASPECTJ
Here are some of the standard terms that would be required to understand AOP concepts.
1.Cross-cutting concerns: Even though most classes in an OO model will perform a single, specific function, they often share common, secondary requirements with other classes. For example, we may want to add logging to classes within the data-access layer and also to classes in the UI layer whenever a thread enters or exits a method. Even though the primary functionality of each class is very different, the code needed to perform the secondary functionality is often identical.
2.Advice: Advice is the extra code that we want to add to our already existing model.
3.Point-cut: Point-cut is the point of execution in the application at which cross-cutting concern needs to be applied.
4.Aspect: Aspect is the combination of the point-cut and the advice.
The other terms that appear often in AOP are introductions (where interfaces/methods/fields can be added to existing classes). These hold tremendous potential for developers,
Just as the important facets of OOP are inheritance, encapsulation, and polymorphism, the components of AOP are join points, pointcut, advice, and introduction.
Let's consider the following example to understand this better.
public class TestClass { public void sayHello () { System.out.println ("Hello, AOP"); } public void sayAnyThing (String s) { System.out.println (s); } public static void main (String[] args) { sayHello (); sayAnyThing ("ok"); } }
This is the existing Java code called TestClass.java. Now if we want to make use of aspects to make two modifications such as: 1.We would like to print a message before and after any call to the TestClass.sayHello() method. 2.We need to test that the argument of the TestClass.sayAnyThing() method is at least three characters.
Here is the AspectJ implementation
1: public aspect MyAspect { 2: public pointcut sayMethodCall (): call (public void TestClass.say*() ); 3: public pointcut sayMethodCallArg (String str): call (public void TestClass.sayAnyThing (String)) && args(str); 4: before(): sayMethodCall() { 5: System.out.println("\n TestClass." + thisJoinPointStaticPart.getSignature().getName() + "start..." ); 6: } 7: after(): sayMethodCall() { 8: System.out.println("\n TestClass." + thisJoinPointStaticPart.getSignature().getName() + " end..."); 9: }
10: before(String str): sayMethodCallArg(str) { 11: if (str .length() < 3) { 12: System.out.println ("Error: I can't say words less than 3 characters"); 13: return; 14: } 15: } 16: }