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

From Expertiza_Wiki
Jump to navigation Jump to search
Line 28: Line 28:


<code>wrap (target, pre, post, *args)</code>
<code>wrap (target, pre, post, *args)</code>
<code>unwrap (target, pre, post, *args)</code>
<code>unwrap (target, pre, post, *args)</code>


Line 36: Line 37:
* <code>*args</code> - target methods, or regular expression matching method names in the target class
* <code>*args</code> - 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 <code>wrap_with_code</code> instance method. Has performance advantages but cannot be unwrapped:


* <code>wrap_with_code (target, preCode, postCode, *args) </code>
<code>wrap_with_code (target, preCode, postCode, *args)</code>


* <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.
To wrap methods from multiple target classes, the AspectR library defines a global <code>wrap_classes</code> method. This is an experimental method and the API is likely to change:


* <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>wrap_classes (aspect, pre, post, classes, *methods)</code


* <code>remove_advice (target, joinpoint, method, advice)</code> - Removes an advice made with the <code>add_advice</code> method.
Parameters:
* <code>aspect</code> - aspect class
* <code>pre</code> - advice method to call before target method invocations
* <code>post</code> - advice method to call after target method invocations
* <code>classes</code> - regular expression to match target class names
* <code>*methods</code> - target methods, or regular expression matching method names in the target classes


=== Methods for controlling dispatching ===
=== Methods for controlling dispatching ===

Revision as of 15:31, 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 for wrapping

To wrap only one join point (before or after method invocation) for a single method in a target class to an advice method in the aspect class, 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)</code

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