Writing Java Aspects ... with JRuby and Aquarium! 26
Aquarium V0.4.0, my AOP library for Ruby, now supports JRuby. Not only do the regular “pure Ruby” Aquarium specs run reliably under JRuby (V1.1RC2), but you can now write aspects for Java types with Aquarium!
There are some important limitations, though. Cartographers of old would mark dangerous or unknown territory on their maps with hic sunt dracones (“here be dragons”), a reference to the old practice of adorning maps with serpents around the edges.
This is true of Aqurium + Java types in JRuby, too, at least for now.
Aquarium uses Ruby’s metaprogramming API extensively and the JRuby team has done some pretty sophisticated work to integrate Java types with Ruby. Hence, it’s not too surprising there are some gotchas. Hopefully, workarounds will be possible for all of them.
The details are discussed on the JRuby page, the README on the Aquarium site, and of course the “specs” in the distribution’s jruby/spec
directory. I’ll summarize them here, after discussing the pros and cons of Aquarium vs. the venerable AspectJ and showing you an example of using Aquarium for Java.
Briefly, Aquarium’s advantages over AspectJ are these:
- You can add and remove advice dynamically at runtime. You can’t remove AspectJ advice.
- You can advise JDK types easily with Aquarium. AspectJ won’t do this by default, but this is really more of a legacy licensing issue than a real technical limitation.
- You can advise individual objects, not just types.
Aquarium’s disadvantages compared to AspectJ include:
- Aquarium will be slower than using AspectJ (although this has not been studied in depth yet).
- Aquarium’s pointcut language is not as full-featured as AspectJ’s.
- There are the bugs and limitations I mentioned above in this initial V0.4.0 release, which I’ll elaborate shortly.
Here is an example of adding tracing calls to a method doIt
in all classes that implement the Java interface com.foo.Work
.
Aspect.new :before, :calls_to => [:doIt, :do_it], :in_types_and_descendents => Java::com.foo.Work do |jp, obj, *args|
log "Entering: #{jp.target_type.name}##{jp.method_name}: object = #{object}, args = #{args.inspect}"
end
There are two important points to notice in this example:
- You can choose to refer to the method as
do_it
(Ruby style) ordoIt
, but these variants are effectively treated as separate methods; advice on one will not affect invocations of the other. So, if you want to be sure to catch all invocations, use both forms. There is a bug (18326) that happens in certain conditions if you use just the Java naming convention. - If the type is an interface, you must use
:types_and_descendents
(or one of the supported variants on the wordtypes
...). Since interfaces don’t have method implementations, you will match no join points unless you use the_and_descendents
clause. (By default, Aquarium warns you when no join points are matched by an aspect.) However, there is a bug (18325) with this approach if Java types are subtyped in Ruby.
Limitations and Bugs
Okay, here’s the “fine print”...
In this (V0.4.0) release, there are some important limitations.
- Aquarium advice on a method in a Java type will only be invoked when the method is called directly from Ruby.
- To have the advice invoked when the method is called from either Java or Ruby, it is necessary to create a Ruby subclass of the Java type and override the method(s) you want to advise. These overrides can just call
super
. Note that it will also be necessary for instances of this Ruby type to be used throughout the application, in both the Java and Ruby code. So, you’ll have to instantiate the object in your Ruby code.
Yea, this isn’t so great, but if you’re motivated… ;)
There are also a few outstanding Aquarium bugs (which could actually be JRuby bugs or quirks of the Aquarium-JRuby “interaction”; I’m not yet sure which).
- Bug #18325: If you have Ruby subclasses of Java types and you advise a Java method in the hierarchy using
:types_and_descendents => MyJavaBaseClassOrInterface
and you call unadvise on the aspect, the advice “infrastructure” is not correctly removed from the Ruby types. Workaround: Either don’t “unadvise” such Ruby types or only advise methods in such Ruby types where the method is explicitly overridden in the Ruby class. (The spec and the Rubyforge bug report provide examples.) - Bug #18326: Normally, you can use either Java- or Ruby-style method names (e.g.,
doSomething
vs.do_something
), for Java types. However, if you write an aspect using the Java-style for a method name and a Ruby subclass of the Java type where the method is actually defined (i.e., the Ruby class doesn’t override the method), Aquarium acts like the JoinPoint is advised, but the advice is never actually called. Workaround: Use the Ruby-style name in this scenario.
So, there is still some work to do, but it’s promising that you can use an aspect framework in one language with another. A primary goal of Aquarium is to make it easy to write simple aspects. My hope is that people who might find AspectJ daunting will still give Aquarium a try.
You sold aquarium to me. I am quite convinced of its benefits. Hehe. I should try it sometime soon.
Guess what’s going on my laptop once I get it back with a new hard drive? It’ll really be interesting to see what craziness I’ll cook up with this. ;)
Useful!
some body is de ssse
liably under JRuby (V1.1RC2), but you can now write aspects for Java types w
This looks a lot like the way “good” inheritance should behave.
An important feature about getting on the internet is you’re able to notice something the particular gifts, whereby a shop it is usually challenging sort even so her or his inventory to uncover what’s right. Mothers really like products which can be unique. Attempt to existing your own mum by using a surprise that’s personalized, as an case in point a wedding ring utilizing their title imprinted inside. Precious metal jewelry lead to excellent gifts given that they immortalize your currenttitle inside rare metal. Any rare metal pendant and also a ring produces a great products on your mum, along with presents that are generally unnoticed .
Intertech Machinery Inc. provides the most precise Plastic Injection Mold and Rubber Molds from Taiwan. With applying excellent unscrewing device in molds, Intertech is also very professional for making flip top Cap Molds in the world.
JAY Follow laptop accessories the detail tips below, you can increase the laptop battery life of a year or more. 1. The laptop battery first thing you should care about the
AspectJ enables the clean modularization of crosscutting
thanks for post i can be use it…
internette görüntülü olarak okey oyunu oyna, gerçek kisilerle tanis, turnuva heyecanini yasa.
You’re in the comm, therefore you participated!!
These christian louboutin patent leather pumps are just the same as selling in the franchise store.Or even better. Quite cheap christian louboutin leather platform pumps.I could say this is the best one I have ever ordered online.
The article has been finished.
it needs a bokmark so i can come back to it later ,nice stuff
rare metal. Any rare metal pendant and also a ring produces a great products on your mum, along with presents that are generally unnoticed .high quality headphones new design headphones
This is a echt couth foreclose for me, Unoriginality cultivated that you are one of the jaunty blogger I e’er saw.Thanks for visor this consultatory add.
I thought Star Wars was too wacky for the general public.
The professional design make you foot more comfortable. Even more tantalizing,this pattern make your legs look as long as you can,it will make you looked more attractive.Moveover,it has reasonable price.If you are a popular woman,do not miss it.
Technical details of Christian Louboutin Velours Scrunch Suede Boots Coffee:
Fashion, delicate, luxurious Christian louboutins shoes on sale, one of its series is Christian Louboutin Tall Boots, is urbanism collocation. This Christian louboutins shoes design makes people new and refreshing. Red soles shoes is personality, your charm will be wonderful performance.
I thought Star Wars was too wacky for the general public.
I thought Star Wars was too wacky for the general public.
GOOD
@
@!!!Australia Beats By Dre Studio dr dre beats headphones beats studio beats pro beats solo hd pro headphones music Official store Monster Beats By Dre Pro
Writing Java Aspects … with JRuby and Aquarium! 22 good post57
Writing Java Aspects … with JRuby and Aquarium! 23 hoo,good article!!I like the post!65
Are you looking for a pair of Fringe Boots? If so, it is useful to know the best clutch purses wholesale places to buy them from. Fringe Boots are extremely versatile and high quality boots. cheap heel boots They benefit from being super comfortable due to the material stretching to form Tory Burch the perfect fit for your feet. The fashionable tassels also create a trendy look Tory Burch reva for any outfit. Shopping for Fringe Boots online is rewarding and definitely the best way to find the most reliable stores for purchasing the boots.
Each and every woman in the whole wide world all wanted to be grand and beautiful?