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

From Expertiza_Wiki
Jump to navigation Jump to search
Line 39: Line 39:
=== AspectR ===
=== AspectR ===


  class User
require aspectr.rb
   attr_accessor :name
include AspectR
   def initialize name
      @name = name
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
  end
end
   
   
  class UserObserver < Aspect
#if $0 == __FILE__
  def new_name(method, object, exitstatus, *args)
  class SomeClass
      puts "method: #{method}"
    def some_method
    puts "New name: #{args[0]}"
      puts "hello"
      sleep 5
    end
   end
   end
  end
   
   
  UserObserver.new.wrap(User, :new_name, nil, /name=/)
  Profiler.new.wrap(SomeClass, :method_start, :method_end, /some/)
  UserObserver.new.wrap(User, :new_name, nil, /name=/)
  SomeClass.new.some_method
 
#end
  bob = User.new "bob"
  ted = User.new "ted"
  bob.name = "fred"
  ted.name = "ed"


=== AspectJ ===
=== AspectJ ===

Revision as of 09:40, 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

Methods

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)

unwrap (AspectR::Aspect)

wrap (AspectR::Aspect)

wrap_classes (AspectR)

wrap_with_code (AspectR::Aspect)

wrappable? (AspectR::Aspect)

Example

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