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

From Expertiza_Wiki
Jump to navigation Jump to search
Line 11: Line 11:
=== Defining Aspects and wrapping methods ===
=== Defining Aspects and wrapping methods ===


AspectR provides a simple mechanism for wrapping methods in a program. One begins by creating a class that inherits from Aspect and defining the wrapper methods which will be called at the join points in the program. 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 for the code>PRE</code> and <code>POST</code> join points, 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.


<code>wrap (target, pre, post, *args)</code> takes the following parameters:
<code>wrap (target, pre, post, *args)</code> takes the following parameters:


** target - The target class
* <code>target</code> - The target class
** pre - Wrapper method to call before an intercepted method call
* <code>pre</code> - Wrapper method to call before an intercepted method call
** post - Wrapper method to call after an intercepted method call
* <code>post</code> - Wrapper method to call after an intercepted method call
** *args - Methods from the target class, or a regular expression matching method names from the target class, to be intercepted
* <code>*args</code> - Methods from the target class, or a regular expression matching method names from the target class, to be intercepted


=== Other methods ===
=== Other methods ===

Revision as of 14:08, 5 October 2010

Aspect-oriented programming and AspectR

AspectR is a very useful Ruby module, but it is not easy to find documentation on it that is appropriate for students taking this class. Find, or construct, documentation that explains what it does without presuming previous knowledge of AspectJ, that describes many or all methods of the module and how they work. Also find or produce an easy-to-understand example that does not involve logging. Show how the example would be implemented in AspectJ and AspectR.

Overview

Motivation

AspectR

Defining Aspects and wrapping methods

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.

wrap (target, pre, post, *args) takes the following parameters:

  • target - The target class
  • pre - Wrapper method to call before an intercepted method call
  • post - Wrapper method to call after an intercepted method call
  • *args - Methods from the target class, or a regular expression matching method names from the target class, to be intercepted

Other methods

  • unwrap (AspectR::Aspect)
  • add_advice (AspectR::Aspect)
  • all_classes (AspectR)
  • disable_advice_dispatching (AspectR::Aspect)
  • dispatch? (AspectR::Aspect)
  • get_methods (AspectR::Aspect)
  • new (AspectR::Aspect)
  • prepare (AspectR::Aspect)
  • remove_advice (AspectR::Aspect)
  • wrap_classes (AspectR)
  • wrap_with_code (AspectR::Aspect)
  • wrappable? (AspectR::Aspect)

Example

The 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