CSC/ECE 517 Summer 2008/wiki1 5 bk: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 48: Line 48:
AspectR and AspectJ both facilitate the use of aspect-oriented programming in their respective languages.  How they go about doing it does differ.  With AspectJ, the AOP code is preprocessed prior to being compiled into bytecode.  Essentially there are two compilations.  With AspectR, the AOP wrapping is performed at runtime.
AspectR and AspectJ both facilitate the use of aspect-oriented programming in their respective languages.  How they go about doing it does differ.  With AspectJ, the AOP code is preprocessed prior to being compiled into bytecode.  Essentially there are two compilations.  With AspectR, the AOP wrapping is performed at runtime.


<source lang="java">
<code>
public aspect ExampleAspect {
aspect ExampleAspect {
     before() : execution(void Account.credit(float)) {
     before() : execution(void Account.credit(float)) {
         System.out.println("About to perform credit operation");
         System.out.println("About to perform credit operation");
Line 57: Line 57:
         : "Consider using Persistence.saveOptimized()";
         : "Consider using Persistence.saveOptimized()";
}
}
</java>
</code>

Revision as of 17:37, 4 June 2008

Hooking Concept

The hooking mechanism in Ruby allows functional events inside a running program to be identified while providing an action that will occur upon detection of the specified event. In this way, hooks serve as a detector of actions within the code, when event x is encountered, y will be performed. Examples where such a technique would prove useful include:

  • Recording when or how many times a method is called.
  • Recording when an object is created.
  • Debugging problematic code.

Example of Hooking System Calls (citation)

The following is an example of a hook being inserted to perform a certain action when the program makes a system call. In order for this to occur, an alias must be created so that the original functionality can still be accessed. In this case, original_system is aliased to the unaltered system. Once this is done, we can freely modify the definition of system to perform just about any operation. This is similar to the way in which a nefarious hook would work by still performing the intended operation, but also appending something extra...

alias_method :original_system, :system

The following code will then proceed to print the command executed ("ls -al" in this case), the timestamp, and then finally the output of the command to standard output.

  1. Ruby Example

module Kernel

 alias_method :original_system, :system
 def system(*args)
   puts "Your program executed the \"#{args.join(', ')}\" command." 
   puts "It was executed at: " + `date`
   puts "The command output the following: "
   original_system(*args)
 end

end system("ls -al")

  1. end of Ruby example

Your program executed the "ls -al" command. It was executed at: Mon Jun 2 21:19:27 EDT 2008 The command output the following: total 20 drwxr-xr-x 2 brking brking 4096 2008-06-02 21:19 . drwxr-xr-x 18 brking brking 4096 2008-06-02 21:19 .. -rw-r--r-- 1 brking brking 134 2008-06-01 22:01 fib.rb -rw-r--r-- 1 brking brking 235 2008-06-02 20:29 hooking.rb -rw-r--r-- 1 brking brking 287 2008-06-02 21:19 moreHooking.rb

Similarly, a method could be hooked to perform additional tasks upon calling the method. The method would still carry out its original functionality, but the new instructions would precede or follow. It is important to point out that while Ruby supports hooking as part of the language, Java does not. However, similar functionality to hooking is provided through Aspect Oriented tools in both languages. A discussion of the specific implementations as well as their advantages and disadvantages with respect to native hooking follows.

Aspect-Oriented Programming Concept

Aspect-oriented programming deals with abstracting operations in code which are independent of the main purpose or function of the code. As an illustration, a program may have two completely unrelated classes, an automobile class and a plant class. Setting aside for a moment trying to conceive of an application where the two would reside together, there could potentially be similar data desired from objects of both classes. For example, it might be worthwhile knowing the time in which each object gets instantiated, logging events on both objects, or even telling when they pass out of scope, These three commonalities are associated with each object of the class, but have nothing to do specifically with the functionality of the object. Clearly there are many other transcending pieces of information that could be attributed to either class.

In the aspect-oriented vernacular, code written to obtain this common data is said to be cross-cutting in that it applies to multiple facets of the code, but does not specifically deal with the business functionality of the code. This cross-cutting code is also known as advice. The specific places in the code where it is determined that advice is needed is referred to as a pointcut or jointpoint. From there, the aspect embodies the combination of the jointpoint and advice for use by the program. In the previous example, there would be aspects for timestamps, event logging, and scope tracking [1]. It is important to note that the creation of aspects leaves the original underlying class intact and simply provides a wrapper which will encompass it. This ability to work on existing code without altering has the added advantage of making it more adaptable to future changes and enhancement requests later in the application's life cycle. Aspect-orientation does not in any way undermine object-orientation. Aspect-oriented programming complements object-oriented programming in that OOP governs top-down relationships while AOP governs left-right relationships within the application [2].

Why Use Hooks Instead of AOP?

Given the nature of the aspect-oriented approach, it is easy draw parallels with hooking techniques. Ruby hooks can accomplish the same objective as an aspect-oriented tool. The primary aspect-oriented tools for Ruby and Java are AspectR and AspectJ respectively.

AspectR Versus ApectJ

AspectR and AspectJ both facilitate the use of aspect-oriented programming in their respective languages. How they go about doing it does differ. With AspectJ, the AOP code is preprocessed prior to being compiled into bytecode. Essentially there are two compilations. With AspectR, the AOP wrapping is performed at runtime.

aspect ExampleAspect {

   before() : execution(void Account.credit(float)) {
       System.out.println("About to perform credit operation");
   }
   declare parents: Account implements BankingEntity;
   declare warning : call(void Persistence.save(Object))
       : "Consider using Persistence.saveOptimized()";

}