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

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
Line 7: Line 7:
== Motivation ==
== Motivation ==


== AspectR ==
== AspectR API ==
 
=== 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, <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 20: Line 18:


* <code>unwrap (target, pre, post, *args)</code> - Undoes a wrapping made with the previous method.
* <code>unwrap (target, pre, post, *args)</code> - Undoes a wrapping made with the previous method.
* <code>wrap_with_code (target, preCode, postCode, *args) </code>
* <code>wrappable? (method)</code> - Indicates if the specified method can be wrapped. See [new] below.


* <code>add_advice (target, joinpoint, method, advice)</code> - Allows you to specify an advice method to be called when the joinpoint is reached in the specified target class and method. Joinpoint must be either <code>PRE</code> or <code>POST</code>.
* <code>add_advice (target, joinpoint, method, advice)</code> - Allows you to specify an advice method to be called when the joinpoint is reached in the specified target class and method. Joinpoint must be either <code>PRE</code> or <code>POST</code>.
Line 27: Line 29:
* <code>disable_advice_dispatching</code> - Disables all dispatching of advice methods in the program.
* <code>disable_advice_dispatching</code> - Disables all dispatching of advice methods in the program.


* get_methods (AspectR::Aspect)  
* <code>get_methods (targret, args)</code>


* prepare (AspectR::Aspect)
* <code>prepare (target)</code>
 
* wrap_with_code (AspectR::Aspect)
 
* wrappable? (AspectR::Aspect)


The following class methods are inherited from the Aspect class:
The following class methods are inherited from the Aspect class:
Line 46: Line 44:


* <code>wrap_classes (aspect, pre, post, classes, *methods)</code> - More flexible yet experimental version of the <code>wrap<code> method to wrap methods in multiple classes. Caller must provide the aspect object and regular expression used to match target classes.  The rest of the parameters are the same as the <code>wrap</code> method. Note that this API is likely to change in future versions of AspectR.
* <code>wrap_classes (aspect, pre, post, classes, *methods)</code> - More flexible yet experimental version of the <code>wrap<code> method to wrap methods in multiple classes. Caller must provide the aspect object and regular expression used to match target classes.  The rest of the parameters are the same as the <code>wrap</code> method. Note that this API is likely to change in future versions of AspectR.
Wraps methods in one or more classes whose class name matches a regular expression.  Unlike the
Caller specifies the aspect class and methods to call before and after each method invocation. A regular expression is used to match on target class names, and either the target methods can be provided or a regu
Specifies an aspect class and advice methods to call before and after method invocations in one or more target classes and methods.  The target classes are speci


== Example ==
== Example ==


The code examples implement a code [http://en.wikipedia.org/wiki/Profiling_%28computer_programming%29 profiler] to measure the duration of method calls.
The following code examples implement a code [http://en.wikipedia.org/wiki/Profiling_%28computer_programming%29 profiler] to measure the duration of method calls.


=== AspectR ===
=== AspectR ===

Revision as of 14:46, 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 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

The following instance methods are inherited from the Aspect class.

  • wrap (target, pre, post, *args) - This is the main utility method which allows you to specify an advice method in the aspect class to be invoked before and after each intercepted method, along with the target class and an *args parameter which may contain the methods in the target class to intercept, or a regular expression to match method names in the target class.
  • unwrap (target, pre, post, *args) - Undoes a wrapping made with the previous method.
  • wrap_with_code (target, preCode, postCode, *args)
  • wrappable? (method) - Indicates if the specified method can be wrapped. See [new] below.
  • add_advice (target, joinpoint, method, advice) - Allows you to specify an advice method to be called when the joinpoint is reached in the specified target class and method. Joinpoint must be either PRE or POST.
  • remove_advice (target, joinpoint, method, advice) - Removes an advice made with the add_advice method.
  • disable_advice_dispatching - Disables all dispatching of advice methods in the program.
  • get_methods (targret, args)
  • prepare (target)

The following class methods are inherited from the Aspect class:

  • new (never_wrap = "^$ ") -
  • dispatch? - Returns true if dispatching to advice methods is enabled, false if disabled.

The following utility methods are provided by the AspectR library:

  • all_classes (regexp = /^.+$/) - Returns all classes whose class name matches a given regular expression.
  • wrap_classes (aspect, pre, post, classes, *methods) - More flexible yet experimental version of the wrap method to wrap methods in multiple classes. Caller must provide the aspect object and regular expression used to match target classes. The rest of the parameters are the same as the wrap method. Note that this API is likely to change in future versions of AspectR.

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