Aspect oriented programming



Download 251.43 Kb.
Page3/3
Date06.08.2017
Size251.43 Kb.
1   2   3

Figure 4

CHAPTER 7

ASPECTJ

Aspect Oriented Programming and Java
AOP is a concept, so it is not bound to a specific programming language. In fact, it can help with the shortcomings of all languages (not only OO languages) that use single, hierarchical decomposition. AOP has been implemented in different languages (for example, C++, Smalltalk, C#, C, and Java). 
Of course, the language that gains a great interest of the research community is the Java language. 
The following is a list of tools that support AOP with Java: 

AspectJ 
Aspect-Werkz 
Hyper/J 
JAC 
JMangler 
MixJuice 
PROSE 
ArchJava 
AspectJ, created at Xerox PARC, was proposed as an extension of the Java language for AOP. Join points, Pointcut, Advice, and Introduction -Where the tools of OOP are inheritance, encapsulation, and polymorphism, the components of AOP are join points, pointcut, advice, and introduction. For a better understanding of these new terms,

let's consider the following simple example. 
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"); 
} 
} 
Listing 1: TestClass.java
Now, we have our existing Java code in TestClass.java. Let's assume that we want to use aspects to do the following modifications: 
We would like to print a message before and after any call to the TestClass.sayHello() method. 
We need to test that the argument of the TestClass.sayAnyThing() method is at least three characters. 
The following list 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: } 
Listing 2: MyAspect.aj 
Line 1 defines an aspect in the same way we define a Java class. Like any Java class, an aspect may have member variables and methods. In addition, it may include pointcuts, advices, and introductions. In Lines 2 and 3, we specify where in the TestClass code our modification will take place. In AspectJ terms, we define two pointcuts. To explain what a pointcut means, we first need to define join points. Join points represent well-defined points in a program's execution. Typical join points in AspectJ include method/constructor calls, method/constructor execution, field get and set, exception handler execution, and static and dynamic initialization. In our example, we have two join points: 
the call to TestClass.sayHello and TestClass.sayAnyThing methods. 
Pointcut is a language construct that picks out a set of join points based on defined criteria. The criteria can be explicit function names, or function names specified by wildcards. 
public pointcut sayMethodCall ():

call( public void TestClass.say*() ); 
In the preceding line, we define a pointcut, named sayMethodCall, that picks out any call to the TestClass.sayHello method. Moreover, it picks out any public TestClass method with zero arguments and a name that starts with "say" (for example, TestClass.sayBye). Pointcuts are used in the definition of advice. An advice in AspectJ is used to define additional code to be executed before, after, or around join points. In our example, Lines 4 and 7 define two advices that will be executed before and after the first pointcut. Finally, Lines 10-15 implement an advice associated with the second pointcut and are used to set a precondition before the execution of the TestClass.sayAnyThing method.Whereas pointcuts and advice let you affect the dynamic execution of a program, introduction 
allows aspects to modify the static structure of a program. By using introduction, aspects can add new methods and variables to a class, declare that a class implements an interface, or convert checked to unchecked exceptions.

AspectJ Compiler
Initially, you have to download the most recent version of AspectJ, available for free at its official site and install it. Compiling and running our example is very easy. Just write: 
ajc MyAspect.aj TestClass.java 
java TestClass 
You should note that the source code at TestClass.java didn't change. To return to the original functionality of your program, just use your Java compiler to re-compile it. Developing with AspectJ Now that you have better sense of what aspect code looks like, let's turn our attention briefly to the business of writing aspects. In other words, let's answer the question, "How can I get the above code to work?" For aspects to affect regular class-based code, the aspects must be woven into the code they modify. To do this using AspectJ, you must compile your class and aspect code with the ajc compiler. ajc can operate either as a compiler or a precompiler, generating either valid .class or .java files, which you can then compile and run in any standard Java environment (with the addition of a small run-time JAR). To compile with AspectJ, you will need to explicitly specify the source files (both aspect and class) that you want to include in a given compilation -- ajc does not simply search your class path for relevant imports the way javac does. This makes sense because each class in a standard Java application acts as a somewhat isolated component. A class needs only the presence of classes that it directly references in order to operate correctly. Aspects represent aggregate behaviours that span multiple classes. Therefore, an AOP program needs to be compiled as a unit rather than one class at a time. By specifying the files that are included in a given compilation, you can also plug and unplug various aspects of your system at compile time. For instance, by including or excluding the logging aspect described earlier from a compilation, the application builder can add or remove method tracing from the Cactus framework. An important limitation to the current version of AspectJ is that its compiler can only weaver aspects into code for which it has the source. In other words, you cannot use ajc to add advice to precompiled classes. The AspectJ team regards this limitation as temporary, and the AspectJ Web site promises that a future version (officially 2.0) will allow byte code modification.

Tool support
Several development tools are included as part of AspectJ's release. This bodes well for AspectJ's future, because it indicates a strong commitment on the part of the authors to making AspectJ friendly to developers. Tool support is particularly important for an aspect-oriented system because program modules can be affected by other modules of which they have no knowledge. 
One of the most important tools released with AspectJ is a graphical structure browser that reveals at a glance how aspects interact with other system components. This structure browser is available as a plug-in for popular IDEs as well as a stand-alone tool. Other Spring aspects for AspectJ. In addition to the Configurable support, spring-aspects.jar contains an AspectJ aspect that can be used to drive Spring's transaction management for types and methods annotated with the Transactional annotation. This is primarily intended for users who want to use Spring's transaction support outside of the Spring container. The aspect 
that interprets Transactional annotations is the Annotation Transaction Aspect. When using this aspect, you must annotate the implementation class (and/or methods within that class), not the interface (if any) that the class implements. AspectJ follows Java's rule that annotations on interfaces are not inherited. 
A Transactional annotation on a class specifies the default transaction semantics for the execution of any public operation in the class. A Transactional annotation on a method within the class overrides the default transaction semantics given by the class annotation (if present). Methods with public, protected, and default visibility may all be annotated. Annotating protected and default visibility methods directly are the only way to get transaction demarcation for the execution of such operations.

Aspect-Oriented Programming in Java 
Object oriented programming has become main stream over the last years, having almost completely replaced the procedural approach. One of the biggest advantages of object orientation is that a software system can be seen as being built of a collection of discrete classes. Each of these classes has a well defined task, its responsibilities are clearly defined. In an OO application, those classes collaborate to achieve the application's overall goal. However, there are parts of a system that cannot be viewed as being the responsibility of only one class, they cross-cut the complete system and affect parts of many classes. Examples might be locking in a distributed application, exception handling, or logging method calls. Of course, the code that handles these parts can be added to each class separately, but that would violate the principle that each class has well-defined responsibilities. This is where AOP comes into play: AOP defines a new program construct, called an aspect, which is used to capture cross-cutting aspects of a software system in separate program entities. The application classes keep their well-defined responsibilities. Additionally, each aspect captures cross-cutting behaviour.

CALLING PROCEDURES

c:\users\s\downloads\aspect\aop6.jpg

Figure 5

CHAPTER 8

COMPARISION TO OTHER PROGRAMMING PARADIGMS

Aspects emerged out of object-oriented programming and computational reflection. AOP languages have functionality similar to, but more restricted than meta object protocols. Aspects relate closely to programming concepts like subjects, mixins, and delegation. Other ways to use aspect-oriented programming paradigms include Composition Filters and the hyperslices approach. Since at least the 1970s, developers have been using forms of interception and dispatch-patching that are similar to some of the implementation techniques for AOP, but these never had the semantics that the crosscutting specifications were written in one place. Designers have considered alternative ways to achieve separation of code, such as C#'s partial types, but such approaches lack a quantification mechanism that allows reaching several join points of the code with one declarative statement.

Procedural programming
In procedure oriented programming the main program is divided in to different 
and distinct functions and also they are programmed in to separate modules. The execution can be in a particular sequence. It follows a top- down approach for program execution. Execution in a given sequence 
FORTRAN, C, COBOL 

Functional programming
In functional programming the program execution is through different well defined functions. If any function require any additional task it includes another sub functions. Evaluating a function Lisp, ML 

Logic programming
In logic programming the program execution based on certain logics. Proving a theorem for Prolog 

Object-oriented programming (OOP)
In object- oriented programming all the real world problems are decomposed in to different entities called objects. Objects are the basic run time entities of a program. Organizing a set of objects Smalltalk, Java, C++ (to some extent).

Aspect-oriented programming (AOP)
In AOP aspects are run at certain join points specified in the program. 
Execution based on certain behaviours AspectJ (a Java extension) Complements OOP ASPECT ORIENTED. Class code unit that encapsulates methods and attributes. Aspect code unit that encapsulates Pointcuts, advice, and attributes. Method signatures define the entry points for the execution of method bodies. Pointcut define the set of entry points (triggers) in which advice is executed. Method bodies implementations of the primary concerns. Advice implementations of the cross cutting concerns. Compiler converts source code into object code. Weaver instruments code (source or object) with advice.

CHAPTER 9

ADVANTAGES AND DISADVANTAGES OF AOP

ADVANTAGES:
The major advantages while using the AOP are follows:

1.) It improves the tractability of the program. 
2.) It can be used for developing simpler and cleaner code. 
3.) The code can be reusable and therefore reduce the redundancy of code. 
4.) It is more easy to maintain than other programming languages. 
This advantage of AOP helps the programmers for developing more powerful softwares.
DISADVANTAGES: Although AOP has several advantages it has some disadvantages. 

1.) It is difficult to understand software because of invisibly injected aspects. 
2.) It has fragile build problems. 
3.) It has a complicated control flow breakage. 
4.) There may be a chance for reduce quality of software if aspects are not appropriately managed.

CHAPTER 10

CONCLUSION

 
Programs are decomposed into suitable cross-cutting building blocks, i.e. a building block affects many parts of the combined program. Building blocks which contain redundant information are overspecified and need to be reduced to a description which removes the overspecification. The reduced description is expressed in an aspect description language. This leads to a solution of a more general problem while at the same time removing the redundancy from the program. The reduced building blocks need to be compiled together into the original or an improved form of the original program. An important issue is how the reduced descriptions collaborate. They need to refer to a common vocabulary (join points) so that the compiler can regenerate the original program."Aspect Oriented Programming", further, does provide a methodology, or at least as much a one as Object Oriented (which is, admittedly, scattered across such things as closures, classes, prototypes, etc.). If you read into Aspect Oriented, you'll learn that the methodology consists of:

(a) Identifying from the language the places that modifications may be performed (join points).

(b) A means of specifying which code to modify or where to add code (pointcuts), a means of specifying the modification to perform (advice or inter-type declarations depending on whether the code is 'run' or not).
We got a different approach about programming paradigms which can be essentially used in frequent manner around the world’s best technologies.

Download 251.43 Kb.

Share with your friends:
1   2   3




The database is protected by copyright ©ininet.org 2020
send message

    Main page