aquarium: aop for ruby - github pages · aquarium: aop for ruby dean wampler object mentor, inc....

30
Aquarium: AOP for Ruby Dean Wampler Object Mentor, Inc. [email protected] AOSD 2008 April 3, 2008 1 Thursday, April 3, 2008

Upload: dangmien

Post on 09-Jul-2018

227 views

Category:

Documents


0 download

TRANSCRIPT

Aquarium: AOP for RubyDean Wampler

Object Mentor, Inc.

[email protected]

AOSD 2008April 3, 2008

1Thursday, April 3, 2008

Goals and Features

Provide an intuitive syntax.

Support runtime addition and removal of advice.

Advise Java through JRuby.

Demonstrate the value of AOP in dynamically-typed languages.

2Thursday, April 3, 2008

Why Ruby??

It’s what the cool kids are using.

“Revenge of the Smalltalkers.”

3Thursday, April 3, 2008

Language trends…and waves of innovation

Late 80’s - early 90’s: C++

Late 90’s: Java

Late 00’s: Ruby

4Thursday, April 3, 2008

Groovy vs. Ruby

Groovy might be better for advising Java.

Ruby is better, otherwise. ☺

5Thursday, April 3, 2008

Provide an intuitive syntax.Domain-specific language for aspect-like behavior?

6Thursday, April 3, 2008

class BankAccount attr_reader :balance def initialize @balance = 0 end def deposit(amount) @balance += amount end def withdraw(amount) @balance -= amount endend

creates getter balance()

@balance attribute

7Thursday, April 3, 2008

Let’s add a persistence aspect...

8Thursday, April 3, 2008

The requirements are:

Before reading the balance,,read the current value from the database.

After changing the balance,,write the current value to the database.

Before accessing the account,,authenticate and authorize the user.

9Thursday, April 3, 2008

Can I “compile” those requirements?

10Thursday, April 3, 2008

Aquarium: AOP for Rubyclass BankAccount …

before \ :calling => [:deposit, :withdraw] \ do |…| # read object state from database

end …

advice type

advice (do … end block)

pointcut

reopen the class

“,” works like “or”

11Thursday, April 3, 2008

2nd Requirement

after \ :calling => [:deposit, :withdraw] \ do |…| # write object state to database

end …

advice type

pointcut

advice

12Thursday, April 3, 2008

3rd Requirement

… before \ :calling => [:deposit, :withdraw],\ :accessing => :balance \ do |…| raise “…” unless user_permitted end end

“,” works like “or”pointcut

13Thursday, April 3, 2008

Small print...

require “aquarium”class BankAccount include Aquarium::AspectDSL before … do |jp, object, *args|

… end

join pointcontext

activeobject

parameterspassed to method

include library

add methods to class

14Thursday, April 3, 2008

Runtime addition and removal of advice.Not limited to static weaving...

15Thursday, April 3, 2008

Temporary aspectsfoo = FooBar.new(…)foo.non_critical_methodaspect = Aspect.new :before, \ :calls_to => :all_methods, \ :in_object => foo do |join_point| puts “Entering #{join_point}”endfoo.critical_method # output happens...

aspect.unadvise # stop the output...

16Thursday, April 3, 2008

Advise Java thru JRuby.The performance of Java,

the flexible power of Ruby.

17Thursday, April 3, 2008

Hic sunt draconesBleeding-edge, juggling-knives approach to Java AOP...

18Thursday, April 3, 2008

Java aspects in Ruby!

foo = Java::com.demo.FooBar.new(…)aspect = Aspect.new :before, \ :calls_to => :critical_method, \ :in_object => foo do |join_point| puts “Entering #{join_point}”endfoo.critical_methodaspect.unadvise

19Thursday, April 3, 2008

AOP for dynamically-typed languages.Metaprogramming isn’t enough.

20Thursday, April 3, 2008

Drawbacks of metaprogramming alone

Have to map AOP design to metaprogramming code.

No Pointcut Language.

21Thursday, April 3, 2008

The future of aspects...

Radical statements:

Languages like Java, .NET will limit aspects to pointcuts+advise.

Dynamic languages promise real innovation in AOSD.

22Thursday, April 3, 2008

Language Oriented Programming

Application

Compiled Objects (Java, …)

Aspects (Pointcuts + Advise)

Domain-Specific Languages

23Thursday, April 3, 2008

Improve Ruby on Rails

What the cool kids use for web apps.

Nice API (effective DSL’s).

Complex MP code inside.

24Thursday, April 3, 2008

One example.What you write:

class Customer < ActiveRecord::Base has_many BankAccounts …end

25Thursday, April 3, 2008

What Rails does:

module ActiveRecord::Associations::…def has_many(…) reflection = create_has_many(…) # “alias_person_has_many_bank_accounts”

name2 = “alias_#{reflection.name}” …

26Thursday, April 3, 2008

continued... … eval <<-EOF alias_method #{name2}, destroy_without_callbacks def destroy_without_callbacks #{reflection.name}.clear #{name2} end EOF …

It’s just before advice!

Original method

27Thursday, April 3, 2008

Refactored with Aquarium

reflection = … before :calling => :destroy_without_callbacks do eval “#{reflection.name}.clear” end

28Thursday, April 3, 2008

Aquarium:

Provides an intuitive syntax.

Supports runtime addition and removal of advice.

Advises Java through JRuby. (sort of…)

Demonstrates the value of AOP in dynamically-typed languages.

3.5 out of 4!

29Thursday, April 3, 2008

Thank you!

For more information:

http://aquarium.rubyforge.org

[email protected]

http://objectmentor.com

http://aspectprogramming.com/papers

30Thursday, April 3, 2008