CSC/ECE 517 Fall 2010/ch3 3d mr: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
= Aspect-oriented programming and AspectR =
= Aspect-oriented programming and AspectR =


AspectR is a programming library that enables [http://en.wikipedia.org/wiki/Aspect-oriented_programming Aspect-oriented programming] [http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 Ruby] programs by providing ways to wrap code around existing methods in a program. It is simpler but similar in functionality to the [http://en.wikipedia.org/wiki/Aspectj AspectJ] library for Java, but less well known. AspectR is distributed under the [http://en.wikipedia.org/wiki/GPL GNU General Public License].
AspectR is a programming library that enables [http://en.wikipedia.org/wiki/Aspect-oriented_programming Aspect-oriented programming] for [http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 Ruby] programs by providing ways to wrap code around existing methods in a program. It is simpler but similar in functionality to the [http://en.wikipedia.org/wiki/Aspectj AspectJ] library for Java, but less well known. AspectR is distributed under the [http://en.wikipedia.org/wiki/GPL GNU General Public License].


== Aspect-Oriented Programming ==
== Aspect-Oriented Programming ==
Line 7: Line 7:
Aspect-Oriented Programming is an approach to modularizing concerns that cut across the core concerns or business logic, of a program.
Aspect-Oriented Programming is an approach to modularizing concerns that cut across the core concerns or business logic, of a program.


=== Motivation ===
==== Motivation ====


The model code in a typical program will often contain not only the business logic but also secondary concerns such as transactions, security, logging, etc.  This clutters the code in the model, making it hard to distinguish the business logic from the secondary code, complicating maintenance and reuse. Furthermore, these secondary concerns cut across multiple core concerns in the program, which violates the separation of concerns. As a result, changing to a different transaction API, for example, results in code modifications throughout the program.
The model code in a typical program will often contain not only the business logic but also secondary concerns such as transactions, security, logging, etc.  This clutters the code in the model, making it hard to distinguish the business logic from the secondary code, complicating maintenance and reuse. Furthermore, these secondary concerns cut across multiple core concerns in the program, which violates the separation of concerns. As a result, changing to a different transaction API, for example, results in code modifications throughout the program.
Line 19: Line 19:
* Join Point - Places in the core business logic that advice should be applied or wrapped around.
* Join Point - Places in the core business logic that advice should be applied or wrapped around.


== AspectR API ==
== The AspectR API ==


AspectR provides a simple mechanism for wrapping methods in a program. One begins by creating an "aspect" class that inherits from the AspectR Aspect class.  You then define the wrapper methods which will be called at the join points in the program. These methods are called Advice methods. AspectR currently supports only two method join points, <code>PRE</code> and <code>POST</code>. The inherited instance method <code>wrap</code> is then used to specify the wrapper methods to be called before and after a method invocation, along with the target class and methods to be intercepted.
AspectR provides a simple mechanism for wrapping methods in a program. One begins by creating an "aspect" class that inherits from the AspectR Aspect class.  You then define the wrapper methods which will be called at the join points in the program. These methods are called Advice methods. AspectR currently supports only two method join points, <code>PRE</code> and <code>POST</code>. The inherited instance method <code>wrap</code> is then used to specify the wrapper methods to be called before and after a method invocation, along with the target class and methods to be intercepted.
Line 32: Line 32:


Parameters:
Parameters:
* <code>target</code> - target class
** <code>target</code> - target class
* <code>joinpoint</code> - must be either <code>PRE</code> or <code>POST</code>
** <code>joinpoint</code> - must be either <code>PRE</code> or <code>POST</code>
* <code>method</code> - target method
** <code>method</code> - target method
* <code>advice</code> - advice method in the aspect class
** <code>advice</code> - advice method in the aspect class


To wrap both the before and after join points, and specify multiple target methods, use the <code>wrap</code> and <code>unwrap</code> instance methods on the Aspect class:
To wrap both the before and after join points, and specify multiple target methods, use the <code>wrap</code> and <code>unwrap</code> instance methods on the Aspect class:

Revision as of 11:30, 6 October 2010

Aspect-oriented programming and AspectR

AspectR is a programming library that enables Aspect-oriented programming for Ruby programs by providing ways to wrap code around existing methods in a program. It is simpler but similar in functionality to the AspectJ library for Java, but less well known. AspectR is distributed under the GNU General Public License.

Aspect-Oriented Programming

Aspect-Oriented Programming is an approach to modularizing concerns that cut across the core concerns or business logic, of a program.

Motivation

The model code in a typical program will often contain not only the business logic but also secondary concerns such as transactions, security, logging, etc. This clutters the code in the model, making it hard to distinguish the business logic from the secondary code, complicating maintenance and reuse. Furthermore, these secondary concerns cut across multiple core concerns in the program, which violates the separation of concerns. As a result, changing to a different transaction API, for example, results in code modifications throughout the program.

Terminology

  • Aspects - Classes that implement functionality needed in many parts of a program, but are not part of the business logic or core concern of the program.
  • Advice - Aspect methods or code to be applied around existing business logic.
  • Join Point - Places in the core business logic that advice should be applied or wrapped around.

The AspectR API

AspectR provides a simple mechanism for wrapping methods in a program. One begins by creating an "aspect" class that inherits from the AspectR Aspect class. You then define the wrapper methods which will be called at the join points in the program. These methods are called Advice methods. AspectR currently supports only two method join points, PRE and POST. The inherited instance method wrap is then used to specify the wrapper methods to be called before and after a method invocation, along with the target class and methods to be intercepted.

Methods for wrapping

To add an advice method to be invoked before or after a single target method, use the add_advice and remove_advice instance methods on the Aspect class:

add_advice (target, joinpoint, method, advice)

remove_advice (target, joinpoint, method, advice)

Parameters:

    • target - target class
    • joinpoint - must be either PRE or POST
    • method - target method
    • advice - advice method in the aspect class

To wrap both the before and after join points, and specify multiple target methods, use the wrap and unwrap instance methods on the Aspect class:

wrap (target, pre, post, *args)

unwrap (target, pre, post, *args)

Parameters:

  • target - target class
  • pre - advice method to call before target method invocations
  • post - advice method to call after target method invocations
  • *args - target methods, or regular expression matching method names in the target class

Alternatively, you may wrap target methods to blocks of advice code using the wrap_with_code instance method. Has performance advantages but cannot be unwrapped:

wrap_with_code (target, preCode, postCode, *args)

To wrap methods from multiple target classes, the AspectR library defines a global wrap_classes method. This is an experimental method and the API is likely to change:

wrap_classes (aspect, pre, post, classes, *methods)

Parameters:

  • aspect - aspect class
  • pre - advice method to call before target method invocations
  • post - advice method to call after target method invocations
  • classes - regular expression to match target class names
  • *methods - target methods, or regular expression matching method names in the target classes

Methods for controlling dispatching

  • disable_advice_dispatching - Disables all dispatching to advice methods by the API. Provided as an instance method so must be called on an instance of the aspect class (any instance).
  • dispatch? - Indicates if dispatching to advice methods is enabled.

Specifying methods that should never be wrapped

  • new (never_wrap = "^$ ") - Class method to create a new instance of the aspect class and specify methods that should never be wrapped.
  • wrappable? (method) - Indicates if the specified method can be wrapped. See [new] below.

Other utility methods

The following methods are used by the API itself but also made public:

  • get_methods (targret, args)
  • prepare (target)
  • all_classes (regexp = /^.+$/) - Returns all classes whose class name matches a given regular expression.

Example

The following code examples implement a code profiler to measure the duration of method calls.

AspectR

require aspectr.rb
include AspectR

class Profiler < Aspect
  def method_start(method, object, exitstatus, *args)
    @begin = Time.now
  end
  def method_end(method, object, exitstatus, *args)
    timeElapsed = Time.now - @begin
    puts "#{object.class}.#{method} took #{timeElapsed} secs"
  end
end

#if $0 == __FILE__
  class SomeClass
    def some_method
      puts "hello"
      sleep 5
    end
  end

  Profiler.new.wrap(SomeClass, :method_start, :method_end, /some/)
  SomeClass.new.some_method
#end

AspectJ

References

External Links