Transcription
Aquarium: AOP for RubyDean WamplerObject Mentor, Inc.dean@objectmentor.comAOSD 2008April 3, 2008Thursday, April 3, 20081
Goals and FeaturesProvide an intuitive syntax.Support runtime addition and removal ofadvice.Advise Java through JRuby.Demonstrate the value of AOP indynamically-typed languages.Thursday, April 3, 20082
Why Ruby?It’s what the cool kids are using.“Revenge of the Smalltalkers.”Thursday, April 3, 20083
Language trends and waves of innovationLate 80’s - early 90’s: C Late 90’s: JavaLate 00’s: RubyThursday, April 3, 20084
Groovy vs. RubyGroovy might be better for advising Java.Ruby is better, otherwise. Thursday, April 3, 20085
Provide an intuitive syntax.Domain-specific language for aspect-likebehavior?Thursday, April 3, 20086
class BankAccountattr reader :balancecreates getterbalance()def initialize@balance 0end@balanceattributedef deposit(amount)@balance amountenddef withdraw(amount)@balance - amountendendThursday, April 3, 20087
Let’s add a persistenceaspect.Thursday, April 3, 20088
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.Thursday, April 3, 20089
Can I “compile” thoserequirements?Thursday, April 3, 200810
Aquarium: AOP for Rubyclass BankAccount advice typebefore \reopen the classpointcut “,” works like “or”:calling [:deposit, :withdraw] \do # read object state from databaseend Thursday, April 3, 2008advice (do end block)11
nd2 Requirementadvice typepointcutafter \:calling [:deposit, :withdraw] \do # write object state to databaseend Thursday, April 3, 2008advice12
rd3Requirement before \pointcut“,” works like “or”:calling [:deposit, :withdraw],\:accessing :balance \do raise “ ” unless user permittedendendThursday, April 3, 200813
Small print.require “aquarium”class BankAccountinclude libraryadd methods to classinclude Aquarium::AspectDSLbefore do jp, object, *args endThursday, April 3, 2008join pointcontextactiveobjectparameterspassed tomethod14
Runtime addition andremoval of advice.Not limited to static weaving.Thursday, April 3, 200815
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.Thursday, April 3, 200816
Advise Java thru JRuby.The performance of Java,the flexible power of Ruby.Thursday, April 3, 200817
Hic suntdraconesBleeding-edge,juggling-knivesapproach to JavaAOP.Thursday, April 3, 200818
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.unadviseThursday, April 3, 200819
AOP for dynamically-typedlanguages.Metaprogramming isn’t enough.Thursday, April 3, 200820
Drawbacks ofmetaprogramming aloneHave to map AOP design tometaprogramming code.No Pointcut Language.Thursday, April 3, 200821
The future of aspects.Radical statements:Languages like Java, .NET will limitaspects to pointcuts advise.Dynamic languages promise realinnovation in AOSD.Thursday, April 3, 200822
Language OrientedProgrammingApplicationDomain-Specific LanguagesAspects (Pointcuts Advise)Compiled Objects (Java, )Thursday, April 3, 200823
Improve Ruby on RailsWhat the cool kids use for web apps.Nice API (effective DSL’s).Complex MP code inside.Thursday, April 3, 200824
One example.What you write:class Customer ActiveRecord::Basehas many BankAccounts endThursday, April 3, 200825
What Rails does:module ActiveRecord::Associations:: def has many( )reflection create has many( )# “alias person has many bank accounts”name2 “alias #{reflection.name}” Thursday, April 3, 200826
continued. eval -EOFalias method #{name2},destroy without callbacksdef destroy without ��s just before advice!Original method Thursday, April 3, 200827
Refactored with Aquariumreflection before :calling :destroy without callbacks doeval “#{reflection.name}.clear”endThursday, April 3, 200828
Aquarium:Provides an intuitive syntax.Supports runtime addition and removal ofadvice.Advises Java through JRuby. (sort of )Demonstrates the value of AOP indynamically-typed languages.3.5 out of 4!Thursday, April 3, 200829
Thank you!For more ogramming.com/papersThursday, April 3, 200830
Aquarium: AOP for Ruby Dean Wampler Object Mentor, Inc. dean@objectmen